Landin what the language server answers discuss source

What `refine lsp` answers

refine lsp is a language server: an editor starts it, talks to it over standard input and output, and gets back what refine would say about the program a file belongs to, as the editor's own diagnostics, navigation, hover text, formatting and quick fixes. The packages under highlight/ start it, as In an editor says. It runs the compiler's own stages, the same loader and the same checks, so what an editor shows is what a build reports, except where this page says otherwise.

refine lsp --stdio is the same: an editor's language client often adds --stdio, and the server accepts it. refine lsp --help prints its help. Any other argument after lsp is a misuse, refused with status 2.

D253 names a decision recorded in spec.md, and [1410] a numbered paragraph of tour.md or spec.md; refine explain D253 prints either.

Where a file belongs

A module is a directory [1410], so a file is analysed with every other .ldn file in its own directory, as the entry module, and its imports are found under the server's roots [1420] in order. The roots are, in this order of preference:

given roots
initializationOptions.roots, a list of file: URIs those, in order
the editor's workspace folders each folder, in order
the editor's root URI that directory

The three sources accept a folder URI with or without a trailing slash; both forms name the same root.

The library the server ships with, core, hosted and platform, follows those roots, found beside the executable as refine finds it, and imports of those three namespaces resolve from it alone. So an editor needs no root for the library, and a file analysed alone still reaches it. For this repository the root is the checkout, which is how the examples are checked: refine check --root=. examples/derived_hosted.

A buffer the editor has not saved is read in place of the file: what the server checks is what is on the screen, and the files around it are what is on disk. That holds however the loader reaches the file (D1069): when a root or an import directory is a symbolic link, the buffer opened under one path is read where an import names the other, a change to it rechecks the modules that read it, and reports about it carry the URI the editor opened. A buffer in a directory that is not on disk yet is the first source of a new module, analysed as that module and found by an import of it (D1070). A buffer with no file at all, such as an editor's untitled one, is analysed alone, as refine check FILE would analyse it, with only the library to import from, and so is every file when there are no roots. fixture.meta means nothing to the server: a fixture directory is analysed as the module its files make, which is not always what the test program compiles.

Build settings choose what --target=, --level=, --option= and --firmware-entry= choose:

{"roots": ["file:///home/me/landin"],
 "target": "cortex-m0",
 "level": "armv7-m",
 "firmwareEntry": "start"}

target is any name --target= takes: linux-x86-64, linux-arm64, darwin-arm64, freebsd-x86-64, freebsd-arm64, linux-rv64, cortex-m0, rv32, synthetic-32, xtensa, windows-x86-64 or windows-arm64. Both read one mapping, and both default to the compiler's own host (D257); refine targets lists the names and each target's CPU feature levels. A server built for a host no target describes checks for synthetic-32 and says, through window/showMessage, that a target has to be named. level is a CPU feature level of that target's family, read after it as refine reads it, and the target's default when absent. An option is named in lowercase letters, digits and underscores, and its value is a string. For example, option enabled: bool = true can be overridden with "options": {"enabled": "false"}. A name or value of another shape is reported once, through window/showMessage, and the rest are used.

The setting is the session's, and it reaches every module the editor opens (D1068). A module whose reached sources declare the option is checked with it, and a value is true, false or signed decimal integer text that fits the declared type; one that does not fit is refused there, as --option= refuses it. A module that declares no option of that name is checked without it, where refine would refuse the command with L0002. The server says so once in a session for each option, through window/logMessage, which an editor keeps in its log: a misspelt name is found there.

