The Language Server Protocol (LSP) defines how an editor talks to language tooling. The editor is the client, the language tool is the server. You write one server per language and every editor that speaks the protocol can use it.
Messages Travel over JSON-RPC
When the cursor rests on a name, the editor sends a textDocument/hover request. Lines and columns are zero-based:
The server answers with the type and documentation of the name at that position:
What Lives Inside the Server
A good language server reuses the compiler front end, but with different expectations:
- Code is usually half-written and broken; the parser must keep building a tree despite errors.
- Instead of recompiling the whole project on every keystroke, only the changed parts are recomputed.
- Answers must come back very quickly, or the editor stutters while you type.
When the compiler and the language server share the same parser and type checker, the error you see in the editor is always the error you get from the build.
Sources
- Language Server Protocol Specification, version 3.17. microsoft.github.io/language-server-protocol
- JSON-RPC 2.0 Specification. jsonrpc.org