/** * The protocol's JSON: how the server writes positions, ranges, * diagnostics, symbols and completions, what it says it can do, and the * codes it answers a failed request with. */ import { JsonBuilder } from 'zena:json'; import { Diagnostic, DiagnosticSeverity } from 'compiler/diagnostics'; import { CompletionItem } from './completion.zena'; import { HoverResult } from './hover.zena'; import { DocumentSymbol } from './symbols.zena'; import { Position, positionOf } from './positions.zena'; import { Edit } from 'lint/source'; /** The JSON-RPC error codes the server answers with. */ export enum ErrorCode { ParseError = -32700, InvalidRequest = -32600, MethodNotFound = -32601, InvalidParams = -32602, InternalError = -32603, } /** A request that cannot be answered, and the code to answer it with. */ export class RequestError extends Error { code: ErrorCode; new(code: ErrorCode, message: String) : code = code, super(message); } /** The protocol's DiagnosticSeverity number for a severity. */ function severityNumber(severity: DiagnosticSeverity): i32 { return match (severity) { case DiagnosticSeverity.Error: 1 case DiagnosticSeverity.Warning: 2 case DiagnosticSeverity.Information: 3 }; } export function writePosition(b: JsonBuilder, position: Position): void { b.beginObject() .key('line').i32(position.line) .key('character').i32(position.character) .endObject(); } export function writeRange(b: JsonBuilder, start: Position, end: Position): void { b.beginObject().key('start'); writePosition(b, start); b.key('end'); writePosition(b, end); b.endObject(); } /** The range of the bytes `start` to `end` of `source`. */ export function writeSpan(b: JsonBuilder, source: String, start: i32, end: i32): void { writeRange(b, positionOf(source, start), positionOf(source, end)); } /** Markdown content: `{kind: 'markdown', value}`. */ function writeMarkdown(b: JsonBuilder, value: String) { b.beginObject().key('kind').string('markdown').key('value').string(value).endObject(); } /** A diagnostic in `source`, the text of the document it belongs to. */ export function writeDiagnostic(b: JsonBuilder, source: String, diagnostic: Diagnostic): void { b.beginObject().key('range'); let location = diagnostic.location; if (location == null) { writeSpan(b, source, 0, 0); } else { writeSpan(b, source, location.start, location.start + location.length); } b.key('severity').i32(severityNumber(diagnostic.severity)) .key('source').string('zena') .key('message').string(diagnostic.message) .endObject(); } /** * A lint finding as a diagnostic: a warning from the rules in * `packages/lint`, with `zena-lint` as its source, which an editor shows * beside other linters', and the rule as its code so a client can filter * on it. */ export function writeLintDiagnostic(b: JsonBuilder, source: String, edit: Edit, message: String): void { b.beginObject().key('range'); writeSpan(b, source, edit.start, edit.end); b.key('severity').i32(2) .key('source').string('zena-lint') .key('code').string(edit.rule) .key('message').string(message) .endObject(); } /** A `TextEdit`: the bytes `start` to `end` of `source` become `newText`. */ export function writeTextEdit(b: JsonBuilder, source: String, start: i32, end: i32, newText: String): void { b.beginObject().key('range'); writeSpan(b, source, start, end); b.key('newText').string(newText).endObject(); } /** * A `CodeAction` editing one document: `edits` applied to `uri`, whose * text is `source`. `diagnosticsJson` is the finding's diagnostic as the * client sent it back, or null for an action that answers no diagnostic. */ export function writeCodeAction( b: JsonBuilder, title: String, kind: String, uri: String, source: String, edits: GrowableArray, diagnosticJson: String | null ): void { b.beginObject().key('title').string(title).key('kind').string(kind); if (diagnosticJson != null) { b.key('diagnostics').beginArray().raw(diagnosticJson).endArray(); } b.key('edit').beginObject().key('changes').beginObject().key(uri).beginArray(); for (let edit in edits) { writeTextEdit(b, source, edit.start, edit.end, edit.replacement); } b.endArray().endObject().endObject().endObject(); } /** A hover: the label as Zena code, then the doc comment if there is one. */ export function writeHover(b: JsonBuilder, hover: HoverResult): void { let label = `\`\`\`zena\n${hover.label}\n\`\`\``; let value = if (hover.docComment == '') { label } else { `${label}\n\n---\n\n${hover.docComment}` }; b.beginObject().key('contents'); writeMarkdown(b, value); b.endObject(); } export function writeSymbol(b: JsonBuilder, source: String, entry: DocumentSymbol): void { b.beginObject() .key('name').string(entry.name) .key('kind').i32(entry.kind as i32) .key('range'); writeSpan(b, source, entry.start, entry.end); b.key('selectionRange'); writeSpan(b, source, entry.selStart, entry.selEnd); if (entry.children.length > 0) { b.key('children').beginArray(); for (let child in entry.children) { writeSymbol(b, source, child); } b.endArray(); } b.endObject(); } export function writeCompletion(b: JsonBuilder, item: CompletionItem): void { b.beginObject() .key('label').string(item.label) .key('kind').i32(item.kind as i32); if (item.detail != '') { b.key('detail').string(item.detail); } if (item.docComment != '') { b.key('documentation'); writeMarkdown(b, item.docComment); } b.endObject(); } /** What the server can do, as `initialize` reports it. */ export function initializeResult(): String { let b = new JsonBuilder(); b.beginObject().key('capabilities').beginObject() // Full text on open and on every change (TextDocumentSyncKind.Full). .key('textDocumentSync').beginObject() .key('openClose').bool(true).key('change').i32(1).endObject() .key('hoverProvider').bool(true) .key('definitionProvider').bool(true) .key('documentSymbolProvider').bool(true) .key('completionProvider').beginObject() .key('triggerCharacters').beginArray().string('.').endArray().endObject() .key('documentFormattingProvider').bool(true) // Each lint finding is a quick fix; `source.fixAll.zena` applies them // all, for clients that run one on save. .key('codeActionProvider').beginObject() .key('codeActionKinds').beginArray() .string('quickfix').string('source.fixAll.zena').endArray() .endObject() .endObject() .key('serverInfo').beginObject().key('name').string('zena').endObject() .endObject(); return b.toString(); }