firmwareEntry is a nonempty routine name for a firmware target: cortex-m0, rv32 or xtensa. When selected, analysis checks the same entry shape as a firmware build and publishes L0502 at an invalid candidate, or at the first entry-module source if it is missing. With no selection, analysis makes no firmware entry claim. A device description's profile applies on every firmware target: cortex-m0, rv32 and xtensa. There a module is checked with the profile of the description it imports (D543), as refine checks it: a handler at a slot that description reserves is L0347 naming the device, a description's own refusals are published at its directives, and with no description the library's constrained reference applies. A description the module imports is published as any imported module is; the reference, which the loader adds and no import names, is published only when something in it is refused. The scripted session firmware-description holds the server to it. Nor does it check the link. L0500, a static Linux link's refusal of a C library or of a C symbol nothing defines, depends on a build's --link, which the server is not given, so refine check reports it and the server does not.

When several settings are refused, each error appears on its own line in that one notification.

What it answers

message what the server does
textDocument/didOpen, didChange, didClose notifications it takes: holds, updates or drops the editor's buffer, then analyses
workspace/didChangeWatchedFiles a notification it takes: rechecks what read a changed file
textDocument/publishDiagnostics a notification it sends: every diagnostic of each source of the module, after every change
textDocument/definition a request: where the name under the cursor is declared
textDocument/hover a request: what the name or expression under the cursor is, and its doc comment
textDocument/formatting a request: the one layout (D252), as edits
textDocument/codeAction a request: each fix of a diagnostic the range touches, as a quick fix; ranges have exclusive ends, while an empty range acts as a cursor position

A diagnostic carries its catalogue code, a link to its explanation on the reading copy of docs/diagnostics.md, every secondary label as related information, and each note at the end of its message. A warning is a warning, and an error an error. The server reports the catalogue's recommended warnings. Compilation warning controls are refused at server startup; they do not configure an editor session. An advisory without a safe edit has no code action. A report with no place in any source, such as a source that cannot be read, goes to window/showMessage when a module's report first holds it, and is not shown again while each later report of that module still does. In a program that prepares a stack and selects a panic handler, the server emits the program's code in memory at none, as refine check does, and reports the L0508 a build at that profile would (D572); a handler that fits at none may still be refused at another profile.

Every source the module reads is published, so an error in a file the editor has not opened is still shown, and one that is fixed is cleared. Closing the last open file of a module clears sources only that module reported. Sources also reported by another open module are reanalysed through that module. Discarding an imported overlay also rechecks open modules that read it, using the remaining overlay or disk bytes.

When the editor reports a disk change through workspace/didChangeWatchedFiles, the server rechecks every open module whose last report read that path or listed its directory, even if that file is not open. A report lists the directory of every source it read, every import directory it found empty, and every directory it looked for an import in and did not find, including those under roots searched before the one that had it. So adding a source to a reached module, giving an empty module its first source, and creating a module an earlier root now supplies all recheck. A change may name a directory, with or without a final slash: what read anything beneath it is rechecked, as when a module's directory is removed or renamed whole. Standalone files remain independent of unrelated siblings. It asks clients that support dynamic watched-file registration to watch **/*.ldn; whether a client reports a removed directory under that pattern is the client's choice. An open buffer still takes precedence over the file on disk. Editors that do not send watched-file notifications must arrange that notification to refresh diagnostics after an external write.

A quick fix is preferred when it is exact: its rule decides the replacement, and applying it keeps what the program means, so an editor may apply it unasked. A likely fix, such as a respelling, or D376's else none in place of a value after the else of a call that returns none, which the session else-none-repair holds the server to, is offered and not preferred. A repair of a line that does not parse is likely, however few were found, except for removing one copy of a token written twice in a row (D261) that is not a bracket (D1075). The edits are the ones refine's report carries, and one fix may change more than one file.

Formatting ignores the editor's tab size and indentation preference: there is one layout, D252's. A source that does not parse is not formatted, and the answer is null; diagnostics report why when the server checks the changed module.

