Why You Should Know the Language Server Protocol
The Language Server protocol is used between a tool (the client) and a language smartness provider (the server) to integrate features like auto complete, goto definition, find all references and alike into the tool.
In other words, it is a standard way to communicate between editor and language tools, like linters, style controls and, generally speaking, everything that needs to check the source code.
From my point of view this a game changer because it makes possible to reuse existing pieces of infrastructure at all levels. If you are a language developer now you can build just one language server to support many editors, including Visual Studio Code and Eclipse Che or Eclipse IDE. If you are creating an editor, you can immediately support many languages implementing a single protocol. And there already libraries in languages such diverse as PHP, Java or Haskell if you need to work with the protocol from your own software.
Description of the Protocol
Microsoft itself provides a simple description of the typical session:
- The user opens a document. The editor notifies the language server that the user opened a document (
didOpen
) and that it has the document in memory. - The user edit the document. The editor notifies the server about the document changes (
didChange
). The language server analyses this information and notifies the tool with the errors and warnings (diagnostics
) that it has founds. - The user request some specific command. The user ask for a definition (
definition
) of a symbol, completion (completion
) of a text from the current cursor position, help with a signature (signatureHelp
), etc. The editor sends the proper request to the server which responds accordingly. The editor can then use this information to satisfy the request of the user. - The user closes the document. The editor notifies the language server that the user closed a document (
didClose)
and it hasn’t the document in memory anymore.
Observations
There is also obviously a complete description of the protocol in its repository. It supports requests that are common to all text, but in a way that is geared toward programming languages. For instance, it exists a request to highlight text in a document (documentHighlight
), which could be used for all kinds of texts, since highlighting something is useful in many contexts. But LSP is designed to better support programming languages with fields dedicated to read/write access to a symbol. It’s important to remember that the protocol supports only textual representation, you can’t use it to operate on binary code.
While it has been designed with source code editors and local development in mind, nothing says that it can’t be used to communicate with remote services and other software. Latency may be a problem for some feature that requires quick back-and-forth, such as autocompletion. But it’s easy to imagine it working with tools like the MySQL Workbench, to check your SQL queries.
History and Current Implementations
According to the history of LSP the first inspiration was editors like Vim and Emacs, that uses demons to support many languages. Then a group of developers created OmniSharp, a set of tools of libraries to simplify the use of .NET languages across many platforms and editors. Finally, Microsoft builds up on this work to create a protocol that is language neutral and relies on JSON-RPC. While at the beginning it was mostly thought for linters, it has now been expanded to be used for all purposes: refactoring, syntax checking and code completion.
Other than the aforementioned Visual Studio Code, Eclipse Che and Eclipse IDE teams, there is language server protocol support for emacs and GNOME Builder. There is a list of implementations of 23 Language Servers, where you can see also that there are not just programming languages, but specifications, like API Elements, for Swagger and API Blueprint. You can also look up on LangServer.org, where you can find a more detailed list of features supported by each implementation, such as Code Completion, Go to Definiton, Find References…
Why It’s Open
You could never accuse Microsoft of not understanding efficiency, but you could have accused it of not caring about open source. When Microsoft started contributing to the open source there was much skepticism and doubt. And given certain previous statements and action of the company it was hard to be blame somebody for it. But now we can say that the company has paid its dues. It releases open source software, it builds on open source and it creates components meant to be used by everyone. That is indeed a drastic change. Among its creations there is Visual Studio Code, which is one of the best editor for developers. One of the keys of its success it’s the smart organization, and innovations such as the Language Server Protocol.
Microsoft contribute to open source because it’s good for business. It allows to work with other companies without the need of much coordination. It works just by supporting a common platform upon which everybody can build its software and services.
Conclusions
If you are interested in language development you know that tools to support it are fundamental to create a successful programming language. And the Language Server Protocol allows language designer and tools developer to support new creations in more editors, using a simple protocol. Even if you just want to create tools for static code analysis to help developers and simplify your life, you are going to find it useful to make it available to many developers out there. What are waiting for? There is even an example implementation in node.
Reference: | Why You Should Know the Language Server Protocol from our JCG partner Federico Tomassetti at the Federico Tomassetti blog. |