54 lines
2.3 KiB
Markdown
54 lines
2.3 KiB
Markdown
---
|
||
title: "LanguageServerProtocol"
|
||
type: concept
|
||
tags: ["LSP", "code-intelligence", "language-server", "IDE", "editor"]
|
||
sources: ["lsp-index-engineer.md"]
|
||
last_updated: 2026-04-29
|
||
---
|
||
|
||
## Definition
|
||
Language Server Protocol(LSP)是 Microsoft 在 2016 年首创的标准化协议,定义了**语言服务器**(Language Server)与**语言客户端**(Editor/IDE)之间的通信规范,使编程语言工具能够在任何编辑器中提供一致的代码智能功能(跳转定义、查找引用、悬停文档、自动完成等),无需为每个编辑器-语言组合单独实现。
|
||
|
||
## Core Mechanism
|
||
|
||
### Protocol Architecture
|
||
- **Language Server**:独立进程,负责语言特定的代码分析(解析、类型检查、语义分析)
|
||
- **Language Client**:编辑器/IDE 端,负责用户交互和结果展示
|
||
- **Communication**:通过 JSON-RPC(2.0)在 stdin/stdout 之间传输消息
|
||
|
||
### LSP 3.17 关键能力
|
||
- **textDocument/didOpen** — 文档打开通知
|
||
- **textDocument/definition** — 跳转到定义
|
||
- **textDocument/references** — 查找引用
|
||
- **textDocument/hover** — 悬停文档
|
||
- **textDocument/completion** — 自动完成
|
||
- **textDocument/publishDiagnostics** — 诊断信息发布
|
||
- **workspace/symbol** — 工作区符号搜索
|
||
|
||
### Capability Negotiation
|
||
客户端在 `initialize` 请求中声明自身能力:
|
||
```json
|
||
{
|
||
"capabilities": {
|
||
"textDocumentSync": 1,
|
||
"definitionProvider": true,
|
||
"referencesProvider": true,
|
||
"hoverProvider": true
|
||
}
|
||
}
|
||
```
|
||
服务器在响应中声明支持的能力。**LSP/Index Engineer 强制要求永远检查服务器能力响应,而非假设支持某功能。**
|
||
|
||
### Lifecycle
|
||
`initialize` → `initialized` → (正常运行)→ `shutdown` → `exit`
|
||
|
||
## Multi-Language Orchestration
|
||
LSP 的核心价值在于使多语言支持标准化。LSP/Index Engineer 的 graphd 通过协调多个语言服务器(TypeScript、PHP、Go、Rust、Python),将各自 LSP 响应统一转换为语义图谱,实现跨语言的代码导航。
|
||
|
||
## Connections
|
||
- [[LanguageServerProtocol]] ← enables ← [[LSPOrchestrator]]
|
||
- [[LanguageServerProtocol]] ← powers ← [[SemanticCodeGraph]]
|
||
- [[LSPOrchestrator]] ← coordinates ← [[TypeScriptLanguageServer]]
|
||
- [[LSPOrchestrator]] ← coordinates ← [[Intelephense]]
|
||
- [[LanguageServerProtocol]] ← uses ← [[LSIF]]
|