Definitions and hover answer from the names and types the module was checked with. A definition is the declared name itself, wherever it is written, and a name with no declaration in source, such as a predeclared type, a keyword or a literal, has none. Hover shows a routine, type or atom as its first line is written, and any other name with its type as the checker's reports spell it, then its doc comment: the run of --- lines directly above a declaration that begins its line [2000]. Over an expression, hover shows its type. A type is spelled as the hovered source would write it: another module's type by the name its import gives, shapes.point, and a private type by its public alias, the way a refusal in that source spells them. A field a selection names, corner.x or the y of offsetof pair.y, or a conformance's through names, has its definition at the field in its struct's body, and where the selection has no value of its own, as in a measurement, hover shows the field as that body declares it. Inside a routine body that does not parse, both answer nothing.

When the only resolution errors are unresolved names, the server keeps the bindings of other names and checks for hover types. Definition and hover still answer in unaffected code. The unresolved name has no definition or type, and diagnostics remain the same as a build's resolution diagnostics; any type errors found during this extra editor check are not published until the names are resolved. A call whose callee is one of those names has no error set for the check to infer from, so a routine that recovers from such a call, one declared ! ... included, keeps the build's diagnostics rather than an inference the check cannot finish; the session unresolved-recovered-call holds the server to this.

Anything else is refused as the protocol says: a request the server does not offer with MethodNotFound, one before initialize with ServerNotInitialized, and one after shutdown with InvalidRequest. The server answers each request before reading the next message, so it ignores $/cancelRequest notifications; a late cancellation cannot affect a later request that reuses the ID. A notification the server does not know is ignored. A defect of the compiler met while a request is answered is the request's InternalError, and one met while a notification is handled or a module is published is said through window/showMessage; the session goes on after each. A module's defect is said when a text first meets it and not again each time that text is published, the text being the module's open documents at their versions. While it lasts, each open document of the module shows that one report, at its first position, in place of its diagnostics, and every other source the module had reported on is cleared unless another open module reports on it too: what an earlier text found is not shown as this one's. The findings return with the first publication that completes (D1250). An analysis that exhausts the stack or the memory is such a defect, and its message says so (D1220), wherever the host reports the exhaustion to the program rather than ending it.

Past a syntax error

refine stops after the parse when a source does not parse. The server does too, with one exception, D253's: when every syntax error lies inside an eligible module routine body, the server checks the rest of the module using stand-ins for those bodies. The routine's name and signature must have parsed. Its body is ineligible if it belongs to a generic or a routine with an inferred error set, is not closed by the end the parser matched, contains a line beginning in column one, or is too short to hold the stand-in. While an eligible body is half written, a type error in the routine below it is still reported, and hover and definitions work everywhere else. Nothing is reported inside the broken body except its own syntax errors, and no warning is given until it parses. Any error outside an eligible body, such as one in a signature, a type or a binding, leaves the module reported exactly as refine reports it.

Positions

A position is a line and a character within it, and the server counts characters in UTF-8 bytes when the editor offers that, and in UTF-16 code units otherwise, as the protocol's default. Lines end at LF, CR LF or a lone CR [1750]. A byte that is not part of well-formed UTF-8 counts as one character, as an editor that shows it as a replacement character counts it. A position past the end of its line is the line's end.

A change is incremental: the server applies each range to the text it holds without copying the text around it. Until the next analysis, retained string payloads occupy no more than twice the visible text bytes, excluding slice metadata, allocator overhead, checked compilations and temporary copies: a stale copy is dropped when the document changes, and the text is copied out once deletions outweigh it. A notification with any malformed change, or a range that ends before it starts, is ignored whole.

When it analyses

After every buffer or reported disk change, but only once the editor has stopped sending: a burst of edits is analysed once, when no more input is waiting, and a request is answered from the documents as they stand when it arrives. Formatting answers from the held text before analysis; diagnostics follow when input is idle. Checked modules stay available for hover, definition and code actions, even when queries alternate between open modules. An open, change, close or watched disk change discards the checked modules; diagnostics then rebuild those affected, and later queries rebuild any others they need. Distinct file URIs naming one path retain separate buffers. A query selects its URI's buffer; switching to different bytes also discards checked modules, including those importing that path. A module with two such documents is published once through each, and what it reads is the union of the two: a source only one of them imports stays reported until neither does. The cache holds at most one compilation per open module, and discards all of them when the session ends. This is a compilation-count bound, not a fixed byte limit: separate entries may duplicate imports.

