24. The language server and its protocol
What an editor knows of CompMD comes from one program, the language server compmd-lsp: its diagnostics are the processor's, its project is read as your host reads it, and its drawing plan is what Neovim and Emacs draw in place. Chapters 21, 22 and 23 are the editors that use it; this chapter is the server itself, for a developer who runs it, configures it, or brings it to another editor: installing it, the project it reads, its settings, and its protocol, the Language Server Protocol's messages it answers and the ones of its own.
The server#
compmd-lsp is a Rust binary, released with the packages at the same version: on npm (npm install -g @compmd/lsp, which installs the binary for the platform through a package of its own), on crates.io (cargo install compmd-lsp), and as six binaries on each GitHub release, with their SHA256SUMS. An editor starts it with compmd-lsp --stdio and speaks the protocol with it over its standard input and output.
Each open document has a worker of its own, which keeps the document's processor and applies each edit to it incrementally, so the diagnostics follow the writer's typing. .md files are read as the spec reads them: CompMD with an md key in their frontmatter, plain Markdown without, which gets Markdown's features only.
The project it reads#
A document's root is the nearest folder above it with a compmd.config.json, or a package.json with a compmd object, as @compmd/vite finds a page's; else the workspace folder the editor gives (mostly a git repository's top); else the document's own folder. The server reads that root as @compmd/vite and the Tauri plugin do (compmd-project, chapter 20): the configuration (chapter 25), the installed packs and their signatures, the document's imports and data files, and the components in the folder, whose props it reads (W104). A change to any of them, on the disk, reloads the open documents. What the writer sees is what the app renders.
A document that imports one of CompMD's own packs, which the site can't load, is warned about on its line, and the server asks once per project whether to add npm as their registry; Add, or the import's quick fix, writes it into the configuration.
Its settings#
The settings are the editor's, under compmd, sent as the protocol's workspace/configuration (or initializationOptions), and applied without a restart:
| Setting | Default | What it is |
|---|---|---|
packs.suggest |
[] |
your own scopes to suggest packs from, besides CompMD's and the project's |
packs.network |
true |
whether the server may reach registries at all |
preview.browser |
the system's | the command the preview opens the page with |
render.enabled |
true |
the drawing at all |
render.heading, code, table, list, checkbox, quote, callout, rule, container, component, param, frontmatter |
true |
each element drawn: true, false or its options (heading.marker, heading.fill, code.language, code.fence, table.borders, container.gutter, frontmatter.rules, frontmatter.dim) |
render.glyphs |
unicode |
unicode, nerd (Nerd Font icons) or ascii; render.glyph overrides one by its name |
render.reveal |
line |
what gives way under the cursor: a mark's line, the whole element, or nothing (off) |
render.markdownFiles |
true |
.md files drawn |
render.values |
true |
values as inlay hints |
markdown.enabled |
true |
.md files served at all |
markdown.wikiLinks |
false |
wiki links in a .md file that isn't CompMD |
tables.alignOnType, tables.alignOnFormat |
true |
tables aligned as they are typed, and when the document is formatted |
links.enabled |
true |
links and headings: definitions, references, hovers, symbols |
links.completion, links.diagnostics, links.rename, links.wiki |
true |
each of their features, and wiki links where a document has them |
trace |
off |
the server's log: off, messages or verbose |
The server's README has each with its options. Each editor's chapter shows where its settings go.
The protocol's messages#
What the server answers of the Language Server Protocol, each from the processor's own state:
| Message | What it gives |
|---|---|
textDocument/publishDiagnostics |
the processor's diagnostics after each edit, with their code, severity, related places and a link to the spec's catalogue; a link's broken-link warning, the server's own |
textDocument/completion |
components (with the import that takes one, in the frontmatter's own syntax), params and their values, slots, frontmatter keys, md. paths, functions, a code block's language; a link's files, anchors, reference labels and wiki pages |
textDocument/hover, textDocument/signatureHelp |
a component's signature and a function's documentation, from the processor's catalogue; a link's target section |
textDocument/definition, textDocument/references |
a component's definition or file; a link's file, heading or passage; every link to a heading, across the project |
textDocument/rename, workspace/willRenameFiles |
a heading, a reference's label or a file renamed, with every link to it |
textDocument/codeAction |
the diagnostics' quick fixes, Generate or Update the signature of a host-language component, a table's rows and columns, Read as CompMD, Open preview |
textDocument/documentSymbol, workspace/symbol |
headings, containers, slots, leaves and definitions; every file's headings |
textDocument/foldingRange |
the frontmatter, containers, definitions and code blocks |
textDocument/semanticTokens/full, /range |
CompMD's syntax by the token names every CompMD grammar shares |
textDocument/inlayHint |
beside each interpolation and expression param, the value the last render gave it |
textDocument/formatting, rangeFormatting, onTypeFormatting |
tables aligned, by formatting and as | or a newline is typed |
Positions are counted in UTF-32 or UTF-8 when the editor offers them, else UTF-16.
CompMD's own messages#
| Message | Direction | What it is |
|---|---|---|
compmd/drawing |
request, client to server | the drawing plan of a range of a document (below) |
compmd/drawing/refresh |
request, server to client | ask again for the plans of the documents drawn: the settings changed, or an edit moved marks beyond the lines it changed |
compmd/blocks |
request, client to server | each code block a code renderer renders, with its language, its component (the renderer) and the range of its text, and the language's other names from its pack (aliases, languageId, extensions), for an editor package to hand the requests inside it to the writer's server for that language |
compmd.preview |
command (workspace/executeCommand) |
the document's page, served on 127.0.0.1 under a random token, the frames on a port of their own, sent each change, and the browser opened at it; its argument is the document's URI, its answer the page's address |
compmd.addRegistry |
command | npm written as the registry of CompMD's own packs into the configuration |
Drawing in an editor#
The server plans what an editor draws in place of, or beside, the text; the editor's renderer only applies the plan, with no knowledge of Markdown or CompMD (a document drawn in the editor). An editor that wants to draw:
-
Declares it in
initialize, in its capabilities'experimental: the primitives it can draw, whether it takes refreshes, and whether it hides Markdown's inline markup in a.mdfile (concealsMarkdown, as Neovim's Markdown parser does), so a table's cells are measured as they show:{ "experimental": { "compmd": { "drawing": { "primitives": ["hide", "replace", "insert", "line", "prefix", "style", "block"], "refreshSupport": true, "concealsMarkdown": false } } } }The server answers
"experimental": { "compmd": { "drawing": {} } }, and plans only with the primitives declared: withoutline, a code block's language goes on its fence; withoutprefix, a container's gutter is aninserton each line. A client that declares nothing gets no plan. -
Asks for the plan of the lines it shows, with some around them, after each edit, on a scroll that shows lines its plan doesn't cover, and when its window is resized:
compmd/drawing, with the document's versioned identifier, the range and, from a renderer that drawsblock, its window's width and whether it wraps:{ "textDocument": { "uri": "file:///notes/harbour.comp.md", "version": 4 }, "range": { "start": { "line": 0, "character": 0 }, "end": { "line": 60, "character": 0 } }, "window": { "width": 80, "wrap": true } }The answer is
{ version, marks }, the plan of the text as the server holds it; a plan for an older version than the buffer's is dropped, and what a plan covers is its range. -
Draws each mark with its own API. A mark is one of seven primitives:
hide(a range not shown),replace(a range shown as a text),insert(a text at a position),line(a line of its own, above or below, filled to the window's edge),prefix(a text before each of some lines, wrapped ones too),style(a range in a style), andblock(lines not shown, and rows in their place: a table wider than a window that wraps, laid out to fit it). A style is a name,compmd.heading.2, never a colour: the renderer maps it to its highlight group or face, so a colour scheme restyles it. -
Reveals: each mark carries
reveal, the lines it gives way on; while the cursor is on one of them, the renderer leaves the mark out, so the writer edits the text as written. -
Asks again when the server sends
compmd/drawing/refresh.
Neovim's renderer draws every primitive but prefix (lua/compmd/drawing.lua), Emacs's all seven (compmd-ts-drawing.el); each is a few hundred lines, and a good start for another editor's. The server's README has each mark's JSON, the table of what is drawn for each element with and without each primitive, and the styles.
Next: the appendices, starting with the configuration's reference.