当前位置:网站首页>The editor is used every day. What is the working principle of language service protocol?
The editor is used every day. What is the working principle of language service protocol?
2022-06-25 12:47:00 【Coconut brine Engineer】
language-service-protocol What is it?
As a development engineer , The most common tool we use every day is the editor . In use , There are some specific functions , such as : Add autocomplete for programming languages 、 Go to definition 、 Hover document, etc . Traditionally , This must be repeated for each individual development tool , Because each tool provides different API To achieve the same function .
language-service To provide language specific intelligence and communication protocols , Enable interprocess communication development tools to communicate .
language-service-protocol( Hereinafter referred to as “LSP”) The purpose of is to standardize the protocol of how such servers and development tools communicate . adopt LSP, A single voice server can be reused in multiple development tools , Thus, it can support multiple languages .
language-service-protocol How it works
language-service Run as a separate process , Development tools use JSON-RPC The language protocol on communicates with the server .
JSON-RPC, Is a stateless and lightweight remote procedure call (RPC) Transmission protocol , Its content is transmitted through JSON Mainly .
JSON-RPC The format is like this :
Content-Length: ...\r\n
\r\n
{
"jsonrpc": "2.0",
"id": 1,
"method": "textDocument/didOpen",
"params": {
...
}
}
method It is an event triggered by the user , such as :
- textDocument/completion Complete the code from the user cursor
- textDocument/signatureHelp Get the function signature information from the user cursor
- textDocument/definition Go from the user click location to definition
- …
The following figure shows a tool and language-service An example of how to communicate during an editing session :
- The user opens the file in the tool : Tool notification language-service The document is open . from now on , The authenticity of the document content no longer exists in the file system , Instead, it is stored in memory by the tool . Now you have to be in tools and language-service Sync content between .
- Users edit : The tool notifies you of document changes language-service, And the language of the document is represented by language-service to update .language-service Will analyze this information , And return the detected errors and warnings to the tool .
- The user executes on the symbol of the open document “ Go to definition ”: The tool sends a request with two parameters ( file URL、 Go to the defined text location ) Send to the server . The server responds according to these two parameters in the request .
- The user closes the document : The tool sends a shutdown notification , notice language-service The document is no longer in memory . The current content is now up to date on the file system .
“ Go to definition ” An example of (c++)
request :
{
"jsonrpc": "2.0",
"id" : 1,
"method": "textDocument/definition",
"params": {
"textDocument": {
"uri": "file:///p%3A/mseng/VSCode/Playgrounds/cpp/use.cpp"
},
"position": {
"line": 3,
"character": 12
}
}
}
Respond :
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"uri": "file:///p%3A/mseng/VSCode/Playgrounds/cpp/provide.cpp",
"range": {
"start": {
"line": 0,
"character": 4
},
"end": {
"line": 0,
"character": 11
}
}
}
}
When users use different languages , Development tools typically launch one for each programming language language-service.
️ Be careful :language-service The actual integration with the tool is not by LSP Defined , But decided by the tool implementer .
LSP Provider and consumer Libraries SDK
- Each development tool usually provides one for integration language-service The library of , For example, for JavaScript/Typescript, There are language clients npm modular .
- For different implementation languages language-service SDK, There is one SDK To implement a specific language language-service. For example, to use Node.js Realization language-service, There is a language server npm modular .
LSP What problems have been solved
Previously, for editors , A interlinked language should implement its own set of code , So there are N Chinese language ,M Of IDE, It means writing N*M Set of codes , There is now a LSP As a middle layer of abstraction , You just need to write N+M The condom code is enough .
Another advantage is language-service Run in a separate process :
- language-service Belong to CPU Intensive program , In order to verify a file correctly , Need to parse a large number of files ( structure AST-> Static program analysis ), stay VSCode Plug in , All plug-ins in each window share one Extension Host, If the language service gets stuck , All plug-ins will be affected . Run in a unique process language-service This performance problem can be avoided .
边栏推荐
- Select randomly by weight [prefix and + dichotomy + random target]
- Geospatial search: implementation principle of KD tree
- Ramda rejects objects with null and empty object values in the data
- Parse JSON format data and save it to entity class
- (3) Pyqt5 tutorial -- > signal and slot preliminary test
- visual studio2019链接opencv
- Elemntui's select+tree implements the search function
- Draw the satellite sky map according to the azimuth and elevation of the satellite (QT Implementation)
- JS SMS countdown implementation (simple code)
- 模块五(微博评论)
猜你喜欢

百度搜索稳定性问题分析的故事
模块五(微博评论)

Embedded software development written examination and interview notes (latest update: February 17, 2022)

高性能负载均衡架构如何实现?
![Select randomly by weight [prefix and + dichotomy + random target]](/img/84/7f930f55f8006a4bf6e23ef05676ac.png)
Select randomly by weight [prefix and + dichotomy + random target]

Singleton mode in PHP to reduce memory consumption

冷启动的最优解决方案

Idea2017 how to set not to automatically open a project at startup

2021-09-02

画图常用配色
随机推荐
Total number of MySQL statistics, used and unused
torch.tensor拼接与list(tensors)
聊聊高可用的 11 个关键技巧
Happy shopkeeper source code -- Introduction to happy shopkeeper system development mode
When MySQL queries fields in JSON format, it takes a property value of JSON data
(2) Pyqt5 tutorial -- > using qtdesigner to separate interface code
Circular exercises of JS
Penetration tool environment -- use of cknife Chinese kitchen knife
PHP numeric array sorting and associative array sorting
Baidu search stability analysis story
2021-09-22
冷启动的最优解决方案
Negative sample image used in yolov5 training
Event triggered when El select Clear clears content
C program linking SQLSERVER database: instance failed
Elemntui's select+tree implements the search function
node. JS architecture optimization: reverse proxy and cache service
The amount is verified, and two zeros are spliced by integers during echo
3+1保障:高可用系统稳定性是如何炼成的?
地理空间搜索:kd树的实现原理