MACIEK905_DREAMSTIME_50188994
Code Maciek905 Dreamstime L 50188994

检查语言服务器协议及其实现

2022年3月24日
The Language Server Protocol (LSP) lets you transcend IDEs. Here’s what you need to know.

What you’ll learn:

  • 语言服务器协议背后的详细信息。
  • 为什么它是必要的。
  • 从哪里开始。

您很有可能您已经使用了一种软件开发工具来利用语言服务器协议(LSP),但甚至没有意识到。LSP旨在促进不同语言的支持,这就是为什么您最喜欢的工具可能比过去更多的语言支持语言的原因之一。

LSP定义为一组开放的规则,描述了语言客户和语言服务器应如何通信(Fig. 1)。所有服务均由服务器处理,并通过协议暴露于客户端。客户端是开发人员使用的标准文本编辑器或使用的集成开发环境(IDE),而服务器是包含有关给定语言的所有智能的过程。


Microsoft2015年开发了该协议,以帮助分开的语言工具和编辑器,从而使编程语言支持可以独立于任何给定编辑器或IDE独立实施和分发。与某些编程环境一样,LSP出于必要而不是创新而增长。在LSP之前,每个开发工具都需要重复大多数工作,因为每个工具都提供了不同的应用程序编程接口(API)来实现相同的功能。

支持这些功能,例如编辑或IDE中编程语言的源代码自动完成或“转到定义”,通常是一个具有挑战性且耗时的过程。它需要编写域模型(扫描仪,解析器,类型检查器,构建器等),用编辑器或IDE的编程语言编写。

例如,Eclipse CDTplugin, which supports C/C++ in theEclipse IDE, is written in Java since the IDE itself also is written in Java. Following this approach would mean implementing a C/C++ domain model in the TypeScript language forVisual Studio Codeand a separate domain model in C# forMicrosoft Visual Studio

How the LSP Functions

可以将客户和服务器视为代码编辑器和语言工具在简单的文本消息中通信。这些消息具有类似于HTTP的标题和JSON-RPCcontent, and they can originate from the client or server. The JSON-RPC protocol defines requests, responses, and notifications and a few basic rules around them(Fig. 2)。One of the key features is that it's designed to work asynchronously, so that clients/servers can deal with messages out of order and with a level of parallelism.


语言服务器协议是关于减少精力的全部内容,没有人喜欢编码时不懈的重复。这就是开发LSP的原因 - JSON-RPC协议允许编辑器/IDE与语言服务器交谈,从而消除该工作和重复。

朗的另一个优点uage server operating in a dedicated process is that it mitigates performance issues related to a single process model. The actual transport channel can either be stdio, sockets, named pipes, or node ipc if both the client and server are written in Node.js.

以下是LSP过程如何工作的示例:

  1. 用户在工具中打开文件(即文档),并通知语言服务器已打开文档(“ textDocument/didopen”)。在这一点上,关于文档内容的真相不再在文件系统上。相反,它由该工具保存在内存中,并保留在整个过程中。
  2. As the user makes edits, the tool notifies the server about the document change ('textDocument/didChange') and the semantic information of the program is updated by the language server. The language server analyzes this information during the process and notifies the tool of the detected errors and warnings (“textDocument/publishDiagnostics”).
  3. 完成后,用户在编辑器中的符号上执行“转到定义”:该工具将带有两个参数的“ textDocument/definity”请求:文档URI和文本位置,从启动到定义请求的位置服务器。然后,服务器以文档URI和符号定义在文档中的位置进行响应。
  4. 该过程完成后,用户关闭文档(文件),并从工具发送“ textDocument/didclose”通知。它告知语言服务器,该文档现在不再在内存中,并且当前内容现在已在文件系统上最新。

The example highlights how the protocol communicates with the server at the document reference and position level. The data types are programming-language-neutral, meaning they apply to all programming languages.

It's important to note that the data types aren’t at the level of a programming-language domain model, which would usually provide abstract syntax trees and compiler symbols, such as resolved-types, namespaces, and others. Because the data types are simple and the programming language is neutral, it simplifies the protocol substantially.

LSP不是通用的解决方案

While the Language Server Protocol is immensely beneficial, it's not an all-encompassing solution and has a few drawbacks. Some developersfoundthat some servers behaved differently to specific requests or responded differently than expected. Other server developers might rely on one client implementation to develop their server, leading to discrepancies between expected and actual results. Even protocol extensions, such as编码, can be server-specific, making it difficult for clients to write generic code for expected responses.

In addition, not every server will be configured in the same way, and not every language server can support all features defined by the protocol, which further negates the one-stop solution. Instead, the LSP provides capabilities that group a set of language features.

A development tool and the language server announce their supported features using capabilities. For example, a server announces that it can handle the “textDocument/definition” request, but it might not handle the “workspace/symbol” request.

为此,无数支持的语言继续从LSP中受益。其中包括新语言,例如Rust,这可以利用协议来支持现代IDE和文本编辑器。这只是语言服务器协议及其可以完成的简单概述。可以在Microsoft上找到更多深入的信息,包括规格和实现githubpage.

赞助

具有集成变压器的汽车,500兆瓦,5 kVRMS隔离的DC/DC模块

具有集成变压器的汽车,500兆瓦,5 kVRMS隔离的DC/DC模块

336-W Auxless AC/DC Power Supply Reference Design with 80 Plus Platinum Compatible Performance

A fully assembled board has been developed for testing and performance validation only, and is not available for sale.. Design files. Download ready-to…

150 ma单通道线性LED驱动器

TPS92612 40-V, 150-mA Single-Channel Linear LED Driver and Constant-Current Source With Protection datasheet

LLC resonant controller with ultra-low standby power and high voltage startup

LLC resonant controller with ultra-low standby power and high voltage startup

表达您的意见!

This site requires you to register or login to post a comment.
尚未添加评论。想开始对话吗?
Baidu