Skip to content

Language Server Protocol

This blog briefs the LSP(language server protocol) specification.

Brief Notes of LSP

LSP is the acronym of Language Server Protocol defined by Microsoft for Vscode. This series introduce the [email protected] as haskell implementation roots on it.

LSP base protocol is an http request like protocol. Its base protocol contains header and content. RPC-JSON is used as the format of content. A rpc-json pre-defines four fields(jsonrpc, id, method,param) at the root level. Extended fields at root level is not correct extension. Hence, a typical request look like the snippet below.

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "textDocument/completion",
  "params": {
    "textDocument": {
      "uri": "file:///path/to/your/code/file.js"
    },
    "position": {
      "line": 8,
      "character": 15
    }
  }
}
The client refers to the language respective plugin instead of vscode itself. Server must send a response back after receiving a request even though the response might carry nothing but null. Notification could send out both by server and client and it shouldn't receive a response.

There are many json structures to communicate between server and client. Then there many language features with different method names, for example, textDocument/completion, textDocument/definition and so forth.

Specification is cramped with every stripe of details, hence, a brief skim reading to get the ideas are enough. In the other words, it should be the encyclopedia to look up when trying to implement a language feature, but not learn it carefully before really coding.