The Language Server Protocol for Avin: JSON-RPC 2.0 over Content-Length framing, the lifecycle,
text documents kept in step with the editor, positions in UTF-16 or UTF-8, and typed handlers for
diagnostics, hover, completion, definitions, references, document symbols, formatting, rename and
semantic tokens, each request answered on a lightweight thread of its own.
The package's module is dev.avinlang.lsp, written in Avin. A program depends on it in its
avin.hocon:
dependencies {
lsp { git = "github.com/avin-lang/lsp", version = "0.0.2" }
}It depends on json, whose derived encoders and decoders read
and write every protocol type.
A server for a toy settings language, whose lines are name = value: a line with no = is an
error, published as a diagnostic, and a hover shows the line under the cursor.
import dev.avinlang.lsp.{
Diagnostic, DiagnosticSeverity, Hover, MarkupContent, Position, PositionEncoding, Range, Server, TextDocument
}
// Every line that is not blank and has no `=`, as an error.
check(doc: TextDocument, encoding: PositionEncoding) -> Vector[Diagnostic] = (0 ..< doc.lineCount).toVector.flatMap(line ->
val text = doc.lineText(line)
if text.isBlank || text.contains("=") then None
else
val start = doc.offsetAt(Position(line, 0), encoding)
val whole = Range(Position(line, 0), doc.positionAt(start + text.byteLength, encoding))
Some(Diagnostic(whole, "expected `name = value`", Some(DiagnosticSeverity.Error))))
server() -> Server = Server("settings", "0.1")
.onDiagnostics((ctx, doc) -> check(doc, ctx.encoding))
.onHover((ctx, p) -> Ok(ctx.document(p.textDocument.uri).map(doc ->
Hover(MarkupContent.plain(s"line ${p.position.line + 1}: ${doc.lineText(p.position.line)}")))))
main(args: Vector[string]) -> int = server().serveArgs(args)
examples/words is a server answering every kind of request, built and driven by the tests.
Each handler takes the Context and the request's params, typed, and answers a Result whose Err
is the ResponseError the request is answered with. Registering a handler announces its capability
at initialize; a method with no handler is answered MethodNotFound, and params that do not read as
the handler's type InvalidParams, naming what is wrong.
| Register | Params | Result |
|---|---|---|
onDiagnostics(find) |
the TextDocument, on each open and change |
Vector[Diagnostic], published |
onHover |
TextDocumentPositionParams |
Option[Hover] |
onCompletion(h, triggerCharacters) |
CompletionParams |
CompletionList |
onDefinition |
TextDocumentPositionParams |
Vector[Location] |
onReferences |
ReferenceParams |
Vector[Location] |
onDocumentSymbols |
DocumentSymbolParams |
Vector[DocumentSymbol] |
onFormatting |
DocumentFormattingParams |
Vector[TextEdit] |
onRename |
RenameParams |
Option[WorkspaceEdit] |
onSemanticTokens(legend, h) |
SemanticTokensParams |
SemanticTokens |
onRequest[P, R](method, h) |
any P with a JsonDecoder |
any R with a JsonEncoder |
onNotification[P](method, h) |
any P with a JsonDecoder |
nothing |
A Context gives the documents open when the message arrived (document(uri)), the encoding
agreed, what the client said at initialize (client), whether the request has been cancelled
(isCancelled), and notify, log, publishDiagnostics, and beginProgress, reportProgress
and endProgress, which report on the request's workDoneToken. semanticTokens(tokens) encodes
tokens placed absolutely as the protocol sends them, each relative to the one before.
- Framing. A message is a header block, each line ended by
\r\n, a blank line and a body ofContent-Lengthbytes. Header names are matched without regard to case and every other header,Content-Typeamong them, is passed over. A missing or badContent-Length, a header block past 8 KiB, a body pastmaxMessageBytes(64 MiB unless given) and input ending inside a message end the server with status 1, since past them there is no telling where the next message starts. A body that is not UTF-8 is answeredParseError, and the next message is read. - JSON-RPC. Text that is not JSON is
ParseError(-32700); JSON that is not a request,"jsonrpc"other than"2.0", or an id that is neither a whole number nor a string isInvalidRequest(-32600); a batch is refused with oneInvalidRequest. An id is written back as it came, a number as a number and a string as a string. A notification is never answered, an unknown one included. - Concurrency. Each request runs on a lightweight thread of its own, so a quick request is answered while a slow one works, and responses go out in the order they are ready. One writer thread writes every message, whole. Notifications are handled in order, before anything read after them: a document's change is in place before the request after it is handled.
- Cancellation.
$/cancelRequestanswers the requestRequestCancelled(-32800) at once, unless its handler answered first; the handler'sisCancelledturns true, and what it answers after is dropped. A cancellation of an id not being answered is passed over. - Lifecycle. A request before
initializeisServerNotInitialized(-32002), and a notification then is dropped;initializea second time, and any request aftershutdown, isInvalidRequest.exitends the server with status 0 aftershutdownand 1 without. - Documents.
didOpen,didChange, full or incremental (the server announces incremental), anddidClose. A change's range is placed as positions are: a line past the last is the end of the text, a character past a line's end is its end, before\r\n, and a range whose end comes first replaces nothing. Diagnostics found for a version that has since changed are dropped, so they never arrive out of order; a closed document's are cleared. - Positions. A character counts UTF-16 code units, the protocol's default, unless the client
offers
utf-8ingeneral.positionEncodings, which is then used.TextDocument.offsetAtandpositionAtconvert between positions and the byte offsets of the text; a position between the two halves of a surrogate pair is the character's start.
serve(input, output) serves any io.Reader and io.ByteWriter. serveArgs(args) connects the way
an editor starts a server, then serves:
--stdio, or no connection named, serves the process's own standard input and output, read and written as bytes. This is what most editors use. Standard output carries the messages and nothing else, so a server must notprintorprintln; its own complaints go to standard error, andctx.logsends a message the editor shows. The server ends when the input ends, with status 1, as afterexitwithoutshutdown.--pipe=<path>connects to the Unix domain socket the client listens on.--socket=<port>connects to the TCP port on the local host the client listens on.
Where several are given the last counts; --clientProcessId and any other argument is passed over.
With a bad one (--socket=x, --pipe naming nothing) or one that cannot be made, the server says
why on standard error and exits with status 2.
The package needs Avin 0.0.35 or later, whose io.stdin() and io.stdout() read and write bytes.
Build the server program, whose main calls serveArgs (avin build -o avin-lsp in its directory;
the name here is an example), and tell the editor to start it with --stdio.
VS Code has no generic client of its own; an extension starts the server through the
vscode-languageclient package:
import { LanguageClient, TransportKind } from "vscode-languageclient/node";
const client = new LanguageClient(
"avin",
"Avin",
{ command: "avin-lsp", args: ["--stdio"], transport: TransportKind.stdio },
{ documentSelector: [{ scheme: "file", language: "avin" }] }
);
client.start();Neovim (0.11 or later):
vim.lsp.config("avin", {
cmd = { "avin-lsp", "--stdio" },
filetypes = { "avin" },
root_markers = { "avin.hocon", ".git" },
})
vim.lsp.enable("avin")Any other client that starts a server as a command works the same way: the command is the
executable, and its one argument --stdio.
Client is the editor's side, for tests and tools: initialize, request, notify, call (a
request and its response), notification(method) and shutdown, with each Message's result[R]()
and params[P]() read through the derived decoders.
avin test in this directory runs the package's tests, in tests/; tests/readme.avin holds this
page's example, and tests/e2e.avin builds examples/words and drives it as a child process over
a socket; tests/stdio.avin drives it over its standard input and output.