@rheon/feel-codemirror 0.0.0-stage → 0.9.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 +43 -2
- package/dist/index.d.ts +57 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +297 -0
- package/dist/index.js.map +1 -0
- package/package.json +54 -4
- package/src/index.ts +338 -0
package/README.md
CHANGED
|
@@ -1,3 +1,44 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @rheon/feel-codemirror
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
CodeMirror 6 support for FEEL from the rheon engine's own parser. The colours, the error marks,
|
|
4
|
+
the completions and the hint of the invoked function are what the engine answers for the text, so the editor reads a text as
|
|
5
|
+
the engine evaluates it: a name of several words is one token, and an error mark stands where
|
|
6
|
+
`evaluate` would refuse the text.
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { EditorView, basicSetup } from 'codemirror';
|
|
10
|
+
import { FeelEngine } from '@rheon/feel-runtime';
|
|
11
|
+
import { feelEditor, setFeelScope } from '@rheon/feel-codemirror';
|
|
12
|
+
|
|
13
|
+
const engine = await FeelEngine.create({ wasmUrl });
|
|
14
|
+
const view = new EditorView({
|
|
15
|
+
parent,
|
|
16
|
+
extensions: [basicSetup, feelEditor({ engine, context: { 'Monthly Salary': 5000 } })],
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// other names in scope, or unary tests
|
|
20
|
+
view.dispatch({ effects: setFeelScope({ dialect: 'unaryTests', outline }) });
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
| Option | Meaning | Default |
|
|
24
|
+
|---|---|---|
|
|
25
|
+
| `engine` | What answers: a `FeelEngine`, or `instanceSource(instance)` for an in-thread engine | required |
|
|
26
|
+
| `dialect`, `context`, `contextFormat`, `outline` | The names in scope, as for the engine's editor operations. With a large context, ask the engine for its `outline` once and give that | expression, no names |
|
|
27
|
+
| `maxDiagnostics` | The most syntax errors that are marked; the engine reports up to 100 | 20 |
|
|
28
|
+
| `templates` | Offer templates of `if`, `for`, `some`, `every`, a function and a context | true |
|
|
29
|
+
| `tree` | Ask for the nodes of the text too (`syntaxOf(state).nodes`) | false |
|
|
30
|
+
| `onSyntax` | Called with every answer the editor takes | none |
|
|
31
|
+
| `signatureHelp` | Inside the parentheses of a built-in function, show its signatures with the parameter of the cursor marked | true |
|
|
32
|
+
|
|
33
|
+
- `syntaxOf(state)` is the last answer the editor took (tokens, errors, nodes).
|
|
34
|
+
- Colours are CSS custom properties with defaults: `--feel-keyword`, `--feel-number`,
|
|
35
|
+
`--feel-string`, `--feel-constant`, `--feel-function`, `--feel-property`, `--feel-name`,
|
|
36
|
+
`--feel-comment`, `--feel-operator`, `--feel-punctuation`, `--feel-error`. The classes are
|
|
37
|
+
`cm-feel-<class>` for the token classes of the engine.
|
|
38
|
+
- The engine is asked after every change. An answer for a text that has changed since is
|
|
39
|
+
dropped.
|
|
40
|
+
- A built-in function is completed with its parentheses and shows its signature and what it
|
|
41
|
+
gives. A parameter that can be named is completed with its colon. The hint has the classes
|
|
42
|
+
`cm-feel-signature`, `-line`, `-active`, `-given` and `-summary`; `signatureHint(call)` builds
|
|
43
|
+
its element from the `call` of a `complete` answer.
|
|
44
|
+
- Tests: `npm test` runs the extension in jsdom with the engine in the same thread.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { StateEffect, type EditorState, type Extension } from '@codemirror/state';
|
|
2
|
+
import { EditorView } from '@codemirror/view';
|
|
3
|
+
import type { CompleteResponse, CompletionCall, EditorOptions, FormatResponse, SyntaxResponse } from '@rheon/feel-runtime';
|
|
4
|
+
/** What answers the editor: a `FeelEngine` (a worker), or anything with these two methods; the answers may be promises or values. */
|
|
5
|
+
export interface FeelSource {
|
|
6
|
+
syntax(expression: string, opts: EditorOptions & {
|
|
7
|
+
tree?: boolean;
|
|
8
|
+
}): Promise<SyntaxResponse> | SyntaxResponse;
|
|
9
|
+
complete(expression: string, cursor: number, opts: EditorOptions): Promise<CompleteResponse> | CompleteResponse;
|
|
10
|
+
/** Needed only for `formatDocument`. */
|
|
11
|
+
format?(expression: string, opts: EditorOptions & {
|
|
12
|
+
width?: number;
|
|
13
|
+
}): Promise<FormatResponse> | FormatResponse;
|
|
14
|
+
}
|
|
15
|
+
/** The names in scope and the dialect of the text: see `EditorOptions` of `@rheon/feel-runtime`. */
|
|
16
|
+
export type FeelScope = EditorOptions;
|
|
17
|
+
export interface FeelEditorConfig extends FeelScope {
|
|
18
|
+
engine: FeelSource;
|
|
19
|
+
/** The most syntax errors that are marked (default 20; 0 marks none). The engine reports up to 100. */
|
|
20
|
+
maxDiagnostics?: number;
|
|
21
|
+
/** Whether the completion offers templates of `if`, `for`, `some`, `every`, a function and a context (default true). */
|
|
22
|
+
templates?: boolean;
|
|
23
|
+
/** Whether the answers hold the nodes of the text (`syntaxOf(state).nodes`; default false). */
|
|
24
|
+
tree?: boolean;
|
|
25
|
+
/** Called with every answer that the editor takes. */
|
|
26
|
+
onSyntax?: (syntax: FeelSyntax) => void;
|
|
27
|
+
/** The most characters of a line that `formatDocument` aims at (default 80). */
|
|
28
|
+
formatWidth?: number;
|
|
29
|
+
/** Whether a hint shows the signatures of the built-in function whose parentheses the cursor is in (default true). */
|
|
30
|
+
signatureHelp?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/** A `syntax` answer that succeeded. */
|
|
33
|
+
export type FeelSyntax = Extract<SyntaxResponse, {
|
|
34
|
+
ok: true;
|
|
35
|
+
}>;
|
|
36
|
+
/** An in-thread engine (`instantiateFeel`) as a source: its `call` takes the requests as objects. */
|
|
37
|
+
export declare function instanceSource(instance: {
|
|
38
|
+
call(request: object): unknown;
|
|
39
|
+
}): FeelSource;
|
|
40
|
+
/** The last answer of the engine that the editor took, or null before the first. After an edit it is the answer for the text before the edit until the next one arrives. */
|
|
41
|
+
export declare function syntaxOf(state: EditorState): FeelSyntax | null;
|
|
42
|
+
/** The hint of a call: its signatures, the parameter of the cursor marked, and what the function gives. */
|
|
43
|
+
export declare function signatureHint(call: CompletionCall): HTMLElement;
|
|
44
|
+
/**
|
|
45
|
+
* The extension: highlighting, error marks and completion from the engine. Change the names in
|
|
46
|
+
* scope or the dialect later with `setFeelScope`.
|
|
47
|
+
*/
|
|
48
|
+
export declare function feelEditor(config: FeelEditorConfig): Extension;
|
|
49
|
+
/** The effect that gives an editor other names in scope or another dialect: `view.dispatch({ effects: setFeelScope(scope) })`. */
|
|
50
|
+
export declare function setFeelScope(scope: FeelScope): StateEffect<unknown>;
|
|
51
|
+
/**
|
|
52
|
+
* A command: replaces the text of the editor with its formatted form (the engine's `format`).
|
|
53
|
+
* A text with syntax errors, or one that already has the layout, stays as it is. Bound to
|
|
54
|
+
* Shift-Alt-F by `feelEditor`. Returns false when the editor has no engine that formats.
|
|
55
|
+
*/
|
|
56
|
+
export declare function formatDocument(view: EditorView): boolean;
|
|
57
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAeA,OAAO,EAAsB,WAAW,EAAc,KAAK,WAAW,EAAE,KAAK,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAClH,OAAO,EAAc,UAAU,EAA8E,MAAM,kBAAkB,CAAC;AACtI,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAkB,aAAa,EAAE,cAAc,EAAqB,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE9J,qIAAqI;AACrI,MAAM,WAAW,UAAU;IACzB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,aAAa,GAAG;QAAE,IAAI,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO,CAAC,cAAc,CAAC,GAAG,cAAc,CAAC;IAC/G,QAAQ,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EA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AClI,wBAAgB,YAAY,CAAC,KAAK,EAAE,SAAS,GAAG,WAAW,CAAC,OAAO,CAAC,CAEnE;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAgBxD"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CodeMirror 6 support for FEEL from the rheon engine's own parser: the colours, the error
|
|
3
|
+
* marks, the completions and the hint of the invoked function are what the engine answers for
|
|
4
|
+
* the text (`syntax`, `complete`, docs/CONTRACT.md §3), so the editor reads a text as the
|
|
5
|
+
* engine evaluates it.
|
|
6
|
+
*
|
|
7
|
+
* const engine = await FeelEngine.create({ wasmUrl });
|
|
8
|
+
* new EditorView({ parent, extensions: [basicSetup, feelEditor({ engine, context })] });
|
|
9
|
+
*
|
|
10
|
+
* The engine is asked after every change of the text. An answer for a text that has changed
|
|
11
|
+
* since is dropped; until the next answer arrives the colours move with the edits.
|
|
12
|
+
*/
|
|
13
|
+
import { autocompletion, snippetCompletion } from '@codemirror/autocomplete';
|
|
14
|
+
import { linter } from '@codemirror/lint';
|
|
15
|
+
import { keymap } from '@codemirror/view';
|
|
16
|
+
import { Compartment, Facet, StateEffect, StateField } from '@codemirror/state';
|
|
17
|
+
import { Decoration, EditorView, ViewPlugin, showTooltip } from '@codemirror/view';
|
|
18
|
+
/** An in-thread engine (`instantiateFeel`) as a source: its `call` takes the requests as objects. */
|
|
19
|
+
export function instanceSource(instance) {
|
|
20
|
+
return {
|
|
21
|
+
syntax: (expression, opts) => instance.call({ op: 'syntax', expression, ...opts }),
|
|
22
|
+
complete: (expression, cursor, opts) => instance.call({ op: 'complete', expression, cursor, ...opts }),
|
|
23
|
+
format: (expression, opts) => instance.call({ op: 'format', expression, ...opts }),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const setSyntax = StateEffect.define();
|
|
27
|
+
const scopeOf = Facet.define({ combine: (values) => values[0] ?? {} });
|
|
28
|
+
const formatterOf = Facet.define({ combine: (values) => values[0] ?? null });
|
|
29
|
+
function marksOf(syntax, length) {
|
|
30
|
+
const ranges = [];
|
|
31
|
+
for (const [kind, from, to] of syntax?.tokens ?? []) {
|
|
32
|
+
if (kind !== 'whitespace' && from < to && to <= length)
|
|
33
|
+
ranges.push(Decoration.mark({ class: `cm-feel-${kind}` }).range(from, to));
|
|
34
|
+
}
|
|
35
|
+
return Decoration.set(ranges, true);
|
|
36
|
+
}
|
|
37
|
+
const held = StateField.define({
|
|
38
|
+
create: () => ({ syntax: null, marks: Decoration.none }),
|
|
39
|
+
update(value, tr) {
|
|
40
|
+
for (const effect of tr.effects) {
|
|
41
|
+
if (effect.is(setSyntax))
|
|
42
|
+
return { syntax: effect.value, marks: marksOf(effect.value, tr.newDoc.length) };
|
|
43
|
+
}
|
|
44
|
+
// until the engine answers for the new text, the colours move with the edits
|
|
45
|
+
return tr.docChanged ? { syntax: value.syntax, marks: value.marks.map(tr.changes) } : value;
|
|
46
|
+
},
|
|
47
|
+
provide: (field) => EditorView.decorations.from(field, (value) => value.marks),
|
|
48
|
+
});
|
|
49
|
+
/** The last answer of the engine that the editor took, or null before the first. After an edit it is the answer for the text before the edit until the next one arrives. */
|
|
50
|
+
export function syntaxOf(state) {
|
|
51
|
+
return state.field(held, false)?.syntax ?? null;
|
|
52
|
+
}
|
|
53
|
+
/** Colours through CSS custom properties (`--feel-keyword` and so on), with defaults for a light and a dark editor. */
|
|
54
|
+
const colours = EditorView.baseTheme({
|
|
55
|
+
'.cm-feel-keyword': { color: 'var(--feel-keyword, #8a2be2)', fontWeight: '600' },
|
|
56
|
+
'.cm-feel-number': { color: 'var(--feel-number, #1d4ed8)' },
|
|
57
|
+
'.cm-feel-string': { color: 'var(--feel-string, #17803d)' },
|
|
58
|
+
'.cm-feel-constant': { color: 'var(--feel-constant, #b45309)' },
|
|
59
|
+
'.cm-feel-function, .cm-feel-type': { color: 'var(--feel-function, #0e7490)' },
|
|
60
|
+
'.cm-feel-property, .cm-feel-key, .cm-feel-parameter': { color: 'var(--feel-property, #a3366b)' },
|
|
61
|
+
'.cm-feel-name, .cm-feel-variable': { color: 'var(--feel-name, inherit)' },
|
|
62
|
+
'.cm-feel-comment': { color: 'var(--feel-comment, #746c5f)', fontStyle: 'italic' },
|
|
63
|
+
'.cm-feel-operator': { color: 'var(--feel-operator, #6a645a)' },
|
|
64
|
+
'.cm-feel-punctuation': { color: 'var(--feel-punctuation, #8a8377)' },
|
|
65
|
+
'.cm-feel-error': { color: 'var(--feel-error, #c0392b)' },
|
|
66
|
+
'.cm-feel-signature': { padding: '4px 8px', fontFamily: 'monospace', fontSize: '0.9em', maxWidth: '40em' },
|
|
67
|
+
'.cm-feel-signature-active': { textDecoration: 'underline' },
|
|
68
|
+
'.cm-feel-signature-given': { opacity: '0.6' },
|
|
69
|
+
'.cm-feel-signature-summary': { fontFamily: 'sans-serif', opacity: '0.8', marginTop: '2px' },
|
|
70
|
+
'&dark .cm-feel-keyword': { color: 'var(--feel-keyword, #c792ea)' },
|
|
71
|
+
'&dark .cm-feel-number': { color: 'var(--feel-number, #8fb0ff)' },
|
|
72
|
+
'&dark .cm-feel-string': { color: 'var(--feel-string, #a5e07b)' },
|
|
73
|
+
'&dark .cm-feel-constant': { color: 'var(--feel-constant, #f0b45a)' },
|
|
74
|
+
'&dark .cm-feel-function, &dark .cm-feel-type': { color: 'var(--feel-function, #5cd0e6)' },
|
|
75
|
+
'&dark .cm-feel-property, &dark .cm-feel-key, &dark .cm-feel-parameter': { color: 'var(--feel-property, #ff8fbf)' },
|
|
76
|
+
'&dark .cm-feel-comment': { color: 'var(--feel-comment, #8f8b83)' },
|
|
77
|
+
'&dark .cm-feel-operator': { color: 'var(--feel-operator, #9b978f)' },
|
|
78
|
+
'&dark .cm-feel-punctuation': { color: 'var(--feel-punctuation, #7d7a73)' },
|
|
79
|
+
});
|
|
80
|
+
/** The templates: each is offered where an expression can start and what is typed starts its first word. */
|
|
81
|
+
const TEMPLATES = [
|
|
82
|
+
['if … then … else …', 'if ${condition} then ${value} else ${other}'],
|
|
83
|
+
['for … in … return …', 'for ${item} in ${list} return ${value}'],
|
|
84
|
+
['some … in … satisfies …', 'some ${item} in ${list} satisfies ${condition}'],
|
|
85
|
+
['every … in … satisfies …', 'every ${item} in ${list} satisfies ${condition}'],
|
|
86
|
+
['function(…) …', 'function(${parameters}) ${body}'],
|
|
87
|
+
['{ key: value }', '{ ${key}: ${value} }'],
|
|
88
|
+
];
|
|
89
|
+
function completionOf(item) {
|
|
90
|
+
const types = { parameter: 'property', name: 'variable', entry: 'property', keyword: 'keyword', function: 'function', type: 'type' };
|
|
91
|
+
return {
|
|
92
|
+
label: item.label,
|
|
93
|
+
type: types[item.kind],
|
|
94
|
+
detail: item.detail ?? item.type ?? undefined,
|
|
95
|
+
info: item.info,
|
|
96
|
+
// an item replaces the text from its own start: the items of one answer can start at different places
|
|
97
|
+
apply: (view, _completion, _from, to) => {
|
|
98
|
+
const insert = item.kind === 'function' ? `${item.label}()` : (item.insert ?? item.label);
|
|
99
|
+
const cursor = item.from + insert.length - (item.kind === 'function' ? 1 : 0);
|
|
100
|
+
view.dispatch({ changes: { from: item.from, to, insert }, selection: { anchor: cursor }, userEvent: 'input.complete' });
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
const setCall = StateEffect.define();
|
|
105
|
+
/** The hint of the invoked function, as a tooltip at the cursor. */
|
|
106
|
+
const hint = StateField.define({
|
|
107
|
+
create: () => null,
|
|
108
|
+
update(value, tr) {
|
|
109
|
+
for (const effect of tr.effects) {
|
|
110
|
+
if (effect.is(setCall))
|
|
111
|
+
return effect.value;
|
|
112
|
+
}
|
|
113
|
+
// the hint of a text or a cursor that has changed goes; the next answer brings it back
|
|
114
|
+
return tr.docChanged || tr.selection ? null : value;
|
|
115
|
+
},
|
|
116
|
+
provide: (field) => showTooltip.from(field),
|
|
117
|
+
});
|
|
118
|
+
/** The index of the parameter of the signature that the argument of the call is, or -1. */
|
|
119
|
+
function activeParameter(signature, call) {
|
|
120
|
+
if (call.named.length > 0)
|
|
121
|
+
return -1;
|
|
122
|
+
const last = signature.parameters.length - 1;
|
|
123
|
+
if (call.argument <= last)
|
|
124
|
+
return call.argument;
|
|
125
|
+
return last >= 0 && signature.parameters[last].rest ? last : -1;
|
|
126
|
+
}
|
|
127
|
+
/** The hint of a call: its signatures, the parameter of the cursor marked, and what the function gives. */
|
|
128
|
+
export function signatureHint(call) {
|
|
129
|
+
const dom = document.createElement('div');
|
|
130
|
+
dom.className = 'cm-feel-signature';
|
|
131
|
+
// the signatures that have a parameter for the argument, or all when none has
|
|
132
|
+
const fitting = call.signatures.filter((signature) => call.named.length > 0 || call.argument === 0 || activeParameter(signature, call) >= 0);
|
|
133
|
+
for (const signature of fitting.length ? fitting : call.signatures) {
|
|
134
|
+
const line = dom.appendChild(document.createElement('div'));
|
|
135
|
+
line.className = 'cm-feel-signature-line';
|
|
136
|
+
line.append(`${call.function}(`);
|
|
137
|
+
const active = activeParameter(signature, call);
|
|
138
|
+
signature.parameters.forEach((parameter, index) => {
|
|
139
|
+
if (index > 0)
|
|
140
|
+
line.append(', ');
|
|
141
|
+
const part = line.appendChild(document.createElement(index === active ? 'strong' : 'span'));
|
|
142
|
+
part.textContent = `${parameter.name}${parameter.rest ? '...' : parameter.optional ? '?' : ''}: ${parameter.type}`;
|
|
143
|
+
if (index === active || call.named.includes(parameter.name))
|
|
144
|
+
part.className = index === active ? 'cm-feel-signature-active' : 'cm-feel-signature-given';
|
|
145
|
+
});
|
|
146
|
+
line.append(`): ${signature.result}`);
|
|
147
|
+
}
|
|
148
|
+
const summary = dom.appendChild(document.createElement('div'));
|
|
149
|
+
summary.className = 'cm-feel-signature-summary';
|
|
150
|
+
summary.textContent = call.summary;
|
|
151
|
+
return dom;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The extension: highlighting, error marks and completion from the engine. Change the names in
|
|
155
|
+
* scope or the dialect later with `setFeelScope`.
|
|
156
|
+
*/
|
|
157
|
+
export function feelEditor(config) {
|
|
158
|
+
const { engine, maxDiagnostics = 20, templates = true, tree = false, onSyntax, signatureHelp = true } = config;
|
|
159
|
+
const scope = { dialect: config.dialect, context: config.context, contextFormat: config.contextFormat, outline: config.outline };
|
|
160
|
+
// asks the engine after every change of the text or of the scope
|
|
161
|
+
const asker = ViewPlugin.fromClass(class {
|
|
162
|
+
view;
|
|
163
|
+
asked = 0;
|
|
164
|
+
constructor(view) {
|
|
165
|
+
this.view = view;
|
|
166
|
+
this.ask();
|
|
167
|
+
}
|
|
168
|
+
update(update) {
|
|
169
|
+
if (update.docChanged || update.startState.facet(scopeOf) !== update.state.facet(scopeOf))
|
|
170
|
+
this.ask();
|
|
171
|
+
}
|
|
172
|
+
ask() {
|
|
173
|
+
const id = ++this.asked;
|
|
174
|
+
const text = this.view.state.doc.toString();
|
|
175
|
+
const take = (answer) => {
|
|
176
|
+
// only the answer to the last question, for the text as it still is
|
|
177
|
+
if (id !== this.asked || this.view.state.doc.toString() !== text)
|
|
178
|
+
return;
|
|
179
|
+
const syntax = answer.ok ? answer : null;
|
|
180
|
+
this.view.dispatch({ effects: setSyntax.of(syntax) });
|
|
181
|
+
if (syntax)
|
|
182
|
+
onSyntax?.(syntax);
|
|
183
|
+
};
|
|
184
|
+
// a dispatch is not allowed inside an update: the answer is taken afterwards
|
|
185
|
+
Promise.resolve()
|
|
186
|
+
.then(() => engine.syntax(text, { ...this.view.state.facet(scopeOf), tree }))
|
|
187
|
+
.then(take, () => undefined);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
// asks the engine for the invoked function at the cursor after every change of the text or the cursor
|
|
191
|
+
const hinter = ViewPlugin.fromClass(class {
|
|
192
|
+
view;
|
|
193
|
+
asked = 0;
|
|
194
|
+
constructor(view) {
|
|
195
|
+
this.view = view;
|
|
196
|
+
this.ask();
|
|
197
|
+
}
|
|
198
|
+
update(update) {
|
|
199
|
+
if (update.docChanged || update.selectionSet || update.startState.facet(scopeOf) !== update.state.facet(scopeOf))
|
|
200
|
+
this.ask();
|
|
201
|
+
}
|
|
202
|
+
ask() {
|
|
203
|
+
const id = ++this.asked;
|
|
204
|
+
const { state } = this.view;
|
|
205
|
+
const selection = state.selection.main;
|
|
206
|
+
if (!selection.empty)
|
|
207
|
+
return;
|
|
208
|
+
const text = state.doc.toString();
|
|
209
|
+
const take = (answer) => {
|
|
210
|
+
const now = this.view.state;
|
|
211
|
+
if (id !== this.asked || now.doc.toString() !== text || now.selection.main.head !== selection.head)
|
|
212
|
+
return;
|
|
213
|
+
const call = answer.ok ? answer.call : undefined;
|
|
214
|
+
if (!call && now.field(hint) === null)
|
|
215
|
+
return;
|
|
216
|
+
this.view.dispatch({ effects: setCall.of(call ? { pos: selection.head, above: true, create: () => ({ dom: signatureHint(call) }) } : null) });
|
|
217
|
+
};
|
|
218
|
+
Promise.resolve()
|
|
219
|
+
.then(() => engine.complete(text, selection.head, state.facet(scopeOf)))
|
|
220
|
+
.then(take, () => undefined);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
const errors = linter((view) => {
|
|
224
|
+
const length = view.state.doc.length;
|
|
225
|
+
return (syntaxOf(view.state)?.diagnostics ?? []).slice(0, maxDiagnostics).map((d) => {
|
|
226
|
+
let from = Math.min(d.from, length);
|
|
227
|
+
let to = Math.min(d.to, length);
|
|
228
|
+
if (from === to && from > 0) {
|
|
229
|
+
// an error at an empty place (a missing part, the end of the text): mark the character before it
|
|
230
|
+
from -= 1;
|
|
231
|
+
}
|
|
232
|
+
else if (from === to && to < length) {
|
|
233
|
+
to += 1;
|
|
234
|
+
}
|
|
235
|
+
return { from, to, severity: 'error', source: 'rheon', message: d.message };
|
|
236
|
+
});
|
|
237
|
+
}, { delay: 0, needsRefresh: (update) => update.transactions.some((tr) => tr.effects.some((effect) => effect.is(setSyntax))) });
|
|
238
|
+
const complete = async (context) => {
|
|
239
|
+
const text = context.state.doc.toString();
|
|
240
|
+
const answer = await engine.complete(text, context.pos, context.state.facet(scopeOf));
|
|
241
|
+
if (context.aborted || !answer.ok)
|
|
242
|
+
return null;
|
|
243
|
+
const from = answer.items.reduce((least, item) => Math.min(least, item.from), context.pos);
|
|
244
|
+
if (answer.items.length === 0 && !context.explicit)
|
|
245
|
+
return null;
|
|
246
|
+
// nothing is offered for an empty word unless asked for, except after a dot and where a parameter is named
|
|
247
|
+
if (from === context.pos && !context.explicit && answer.expects !== 'property' && answer.expects !== 'parameter')
|
|
248
|
+
return null;
|
|
249
|
+
const options = answer.items.map(completionOf);
|
|
250
|
+
if (templates && answer.expects === 'expression') {
|
|
251
|
+
const typed = text.slice(from, context.pos).toLowerCase();
|
|
252
|
+
for (const [label, template] of TEMPLATES) {
|
|
253
|
+
if (typed !== '' && template.toLowerCase().startsWith(typed))
|
|
254
|
+
options.push(snippetCompletion(template, { label, type: 'text', boost: -1 }));
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return options.length ? { from, to: context.pos, options, filter: false } : null;
|
|
258
|
+
};
|
|
259
|
+
return [
|
|
260
|
+
scopeCompartment.of(scopeOf.of(scope)),
|
|
261
|
+
formatterOf.of({ engine, width: config.formatWidth }),
|
|
262
|
+
held,
|
|
263
|
+
asker,
|
|
264
|
+
colours,
|
|
265
|
+
errors,
|
|
266
|
+
autocompletion({ override: [complete] }),
|
|
267
|
+
keymap.of([{ key: 'Shift-Alt-f', run: formatDocument }]),
|
|
268
|
+
signatureHelp ? [hint, hinter] : [],
|
|
269
|
+
];
|
|
270
|
+
}
|
|
271
|
+
const scopeCompartment = new Compartment();
|
|
272
|
+
/** The effect that gives an editor other names in scope or another dialect: `view.dispatch({ effects: setFeelScope(scope) })`. */
|
|
273
|
+
export function setFeelScope(scope) {
|
|
274
|
+
return scopeCompartment.reconfigure(scopeOf.of(scope));
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* A command: replaces the text of the editor with its formatted form (the engine's `format`).
|
|
278
|
+
* A text with syntax errors, or one that already has the layout, stays as it is. Bound to
|
|
279
|
+
* Shift-Alt-F by `feelEditor`. Returns false when the editor has no engine that formats.
|
|
280
|
+
*/
|
|
281
|
+
export function formatDocument(view) {
|
|
282
|
+
const formatter = view.state.facet(formatterOf);
|
|
283
|
+
if (!formatter?.engine.format)
|
|
284
|
+
return false;
|
|
285
|
+
const text = view.state.doc.toString();
|
|
286
|
+
Promise.resolve()
|
|
287
|
+
.then(() => formatter.engine.format(text, { ...view.state.facet(scopeOf), width: formatter.width }))
|
|
288
|
+
.then((answer) => {
|
|
289
|
+
// only for the text that was sent
|
|
290
|
+
if (!answer.ok || !answer.changed || view.state.doc.toString() !== text)
|
|
291
|
+
return;
|
|
292
|
+
const cursor = Math.min(view.state.selection.main.head, answer.formatted.length);
|
|
293
|
+
view.dispatch({ changes: { from: 0, to: text.length, insert: answer.formatted }, selection: { anchor: cursor }, userEvent: 'input.format' });
|
|
294
|
+
}, () => undefined);
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAkE,MAAM,0BAA0B,CAAC;AAC7I,OAAO,EAAE,MAAM,EAAmB,MAAM,kBAAkB,CAAC;AAC3D,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,WAAW,EAAE,UAAU,EAAoC,MAAM,mBAAmB,CAAC;AAClH,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAqD,MAAM,kBAAkB,CAAC;AAiCtI,qGAAqG;AACrG,MAAM,UAAU,cAAc,CAAC,QAA4C;IACzE,OAAO;QACL,MAAM,EAAE,CAAC,UAAU,EAAE,IAAI,EAAE,EAAE,CAAC,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AAC,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,IAAI;YAAE,OAAO;QAChF,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;QACjF,IAAI,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,EAAE,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,SAAS,EAAE,EAAE,SAAS,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,SAAS,EAAE,cAAc,EAAE,CAAC,CAAC;IAC/I,CAAC,EACD,GAAG,EAAE,CAAC,SAAS,CAChB,CAAC;IACJ,OAAO,IAAI,CAAC;AACd,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rheon/feel-codemirror",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "CodeMirror 6 support for FEEL from the rheon engine's own parser: highlighting, error marks and completion",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"feel",
|
|
7
|
+
"dmn",
|
|
8
|
+
"codemirror",
|
|
9
|
+
"editor",
|
|
10
|
+
"rheon"
|
|
11
|
+
],
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/spearsandshields/rheon.git",
|
|
15
|
+
"directory": "packages/feel-codemirror"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://spearsandshields.github.io/rheon/",
|
|
18
|
+
"bugs": "https://github.com/spearsandshields/rheon/issues",
|
|
19
|
+
"license": "MIT OR Apache-2.0",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"main": "./dist/index.js",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"src",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "rm -rf dist && tsc -p tsconfig.json",
|
|
41
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
42
|
+
"prepack": "npm run build",
|
|
43
|
+
"test": "npm run build && node --test --test-timeout=120000 test/*.test.mjs"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@codemirror/autocomplete": "^6.0.0",
|
|
47
|
+
"@codemirror/lint": "^6.0.0",
|
|
48
|
+
"@codemirror/state": "^6.0.0",
|
|
49
|
+
"@codemirror/view": "^6.0.0",
|
|
50
|
+
"@rheon/feel-runtime": "0.9.0"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"jsdom": "^31.0.1",
|
|
54
|
+
"typescript": "^5.9.0"
|
|
55
|
+
}
|
|
56
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CodeMirror 6 support for FEEL from the rheon engine's own parser: the colours, the error
|
|
3
|
+
* marks, the completions and the hint of the invoked function are what the engine answers for
|
|
4
|
+
* the text (`syntax`, `complete`, docs/CONTRACT.md §3), so the editor reads a text as the
|
|
5
|
+
* engine evaluates it.
|
|
6
|
+
*
|
|
7
|
+
* const engine = await FeelEngine.create({ wasmUrl });
|
|
8
|
+
* new EditorView({ parent, extensions: [basicSetup, feelEditor({ engine, context })] });
|
|
9
|
+
*
|
|
10
|
+
* The engine is asked after every change of the text. An answer for a text that has changed
|
|
11
|
+
* since is dropped; until the next answer arrives the colours move with the edits.
|
|
12
|
+
*/
|
|
13
|
+
import { autocompletion, snippetCompletion, type Completion, type CompletionContext, type CompletionResult } from '@codemirror/autocomplete';
|
|
14
|
+
import { linter, type Diagnostic } from '@codemirror/lint';
|
|
15
|
+
import { keymap } from '@codemirror/view';
|
|
16
|
+
import { Compartment, Facet, StateEffect, StateField, type EditorState, type Extension } from '@codemirror/state';
|
|
17
|
+
import { Decoration, EditorView, ViewPlugin, showTooltip, type DecorationSet, type Tooltip, type ViewUpdate } from '@codemirror/view';
|
|
18
|
+
import type { CompleteResponse, CompletionCall, CompletionItem, EditorOptions, FormatResponse, FunctionSignature, SyntaxResponse } from '@rheon/feel-runtime';
|
|
19
|
+
|
|
20
|
+
/** What answers the editor: a `FeelEngine` (a worker), or anything with these two methods; the answers may be promises or values. */
|
|
21
|
+
export interface FeelSource {
|
|
22
|
+
syntax(expression: string, opts: EditorOptions & { tree?: boolean }): Promise<SyntaxResponse> | SyntaxResponse;
|
|
23
|
+
complete(expression: string, cursor: number, opts: EditorOptions): Promise<CompleteResponse> | CompleteResponse;
|
|
24
|
+
/** Needed only for `formatDocument`. */
|
|
25
|
+
format?(expression: string, opts: EditorOptions & { width?: number }): Promise<FormatResponse> | FormatResponse;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The names in scope and the dialect of the text: see `EditorOptions` of `@rheon/feel-runtime`. */
|
|
29
|
+
export type FeelScope = EditorOptions;
|
|
30
|
+
|
|
31
|
+
export interface FeelEditorConfig extends FeelScope {
|
|
32
|
+
engine: FeelSource;
|
|
33
|
+
/** The most syntax errors that are marked (default 20; 0 marks none). The engine reports up to 100. */
|
|
34
|
+
maxDiagnostics?: number;
|
|
35
|
+
/** Whether the completion offers templates of `if`, `for`, `some`, `every`, a function and a context (default true). */
|
|
36
|
+
templates?: boolean;
|
|
37
|
+
/** Whether the answers hold the nodes of the text (`syntaxOf(state).nodes`; default false). */
|
|
38
|
+
tree?: boolean;
|
|
39
|
+
/** Called with every answer that the editor takes. */
|
|
40
|
+
onSyntax?: (syntax: FeelSyntax) => void;
|
|
41
|
+
/** The most characters of a line that `formatDocument` aims at (default 80). */
|
|
42
|
+
formatWidth?: number;
|
|
43
|
+
/** Whether a hint shows the signatures of the built-in function whose parentheses the cursor is in (default true). */
|
|
44
|
+
signatureHelp?: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A `syntax` answer that succeeded. */
|
|
48
|
+
export type FeelSyntax = Extract<SyntaxResponse, { ok: true }>;
|
|
49
|
+
|
|
50
|
+
/** An in-thread engine (`instantiateFeel`) as a source: its `call` takes the requests as objects. */
|
|
51
|
+
export function instanceSource(instance: { call(request: object): unknown }): FeelSource {
|
|
52
|
+
return {
|
|
53
|
+
syntax: (expression, opts) => instance.call({ op: 'syntax', expression, ...opts }) as SyntaxResponse,
|
|
54
|
+
complete: (expression, cursor, opts) => instance.call({ op: 'complete', expression, cursor, ...opts }) as CompleteResponse,
|
|
55
|
+
format: (expression, opts) => instance.call({ op: 'format', expression, ...opts }) as FormatResponse,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const setSyntax = StateEffect.define<FeelSyntax | null>();
|
|
60
|
+
const scopeOf = Facet.define<FeelScope, FeelScope>({ combine: (values) => values[0] ?? {} });
|
|
61
|
+
const formatterOf = Facet.define<{ engine: FeelSource; width?: number }, { engine: FeelSource; width?: number } | null>({ combine: (values) => values[0] ?? null });
|
|
62
|
+
|
|
63
|
+
interface Held {
|
|
64
|
+
syntax: FeelSyntax | null;
|
|
65
|
+
marks: DecorationSet;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function marksOf(syntax: FeelSyntax | null, length: number): DecorationSet {
|
|
69
|
+
const ranges = [];
|
|
70
|
+
for (const [kind, from, to] of syntax?.tokens ?? []) {
|
|
71
|
+
if (kind !== 'whitespace' && from < to && to <= length) ranges.push(Decoration.mark({ class: `cm-feel-${kind}` }).range(from, to));
|
|
72
|
+
}
|
|
73
|
+
return Decoration.set(ranges, true);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const held = StateField.define<Held>({
|
|
77
|
+
create: () => ({ syntax: null, marks: Decoration.none }),
|
|
78
|
+
update(value, tr) {
|
|
79
|
+
for (const effect of tr.effects) {
|
|
80
|
+
if (effect.is(setSyntax)) return { syntax: effect.value, marks: marksOf(effect.value, tr.newDoc.length) };
|
|
81
|
+
}
|
|
82
|
+
// until the engine answers for the new text, the colours move with the edits
|
|
83
|
+
return tr.docChanged ? { syntax: value.syntax, marks: value.marks.map(tr.changes) } : value;
|
|
84
|
+
},
|
|
85
|
+
provide: (field) => EditorView.decorations.from(field, (value) => value.marks),
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
/** The last answer of the engine that the editor took, or null before the first. After an edit it is the answer for the text before the edit until the next one arrives. */
|
|
89
|
+
export function syntaxOf(state: EditorState): FeelSyntax | null {
|
|
90
|
+
return state.field(held, false)?.syntax ?? null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Colours through CSS custom properties (`--feel-keyword` and so on), with defaults for a light and a dark editor. */
|
|
94
|
+
const colours = EditorView.baseTheme({
|
|
95
|
+
'.cm-feel-keyword': { color: 'var(--feel-keyword, #8a2be2)', fontWeight: '600' },
|
|
96
|
+
'.cm-feel-number': { color: 'var(--feel-number, #1d4ed8)' },
|
|
97
|
+
'.cm-feel-string': { color: 'var(--feel-string, #17803d)' },
|
|
98
|
+
'.cm-feel-constant': { color: 'var(--feel-constant, #b45309)' },
|
|
99
|
+
'.cm-feel-function, .cm-feel-type': { color: 'var(--feel-function, #0e7490)' },
|
|
100
|
+
'.cm-feel-property, .cm-feel-key, .cm-feel-parameter': { color: 'var(--feel-property, #a3366b)' },
|
|
101
|
+
'.cm-feel-name, .cm-feel-variable': { color: 'var(--feel-name, inherit)' },
|
|
102
|
+
'.cm-feel-comment': { color: 'var(--feel-comment, #746c5f)', fontStyle: 'italic' },
|
|
103
|
+
'.cm-feel-operator': { color: 'var(--feel-operator, #6a645a)' },
|
|
104
|
+
'.cm-feel-punctuation': { color: 'var(--feel-punctuation, #8a8377)' },
|
|
105
|
+
'.cm-feel-error': { color: 'var(--feel-error, #c0392b)' },
|
|
106
|
+
'.cm-feel-signature': { padding: '4px 8px', fontFamily: 'monospace', fontSize: '0.9em', maxWidth: '40em' },
|
|
107
|
+
'.cm-feel-signature-active': { textDecoration: 'underline' },
|
|
108
|
+
'.cm-feel-signature-given': { opacity: '0.6' },
|
|
109
|
+
'.cm-feel-signature-summary': { fontFamily: 'sans-serif', opacity: '0.8', marginTop: '2px' },
|
|
110
|
+
'&dark .cm-feel-keyword': { color: 'var(--feel-keyword, #c792ea)' },
|
|
111
|
+
'&dark .cm-feel-number': { color: 'var(--feel-number, #8fb0ff)' },
|
|
112
|
+
'&dark .cm-feel-string': { color: 'var(--feel-string, #a5e07b)' },
|
|
113
|
+
'&dark .cm-feel-constant': { color: 'var(--feel-constant, #f0b45a)' },
|
|
114
|
+
'&dark .cm-feel-function, &dark .cm-feel-type': { color: 'var(--feel-function, #5cd0e6)' },
|
|
115
|
+
'&dark .cm-feel-property, &dark .cm-feel-key, &dark .cm-feel-parameter': { color: 'var(--feel-property, #ff8fbf)' },
|
|
116
|
+
'&dark .cm-feel-comment': { color: 'var(--feel-comment, #8f8b83)' },
|
|
117
|
+
'&dark .cm-feel-operator': { color: 'var(--feel-operator, #9b978f)' },
|
|
118
|
+
'&dark .cm-feel-punctuation': { color: 'var(--feel-punctuation, #7d7a73)' },
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
/** The templates: each is offered where an expression can start and what is typed starts its first word. */
|
|
122
|
+
const TEMPLATES: [string, string][] = [
|
|
123
|
+
['if … then … else …', 'if ${condition} then ${value} else ${other}'],
|
|
124
|
+
['for … in … return …', 'for ${item} in ${list} return ${value}'],
|
|
125
|
+
['some … in … satisfies …', 'some ${item} in ${list} satisfies ${condition}'],
|
|
126
|
+
['every … in … satisfies …', 'every ${item} in ${list} satisfies ${condition}'],
|
|
127
|
+
['function(…) …', 'function(${parameters}) ${body}'],
|
|
128
|
+
['{ key: value }', '{ ${key}: ${value} }'],
|
|
129
|
+
];
|
|
130
|
+
|
|
131
|
+
function completionOf(item: CompletionItem): Completion {
|
|
132
|
+
const types: Record<CompletionItem['kind'], string> = { parameter: 'property', name: 'variable', entry: 'property', keyword: 'keyword', function: 'function', type: 'type' };
|
|
133
|
+
return {
|
|
134
|
+
label: item.label,
|
|
135
|
+
type: types[item.kind],
|
|
136
|
+
detail: item.detail ?? item.type ?? undefined,
|
|
137
|
+
info: item.info,
|
|
138
|
+
// an item replaces the text from its own start: the items of one answer can start at different places
|
|
139
|
+
apply: (view, _completion, _from, to) => {
|
|
140
|
+
const insert = item.kind === 'function' ? `${item.label}()` : (item.insert ?? item.label);
|
|
141
|
+
const cursor = item.from + insert.length - (item.kind === 'function' ? 1 : 0);
|
|
142
|
+
view.dispatch({ changes: { from: item.from, to, insert }, selection: { anchor: cursor }, userEvent: 'input.complete' });
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const setCall = StateEffect.define<Tooltip | null>();
|
|
148
|
+
|
|
149
|
+
/** The hint of the invoked function, as a tooltip at the cursor. */
|
|
150
|
+
const hint = StateField.define<Tooltip | null>({
|
|
151
|
+
create: () => null,
|
|
152
|
+
update(value, tr) {
|
|
153
|
+
for (const effect of tr.effects) {
|
|
154
|
+
if (effect.is(setCall)) return effect.value;
|
|
155
|
+
}
|
|
156
|
+
// the hint of a text or a cursor that has changed goes; the next answer brings it back
|
|
157
|
+
return tr.docChanged || tr.selection ? null : value;
|
|
158
|
+
},
|
|
159
|
+
provide: (field) => showTooltip.from(field),
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
/** The index of the parameter of the signature that the argument of the call is, or -1. */
|
|
163
|
+
function activeParameter(signature: FunctionSignature, call: CompletionCall): number {
|
|
164
|
+
if (call.named.length > 0) return -1;
|
|
165
|
+
const last = signature.parameters.length - 1;
|
|
166
|
+
if (call.argument <= last) return call.argument;
|
|
167
|
+
return last >= 0 && signature.parameters[last].rest ? last : -1;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** The hint of a call: its signatures, the parameter of the cursor marked, and what the function gives. */
|
|
171
|
+
export function signatureHint(call: CompletionCall): HTMLElement {
|
|
172
|
+
const dom = document.createElement('div');
|
|
173
|
+
dom.className = 'cm-feel-signature';
|
|
174
|
+
// the signatures that have a parameter for the argument, or all when none has
|
|
175
|
+
const fitting = call.signatures.filter((signature) => call.named.length > 0 || call.argument === 0 || activeParameter(signature, call) >= 0);
|
|
176
|
+
for (const signature of fitting.length ? fitting : call.signatures) {
|
|
177
|
+
const line = dom.appendChild(document.createElement('div'));
|
|
178
|
+
line.className = 'cm-feel-signature-line';
|
|
179
|
+
line.append(`${call.function}(`);
|
|
180
|
+
const active = activeParameter(signature, call);
|
|
181
|
+
signature.parameters.forEach((parameter, index) => {
|
|
182
|
+
if (index > 0) line.append(', ');
|
|
183
|
+
const part = line.appendChild(document.createElement(index === active ? 'strong' : 'span'));
|
|
184
|
+
part.textContent = `${parameter.name}${parameter.rest ? '...' : parameter.optional ? '?' : ''}: ${parameter.type}`;
|
|
185
|
+
if (index === active || call.named.includes(parameter.name)) part.className = index === active ? 'cm-feel-signature-active' : 'cm-feel-signature-given';
|
|
186
|
+
});
|
|
187
|
+
line.append(`): ${signature.result}`);
|
|
188
|
+
}
|
|
189
|
+
const summary = dom.appendChild(document.createElement('div'));
|
|
190
|
+
summary.className = 'cm-feel-signature-summary';
|
|
191
|
+
summary.textContent = call.summary;
|
|
192
|
+
return dom;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The extension: highlighting, error marks and completion from the engine. Change the names in
|
|
197
|
+
* scope or the dialect later with `setFeelScope`.
|
|
198
|
+
*/
|
|
199
|
+
export function feelEditor(config: FeelEditorConfig): Extension {
|
|
200
|
+
const { engine, maxDiagnostics = 20, templates = true, tree = false, onSyntax, signatureHelp = true } = config;
|
|
201
|
+
const scope: FeelScope = { dialect: config.dialect, context: config.context, contextFormat: config.contextFormat, outline: config.outline };
|
|
202
|
+
|
|
203
|
+
// asks the engine after every change of the text or of the scope
|
|
204
|
+
const asker = ViewPlugin.fromClass(
|
|
205
|
+
class {
|
|
206
|
+
private asked = 0;
|
|
207
|
+
constructor(readonly view: EditorView) {
|
|
208
|
+
this.ask();
|
|
209
|
+
}
|
|
210
|
+
update(update: ViewUpdate) {
|
|
211
|
+
if (update.docChanged || update.startState.facet(scopeOf) !== update.state.facet(scopeOf)) this.ask();
|
|
212
|
+
}
|
|
213
|
+
private ask() {
|
|
214
|
+
const id = ++this.asked;
|
|
215
|
+
const text = this.view.state.doc.toString();
|
|
216
|
+
const take = (answer: SyntaxResponse) => {
|
|
217
|
+
// only the answer to the last question, for the text as it still is
|
|
218
|
+
if (id !== this.asked || this.view.state.doc.toString() !== text) return;
|
|
219
|
+
const syntax = answer.ok ? answer : null;
|
|
220
|
+
this.view.dispatch({ effects: setSyntax.of(syntax) });
|
|
221
|
+
if (syntax) onSyntax?.(syntax);
|
|
222
|
+
};
|
|
223
|
+
// a dispatch is not allowed inside an update: the answer is taken afterwards
|
|
224
|
+
Promise.resolve()
|
|
225
|
+
.then(() => engine.syntax(text, { ...this.view.state.facet(scopeOf), tree }))
|
|
226
|
+
.then(take, () => undefined);
|
|
227
|
+
}
|
|
228
|
+
},
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
// asks the engine for the invoked function at the cursor after every change of the text or the cursor
|
|
232
|
+
const hinter = ViewPlugin.fromClass(
|
|
233
|
+
class {
|
|
234
|
+
private asked = 0;
|
|
235
|
+
constructor(readonly view: EditorView) {
|
|
236
|
+
this.ask();
|
|
237
|
+
}
|
|
238
|
+
update(update: ViewUpdate) {
|
|
239
|
+
if (update.docChanged || update.selectionSet || update.startState.facet(scopeOf) !== update.state.facet(scopeOf)) this.ask();
|
|
240
|
+
}
|
|
241
|
+
private ask() {
|
|
242
|
+
const id = ++this.asked;
|
|
243
|
+
const { state } = this.view;
|
|
244
|
+
const selection = state.selection.main;
|
|
245
|
+
if (!selection.empty) return;
|
|
246
|
+
const text = state.doc.toString();
|
|
247
|
+
const take = (answer: CompleteResponse) => {
|
|
248
|
+
const now = this.view.state;
|
|
249
|
+
if (id !== this.asked || now.doc.toString() !== text || now.selection.main.head !== selection.head) return;
|
|
250
|
+
const call = answer.ok ? answer.call : undefined;
|
|
251
|
+
if (!call && now.field(hint) === null) return;
|
|
252
|
+
this.view.dispatch({ effects: setCall.of(call ? { pos: selection.head, above: true, create: () => ({ dom: signatureHint(call) }) } : null) });
|
|
253
|
+
};
|
|
254
|
+
Promise.resolve()
|
|
255
|
+
.then(() => engine.complete(text, selection.head, state.facet(scopeOf)))
|
|
256
|
+
.then(take, () => undefined);
|
|
257
|
+
}
|
|
258
|
+
},
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
const errors = linter(
|
|
262
|
+
(view): Diagnostic[] => {
|
|
263
|
+
const length = view.state.doc.length;
|
|
264
|
+
return (syntaxOf(view.state)?.diagnostics ?? []).slice(0, maxDiagnostics).map((d) => {
|
|
265
|
+
let from = Math.min(d.from, length);
|
|
266
|
+
let to = Math.min(d.to, length);
|
|
267
|
+
if (from === to && from > 0) {
|
|
268
|
+
// an error at an empty place (a missing part, the end of the text): mark the character before it
|
|
269
|
+
from -= 1;
|
|
270
|
+
} else if (from === to && to < length) {
|
|
271
|
+
to += 1;
|
|
272
|
+
}
|
|
273
|
+
return { from, to, severity: 'error', source: 'rheon', message: d.message };
|
|
274
|
+
});
|
|
275
|
+
},
|
|
276
|
+
{ delay: 0, needsRefresh: (update) => update.transactions.some((tr) => tr.effects.some((effect) => effect.is(setSyntax))) },
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
const complete = async (context: CompletionContext): Promise<CompletionResult | null> => {
|
|
280
|
+
const text = context.state.doc.toString();
|
|
281
|
+
const answer = await engine.complete(text, context.pos, context.state.facet(scopeOf));
|
|
282
|
+
if (context.aborted || !answer.ok) return null;
|
|
283
|
+
const from = answer.items.reduce((least, item) => Math.min(least, item.from), context.pos);
|
|
284
|
+
if (answer.items.length === 0 && !context.explicit) return null;
|
|
285
|
+
// nothing is offered for an empty word unless asked for, except after a dot and where a parameter is named
|
|
286
|
+
if (from === context.pos && !context.explicit && answer.expects !== 'property' && answer.expects !== 'parameter') return null;
|
|
287
|
+
const options = answer.items.map(completionOf);
|
|
288
|
+
if (templates && answer.expects === 'expression') {
|
|
289
|
+
const typed = text.slice(from, context.pos).toLowerCase();
|
|
290
|
+
for (const [label, template] of TEMPLATES) {
|
|
291
|
+
if (typed !== '' && template.toLowerCase().startsWith(typed)) options.push(snippetCompletion(template, { label, type: 'text', boost: -1 }));
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return options.length ? { from, to: context.pos, options, filter: false } : null;
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
return [
|
|
298
|
+
scopeCompartment.of(scopeOf.of(scope)),
|
|
299
|
+
formatterOf.of({ engine, width: config.formatWidth }),
|
|
300
|
+
held,
|
|
301
|
+
asker,
|
|
302
|
+
colours,
|
|
303
|
+
errors,
|
|
304
|
+
autocompletion({ override: [complete] }),
|
|
305
|
+
keymap.of([{ key: 'Shift-Alt-f', run: formatDocument }]),
|
|
306
|
+
signatureHelp ? [hint, hinter] : [],
|
|
307
|
+
];
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const scopeCompartment = new Compartment();
|
|
311
|
+
|
|
312
|
+
/** The effect that gives an editor other names in scope or another dialect: `view.dispatch({ effects: setFeelScope(scope) })`. */
|
|
313
|
+
export function setFeelScope(scope: FeelScope): StateEffect<unknown> {
|
|
314
|
+
return scopeCompartment.reconfigure(scopeOf.of(scope));
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* A command: replaces the text of the editor with its formatted form (the engine's `format`).
|
|
319
|
+
* A text with syntax errors, or one that already has the layout, stays as it is. Bound to
|
|
320
|
+
* Shift-Alt-F by `feelEditor`. Returns false when the editor has no engine that formats.
|
|
321
|
+
*/
|
|
322
|
+
export function formatDocument(view: EditorView): boolean {
|
|
323
|
+
const formatter = view.state.facet(formatterOf);
|
|
324
|
+
if (!formatter?.engine.format) return false;
|
|
325
|
+
const text = view.state.doc.toString();
|
|
326
|
+
Promise.resolve()
|
|
327
|
+
.then(() => formatter.engine.format!(text, { ...view.state.facet(scopeOf), width: formatter.width }))
|
|
328
|
+
.then(
|
|
329
|
+
(answer) => {
|
|
330
|
+
// only for the text that was sent
|
|
331
|
+
if (!answer.ok || !answer.changed || view.state.doc.toString() !== text) return;
|
|
332
|
+
const cursor = Math.min(view.state.selection.main.head, answer.formatted.length);
|
|
333
|
+
view.dispatch({ changes: { from: 0, to: text.length, insert: answer.formatted }, selection: { anchor: cursor }, userEvent: 'input.format' });
|
|
334
|
+
},
|
|
335
|
+
() => undefined,
|
|
336
|
+
);
|
|
337
|
+
return true;
|
|
338
|
+
}
|