src/lsp/proto.bend source
src/lsp/proto.bend on the hub · documented module
# lsp/proto: the messages the server sends, and the little it reads of a URI.import Baseimport ezjson@1.2.0.0/main.bend as Ezjsonimport ezjson@1.2.0.0/src/value.bend as Jimport ./frame.bend as Frameimport ./report.bend as Repimport ../syntax/outline.bend as Outlineimport ../syntax/bind.bend as Bindimport ./semantic.bend as Semanticimport ../finding.bend as Lintimport ../codes.bend as Codesimport ../config.bend as Configimport ../version.bend as Verimport ./enc.bend as Enc# strings as JSON stringsdef strings(s: List<&2, String>) -> List<&2, J.Json>: match s: case Nil{}: Nil{} case Con{h, t}: J.JStr{h} <> strings(t)# a reply to a requestdef response(id: J.Json, result: J.Json) -> J.Json: Ezjson.obj([("jsonrpc", J.JStr{"2.0"}), ("id", id), ("result", result)])# a refusal of a request, with LSP's error code and a messagedef failure(id: J.Json, code: String, message: String) -> J.Json: Ezjson.obj([("jsonrpc", J.JStr{"2.0"}), ("id", id), ("error", Ezjson.obj([("code", J.JNum{code}), ("message", J.JStr{message})]))])# a notification: no id, no reply expecteddef notify(method: String, params: J.Json) -> J.Json: Ezjson.obj([("jsonrpc", J.JStr{"2.0"}), ("method", J.JStr{method}), ("params", params)])# what the server can do. It takes whole-text edits (change: 1) for hover,# definition and symbols; diagnostics still follow open and save, as the# checker reads files and their imports from diskdef capabilities(e: Enc.Enc) -> J.Json: Ezjson.obj([ ("capabilities", Ezjson.obj([ ("positionEncoding", J.JStr{Enc.name(e)}), ("textDocumentSync", Ezjson.obj([("openClose", J.JBool{True{}}), ("change", J.JNum{U32.show(1)}), ("save", J.JBool{True{}})])), ("hoverProvider", J.JBool{True{}}), ("definitionProvider", J.JBool{True{}}), ("documentSymbolProvider", J.JBool{True{}}), ("completionProvider", Ezjson.obj([("triggerCharacters", Ezjson.arr([J.JStr{"."}]))])), ("referencesProvider", J.JBool{True{}}), ("renameProvider", J.JBool{True{}}), ("semanticTokensProvider", Ezjson.obj([ ("legend", Ezjson.obj([("tokenTypes", Ezjson.arr(strings(Semantic.legend()))), ("tokenModifiers", Ezjson.arr([]))])), ("full", J.JBool{True{}})]))])), ("serverInfo", Ezjson.obj([("name", J.JStr{"bolt"}), ("version", J.JStr{Ver.text()})]))])# an LSP position as sent: a line and a character already in the negotiated# encodingdef position.at(line: U32, character: U32) -> J.Json: Ezjson.obj([("line", J.JNum{U32.show(line)}), ("character", J.JNum{U32.show(character)})])# an LSP position (0-based line and a code-point character), its character# sent in the document's negotiated encodingdef position(c: Enc.Cols, +line: U32, character: U32) -> J.Json: position.at(line, Enc.out(c, line, character))# the checker gives a line and no columns: a diagnostic covers its whole linedef diagnostic(+c: Enc.Cols, d: Rep.Diag) -> J.Json: Rep.Diag{+line, msg} = d Ezjson.obj([ ("range", Ezjson.obj([("start", position(c, line, 0)), ("end", position(c, (line + 1 : U32), 0))])), ("severity", J.JNum{U32.show(1)}), ("source", J.JStr{"bend"}), ("message", J.JStr{msg})])# diagnostics as JSONdef diagnostics(ds: List<&2, Rep.Diag>, +c: Enc.Cols) -> List<&2, J.Json>: match ds: case Nil{}: Nil{} case Con{d, t}: diagnostic(c, d) <> diagnostics(t, c)# a finding's range: its name, or from its column to the end of the linedef finding_range(+c: Enc.Cols, +line: U32, +col: U32, +len: U32) -> J.Json: Ezjson.obj([("start", position(c, line, col)), ("end", Bool.pick(J.Json, U32.is_eq(len, 0), position(c, (line + 1 : U32), 0), position(c, line, (col + len : U32))))])# a graded lint finding as a diagnostic: an error or a warning by its level,# its stable code, and bolt(group:slug) as the sourcedef finding(c: Enc.Cols, g: Lint.Graded) -> J.Json: Lint.Graded{level, Lint.Finding{path, line, col, len, +rule, msg}} = g Ezjson.obj([("range", finding_range(c, line, col, len)), ("severity", J.JNum{U32.show(Bool.pick(U32, Config.is_error(level), 1, 2))}), ("code", J.JStr{Codes.code(rule)}), ("source", J.JStr{Codes.source(rule)}), ("message", J.JStr{msg})])# findings as JSONdef findings(gs: List<&2, Lint.Graded>, +c: Enc.Cols) -> List<&2, J.Json>: match gs: case Nil{}: Nil{} case Con{g, t}: finding(c, g) <> findings(t, c)# the publishDiagnostics notification for a document: the checker's errors,# then the linter's findingsdef publish(+c: Enc.Cols, uri: String, ds: List<&2, Rep.Diag>, gs: List<&2, Lint.Graded>) -> J.Json: notify("textDocument/publishDiagnostics", Ezjson.obj([("uri", J.JStr{uri}), ("diagnostics", Ezjson.arr(List.append(&2, J.Json, diagnostics(ds, c), findings(gs, c))))]))# a whole line as a rangedef line_range(+c: Enc.Cols, +line: U32) -> J.Json: Ezjson.obj([("start", position(c, line, 0)), ("end", position(c, (line + 1 : U32), 0))])# an item as hover text: its signature as code, then its doc commentdef hover.text(sig: String, +doc: String) -> String: "```bend\n" ++ sig ++ "\n```" ++ Bool.pick(String, String.is_empty(doc), "", "\n\n" ++ doc)# an item as hover: signature, then docdef hover(item: Outline.Item) -> J.Json: Outline.Item{k, name, line, sig, doc, path} = item Ezjson.obj([("contents", Ezjson.obj([("kind", J.JStr{"markdown"}), ("value", J.JStr{hover.text(sig, doc)})]))])# an item's location: its whole linedef location(c: Enc.Cols, uri: String, item: Outline.Item) -> J.Json: Outline.Item{k, name, line, sig, doc, path} = item Ezjson.obj([("uri", J.JStr{uri}), ("range", line_range(c, line))])# what a binder is, in wordsdef kind_name(k: Bind.BindKind) -> String: match k: case Bind.KItem{}: "item" case Bind.KCtor{}: "constructor" case Bind.KParam{}: "parameter" case Bind.KTypeParam{}: "type parameter" case Bind.KField{}: "field" case Bind.KLocal{}: "local" case Bind.KPat{}: "local" case Bind.KFor{}: "local" case Bind.KTypeVar{}: "type variable"# where a local was bound, for hoverdef where_bound(k: Bind.BindKind, line: U32) -> String: match k: case Bind.KLocal{}: ", bound on line " ++ U32.show((line + 1 : U32)) case Bind.KPat{}: ", bound on line " ++ U32.show((line + 1 : U32)) case Bind.KFor{}: ", bound on line " ++ U32.show((line + 1 : U32)) case other: ""# a binder of the document: the text that bound it, and what it is. The# server knows binding sites, not types: only a declaration carries onedef hover.local(b: Bind.Bind) -> J.Json: Bind.Bind{name, line, col, +kind, note} = b Ezjson.obj([("contents", Ezjson.obj([("kind", J.JStr{"markdown"}), ("value", J.JStr{"```bend\n" ++ note ++ "\n```\n\n" ++ kind_name(kind) ++ where_bound(kind, line)})]))])# exactly a name's rangedef name_range(+c: Enc.Cols, +line: U32, +col: U32, name: String) -> J.Json: Ezjson.obj([("start", position(c, line, col)), ("end", position(c, line, (col + U32.from_nat(String.length(name)) : U32)))])# exactly the binder's namedef location.local(c: Enc.Cols, uri: String, b: Bind.Bind) -> J.Json: Bind.Bind{name, line, col, kind, note} = b Ezjson.obj([("uri", J.JStr{uri}), ("range", name_range(c, line, col, name))])# LSP's SymbolKind: module 2, function 12, constant 14, enum member 22, struct 23def symbol.kind(k: Outline.ItemKind) -> U32: match k: case Outline.IImport{}: 2 case Outline.IDef{}: 12 case Outline.ILaw{}: 14 case Outline.IType{}: 23 case Outline.ICtor{}: 22 case Outline.ILocal{}: 13# an item as a DocumentSymboldef symbol(+c: Enc.Cols, item: Outline.Item) -> J.Json: Outline.Item{k, name, +line, sig, doc, path} = item Ezjson.obj([("name", J.JStr{name}), ("kind", J.JNum{U32.show(symbol.kind(k))}), ("range", line_range(c, line)), ("selectionRange", line_range(c, line))])def symbols.go(items: List<&2, Outline.Item>, +c: Enc.Cols) -> List<&2, J.Json>: match items: case Nil{}: Nil{} case Con{item, t}: symbol(c, item) <> symbols.go(t, c)# the document symbolsdef symbols(c: Enc.Cols, items: List<&2, Outline.Item>) -> J.Json: Ezjson.arr(symbols.go(items, c))# LSP's CompletionItemKind: function 3, module 9, enum member 20, constant 21,# struct 22def completion.kind(k: Outline.ItemKind) -> U32: match k: case Outline.IImport{}: 9 case Outline.IDef{}: 3 case Outline.ILaw{}: 21 case Outline.IType{}: 22 case Outline.ICtor{}: 20 case Outline.ILocal{}: 6# a text's first linedef first_line(s2: String) -> String: Maybe.default(&2, String, List.head(&2, String, String.lines(s2)), "")# a candidate replaces everything typed of the name, dots included: the# editor's own idea of a word stops at a dotdef completion(+c: Enc.Cols, item: Outline.Item, +line: U32, start: U32, col: U32) -> J.Json: Outline.Item{k, +name, l, sig, doc, path} = item Ezjson.obj([("label", J.JStr{name}), ("kind", J.JNum{U32.show(completion.kind(k))}), ("detail", J.JStr{first_line(sig)}), ("documentation", Ezjson.obj([("kind", J.JStr{"markdown"}), ("value", J.JStr{doc})])), ("filterText", J.JStr{name}), ("textEdit", Ezjson.obj([("range", Ezjson.obj([("start", position(c, line, start)), ("end", position(c, line, col))])), ("newText", J.JStr{name})]))])def completions.go( items: List<&2, Outline.Item>, +c: Enc.Cols, +line: U32, +start: U32, +col: U32) -> List<&2, J.Json>: match items: case Nil{}: Nil{} case Con{item, t}: completion(c, item, line, start, col) <> completions.go(t, c, line, start, col)# isIncomplete: the server filtered by the prefix, so the editor asks again# as the prefix growsdef completions(c: Enc.Cols, items: List<&2, Outline.Item>, line: U32, start: U32, col: U32) -> J.Json: Ezjson.obj([("isIncomplete", J.JBool{True{}}), ("items", Ezjson.arr(completions.go(items, c, line, start, col)))])# a hex digit's value: 0-9, then a-f and A-F as 10-15def hex(ch: Char) -> U32: +x = Char.to_u32(ch) Bool.pick(U32, U32.is_le(x, 57), (x - 48 : U32), Bool.pick(U32, U32.is_ge(x, 97), (x - 87 : U32), (x - 55 : U32)))# a file URI's path: the scheme dropped, the %XX bytes decoded as UTF-8def unpercent(cs: List<&2, Char>) -> List<&2, U32>: match cs: case Nil{}: Nil{} case Con{'%', Con{a, Con{b, t}}}: +hi = hex(a) +lo = hex(b) (hi * 16 + lo : U32) <> unpercent(t) case Con{c, t}: Frame.encode.one(Char.to_u32(c), unpercent(t))# a file URI's path: the scheme dropped, the %XX bytes decoded as UTF-8def path(+uri: String) -> String: Frame.decode(unpercent(String.to_list( Bool.pick(String, String.starts_with(uri, "file://"), String.drop(uri, 7n), uri))))# references and rename# ---------------------def locations.go(ps: List<&2, Bind.Pos>, +cc: Enc.Cols, +uri: String, +name: String) -> List<&2, J.Json>: match ps: case Nil{}: Nil{} case Con{Bind.Pos{l, c}, rest}: Ezjson.obj([("uri", J.JStr{uri}), ("range", name_range(cc, l, c, name))]) <> locations.go(rest, cc, uri, name)# the positions a name is at, as Locationsdef locations(c: Enc.Cols, uri: String, name: String, ps: List<&2, Bind.Pos>) -> J.Json: Ezjson.arr(locations.go(ps, c, uri, name))def edits.go(ps: List<&2, Bind.Pos>, +cc: Enc.Cols, +name: String, +new: String) -> List<&2, J.Json>: match ps: case Nil{}: Nil{} case Con{Bind.Pos{l, c}, rest}: Ezjson.obj([("range", name_range(cc, l, c, name)), ("newText", J.JStr{new})]) <> edits.go(rest, cc, name, new)# a WorkspaceEdit over the one documentdef rename(c: Enc.Cols, uri: String, name: String, new: String, ps: List<&2, Bind.Pos>) -> J.Json: Ezjson.obj([("changes", Ezjson.obj([(uri, Ezjson.arr(edits.go(ps, c, name, new)))]))])# numbers as JSON numbersdef nums(ns: List<&2, U32>) -> List<&2, J.Json>: match ns: case Nil{}: Nil{} case Con{h, t}: J.JNum{U32.show(h)} <> nums(t)# the semantic tokens responsedef semantic(data: List<&2, U32>) -> J.Json: Ezjson.obj([("data", Ezjson.arr(nums(data)))])