Hey there! Thanks for checking out my page. If you'd like to chat, email me at luciacerchie at gmail dot com.

The Language Server Protocol

Code editors these days come with some fancy schmancy features like code completion|, syntax highlighting, and refactoring routines.

How do language developers support these features? Let's find out.


Before the LSP, language developers faced an issue when it came to supporting different code editors.

The work (including writing type checkers and builders) involved in supporting features like goToDefinition and autocomplete had to be repeated per editor.

imageofgraph

This means the work of supporting, say 3 languages in 3 editors is the work of 3x3.

1

But once the unifying Language Server Protocol was introduced by Microsoft in 2015[1], the work of developers to support 3 languages in 3 editors was reduced to 3x1!


Ok, so what is the Language Server Protocol? Well, it uses JSON-RPC to define a common protocol for code editors and language servers to interact with.

These servers using the protocol can handle requests from each code editor, or client, asynchronously. To see what that means, press 'send' on the animation below.

Request 1
Client
Server
Request 2

So what happens, say, when you hover over (or, if you're on mobile here, click on) a variableName and click goToDefinition?

X
variableName = newValue;

Server Requests

When you click on goToDefinition, you trigger a request from the editor, the client, to the server. This request is for the textDocument/definition and includes the parameters {documentURI, position}.

The server then responds with the Location of the definition, so the client can send you there.

(And because it can handle these requests async, it can handle multiple requests at once.)

{Document URI, position}
client
server

In this way, the Language Server Protocol handles requests from code editors to support features for different languages in each client.

Resources

This page has provided a high-level overview -- it's definitely the very tip of the iceberg, even conceptually. Here are some resources to help you dive deeper.

Official Documents

Tutorials

About the author

Lucia Cerchie's headshot

Lucia Cerchie is a developer experience professional who loves to learn in public. You can find more of her content on her Github page and her blog. Reach out via LinkedIn.

Learn what Lucia learned by making this site.

[1] Footnote: Running curl -X GET https://api.github.com/repos/microsoft/language-server-protocol reveals "created_at": "2015-09-04T09:24:55Z"