Why You Should Know Language Server Protocol
Why You Should Know Language Server Protocol
If you're interested in language development, you know that tools are fundamental. Language Server Protocol allows developers to easily support creations in more editors.
Join the DZone community and get the full member experience.Join For Free
How to Transform Your Business in the Digital Age: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.
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. — Language Server Protocol
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. There are 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
- 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 edits 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 found.
- 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.
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 use demons to support many languages. Then, a group of developers created OmniSharp, a set of tools and 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 definition, find references, etc.
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 blame somebody for it. However, 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 editors for developers. One of the keys to its success its smart organization and innovations such as the Language Server Protocol.
Microsoft contributes to open source because it’s good for business. It allows us 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.
If you are interested in language development, you know that tools to support it are fundamental to create a successful programming language. The Language Server Protocol allows language designers and tool developers 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 you waiting for? There is even an example implementation in the node.
Published at DZone with permission of Federico Tomassetti , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.