# 基础设施设计 — LSP 集成
## 文档元数据
- 项目名称: free-code
- 文档类型: 基础设施设计
- 原始代码来源: `../../src/services/lsp/`(7个文件)
- 原始设计意图: 在 .NET 中封装语言服务器生命周期、文件同步与 9 种核心 LSP 操作,并支持按扩展名路由与诊断基线比较
- 交叉引用: [基础设施设计总览](基础设施设计.md) | [核心模块设计-工具系统](../核心模块设计/核心模块设计-工具系统.md)
## 设计目标
LSP 层用于承载代码补全、诊断、跳转与重命名等 IDE 能力,为编辑体验提供统一协议封装。它既要兼容多语言服务器,又要支持懒启动、文件同步和跨文件诊断聚合。
## 11.1 ILspClientManager 接口定义
```csharp
///
/// LSP 客户端管理器 — 管理 LSP 服务器实例
/// 对应原始 LSPServerManager.ts
///
public interface ILspClientManager
{
/// 初始化 (加载配置, 懒启动)
Task InitializeAsync(CancellationToken ct = default);
/// 关闭所有服务器
Task ShutdownAsync();
/// 获取文件对应的 LSP 服务器
ILspServerInstance? GetServerForFile(string filePath);
/// 确保文件对应的 LSP 服务器已启动
Task EnsureServerStartedAsync(string filePath);
/// 发送请求到文件对应的 LSP 服务器
Task SendRequestAsync(string filePath, string method, object? parameters);
// LSP 操作 (对应原始 LSPTool 的 9 种操作)
Task GoToDefinitionAsync(string filePath, int line, int character);
Task FindReferencesAsync(string filePath, int line, int character);
Task HoverAsync(string filePath, int line, int character);
Task DocumentSymbolsAsync(string filePath);
Task WorkspaceSymbolsAsync(string query);
Task GetDiagnosticsAsync(string filePath);
Task PrepareRenameAsync(string filePath, int line, int character);
Task RenameAsync(string filePath, int line, int character, string newName);
Task GetCodeActionsAsync(string filePath, int line, int character);
// 文件同步
Task OpenFileAsync(string filePath, string content);
Task ChangeFileAsync(string filePath, string content);
Task SaveFileAsync(string filePath);
Task CloseFileAsync(string filePath);
/// 所有运行中的服务器
IReadOnlyDictionary GetAllServers();
/// 是否至少有一个服务器已连接
bool IsConnected { get; }
}
```
## 11.2 LspServerInstance 实现
```csharp
///
/// 单个 LSP 服务器实例 — 管理子进程生命周期
/// 对应原始 LSPServerInstance.ts
///
public interface ILspServerInstance
{
string Name { get; }
string Command { get; }
IReadOnlyDictionary ExtensionToLanguage { get; }
LspServerState State { get; }
Task StartAsync();
Task StopAsync();
Task SendRequestAsync(string method, object? parameters);
Task SendNotificationAsync(string method, object? parameters);
void OnRequest(string method, Func