Avatar
Krishna Biradar
  • August 14, 2024

What is LSP? A Simple Overview

Language Server Protocol (LSP) is a standard way for coding tools like text editors to talk to language servers. It is not restricted to programming languages though, it can be used for any kind of text-based language or Domain specific languages (DSL). It is based on JSON-RPC protocol and the LSP specification defines a set of JSON-RPC messages (requests, responses, and notifications) exchanged between LSP clients and LSP servers.

An LSP message consists of two parts:
  1. Header
  2. Body of the message

Lsp Message format LSP message format

According to the LSP spec there are three kinds of messages :

  1. Request message - A request message describes communication between the client and server. Each processed request must return a response to the sender. Every request message must contain and id, method and params
  2. Response message - A response message is sent in response to a request message. In case there is no any appropriate response for a given request, the result is set to null to comply with JSON-RPC rules. Every response message must contain the id field indicating the request id to which this response belongs to and also contains the result or error details
  3. Notification message - A notification message does not have any response message, it just acts like an event. No any response is needed for a notification message.

Additional Features:

  • Cancellation: Stops long-running requests
  • Progress: Updates on ongoing operations

The life cycle of starting and ending the communication is managed by the LSP client (e.g. a tool like VS Code or Neovim), The client decides when to start communicating with the server and when to shutdown the server.

Flow Life cycle of a LSP connection between a LSP client and LSP server

Capabilities:

  • Capabilities group language features
  • Not all servers support all features
  • Both tools and servers announce their supported features
  • Capabilities are exchanged during initialization

Request, Notification, and Response Ordering:

  • Responses generally sent in same order as requests received
  • Servers may use parallel execution and reorder responses
  • Reordering allowed if it doesn’t affect correctness
    • Example: ‘textDocument/completion’ and ‘textDocument/signatureHelp’ can be reordered
    • Some requests (like ‘textDocument/definition’ and ‘textDocument/rename’) should maintain order

Protocol Rules:

  • Prefers object-type parameters
  • One server typically serves one tool
  • No built-in support for server sharing between tools

Inspecting LSP messages on VS Code

Note that this log is useful for developing and testing the Language Server and the logs can be lengthy, just using the editor for a few seconds could generate thousands of lines of LSP log.

  1. Create a new directory
  2. Create .vscode directory and create a settings.json file inside that directory
  3. Put the following contents inside settings.json
{
  "html.trace.server": "verbose",
  "json.trace.server": "verbose",
  "typescript.tsserver.log": "verbose",
  "typescript.tsserver.enableTracing": true
}
  1. Open new terminal in VS Code

  2. Click on the output tab, and select Typescript from the dropdown, the log file will be printed on the console, you can check the contents of the log file to inspect the LSP messages vs-code-output.png

lsp
Buy Me A Coffee