@ds-lang/monaco-editor 0.0.0-stage → 1.1.1
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/LICENSE +691 -0
- package/README.md +13 -2
- package/dist/THIRD_PARTY_NOTICES.md +77 -0
- package/dist/compiler-worker.js +4 -0
- package/dist/compiler-worker.js.map +7 -0
- package/dist/ds-monaco-editor.js +103 -0
- package/dist/ds-monaco-editor.js.map +7 -0
- package/dist/lsp-worker.js +16 -0
- package/dist/lsp-worker.js.map +7 -0
- package/package.json +56 -4
- package/src/compiler-worker.mjs +5 -0
- package/src/debugger.mjs +472 -0
- package/src/diagnostics.mjs +65 -0
- package/src/ds-language.mjs +102 -0
- package/src/editor.mjs +108 -0
- package/src/index.mjs +10 -0
- package/src/lsp-worker.mjs +6 -0
- package/src/lsp.mjs +248 -0
- package/src/node/ds-lsp.mjs +5 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Dan Finster and the Desert Sage Contributors
|
|
2
|
+
// SPDX-License-Identifier: BSD-4-Clause OR AGPL-3.0-or-later
|
|
3
|
+
// DS in Monaco: the language, its configuration and its Monarch tokenizer. As
|
|
4
|
+
// in ds-codemirror-editor, a unit literal (a number welded to a unit symbol)
|
|
5
|
+
// is its own token, `number.unit`, and a capitalised name, a schema's, is
|
|
6
|
+
// `type.identifier`. Colours are the host's Monaco theme's.
|
|
7
|
+
|
|
8
|
+
/** The language's id in Monaco. */
|
|
9
|
+
export const DS_LANGUAGE_ID = 'ds';
|
|
10
|
+
|
|
11
|
+
const UNIT = /\d[\d_]*(?:\.\d[\d_]*)?(?:[eE][+-]?\d+)?_?[a-zA-Zµ°ΩÅ][\wµ°ΩÅ]*/;
|
|
12
|
+
const NUMBER = /\d[\d_]*(?:\.\d[\d_]*)?(?:[eE][+-]?\d+)?/;
|
|
13
|
+
|
|
14
|
+
/** The Monarch tokenizer. */
|
|
15
|
+
export const dsMonarch = {
|
|
16
|
+
// Every rule is compiled with the `u` flag: a DS name is XID ([LNAM.2]).
|
|
17
|
+
unicode: true,
|
|
18
|
+
// DS's reserved words ([LLEX.9.6]), by class.
|
|
19
|
+
keywords: [
|
|
20
|
+
// clauses
|
|
21
|
+
'activated', 'activation', 'after', 'alias', 'all_time', 'ambient', 'as', 'at', 'base', 'by', 'cache_deleted', 'cached',
|
|
22
|
+
'called', 'capacity', 'cooldown', 'count', 'default', 'dimension', 'emitted', 'empty', 'error', 'explicit', 'field',
|
|
23
|
+
'filter_fields', 'for', 'formula', 'from_base', 'fx_pair', 'gap', 'has', 'hold', 'hydrates', 'hydration', 'implicit',
|
|
24
|
+
'initial', 'inputs', 'inverting', 'key', 'kwarg', 'latest', 'let', 'literal', 'logged', 'logical', 'max_limit',
|
|
25
|
+
'max_window', 'mutable', 'never', 'no', 'none', 'on', 'over', 'persist', 'produces', 'published', 'record_event',
|
|
26
|
+
'recorded', 'required', 'response', 'responses', 'retained', 'retracted', 'sample', 'sampled', 'scalar', 'scope', 'skip',
|
|
27
|
+
'sliding', 'storage', 'then', 'timeout', 'to', 'to_base', 'tolerance', 'ttl', 'tumbling', 'type', 'until', 'via',
|
|
28
|
+
'where', 'within', 'wm', 'zero',
|
|
29
|
+
// actions
|
|
30
|
+
'cache', 'cache_delete', 'call', 'emit', 'log', 'publish', 'withdraw',
|
|
31
|
+
// declarations
|
|
32
|
+
'const', 'from', 'import', 'opaque', 'rule', 'schema', 'series', 'service', 'test', 'unit', 'use',
|
|
33
|
+
// operators
|
|
34
|
+
'and', 'contains', 'in', 'is', 'not', 'or', 'overlaps',
|
|
35
|
+
// rate modifiers
|
|
36
|
+
'nocooldown', 'retain',
|
|
37
|
+
],
|
|
38
|
+
typeKeywords: ['Bool', 'Bytes', 'ComputationError', 'Date', 'Duration', 'Float', 'Int', 'Interval', 'Json', 'Schema', 'String', 'Time', 'TypeError'],
|
|
39
|
+
constants: ['false', 'inf', 'nan', 'NaN', 'null', 'true'],
|
|
40
|
+
builtins: [
|
|
41
|
+
'abs', 'len', 'monotonic_ns', 'now', 'round', 'dwell',
|
|
42
|
+
'active_fraction', 'bool_and', 'bool_or', 'count_distinct', 'count_of', 'direction', 'first', 'integral', 'last', 'max',
|
|
43
|
+
'mean', 'median', 'min', 'onset_rate', 'pstdev', 'pvariance', 'rate', 'stdev', 'sum', 'variance',
|
|
44
|
+
],
|
|
45
|
+
tokenizer: {
|
|
46
|
+
root: [
|
|
47
|
+
[/#.*$/, 'comment'],
|
|
48
|
+
[/"""/, 'string', '@longString'],
|
|
49
|
+
[/"/, 'string', '@string'],
|
|
50
|
+
[/(~)(\s*)(\/(?:\\.|[^/\\\n])*\/)/, ['operator', 'white', 'regexp']],
|
|
51
|
+
[UNIT, 'number.unit'],
|
|
52
|
+
[NUMBER, 'number'],
|
|
53
|
+
[/->/, 'keyword'],
|
|
54
|
+
// A DS name: XID_Start or `_`, then XID_Continue. A capitalised one,
|
|
55
|
+
// in any script with case, is a schema's, a type, as
|
|
56
|
+
// ds-codemirror-editor's mode has it.
|
|
57
|
+
[/[\p{XID_Start}_]\p{XID_Continue}*/u, { cases: { '@keywords': 'keyword', '@typeKeywords': 'type', '@constants': 'constant', '@builtins': 'predefined', '~[\\p{Lu}\\p{Lt}].*': 'type.identifier', '@default': 'identifier' } }],
|
|
58
|
+
],
|
|
59
|
+
string: [
|
|
60
|
+
[/\{[^}]*\}/, 'variable'],
|
|
61
|
+
[/\\./, 'string.escape'],
|
|
62
|
+
[/"/, 'string', '@pop'],
|
|
63
|
+
[/[^"\\{]+/, 'string'],
|
|
64
|
+
],
|
|
65
|
+
longString: [
|
|
66
|
+
[/"""/, 'string', '@pop'],
|
|
67
|
+
[/[^"]+/, 'string'],
|
|
68
|
+
[/"/, 'string'],
|
|
69
|
+
],
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/** The language configuration: comments, brackets, pairs. */
|
|
74
|
+
export const dsLanguageConfiguration = {
|
|
75
|
+
comments: { lineComment: '#' },
|
|
76
|
+
brackets: [
|
|
77
|
+
['(', ')'],
|
|
78
|
+
['[', ']'],
|
|
79
|
+
['{', '}'],
|
|
80
|
+
],
|
|
81
|
+
autoClosingPairs: [
|
|
82
|
+
{ open: '(', close: ')' },
|
|
83
|
+
{ open: '[', close: ']' },
|
|
84
|
+
{ open: '{', close: '}' },
|
|
85
|
+
{ open: '"', close: '"' },
|
|
86
|
+
],
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Registers DS with the host's Monaco, once.
|
|
91
|
+
*
|
|
92
|
+
* @param {any} monaco the host's monaco
|
|
93
|
+
* @returns {string} the language's id
|
|
94
|
+
*/
|
|
95
|
+
export function registerDsLanguage(monaco) {
|
|
96
|
+
if (!monaco.languages.getLanguages().some(l => l.id === DS_LANGUAGE_ID)) {
|
|
97
|
+
monaco.languages.register({ id: DS_LANGUAGE_ID, extensions: ['.ds'], aliases: ['DS', 'Desert Sage'] });
|
|
98
|
+
monaco.languages.setLanguageConfiguration(DS_LANGUAGE_ID, dsLanguageConfiguration);
|
|
99
|
+
monaco.languages.setMonarchTokensProvider(DS_LANGUAGE_ID, dsMonarch);
|
|
100
|
+
}
|
|
101
|
+
return DS_LANGUAGE_ID;
|
|
102
|
+
}
|
package/src/editor.mjs
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Dan Finster and the Desert Sage Contributors
|
|
2
|
+
// SPDX-License-Identifier: BSD-4-Clause OR AGPL-3.0-or-later
|
|
3
|
+
// A DS editor in the host's Monaco: the language, the compiler's diagnostics
|
|
4
|
+
// as markers, and the host's ds lsp, as ds-codemirror-editor's createEditor
|
|
5
|
+
// gives them in CodeMirror. The kit does not bundle Monaco: the host passes
|
|
6
|
+
// its own.
|
|
7
|
+
import { registerDsLanguage } from './ds-language.mjs';
|
|
8
|
+
import { MARKER_OWNER, toMonacoMarkers } from './diagnostics.mjs';
|
|
9
|
+
import { dsLsp } from './lsp.mjs';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Checks a model with the compiler after each pause in typing, and sets its
|
|
13
|
+
* markers. A failed call sets none.
|
|
14
|
+
*
|
|
15
|
+
* @param {any} monaco the host's monaco
|
|
16
|
+
* @param {any} model the model
|
|
17
|
+
* @param {{diagnose: (source: string) => Promise<any>}} compiler the compiler (ds-editor-core's startCompiler)
|
|
18
|
+
* @param {{file?: string, delay?: number, onClean?: (text: string) => void}} [options] the document's name in the compiler's diagnostics, the pause in milliseconds (500 unless given), and what hears text the compiler finds no error in
|
|
19
|
+
* @returns {{check: () => Promise<void>, dispose: () => void}} a check now, and what stops checking
|
|
20
|
+
*/
|
|
21
|
+
export function dsLinter(monaco, model, compiler, { file = 'rules.ds', delay = 500, onClean } = {}) {
|
|
22
|
+
let timer = null;
|
|
23
|
+
let latest = 0;
|
|
24
|
+
let disposed = false;
|
|
25
|
+
const check = async () => {
|
|
26
|
+
const run = ++latest;
|
|
27
|
+
const source = model.getValue();
|
|
28
|
+
let markers;
|
|
29
|
+
try {
|
|
30
|
+
markers = toMonacoMarkers(model, await compiler.diagnose(source), file, monaco.MarkerSeverity);
|
|
31
|
+
} catch {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
// A later check, or a closed model, has the last word.
|
|
35
|
+
if (disposed || run !== latest || model.isDisposed()) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
monaco.editor.setModelMarkers(model, MARKER_OWNER, markers);
|
|
39
|
+
if (onClean && !markers.some(m => m.severity === monaco.MarkerSeverity.Error)) {
|
|
40
|
+
onClean(source);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
const listener = model.onDidChangeContent(() => {
|
|
44
|
+
clearTimeout(timer);
|
|
45
|
+
timer = setTimeout(check, delay);
|
|
46
|
+
});
|
|
47
|
+
timer = setTimeout(check, 0);
|
|
48
|
+
return {
|
|
49
|
+
check,
|
|
50
|
+
dispose() {
|
|
51
|
+
disposed = true;
|
|
52
|
+
clearTimeout(timer);
|
|
53
|
+
listener.dispose();
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A DS editor in an element.
|
|
60
|
+
*
|
|
61
|
+
* @param {object} options the editor
|
|
62
|
+
* @param {any} options.monaco the host's monaco
|
|
63
|
+
* @param {HTMLElement} options.parent where it goes
|
|
64
|
+
* @param {string} [options.doc] its text
|
|
65
|
+
* @param {{diagnose: (source: string) => Promise<any>}} [options.compiler] the compiler; without it, no markers
|
|
66
|
+
* @param {(messages: object[]) => Promise<any>} [options.lsp] carries messages to the host's ds lsp; without it, no completion, hover, signature help, definition, outline, semantic highlighting, references or rename
|
|
67
|
+
* @param {string} [options.file] the document's name in the compiler's diagnostics
|
|
68
|
+
* @param {(text: string) => void} [options.onChange] called with the text after each change
|
|
69
|
+
* @param {object} [options.options] the host's own editor options, over the kit's
|
|
70
|
+
* @returns {any} the editor (monaco.editor.IStandaloneCodeEditor); its dispose closes the model, the checks and the lsp document
|
|
71
|
+
*/
|
|
72
|
+
export function createEditor({ monaco, parent, doc = '', compiler, lsp, file = 'rules.ds', onChange, options = {} }) {
|
|
73
|
+
const language = registerDsLanguage(monaco);
|
|
74
|
+
const model = monaco.editor.createModel(doc, language);
|
|
75
|
+
const editor = monaco.editor.create(parent, {
|
|
76
|
+
model,
|
|
77
|
+
automaticLayout: true,
|
|
78
|
+
minimap: { enabled: false },
|
|
79
|
+
tabSize: 4,
|
|
80
|
+
insertSpaces: true,
|
|
81
|
+
// ds lsp's semantic tokens, over the grammar's colours, where the host's theme colours their types.
|
|
82
|
+
'semanticHighlighting.enabled': true,
|
|
83
|
+
...options,
|
|
84
|
+
});
|
|
85
|
+
const owned = [];
|
|
86
|
+
const server = lsp ? dsLsp(monaco, model, lsp) : null;
|
|
87
|
+
if (server) {
|
|
88
|
+
owned.push(server);
|
|
89
|
+
}
|
|
90
|
+
if (compiler) {
|
|
91
|
+
// Completion and hover answer from the last text that compiled: the
|
|
92
|
+
// server is given the text whenever the compiler finds no error in it.
|
|
93
|
+
const onClean = server ? text => server.doc.sync(text) : undefined;
|
|
94
|
+
owned.push(dsLinter(monaco, model, compiler, { file, onClean }));
|
|
95
|
+
}
|
|
96
|
+
if (onChange) {
|
|
97
|
+
owned.push(model.onDidChangeContent(() => onChange(model.getValue())));
|
|
98
|
+
}
|
|
99
|
+
const dispose = editor.dispose.bind(editor);
|
|
100
|
+
editor.dispose = () => {
|
|
101
|
+
for (const o of owned) {
|
|
102
|
+
o.dispose();
|
|
103
|
+
}
|
|
104
|
+
dispose();
|
|
105
|
+
model.dispose();
|
|
106
|
+
};
|
|
107
|
+
return editor;
|
|
108
|
+
}
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Dan Finster and the Desert Sage Contributors
|
|
2
|
+
// SPDX-License-Identifier: BSD-4-Clause OR AGPL-3.0-or-later
|
|
3
|
+
// The DS editor kit for Monaco, its page entry: ds-editor-core, which any
|
|
4
|
+
// editor shares, and Monaco's parts. Monaco itself is the host's.
|
|
5
|
+
export * from '@ds-lang/editor-core';
|
|
6
|
+
export { DS_LANGUAGE_ID, dsLanguageConfiguration, dsMonarch, registerDsLanguage } from './ds-language.mjs';
|
|
7
|
+
export { MARKER_OWNER, positionOf, toMonacoMarkers } from './diagnostics.mjs';
|
|
8
|
+
export { createEditor, dsLinter } from './editor.mjs';
|
|
9
|
+
export { dsLsp, lspDocument } from './lsp.mjs';
|
|
10
|
+
export { dsDebugger } from './debugger.mjs';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Dan Finster and the Desert Sage Contributors
|
|
2
|
+
// SPDX-License-Identifier: BSD-4-Clause OR AGPL-3.0-or-later
|
|
3
|
+
// ds lsp's thread, ds-editor-core's, for a host that runs no ds lsp (its
|
|
4
|
+
// page gives the editor startBrowserLsp's lsp): built into dist/lsp-worker.js
|
|
5
|
+
// for a host page, and exported as ./lsp-worker.
|
|
6
|
+
import '@ds-lang/editor-core/lsp-worker';
|
package/src/lsp.mjs
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Dan Finster and the Desert Sage Contributors
|
|
2
|
+
// SPDX-License-Identifier: BSD-4-Clause OR AGPL-3.0-or-later
|
|
3
|
+
// DS's language server, `ds lsp`, in Monaco: completion, hover, signature
|
|
4
|
+
// help, go to definition, the outline (Ctrl-Shift-O), semantic highlighting,
|
|
5
|
+
// references (Shift-F12) and rename (F2), in Monaco's own widgets, each kept
|
|
6
|
+
// to the document (ds-editor-core's lsp-results). The host runs the server
|
|
7
|
+
// and hands the editor one function that carries messages to it, as for
|
|
8
|
+
// ds-codemirror-editor.
|
|
9
|
+
//
|
|
10
|
+
// Monaco's providers are per language, not per editor, so they are
|
|
11
|
+
// registered once for each Monaco, and answer through the document of the
|
|
12
|
+
// model asked about (ds-editor-core's lspDocument). Positions: LSP's are
|
|
13
|
+
// 0-based UTF-16 units, Monaco's 1-based UTF-16 units.
|
|
14
|
+
import {
|
|
15
|
+
documentSymbols,
|
|
16
|
+
localRanges,
|
|
17
|
+
localRename,
|
|
18
|
+
lspDocument as coreDocument,
|
|
19
|
+
SEMANTIC_LEGEND,
|
|
20
|
+
} from '@ds-lang/editor-core';
|
|
21
|
+
import { DS_LANGUAGE_ID } from './ds-language.mjs';
|
|
22
|
+
|
|
23
|
+
export { LSP_METHODS } from '@ds-lang/editor-core';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* One editor's document on the server: ds-editor-core's, its untitled: URIs
|
|
27
|
+
* under ds-monaco-editor's name.
|
|
28
|
+
*
|
|
29
|
+
* @param {(messages: object[]) => Promise<any>} send carries messages to the host's ds lsp
|
|
30
|
+
* @param {{uri?: string, editor?: string}} [options] as ds-editor-core's lspDocument takes them
|
|
31
|
+
*/
|
|
32
|
+
export function lspDocument(send, options = {}) {
|
|
33
|
+
return coreDocument(send, { editor: 'ds-monaco-editor', ...options });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Each Monaco's documents, by model URI, once its providers are registered. */
|
|
37
|
+
const registries = new WeakMap();
|
|
38
|
+
|
|
39
|
+
const toLsp = p => ({ line: p.lineNumber - 1, character: p.column - 1 });
|
|
40
|
+
|
|
41
|
+
const toRange = r => ({
|
|
42
|
+
startLineNumber: r.start.line + 1,
|
|
43
|
+
startColumn: r.start.character + 1,
|
|
44
|
+
endLineNumber: r.end.line + 1,
|
|
45
|
+
endColumn: r.end.character + 1,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const markdown = d => (d === undefined || d === null ? undefined : typeof d === 'string' ? d : { value: d.value });
|
|
49
|
+
|
|
50
|
+
function register(monaco) {
|
|
51
|
+
const docs = new Map();
|
|
52
|
+
const K = monaco.languages.CompletionItemKind;
|
|
53
|
+
// LSP's CompletionItemKind numbers, as ds lsp gives them, to Monaco's.
|
|
54
|
+
const kinds = { 3: K.Function, 5: K.Field, 6: K.Variable, 7: K.Class, 9: K.Module, 14: K.Keyword, 21: K.Constant, 22: K.Struct, 24: K.Operator };
|
|
55
|
+
const ask = (model, method, params) => {
|
|
56
|
+
const doc = docs.get(model.uri.toString());
|
|
57
|
+
return doc ? doc.ask(model.getValue(), method, params) : Promise.resolve(null);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
monaco.languages.registerCompletionItemProvider(DS_LANGUAGE_ID, {
|
|
61
|
+
triggerCharacters: ['.', ':'],
|
|
62
|
+
async provideCompletionItems(model, position) {
|
|
63
|
+
const r = await ask(model, 'textDocument/completion', { position: toLsp(position) });
|
|
64
|
+
const items = (r && (Array.isArray(r) ? r : r.items)) || [];
|
|
65
|
+
const word = model.getWordUntilPosition(position);
|
|
66
|
+
const range = {
|
|
67
|
+
startLineNumber: position.lineNumber,
|
|
68
|
+
endLineNumber: position.lineNumber,
|
|
69
|
+
startColumn: word.startColumn,
|
|
70
|
+
endColumn: word.endColumn,
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
suggestions: items.map(it => ({
|
|
74
|
+
label: it.label,
|
|
75
|
+
kind: kinds[it.kind] !== undefined ? kinds[it.kind] : K.Text,
|
|
76
|
+
detail: it.detail,
|
|
77
|
+
documentation: markdown(it.documentation),
|
|
78
|
+
insertText: it.insertText || it.label,
|
|
79
|
+
insertTextRules: it.insertTextFormat === 2 ? monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet : undefined,
|
|
80
|
+
range,
|
|
81
|
+
})),
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
monaco.languages.registerHoverProvider(DS_LANGUAGE_ID, {
|
|
87
|
+
async provideHover(model, position) {
|
|
88
|
+
const r = await ask(model, 'textDocument/hover', { position: toLsp(position) });
|
|
89
|
+
if (!r || !r.contents) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
const value = typeof r.contents === 'string' ? r.contents : r.contents.value;
|
|
93
|
+
return value ? { contents: [{ value }], range: r.range ? toRange(r.range) : undefined } : null;
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
monaco.languages.registerSignatureHelpProvider(DS_LANGUAGE_ID, {
|
|
98
|
+
signatureHelpTriggerCharacters: ['(', ','],
|
|
99
|
+
async provideSignatureHelp(model, position) {
|
|
100
|
+
const r = await ask(model, 'textDocument/signatureHelp', { position: toLsp(position) });
|
|
101
|
+
if (!r || !r.signatures || !r.signatures.length) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
value: {
|
|
106
|
+
signatures: r.signatures.map(s => ({
|
|
107
|
+
label: s.label,
|
|
108
|
+
documentation: markdown(s.documentation),
|
|
109
|
+
parameters: (s.parameters || []).map(p => ({ label: p.label })),
|
|
110
|
+
})),
|
|
111
|
+
activeSignature: r.activeSignature || 0,
|
|
112
|
+
activeParameter: r.activeParameter || 0,
|
|
113
|
+
},
|
|
114
|
+
dispose() {},
|
|
115
|
+
};
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
monaco.languages.registerDefinitionProvider(DS_LANGUAGE_ID, {
|
|
120
|
+
async provideDefinition(model, position) {
|
|
121
|
+
const doc = docs.get(model.uri.toString());
|
|
122
|
+
const r = await ask(model, 'textDocument/definition', { position: toLsp(position) });
|
|
123
|
+
const found = (Array.isArray(r) ? r : r ? [r] : []).filter(l => doc && l.uri === doc.uri);
|
|
124
|
+
return found.map(l => ({ uri: model.uri, range: toRange(l.range) }));
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// LSP's SymbolKind counts from 1 (File), Monaco's from 0.
|
|
129
|
+
const symbol = s => ({
|
|
130
|
+
name: s.name,
|
|
131
|
+
detail: s.detail,
|
|
132
|
+
kind: Math.max(0, s.kind - 1),
|
|
133
|
+
tags: [],
|
|
134
|
+
range: toRange(s.range),
|
|
135
|
+
selectionRange: toRange(s.selectionRange),
|
|
136
|
+
children: s.children.map(symbol),
|
|
137
|
+
});
|
|
138
|
+
monaco.languages.registerDocumentSymbolProvider(DS_LANGUAGE_ID, {
|
|
139
|
+
displayName: 'ds lsp',
|
|
140
|
+
async provideDocumentSymbols(model) {
|
|
141
|
+
const doc = docs.get(model.uri.toString());
|
|
142
|
+
const r = await ask(model, 'textDocument/documentSymbol', {});
|
|
143
|
+
return doc ? documentSymbols(r, doc.uri).map(symbol) : [];
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const legend = { tokenTypes: [...SEMANTIC_LEGEND.tokenTypes], tokenModifiers: [...SEMANTIC_LEGEND.tokenModifiers] };
|
|
148
|
+
monaco.languages.registerDocumentSemanticTokensProvider(DS_LANGUAGE_ID, {
|
|
149
|
+
getLegend: () => legend,
|
|
150
|
+
async provideDocumentSemanticTokens(model) {
|
|
151
|
+
const r = await ask(model, 'textDocument/semanticTokens/full', {});
|
|
152
|
+
return r && Array.isArray(r.data) ? { data: new Uint32Array(r.data) } : null;
|
|
153
|
+
},
|
|
154
|
+
releaseDocumentSemanticTokens() {},
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// The declaration among the references, whichever Monaco action asks, as
|
|
158
|
+
// ds-codemirror-editor lists them: Shift-F12 then peeks every site.
|
|
159
|
+
monaco.languages.registerReferenceProvider(DS_LANGUAGE_ID, {
|
|
160
|
+
async provideReferences(model, position) {
|
|
161
|
+
const doc = docs.get(model.uri.toString());
|
|
162
|
+
const r = await ask(model, 'textDocument/references', {
|
|
163
|
+
position: toLsp(position),
|
|
164
|
+
context: { includeDeclaration: true },
|
|
165
|
+
});
|
|
166
|
+
return doc ? localRanges(r, doc.uri).map(range => ({ uri: model.uri, range: toRange(range) })) : [];
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// A rename's edits, or why there are none.
|
|
171
|
+
const renameAt = async (model, position, newName) => {
|
|
172
|
+
const doc = docs.get(model.uri.toString());
|
|
173
|
+
if (!doc) {
|
|
174
|
+
return { error: 'no language server' };
|
|
175
|
+
}
|
|
176
|
+
const r = await doc.request(model.getValue(), 'textDocument/rename', { position: toLsp(position), newName });
|
|
177
|
+
if (!r || r.error) {
|
|
178
|
+
return { error: r ? r.error : 'the language server did not answer' };
|
|
179
|
+
}
|
|
180
|
+
return localRename(r.result, doc.uri);
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
monaco.languages.registerRenameProvider(DS_LANGUAGE_ID, {
|
|
184
|
+
// ds lsp has no prepareRename: F2 asks for the name renamed to
|
|
185
|
+
// itself, so a name it refuses is refused at once, in Monaco's message
|
|
186
|
+
// at the cursor (a refusal of the edits goes to Monaco's notification
|
|
187
|
+
// service, which a standalone page does not show).
|
|
188
|
+
async resolveRenameLocation(model, position) {
|
|
189
|
+
const word = model.getWordAtPosition(position);
|
|
190
|
+
if (!word) {
|
|
191
|
+
return { range: toRange({ start: toLsp(position), end: toLsp(position) }), text: '', rejectReason: 'no name here' };
|
|
192
|
+
}
|
|
193
|
+
const range = {
|
|
194
|
+
startLineNumber: position.lineNumber,
|
|
195
|
+
startColumn: word.startColumn,
|
|
196
|
+
endLineNumber: position.lineNumber,
|
|
197
|
+
endColumn: word.endColumn,
|
|
198
|
+
};
|
|
199
|
+
const trial = await renameAt(model, position, word.word);
|
|
200
|
+
return trial.error ? { range, text: word.word, rejectReason: `${word.word} is not renamed: ${trial.error}` } : { range, text: word.word };
|
|
201
|
+
},
|
|
202
|
+
async provideRenameEdits(model, position, newName) {
|
|
203
|
+
const local = await renameAt(model, position, newName);
|
|
204
|
+
if (local.error) {
|
|
205
|
+
return { edits: [], rejectReason: local.error };
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
edits: local.edits.map(e => ({
|
|
209
|
+
resource: model.uri,
|
|
210
|
+
versionId: undefined,
|
|
211
|
+
textEdit: { range: toRange(e.range), text: e.newText },
|
|
212
|
+
})),
|
|
213
|
+
};
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
return docs;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* A model's link to the host's ds lsp: its document on the server, and the
|
|
222
|
+
* providers, registered with this Monaco the first time.
|
|
223
|
+
*
|
|
224
|
+
* @param {any} monaco the host's monaco
|
|
225
|
+
* @param {any} model the editor's model, of the DS language
|
|
226
|
+
* @param {(messages: object[]) => Promise<any>} send carries messages to the host's ds lsp
|
|
227
|
+
* @param {{uri?: string}} [options] the document's URI on the server, an untitled: one under ds-monaco-editor's name unless given
|
|
228
|
+
* @returns {{doc: ReturnType<typeof lspDocument>, dispose: () => void}} the document, and what closes it
|
|
229
|
+
*/
|
|
230
|
+
export function dsLsp(monaco, model, send, options = {}) {
|
|
231
|
+
let docs = registries.get(monaco);
|
|
232
|
+
if (!docs) {
|
|
233
|
+
docs = register(monaco);
|
|
234
|
+
registries.set(monaco, docs);
|
|
235
|
+
}
|
|
236
|
+
const key = model.uri.toString();
|
|
237
|
+
const doc = lspDocument(send, options);
|
|
238
|
+
docs.set(key, doc);
|
|
239
|
+
return {
|
|
240
|
+
doc,
|
|
241
|
+
dispose() {
|
|
242
|
+
if (docs.get(key) === doc) {
|
|
243
|
+
docs.delete(key);
|
|
244
|
+
}
|
|
245
|
+
doc.close();
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Dan Finster and the Desert Sage Contributors
|
|
2
|
+
// SPDX-License-Identifier: BSD-4-Clause OR AGPL-3.0-or-later
|
|
3
|
+
// DS's language server for a host that runs Node, ds-editor-core's: exported
|
|
4
|
+
// as ./lsp-server.
|
|
5
|
+
export * from '@ds-lang/editor-core/lsp-server';
|