Language Server Protocol (LSP)
From the Wikipedia:
The Language Server Protocol (LSP) is an open, JSON-RPC-based protocol for use between source code editors or integrated development environments (IDEs) and servers that provide “language intelligence tools” programming language-specific features like code completion, syntax highlighting and marking of warnings and errors, as well as refactoring routines.
The goal of the protocol is to allow programming language support, for instance syntax highlighting, error messages highlighting, completion, etc. to be implemented and distributed independently of any given editor or IDE.
In the early 2020s, LSP quickly became a “norm” for language intelligence tools providers.
The Language Server Protocol allows for decoupling language services from the editor so that the services may be contained within a general-purpose language server. Any editor can inherit sophisticated support for many different languages by making use of existing language servers. Similarly, a programmer involved with the development of a new programming language can make services for that language available to existing editing tools.
How it works
For instance, in VS Code, a language server has two parts:
- Language Client: A normal VS Code extension written in JavaScript / TypeScript. This extension has access to all VS Code Namespace API.
- Language Server: A language analysis tool running in a separate process.
There are two benefits of running the Language Server in a separate process:
The analysis tool can be implemented in any languages, as long as it can communicate with the Language Client following the Language Server Protocol.
As language analysis tools are often heavy on CPU and Memory usage, running them in separate process avoids performance cost.
Here is an illustration of VS Code running two Language Server extensions:
The HTML Language Client and PHP Language Client on the left are normal VS Code extensions written in TypeScript.
Each of them instantiates a corresponding Language Server and communicates with them through LSP.
Although the PHP Language Server on the right is written in PHP, it can still communicate with the PHP Language Client through LSP.
Tools for implementing LSP
- Langium is an open source language engineering tool with first-class support for the Language Server Protocol, written in TypeScript and running in Node.js.
References
- Follow the tutorial LSP Sample - A simple Language Server for plain text files