# 语言服务器协议

**语言服务器协议**（Language Server Protocol，LSP）是一种开放的、基于 JSON-RPC 的协议，它标准化了开发工具（如作为客户端的编辑器或 IDE）与语言服务器之间的通信。语言服务器提供自动补全、转到定义、查找所有引用等语言功能。<sup>[1](https://microsoft.github.io/language-server-protocol/)</sup> 其目的既是技术上的，也是经济上的：与其让每个工具都为每种语言实现语言支持，不如让单个语言服务器在多个开发工具中复用；而一个工具只要实现一次客户端，就能支持多种语言。<sup>[1](https://microsoft.github.io/language-server-protocol/)</sup> 2025 年的一篇研究论文将这一点表述为：语言与编辑器的组合数从 \( L \times E \) 减少到 \( L + E \)。<sup>[2](https://arxiv.org/abs/2509.15150)</sup>

| 关键事实 | 详情 |
|---|---|
| 标准化的内容 | 开发工具与语言服务器进程之间使用 JSON-RPC 发送的消息格式。<sup>[1](https://microsoft.github.io/language-server-protocol/)</sup> |
| 解决的问题 | 将语言与编辑器的集成工作从 \( L \times E \) 种组合减少到 \( L + E \)。<sup>[2](https://arxiv.org/abs/2509.15150)</sup> |
| 当前规范 | 版本 3.18；3.18.0 变更日志的日期为 2026 年 6 月 4 日，新增了内联补全、动态文本文档内容、多范围格式化等功能。<sup>[3](https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/lsp/3.18/specification.md)</sup> |
| 发布 | 微软于 2016 年 6 月 27 日与红帽（Red Hat）和 Codenvy 联合发布，规范被迁移至公开的 GitHub 仓库。<sup>[4](https://www.javacodegeeks.com/2026/08/the-language-server-protocol-the-quiet-standardization-of-developer-tooling.html)</sup><sup> • </sup><sup>[5](https://code.visualstudio.com/blogs/2016/06/27/common-language-protocol?source=post_page---------------------------)</sup> |
| 架构 | 语言服务器运行在独立的进程中；传输方式可以是 stdio、套接字、命名管道，或当双方都运行在 Node.js 上时的 node ipc。<sup>[6](https://learn.microsoft.com/en-us/visualstudio/extensibility/language-server-protocol?view=visualstudio)</sup> |
| 功能协商 | 客户端和服务器在启动时通过“能力”（capabilities）声明各自支持的功能集。<sup>[6](https://learn.microsoft.com/en-us/visualstudio/extensibility/language-server-protocol?view=visualstudio)</sup> |
| 采用规模 | 2022 年的一项学术研究统计了 121 个现有语言服务器，并分析了其中 30 个的源代码。<sup>[7](https://pure.itu.dk/en/publications/editing-support-for-software-languages-implementation-practices-i/)</sup> |

## 起源

该协议源自两个更早的语言服务器项目。OmniSharp 为 C# 构建了丰富的编辑功能，最初使用带 JSON 负载的 HTTP，并集成到包括 [Visual Studio Code](https://www.edgepedia.org/zh/visual-studio-code) 在内的多个编辑器中。大约同一时期，微软在开发一个 [TypeScript](https://www.edgepedia.org/zh/typescript) 语言服务器，旨在为 Emacs 和 [Sublime Text](https://www.edgepedia.org/zh/sublime-text) 等编辑器提供 TypeScript 支持；在该项目中，编辑器通过标准输入/标准输出进行通信，JSON 负载的灵感来自 V8 调试器协议。在集成这两个不同的语言服务器之后，VS Code 团队开始探索一种面向编辑器和 IDE 的通用协议，从 TypeScript 服务器的协议出发，并以受 VS Code 语言 API 启发的语言功能加以扩展，底层由 JSON-RPC 支持远程调用。<sup>[6](https://learn.microsoft.com/en-us/visualstudio/extensibility/language-server-protocol?view=visualstudio)</sup>

团队以静态代码检查语言服务器为原型验证该协议，包括 VS Code 的 ESLint 和 TSLint 扩展，二者都用 TypeScript/[JavaScript](https://www.edgepedia.org/zh/javascript) 在 Node.js 上实现，并共享一个实现协议客户端与服务器部分的库。让服务器在多次编辑之间持续运行，避免了每次用户改动都启动新的静态代码检查进程。<sup>[6](https://learn.microsoft.com/en-us/visualstudio/extensibility/language-server-protocol?view=visualstudio)</sup> 该协议于 2016 年 6 月 27 日与红帽和 Codenvy 一同公开发布；Codenvy 将其加入 Eclipse Che，红帽则开始开发一个独立的 Java 语言服务器，可供任何使用该协议的工具使用。<sup>[5](https://code.visualstudio.com/blogs/2016/06/27/common-language-protocol?source=post_page---------------------------)</sup><sup> • </sup><sup>[4](https://www.javacodegeeks.com/2026/08/the-language-server-protocol-the-quiet-standardization-of-developer-tooling.html)</sup> Visual Studio 于 2017 年 11 月通过预览版加入 Visual Studio Code 的行列，开始支持该协议，该预览版以 Visual Studio 2017 Preview 扩展的形式提供。<sup>[8](https://devblogs.microsoft.com/scripting/announcing-language-server-protocol-preview-release/)</sup>

## 工作原理

语言服务器运行在独立的进程中，工具通过 JSON-RPC 与其通信。在专用进程中运行服务器也避免了单进程模型的性能问题。传输通道可以是 stdio、套接字、命名管道，或当客户端和服务器都用 Node.js 编写时的 node ipc。<sup>[6](https://learn.microsoft.com/en-us/visualstudio/extensibility/language-server-protocol?view=visualstudio)</sup> 规范指出，服务器的生命周期由客户端管理，例如 VS Code 或 Emacs 这样的工具。<sup>[3](https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/lsp/3.18/specification.md)</sup>

一次常规的编辑会话遵循重复的模式：<sup>[6](https://learn.microsoft.com/en-us/visualstudio/extensibility/language-server-protocol?view=visualstudio)</sup>

1. 用户打开文件：工具发送 `textDocument/didOpen` 通知，此后文档内容由工具保存在内存中，而不再从文件系统读取。
2. 用户编辑：工具发送 `textDocument/didChange`，服务器更新其语义信息，并通过 `textDocument/publishDiagnostics` 回报检测到的错误和警告。
3. 用户调用“转到定义”之类的功能：工具发送带有文档 URI 和光标位置的 `textDocument/definition` 请求，服务器返回该符号定义的 URI 和范围。
4. 用户关闭文档：`textDocument/didClose` 通知告知服务器，文件系统中的内容重新成为最新版本。

**编辑器层面的数据类型。** 协议的数据类型是编辑器概念，例如打开的文本文档和光标位置，而不是抽象语法树、编译器符号这类编程语言领域模型概念。微软的概述将这一选择列为协议成功的原因之一：标准化文本文档 URI 或光标位置，要比跨不同编程语言标准化抽象语法树简单得多。<sup>[6](https://learn.microsoft.com/en-us/visualstudio/extensibility/language-server-protocol?view=visualstudio)</sup>

**能力。** 并非每个服务器都支持所有功能，因此客户端和服务器通过能力声明各自支持的功能集。服务器可能处理 `textDocument/definition` 但不处理 `workspace/symbol`；客户端同样可以声明它在文档保存前提供通知，以便服务器计算格式化编辑。<sup>[6](https://learn.microsoft.com/en-us/visualstudio/extensibility/language-server-protocol?view=visualstudio)</sup>

## 规范涵盖的功能

3.18 规范将语言功能组织为请求、响应和通知，涵盖以下内容（以及其他功能）：声明、[定义](https://www.edgepedia.org/zh/definition)、类型定义、实现、引用、调用层次、类型层次、文档高亮、文档链接、悬停、代码透镜、折叠范围、选择范围、文档符号、语义标记、内联提示、内联值、标识名、补全、发布式与拉取式诊断、签名帮助、代码操作、文档颜色、格式化、范围格式化、输入时格式化、重命名、关联编辑范围、内联补全；工作区功能包括工作区符号、配置、工作区文件夹、文件创建/重命名/删除事件、监视文件以及执行命令。<sup>[3](https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/lsp/3.18/specification.md)</sup>

3.18.0 变更日志（日期为 2026 年 6 月 4 日）新增了内联补全支持、动态文本文档内容支持、折叠范围刷新支持、一次格式化多个范围、工作区编辑中的代码片段支持、文档过滤器的相对模式支持、代码操作类型文档、签名帮助上可为空的 `activeParameter`、命令工具提示以及工作区编辑的元数据。<sup>[3](https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/lsp/3.18/specification.md)</sup>

## 在各编辑器和 IDE 中的采用

**Eclipse。** Eclipse LSP4E 项目使 Eclipse IDE 能够使用语言服务器，并提供补全、悬停、大纲、问题/标记和快速修复等典型的富编辑器功能。2017 年列出的采用者包括：使用 OmniSharp 语言服务器进行 C# 开发的 aCute、使用 VS Code 语言服务器进行 Web 编辑的 BlueSky，以及使用 Sourcegraph 的 PHP 语言服务器的 Lsp4e-php。<sup>[9](https://www.eclipse.org/community/eclipse_newsletter/2017/may/article3.php)</sup>

**基于 IntelliJ 的 IDE。** IntelliJ 平台以扩展形式向商业 IDE 暴露 LSP API，包括 [IntelliJ IDEA](https://www.edgepedia.org/zh/intellij-idea)、WebStorm、PhpStorm、PyCharm、RubyMine、CLion、GoLand、Rider 和 RustRover；使用该 API 的插件必须面向 2023.2 或更高版本，自 2025.1 起，统一版 PyCharm 无需 Pro 订阅也支持该功能。JetBrains 的文档指出，其标准的自定义语言支持在与 IDE 功能的集成方面仍比呈现来自语言服务器的数据更广泛，因此 LSP 被定位为附加价值，而非现有语言 API 的替代品。<sup>[10](https://plugins.jetbrains.com/docs/intellij/language-server-protocol.html?section=English)</sup>

**其他客户端。** 任何 LSP 客户端都可以使用该协议，文中以 VS Code、Zed 和 Neovim 为例，而 Neovim 自带内置的 LSP 客户端。<sup>[11](https://github.com/jbearak/raven/blob/main/docs/comparison.md)</sup> 2022 年的一项研究将 LSP 描述为实现语言编辑支持的事实标准，且与编辑工具本身相分离。<sup>[7](https://pure.itu.dk/en/publications/editing-support-for-software-languages-implementation-practices-i/)</sup>

## 局限

规范在若干方面保持开放。它没有规定客户端与服务器之间的通信通道（常见选择包括标准输入/输出和 TCP 连接）、如何从 IDE 调用命令、如何设置配置选项，以及如何获取、安装和启动语言服务器。客户端与服务器之间的这些实现差异，可能恰恰产生 LSP 本意要解决的不兼容问题。<sup>[12](https://elib.uni-stuttgart.de/server/api/core/bitstreams/9dcf6c47-37ed-4608-978b-a97c3b16b54f/content)</sup> 该协议也不包含对语法高亮的支持；使用语言服务器的 Eclipse 插件通常依赖 TextMate 语法和 Eclipse TM4E 项目来实现这一目的。<sup>[9](https://www.eclipse.org/community/eclipse_newsletter/2017/may/article3.php)</sup>

## 超越传统编辑器的 LSP

LSP 的通用概念和架构模式使应用能够超越对编程语言的编辑支持；2023 年的一篇 EMISAJ 论文描述了它在建模工具方面的潜力，指出语言客户端只需理解协议，而无需理解特定的编程语言。<sup>[13](https://www.emisa-journal.org/emisa/article/view/320)</sup>

该协议也已进入 AI 辅助开发领域。一项针对编程智能体的测量研究发现了这种使用方式中的权衡：在以真实测试执行评分的编辑任务中，grep 完美解决了多文件重命名，仅提供位置的 LSP 因遗漏某个调用点而在四分之三的任务中失败，即便是完整、索引预热、文本增强的 LSP 也弥补了大部分差距，但仍无法完全弥合，因为重命名还必须触及语义引用所排除的注释和字符串。<sup>[15](https://arxiv.org/abs/2608.13568)</sup>

## References

1. [Official page for Language Server Protocol, microsoft.github.io](https://microsoft.github.io/language-server-protocol/)
2. [Code Less to Code More: Streamlining Language Server Protocol and Type System Development for Language Families, arXiv:2509.15150](https://arxiv.org/abs/2509.15150)
3. [Language Server Protocol Specification - 3.18, microsoft/language-server-protocol on GitHub](https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/lsp/3.18/specification.md)
4. [The Language Server Protocol: The Quiet Standardization of Developer Tooling, Java Code Geeks](https://www.javacodegeeks.com/2026/08/the-language-server-protocol-the-quiet-standardization-of-developer-tooling.html)
5. [A Common Protocol for Languages, Visual Studio Code blog, June 27, 2016](https://code.visualstudio.com/blogs/2016/06/27/common-language-protocol?source=post_page---------------------------)
6. [Language Server Protocol Overview, Microsoft Learn (Visual Studio)](https://learn.microsoft.com/en-us/visualstudio/extensibility/language-server-protocol?view=visualstudio)
7. [Barros, Peldszus, Assunção, Berger (2022). Editing support for software languages: implementation practices in language server protocols, MODELS 2022, pp. 232-243](https://pure.itu.dk/en/publications/editing-support-for-software-languages-implementation-practices-i/)
8. [Announcing Language Server Protocol Preview Release, Microsoft Scripting Blog, November 21, 2017](https://devblogs.microsoft.com/scripting/announcing-language-server-protocol-preview-release/)
9. [Using Language Servers to Edit Code in the Eclipse IDE, Eclipse Foundation newsletter, May 2017](https://www.eclipse.org/community/eclipse_newsletter/2017/may/article3.php)
10. [Language Server Protocol (LSP), IntelliJ Platform Plugin SDK](https://plugins.jetbrains.com/docs/intellij/language-server-protocol.html?section=English)
11. [Comparison with Other R Tools, Raven documentation on GitHub](https://github.com/jbearak/raven/blob/main/docs/comparison.md)
12. [2.4 Limitations, University of Stuttgart publication](https://elib.uni-stuttgart.de/server/api/core/bitstreams/9dcf6c47-37ed-4608-978b-a97c3b16b54f/content)
13. [Bork, Langer (2023). Language Server Protocol: An Introduction to the Protocol, its Use, and Adoption for Web Modeling Tools, EMISAJ Vol. 18](https://www.emisa-journal.org/emisa/article/view/320)
14. [AI IDE Stack Comparison: VS Code, JetBrains AI, and Cursor](https://essamamdani.com/blog/ai-ide-stack-comparison-vscode-jetbrains-cursor-2026)
15. [Does a Language Server Save Tokens for Coding Agents? A Measurement Methodology and Preliminary Study, arXiv:2608.13568](https://arxiv.org/abs/2608.13568)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Frameworks, libraries, and APIs

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/language-server-protocol

English original: https://www.edgepedia.org/language-server-protocol