A sound entry source is parsed once per analysis, even if another source has a broken body. The stand-in compilation takes unchanged syntax trees and spacing from the original, then parses the stand-in source. The original is released after transfer; the checked result may remain cached. The transfer temporarily holds both compilations and increases peak memory. The memory suite checks repeated sessions and edits for accumulation.

On Linux/glibc, compiler/tests/server/measure_memory.py compares two built compilers over four modules sharing an import, two with broken bodies. It records allocator-held bytes at idle, peak resident memory, repeated and alternating queries, twenty edits, a 256-edit burst, import open/close and session teardown. The burst uses ranges when advertised and equivalent full replacements otherwise. The JSON's historical live_bytes count includes freed blocks in glibc's per-thread cache. Both measures include slice metadata and allocator overhead; neither isolates a single cache or proves that application allocations remain live. Pass --baseline, --refine and --output to retain the comparison. The probe is a temporary host library; it does not alter either compiler.

During one publication round with several stale modules, previously published paths identify shared imports. Only those paths enter an exact-text syntax cache, with held editor bytes snapshotted after pending edits are flushed. The syntax cache is released at the end of the round; checked module compilations may remain cached. Name resolution and checking still run per module because their answers depend on its whole program and options. Shared parses and any stand-in transfer temporarily overlap those checked compilations; neither cache has a fixed byte bound.

Limits

bound
a message's header block 4 KiB
a message's body 64 MiB; a longer one is skipped
a document what a message's body carries: no smaller bound, and none set by the host's stack
JSON nesting 64 levels
an integer in a message within 2^53 - 1

A header that cannot be read, or input that ends inside a message, ends the server with status 1, because there is no way to find where the next message starts. A message that is not JSON is answered with ParseError and the server goes on. exit after shutdown is status 0, and any other end is 1.

In an editor

The packages under highlight/ start the server, each where its editor makes that a matter of configuration; highlight/README.md says how to install each. refine must be on the editor's path: install it as the README describes. nix build .#refine alone leaves it in result/bin, which then has to be put on the path.

editor how it starts refine lsp
Neovim 0.11+ highlight/nvim/lsp/refine.lua, enabled by the package when refine is on the path
Helix highlight/helix/languages.toml names refine as Landin's language server
Emacs highlight/emacs/landin-mode.el registers it with Eglot; M-x eglot starts it
Zed highlight/zed names it as Landin's language server and finds refine on the path
VS Code and its descendants highlight/textmate's client starts it; landin.server.path names another compiler, and landin.server.target, landin.server.level and landin.server.options set the analysis target and build options
Vim highlight/vim registers it with vim-lsp when vim-lsp is installed
Sublime Text highlight/sublime/LSP-refine.sublime-settings, for the LSP package
Kate highlight/kate/lsp-client.json, for the LSP Client plugin

The shipped Neovim configuration uses the nearest .git directory as its root. Without Git it uses Neovim's working directory for files beneath that directory, or the opened file's directory otherwise. Start Neovim in a Git-free project's root so imports in sibling modules are visible. Neovim's vim.lsp.config can override root_dir when a project needs other roots.

highlight/test_adapters.py holds every one of these to refine lsp, and highlight/test.sh starts the server through Neovim and Emacs when LANDIN_REFINE names a compiler. Nano and Notepad++ have no language client, and JetBrains IDEs and Eclipse load the TextMate grammar alone.

Where this comes from

This page is derived: D253 in spec.md records the one way the server reports more than refine does and what it was chosen over, and Landin.Server is the implementation. Every capability above is pinned by a scripted session under compiler/tests/server/, which the test program runs against the server in-process and native_session.py runs through the executable.