Files
nexus/wiki/concepts/LanguageServerProtocol.md
2026-05-03 05:42:12 +08:00

2.3 KiB
Raw Blame History

title, type, tags, sources, last_updated
title type tags sources last_updated
LanguageServerProtocol concept
LSP
code-intelligence
language-server
IDE
editor
lsp-index-engineer.md
2026-04-29

Definition

Language Server ProtocolLSP是 Microsoft 在 2016 年首创的标准化协议,定义了语言服务器Language Server语言客户端Editor/IDE之间的通信规范使编程语言工具能够在任何编辑器中提供一致的代码智能功能跳转定义、查找引用、悬停文档、自动完成等无需为每个编辑器-语言组合单独实现。

Core Mechanism

Protocol Architecture

  • Language Server:独立进程,负责语言特定的代码分析(解析、类型检查、语义分析)
  • Language Client:编辑器/IDE 端,负责用户交互和结果展示
  • Communication:通过 JSON-RPC2.0)在 stdin/stdout 之间传输消息

LSP 3.17 关键能力

  • textDocument/didOpen — 文档打开通知
  • textDocument/definition — 跳转到定义
  • textDocument/references — 查找引用
  • textDocument/hover — 悬停文档
  • textDocument/completion — 自动完成
  • textDocument/publishDiagnostics — 诊断信息发布
  • workspace/symbol — 工作区符号搜索

Capability Negotiation

客户端在 initialize 请求中声明自身能力:

{
  "capabilities": {
    "textDocumentSync": 1,
    "definitionProvider": true,
    "referencesProvider": true,
    "hoverProvider": true
  }
}

服务器在响应中声明支持的能力。LSP/Index Engineer 强制要求永远检查服务器能力响应,而非假设支持某功能。

Lifecycle

initializeinitialized → (正常运行)→ shutdownexit

Multi-Language Orchestration

LSP 的核心价值在于使多语言支持标准化。LSP/Index Engineer 的 graphd 通过协调多个语言服务器TypeScript、PHP、Go、Rust、Python将各自 LSP 响应统一转换为语义图谱,实现跨语言的代码导航。

Connections