@teipublisher/oddity-language-server 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -0
- package/dist/edit.js +898 -0
- package/dist/edit.js.map +4 -4
- package/dist/index.js +898 -0
- package/dist/index.js.map +4 -4
- package/dist/node.cjs +1038 -75
- package/dist/node.cjs.map +4 -4
- package/package.json +1 -1
- package/schema/README.md +4 -0
- package/schema/opm/opm.schema.json +402 -0
- package/src/core/opmToml.ts +432 -0
- package/src/core/tomlScan.ts +155 -0
- package/src/edit.ts +1 -0
- package/src/index.ts +1 -0
- package/src/server/server.ts +77 -3
package/src/server/server.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import {
|
|
4
4
|
type Connection, type CompletionItem, type Diagnostic, type DocumentSymbol, type InitializeResult, type SelectionRange,
|
|
5
5
|
TextDocuments, TextDocumentSyncKind, CompletionItemKind, CompletionTriggerKind, DiagnosticSeverity,
|
|
6
|
-
InsertTextFormat, MarkupKind,
|
|
6
|
+
InsertTextFormat, MarkupKind, SymbolKind,
|
|
7
7
|
} from 'vscode-languageserver';
|
|
8
8
|
import { TextDocument } from 'vscode-languageserver-textdocument';
|
|
9
9
|
import type { Grammar } from 'salve-annos';
|
|
@@ -15,6 +15,8 @@ import { possibleAt } from '../core/complete.ts';
|
|
|
15
15
|
import { matchingTags } from '../core/tags.ts';
|
|
16
16
|
import { elementsAround } from '../core/elements.ts';
|
|
17
17
|
import { oddOutline, teiOutline, elementOutline, type OutlineSymbol } from '../core/outline.ts';
|
|
18
|
+
import { checkOpmToml, completeOpmToml, describeOpmToml, isOpmToml, tableName } from '../core/opmToml.ts';
|
|
19
|
+
import { scanToml } from '../core/tomlScan.ts';
|
|
18
20
|
|
|
19
21
|
export const SOURCE = 'oddity-ls';
|
|
20
22
|
|
|
@@ -39,6 +41,9 @@ export const SCHEMA_CHANGED = 'oddity/schemaChanged';
|
|
|
39
41
|
export const ALLOWED_ELEMENTS = 'oddity/allowedElements';
|
|
40
42
|
|
|
41
43
|
const DELAY = 250;
|
|
44
|
+
const XML_TRIGGERS = ['<', ' ', '"', "'"];
|
|
45
|
+
/** Characters a snippet treats as syntax. */
|
|
46
|
+
const snippetEscape = (s: string) => s.replace(/[\\$}]/g, m => '\\' + m);
|
|
42
47
|
|
|
43
48
|
export function startServer(connection: Connection, host: Host): void {
|
|
44
49
|
const documents = new TextDocuments(TextDocument);
|
|
@@ -57,7 +62,8 @@ export function startServer(connection: Connection, host: Host): void {
|
|
|
57
62
|
return {
|
|
58
63
|
capabilities: {
|
|
59
64
|
textDocumentSync: TextDocumentSyncKind.Incremental,
|
|
60
|
-
completionProvider: { triggerCharacters: [
|
|
65
|
+
completionProvider: { triggerCharacters: [...XML_TRIGGERS, '[', '='] },
|
|
66
|
+
hoverProvider: true,
|
|
61
67
|
linkedEditingRangeProvider: true,
|
|
62
68
|
documentSymbolProvider: true,
|
|
63
69
|
selectionRangeProvider: true,
|
|
@@ -87,6 +93,18 @@ export function startServer(connection: Connection, host: Host): void {
|
|
|
87
93
|
const doc = documents.get(uri);
|
|
88
94
|
if (!doc) return;
|
|
89
95
|
const version = doc.version, text = doc.getText();
|
|
96
|
+
if (isOpmToml(uri)) {
|
|
97
|
+
const diagnostics = checkOpmToml(text).map((p): Diagnostic => ({
|
|
98
|
+
range: { start: doc.positionAt(p.from), end: doc.positionAt(p.to) },
|
|
99
|
+
message: p.message,
|
|
100
|
+
severity: p.severity === 'error' ? DiagnosticSeverity.Error : DiagnosticSeverity.Warning,
|
|
101
|
+
source: SOURCE,
|
|
102
|
+
code: 'opm-toml-' + p.code,
|
|
103
|
+
tags: p.code === 'deprecated' ? [2 /* Deprecated */] : undefined,
|
|
104
|
+
}));
|
|
105
|
+
void connection.sendDiagnostics({ uri, version, diagnostics });
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
90
108
|
const choice = choiceOf(doc);
|
|
91
109
|
const key = choice ? `${choice.via} ${choice.url}` : '';
|
|
92
110
|
if (announced.get(uri) !== key) {
|
|
@@ -141,6 +159,8 @@ export function startServer(connection: Connection, host: Host): void {
|
|
|
141
159
|
connection.onCompletion(async ({ textDocument, position, context }) => {
|
|
142
160
|
const doc = documents.get(textDocument.uri);
|
|
143
161
|
if (!doc) return [];
|
|
162
|
+
if (isOpmToml(doc.uri)) return tomlCompletion(doc, position);
|
|
163
|
+
if (context?.triggerCharacter && !XML_TRIGGERS.includes(context.triggerCharacter)) return [];
|
|
144
164
|
const { grammar } = await grammarOf(choiceOf(doc));
|
|
145
165
|
if (!grammar) return [];
|
|
146
166
|
const at = doc.offsetAt(position);
|
|
@@ -178,9 +198,41 @@ export function startServer(connection: Connection, host: Host): void {
|
|
|
178
198
|
});
|
|
179
199
|
});
|
|
180
200
|
|
|
201
|
+
function tomlCompletion(doc: TextDocument, position: { line: number; character: number }): CompletionItem[] {
|
|
202
|
+
const r = completeOpmToml(doc.getText(), doc.offsetAt(position));
|
|
203
|
+
if (!r) return [];
|
|
204
|
+
const range = { start: doc.positionAt(r.from), end: doc.positionAt(r.to) };
|
|
205
|
+
const KIND = { table: CompletionItemKind.Module, key: CompletionItemKind.Property, value: CompletionItemKind.Value } as const;
|
|
206
|
+
return r.options.map((o, i): CompletionItem => {
|
|
207
|
+
const snippet = o.cursor !== undefined && o.cursor < o.apply.length;
|
|
208
|
+
return {
|
|
209
|
+
label: o.label,
|
|
210
|
+
kind: KIND[o.kind],
|
|
211
|
+
labelDetails: o.detail ? { description: o.detail } : undefined,
|
|
212
|
+
detail: o.detail,
|
|
213
|
+
documentation: o.info ? { kind: MarkupKind.PlainText, value: o.info } : undefined,
|
|
214
|
+
sortText: String(i).padStart(5, '0'),
|
|
215
|
+
filterText: doc.getText(range).startsWith('"') ? o.label : o.label.replace(/^\[+/, ''),
|
|
216
|
+
insertTextFormat: snippet ? InsertTextFormat.Snippet : InsertTextFormat.PlainText,
|
|
217
|
+
textEdit: { range, newText: snippet ? snippetEscape(o.apply.slice(0, o.cursor)) + '$0' + snippetEscape(o.apply.slice(o.cursor)) : o.apply },
|
|
218
|
+
};
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
connection.onHover(({ textDocument, position }) => {
|
|
223
|
+
const doc = documents.get(textDocument.uri);
|
|
224
|
+
if (!doc || !isOpmToml(doc.uri)) return null;
|
|
225
|
+
const h = describeOpmToml(doc.getText(), doc.offsetAt(position));
|
|
226
|
+
if (!h) return null;
|
|
227
|
+
return {
|
|
228
|
+
range: { start: doc.positionAt(h.from), end: doc.positionAt(h.to) },
|
|
229
|
+
contents: { kind: MarkupKind.Markdown, value: `**${h.title}**: ${h.detail}${h.info ? '\n\n' + h.info : ''}` },
|
|
230
|
+
};
|
|
231
|
+
});
|
|
232
|
+
|
|
181
233
|
connection.languages.onLinkedEditingRange(({ textDocument, position }) => {
|
|
182
234
|
const doc = documents.get(textDocument.uri);
|
|
183
|
-
if (!doc) return null;
|
|
235
|
+
if (!doc || isOpmToml(doc.uri)) return null;
|
|
184
236
|
const ranges = matchingTags(doc.getText(), doc.offsetAt(position));
|
|
185
237
|
if (!ranges) return null;
|
|
186
238
|
return {
|
|
@@ -193,6 +245,7 @@ export function startServer(connection: Connection, host: Host): void {
|
|
|
193
245
|
const doc = documents.get(textDocument.uri);
|
|
194
246
|
if (!doc) return [];
|
|
195
247
|
const text = doc.getText();
|
|
248
|
+
if (isOpmToml(doc.uri)) return tomlOutline(doc);
|
|
196
249
|
let symbols: OutlineSymbol[];
|
|
197
250
|
try {
|
|
198
251
|
symbols = isOdd(doc.uri) ? oddOutline(text) : vocabulary(text) === 'tei' ? teiOutline(text) : elementOutline(text);
|
|
@@ -210,10 +263,31 @@ export function startServer(connection: Connection, host: Host): void {
|
|
|
210
263
|
return symbols.map(convert);
|
|
211
264
|
});
|
|
212
265
|
|
|
266
|
+
// the tables of opm.toml, each with its keys
|
|
267
|
+
function tomlOutline(doc: TextDocument): DocumentSymbol[] {
|
|
268
|
+
const text = doc.getText(), scan = scanToml(text);
|
|
269
|
+
const range = (from: number, to: number) => ({ start: doc.positionAt(from), end: doc.positionAt(to) });
|
|
270
|
+
const tables = scan.tables.map((t, n): DocumentSymbol => {
|
|
271
|
+
const end = n + 1 < scan.tables.length ? scan.tables[n + 1].from : text.length;
|
|
272
|
+
const keys = scan.keys.filter(k => k.from > t.from && k.from < end);
|
|
273
|
+
const last = keys.length ? keys[keys.length - 1].valueTo : t.to;
|
|
274
|
+
return {
|
|
275
|
+
name: tableName(t.path), kind: t.array ? SymbolKind.Array : SymbolKind.Module,
|
|
276
|
+
range: range(t.from, last), selectionRange: range(t.from, t.to),
|
|
277
|
+
children: keys.map(k => ({
|
|
278
|
+
name: text.slice(k.from, k.to), kind: SymbolKind.Property,
|
|
279
|
+
range: range(k.from, k.valueTo), selectionRange: range(k.from, k.to),
|
|
280
|
+
})),
|
|
281
|
+
};
|
|
282
|
+
});
|
|
283
|
+
return tables;
|
|
284
|
+
}
|
|
285
|
+
|
|
213
286
|
// Expand Selection steps through the enclosing elements
|
|
214
287
|
connection.onSelectionRanges(({ textDocument, positions }) => {
|
|
215
288
|
const doc = documents.get(textDocument.uri);
|
|
216
289
|
if (!doc) return [];
|
|
290
|
+
if (isOpmToml(doc.uri)) return positions.map(position => ({ range: { start: position, end: position } }));
|
|
217
291
|
const text = doc.getText();
|
|
218
292
|
return positions.map(position => {
|
|
219
293
|
const at = doc.offsetAt(position);
|