@stacksjs/components 0.2.854 → 0.2.857
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/dist/CodeEditor-jg4w7e15.stx +497 -0
- package/dist/ContextMenu-43g3w5tp.stx +188 -0
- package/dist/DataGrid-7rt7v865.stx +376 -0
- package/dist/GraphView-0t3t9mec.stx +320 -0
- package/dist/JsonTree-satc4010.stx +317 -0
- package/dist/PreviewFrame-erbgvzj8.stx +257 -0
- package/dist/SplitPane-7sqfdwqa.stx +159 -0
- package/dist/code-editor/editor.js +2166 -0
- package/dist/component-api/component-api.js +36 -0
- package/dist/components/CodeBlock.d.ts +12 -5
- package/dist/context-menu/context-menu.js +575 -0
- package/dist/data-grid/data-grid.js +1244 -0
- package/dist/graph-view/graph-view.js +1408 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +34 -15
- package/dist/json-tree/json-tree.js +1121 -0
- package/dist/preview-frame/preview-frame.js +356 -0
- package/dist/split-pane/split-pane.js +364 -0
- package/dist/ui/code-editor/CodeEditor.d.ts +46 -0
- package/dist/ui/code-editor/controller.d.ts +103 -0
- package/dist/ui/code-editor/core/commands.d.ts +55 -0
- package/dist/ui/code-editor/core/completion.d.ts +40 -0
- package/dist/ui/code-editor/core/editor.d.ts +69 -0
- package/dist/ui/code-editor/core/highlight.d.ts +61 -0
- package/dist/ui/code-editor/core/history.d.ts +27 -0
- package/dist/ui/code-editor/core/index.d.ts +13 -0
- package/dist/ui/code-editor/core/keymap.d.ts +38 -0
- package/dist/ui/code-editor/core/selection.d.ts +30 -0
- package/dist/ui/code-editor/core/text.d.ts +64 -0
- package/dist/ui/code-editor/editor.d.ts +15 -0
- package/dist/ui/code-editor/index.d.ts +29 -0
- package/dist/ui/code-editor/tokenizer.d.ts +18 -0
- package/dist/ui/code-editor/view.d.ts +59 -0
- package/dist/ui/context-menu/ContextMenu.d.ts +14 -0
- package/dist/ui/context-menu/context-menu.d.ts +8 -0
- package/dist/ui/context-menu/core.d.ts +74 -0
- package/dist/ui/context-menu/index.d.ts +11 -0
- package/dist/ui/context-menu/view.d.ts +36 -0
- package/dist/ui/data-grid/DataGrid.d.ts +30 -0
- package/dist/ui/data-grid/core.d.ts +141 -0
- package/dist/ui/data-grid/data-grid.d.ts +9 -0
- package/dist/ui/data-grid/index.d.ts +20 -0
- package/dist/ui/data-grid/view.d.ts +56 -0
- package/dist/ui/graph-view/GraphView.d.ts +21 -0
- package/dist/ui/graph-view/core/edges.d.ts +34 -0
- package/dist/ui/graph-view/core/force.d.ts +10 -0
- package/dist/ui/graph-view/core/graph.d.ts +83 -0
- package/dist/ui/graph-view/core/index.d.ts +5 -0
- package/dist/ui/graph-view/core/layered.d.ts +24 -0
- package/dist/ui/graph-view/core/viewport.d.ts +34 -0
- package/dist/ui/graph-view/graph-view.d.ts +9 -0
- package/dist/ui/graph-view/index.d.ts +21 -0
- package/dist/ui/graph-view/view.d.ts +51 -0
- package/dist/ui/json-tree/JsonTree.d.ts +18 -0
- package/dist/ui/json-tree/core.d.ts +108 -0
- package/dist/ui/json-tree/index.d.ts +18 -0
- package/dist/ui/json-tree/json-tree.d.ts +10 -0
- package/dist/ui/json-tree/tinker.d.ts +84 -0
- package/dist/ui/json-tree/view.d.ts +59 -0
- package/dist/ui/preview-frame/PreviewFrame.d.ts +44 -0
- package/dist/ui/preview-frame/core.d.ts +81 -0
- package/dist/ui/preview-frame/index.d.ts +19 -0
- package/dist/ui/preview-frame/preview-frame.d.ts +8 -0
- package/dist/ui/preview-frame/view.d.ts +37 -0
- package/dist/ui/split-pane/SplitPane.d.ts +18 -0
- package/dist/ui/split-pane/core.d.ts +58 -0
- package/dist/ui/split-pane/index.d.ts +15 -0
- package/dist/ui/split-pane/split-pane.d.ts +8 -0
- package/dist/ui/split-pane/view.d.ts +27 -0
- package/dist/ui/virtual-table/index.d.ts +6 -0
- package/dist/utils/component-api.d.ts +4 -0
- package/dist/utils/highlighter.d.ts +19 -1
- package/dist/utils/highlighter.js +24 -7
- package/dist/utils/syntax-theme.d.ts +44 -0
- package/package.json +44 -4
- package/src/components/CodeBlock.stx +195 -17
- package/src/index.ts +10 -0
- package/src/ui/code-editor/CodeEditor.stx +497 -0
- package/src/ui/code-editor/README.md +135 -0
- package/src/ui/code-editor/controller.ts +517 -0
- package/src/ui/code-editor/core/commands.ts +381 -0
- package/src/ui/code-editor/core/completion.ts +212 -0
- package/src/ui/code-editor/core/editor.ts +319 -0
- package/src/ui/code-editor/core/highlight.ts +203 -0
- package/src/ui/code-editor/core/history.ts +190 -0
- package/src/ui/code-editor/core/index.ts +14 -0
- package/src/ui/code-editor/core/keymap.ts +125 -0
- package/src/ui/code-editor/core/selection.ts +104 -0
- package/src/ui/code-editor/core/text.ts +252 -0
- package/src/ui/code-editor/editor.ts +16 -0
- package/src/ui/code-editor/index.ts +46 -0
- package/src/ui/code-editor/tokenizer.ts +74 -0
- package/src/ui/code-editor/view.ts +1066 -0
- package/src/ui/context-menu/ContextMenu.stx +188 -0
- package/src/ui/context-menu/README.md +117 -0
- package/src/ui/context-menu/context-menu.ts +8 -0
- package/src/ui/context-menu/core.ts +250 -0
- package/src/ui/context-menu/index.ts +16 -0
- package/src/ui/context-menu/view.ts +540 -0
- package/src/ui/data-grid/DataGrid.stx +376 -0
- package/src/ui/data-grid/README.md +204 -0
- package/src/ui/data-grid/core.ts +813 -0
- package/src/ui/data-grid/data-grid.ts +9 -0
- package/src/ui/data-grid/index.ts +34 -0
- package/src/ui/data-grid/view.ts +806 -0
- package/src/ui/graph-view/GraphView.stx +320 -0
- package/src/ui/graph-view/README.md +210 -0
- package/src/ui/graph-view/core/edges.ts +180 -0
- package/src/ui/graph-view/core/force.ts +237 -0
- package/src/ui/graph-view/core/graph.ts +277 -0
- package/src/ui/graph-view/core/index.ts +5 -0
- package/src/ui/graph-view/core/layered.ts +256 -0
- package/src/ui/graph-view/core/viewport.ts +141 -0
- package/src/ui/graph-view/graph-view.ts +9 -0
- package/src/ui/graph-view/index.ts +35 -0
- package/src/ui/graph-view/view.ts +809 -0
- package/src/ui/json-tree/JsonTree.stx +317 -0
- package/src/ui/json-tree/README.md +193 -0
- package/src/ui/json-tree/core.ts +588 -0
- package/src/ui/json-tree/index.ts +28 -0
- package/src/ui/json-tree/json-tree.ts +10 -0
- package/src/ui/json-tree/tinker.ts +349 -0
- package/src/ui/json-tree/view.ts +665 -0
- package/src/ui/preview-frame/PreviewFrame.stx +257 -0
- package/src/ui/preview-frame/README.md +107 -0
- package/src/ui/preview-frame/core.ts +195 -0
- package/src/ui/preview-frame/index.ts +31 -0
- package/src/ui/preview-frame/preview-frame.ts +8 -0
- package/src/ui/preview-frame/view.ts +336 -0
- package/src/ui/split-pane/README.md +87 -0
- package/src/ui/split-pane/SplitPane.stx +159 -0
- package/src/ui/split-pane/core.ts +200 -0
- package/src/ui/split-pane/index.ts +24 -0
- package/src/ui/split-pane/split-pane.ts +8 -0
- package/src/ui/split-pane/view.ts +305 -0
- package/src/ui/virtual-table/index.ts +6 -0
- package/src/utils/component-api.ts +72 -0
- package/src/utils/highlighter.ts +121 -218
- package/src/utils/syntax-theme.ts +144 -0
|
@@ -0,0 +1,2166 @@
|
|
|
1
|
+
// src/ui/code-editor/core/selection.ts
|
|
2
|
+
function range(anchor, head = anchor) {
|
|
3
|
+
return { anchor, head };
|
|
4
|
+
}
|
|
5
|
+
function singleSelection(anchor, head = anchor) {
|
|
6
|
+
return { ranges: [{ anchor, head }], main: 0 };
|
|
7
|
+
}
|
|
8
|
+
function rangeFrom(value) {
|
|
9
|
+
return Math.min(value.anchor, value.head);
|
|
10
|
+
}
|
|
11
|
+
function rangeTo(value) {
|
|
12
|
+
return Math.max(value.anchor, value.head);
|
|
13
|
+
}
|
|
14
|
+
function rangeEmpty(value) {
|
|
15
|
+
return value.anchor === value.head;
|
|
16
|
+
}
|
|
17
|
+
function mainRange(selection) {
|
|
18
|
+
return selection.ranges[selection.main] ?? selection.ranges[0];
|
|
19
|
+
}
|
|
20
|
+
function selectionsEqual(a, b) {
|
|
21
|
+
if (a.main !== b.main || a.ranges.length !== b.ranges.length)
|
|
22
|
+
return false;
|
|
23
|
+
return a.ranges.every((value, index) => value.anchor === b.ranges[index].anchor && value.head === b.ranges[index].head);
|
|
24
|
+
}
|
|
25
|
+
function clampSelection(selection, length) {
|
|
26
|
+
const clamp = (offset) => Math.max(0, Math.min(length, offset));
|
|
27
|
+
return {
|
|
28
|
+
main: Math.min(selection.main, selection.ranges.length - 1),
|
|
29
|
+
ranges: selection.ranges.map((value) => ({ anchor: clamp(value.anchor), head: clamp(value.head) }))
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function mapOffset(offset, changes, assoc = 1) {
|
|
33
|
+
let shift = 0;
|
|
34
|
+
for (const change of changes) {
|
|
35
|
+
if (change.from > offset)
|
|
36
|
+
break;
|
|
37
|
+
if (change.to <= offset) {
|
|
38
|
+
if (change.from === offset && change.to === offset && assoc < 0)
|
|
39
|
+
break;
|
|
40
|
+
shift += change.insert.length - (change.to - change.from);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
return change.from + shift + (assoc < 0 ? 0 : change.insert.length);
|
|
44
|
+
}
|
|
45
|
+
return offset + shift;
|
|
46
|
+
}
|
|
47
|
+
function mapSelection(selection, changes) {
|
|
48
|
+
return {
|
|
49
|
+
main: selection.main,
|
|
50
|
+
ranges: selection.ranges.map((value) => ({
|
|
51
|
+
anchor: mapOffset(value.anchor, changes, 1),
|
|
52
|
+
head: mapOffset(value.head, changes, 1)
|
|
53
|
+
}))
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/ui/code-editor/core/text.ts
|
|
58
|
+
function normalizeNewlines(text) {
|
|
59
|
+
return text.includes("\r") ? text.replace(/\r\n?/g, `
|
|
60
|
+
`) : text;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
class TextDocument {
|
|
64
|
+
lines;
|
|
65
|
+
starts = [0];
|
|
66
|
+
validStarts = 1;
|
|
67
|
+
total;
|
|
68
|
+
text;
|
|
69
|
+
constructor(text = "") {
|
|
70
|
+
const normalized = normalizeNewlines(text);
|
|
71
|
+
this.lines = normalized.split(`
|
|
72
|
+
`);
|
|
73
|
+
this.total = normalized.length;
|
|
74
|
+
this.text = normalized;
|
|
75
|
+
}
|
|
76
|
+
get lineCount() {
|
|
77
|
+
return this.lines.length;
|
|
78
|
+
}
|
|
79
|
+
get length() {
|
|
80
|
+
return this.total;
|
|
81
|
+
}
|
|
82
|
+
line(index) {
|
|
83
|
+
return this.lines[index] ?? "";
|
|
84
|
+
}
|
|
85
|
+
toString() {
|
|
86
|
+
if (this.text === null)
|
|
87
|
+
this.text = this.lines.join(`
|
|
88
|
+
`);
|
|
89
|
+
return this.text;
|
|
90
|
+
}
|
|
91
|
+
adoptText(text) {
|
|
92
|
+
if (text.length === this.total)
|
|
93
|
+
this.text = text;
|
|
94
|
+
}
|
|
95
|
+
startOf(index) {
|
|
96
|
+
if (index >= this.validStarts) {
|
|
97
|
+
let offset = this.starts[this.validStarts - 1];
|
|
98
|
+
for (let line = this.validStarts;line <= index; line++) {
|
|
99
|
+
offset += this.lines[line - 1].length + 1;
|
|
100
|
+
this.starts[line] = offset;
|
|
101
|
+
}
|
|
102
|
+
this.validStarts = index + 1;
|
|
103
|
+
}
|
|
104
|
+
return this.starts[index];
|
|
105
|
+
}
|
|
106
|
+
clampOffset(offset) {
|
|
107
|
+
return Math.max(0, Math.min(this.total, offset));
|
|
108
|
+
}
|
|
109
|
+
lineIndexAt(offset) {
|
|
110
|
+
const target = this.clampOffset(offset);
|
|
111
|
+
let low = 0;
|
|
112
|
+
let high = this.lines.length - 1;
|
|
113
|
+
while (low < high) {
|
|
114
|
+
const middle = low + high + 1 >> 1;
|
|
115
|
+
if (this.startOf(middle) <= target)
|
|
116
|
+
low = middle;
|
|
117
|
+
else
|
|
118
|
+
high = middle - 1;
|
|
119
|
+
}
|
|
120
|
+
return low;
|
|
121
|
+
}
|
|
122
|
+
lineAt(offset) {
|
|
123
|
+
return this.lineInfo(this.lineIndexAt(offset));
|
|
124
|
+
}
|
|
125
|
+
lineInfo(index) {
|
|
126
|
+
const clamped = Math.max(0, Math.min(this.lines.length - 1, index));
|
|
127
|
+
const from = this.startOf(clamped);
|
|
128
|
+
const text = this.lines[clamped];
|
|
129
|
+
return { index: clamped, from, to: from + text.length, text };
|
|
130
|
+
}
|
|
131
|
+
positionAt(offset) {
|
|
132
|
+
const target = this.clampOffset(offset);
|
|
133
|
+
const line = this.lineIndexAt(target);
|
|
134
|
+
return { line, column: target - this.startOf(line) };
|
|
135
|
+
}
|
|
136
|
+
offsetAt(position) {
|
|
137
|
+
const line = Math.max(0, Math.min(this.lines.length - 1, position.line));
|
|
138
|
+
const column = Math.max(0, Math.min(this.lines[line].length, position.column));
|
|
139
|
+
return this.startOf(line) + column;
|
|
140
|
+
}
|
|
141
|
+
slice(from, to) {
|
|
142
|
+
const start = this.clampOffset(Math.min(from, to));
|
|
143
|
+
const end = this.clampOffset(Math.max(from, to));
|
|
144
|
+
if (start === end)
|
|
145
|
+
return "";
|
|
146
|
+
const first = this.positionAt(start);
|
|
147
|
+
const last = this.positionAt(end);
|
|
148
|
+
if (first.line === last.line)
|
|
149
|
+
return this.lines[first.line].slice(first.column, last.column);
|
|
150
|
+
const parts = [this.lines[first.line].slice(first.column)];
|
|
151
|
+
for (let line = first.line + 1;line < last.line; line++)
|
|
152
|
+
parts.push(this.lines[line]);
|
|
153
|
+
parts.push(this.lines[last.line].slice(0, last.column));
|
|
154
|
+
return parts.join(`
|
|
155
|
+
`);
|
|
156
|
+
}
|
|
157
|
+
replace(from, to, insert) {
|
|
158
|
+
const start = this.clampOffset(Math.min(from, to));
|
|
159
|
+
const end = this.clampOffset(Math.max(from, to));
|
|
160
|
+
const text = normalizeNewlines(insert);
|
|
161
|
+
const first = this.positionAt(start);
|
|
162
|
+
const last = this.positionAt(end);
|
|
163
|
+
const removed = this.slice(start, end);
|
|
164
|
+
const head = this.lines[first.line].slice(0, first.column);
|
|
165
|
+
const tail = this.lines[last.line].slice(last.column);
|
|
166
|
+
const inserted = text.split(`
|
|
167
|
+
`);
|
|
168
|
+
inserted[0] = head + inserted[0];
|
|
169
|
+
inserted[inserted.length - 1] += tail;
|
|
170
|
+
const removedLines = last.line - first.line + 1;
|
|
171
|
+
if (removedLines === 1 && inserted.length === 1)
|
|
172
|
+
this.lines[first.line] = inserted[0];
|
|
173
|
+
else
|
|
174
|
+
this.lines.splice(first.line, removedLines, ...inserted);
|
|
175
|
+
this.total += text.length - (end - start);
|
|
176
|
+
this.text = null;
|
|
177
|
+
this.validStarts = Math.min(this.validStarts, first.line + 1);
|
|
178
|
+
this.starts.length = this.validStarts;
|
|
179
|
+
return {
|
|
180
|
+
from: start,
|
|
181
|
+
to: end,
|
|
182
|
+
insert: text,
|
|
183
|
+
removed,
|
|
184
|
+
fromLine: first.line,
|
|
185
|
+
removedLines,
|
|
186
|
+
insertedLines: inserted.length
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function leadingWhitespace(text) {
|
|
191
|
+
let end = 0;
|
|
192
|
+
while (end < text.length && (text.charCodeAt(end) === 32 || text.charCodeAt(end) === 9))
|
|
193
|
+
end++;
|
|
194
|
+
return text.slice(0, end);
|
|
195
|
+
}
|
|
196
|
+
function indentWidth(whitespace, tabSize) {
|
|
197
|
+
let width = 0;
|
|
198
|
+
for (let index = 0;index < whitespace.length; index++)
|
|
199
|
+
width = whitespace.charCodeAt(index) === 9 ? width + tabSize - width % tabSize : width + 1;
|
|
200
|
+
return width;
|
|
201
|
+
}
|
|
202
|
+
function indentString(width, tabSize, insertSpaces) {
|
|
203
|
+
if (width <= 0)
|
|
204
|
+
return "";
|
|
205
|
+
if (insertSpaces)
|
|
206
|
+
return " ".repeat(width);
|
|
207
|
+
return "\t".repeat(Math.floor(width / tabSize)) + " ".repeat(width % tabSize);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// src/ui/code-editor/core/commands.ts
|
|
211
|
+
var WORD = /[\p{L}\p{N}_$]/u;
|
|
212
|
+
function isWordCharacter(character) {
|
|
213
|
+
return character !== "" && WORD.test(character);
|
|
214
|
+
}
|
|
215
|
+
function selectedLines(editor) {
|
|
216
|
+
const from = rangeFrom(editor.main);
|
|
217
|
+
const to = rangeTo(editor.main);
|
|
218
|
+
const first = editor.doc.lineIndexAt(from);
|
|
219
|
+
let last = editor.doc.lineIndexAt(to);
|
|
220
|
+
if (last > first && editor.doc.lineInfo(last).from === to)
|
|
221
|
+
last--;
|
|
222
|
+
return { first, last };
|
|
223
|
+
}
|
|
224
|
+
function linewiseSelection(editor, changes) {
|
|
225
|
+
const { anchor, head } = editor.main;
|
|
226
|
+
if (anchor === head)
|
|
227
|
+
return { anchor: mapOffset(anchor, changes, 1), head: mapOffset(head, changes, 1) };
|
|
228
|
+
const start = Math.min(anchor, head);
|
|
229
|
+
const atLineStart = editor.doc.lineAt(start).from === start;
|
|
230
|
+
const map = (offset) => mapOffset(offset, changes, offset === start && atLineStart ? -1 : 1);
|
|
231
|
+
return { anchor: map(anchor), head: map(head) };
|
|
232
|
+
}
|
|
233
|
+
var insertNewlineAndIndent = (editor) => {
|
|
234
|
+
if (editor.readOnly)
|
|
235
|
+
return false;
|
|
236
|
+
const from = rangeFrom(editor.main);
|
|
237
|
+
const to = rangeTo(editor.main);
|
|
238
|
+
const line = editor.doc.lineAt(from);
|
|
239
|
+
const before = line.text.slice(0, from - line.from);
|
|
240
|
+
const after = editor.doc.slice(to, editor.doc.lineAt(to).to);
|
|
241
|
+
const base = leadingWhitespace(before);
|
|
242
|
+
const trimmed = before.trimEnd();
|
|
243
|
+
const opener = trimmed.charAt(trimmed.length - 1);
|
|
244
|
+
const closer = editor.language.brackets[opener];
|
|
245
|
+
if (closer === undefined) {
|
|
246
|
+
const insert = `
|
|
247
|
+
${base}`;
|
|
248
|
+
editor.dispatch({
|
|
249
|
+
changes: [{ from, to, insert }],
|
|
250
|
+
selection: { anchor: from + insert.length, head: from + insert.length },
|
|
251
|
+
kind: "newline"
|
|
252
|
+
});
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
const deeper = indentString(indentWidth(base, editor.tabSize) + editor.tabSize, editor.tabSize, editor.insertSpaces);
|
|
256
|
+
const head = `
|
|
257
|
+
${deeper}`;
|
|
258
|
+
const between = after.trimStart().startsWith(closer);
|
|
259
|
+
const end = between ? to + (after.length - after.trimStart().length) : to;
|
|
260
|
+
const insert = between ? `${head}
|
|
261
|
+
${base}` : head;
|
|
262
|
+
editor.dispatch({
|
|
263
|
+
changes: [{ from, to: end, insert }],
|
|
264
|
+
selection: { anchor: from + head.length, head: from + head.length },
|
|
265
|
+
kind: "newline"
|
|
266
|
+
});
|
|
267
|
+
return true;
|
|
268
|
+
};
|
|
269
|
+
var indentMore = (editor) => {
|
|
270
|
+
if (editor.readOnly)
|
|
271
|
+
return false;
|
|
272
|
+
const from = rangeFrom(editor.main);
|
|
273
|
+
const to = rangeTo(editor.main);
|
|
274
|
+
const { first, last } = selectedLines(editor);
|
|
275
|
+
if (first === last && editor.doc.lineIndexAt(to) === first) {
|
|
276
|
+
const column = from - editor.doc.lineInfo(first).from;
|
|
277
|
+
const width = indentWidth(editor.doc.line(first).slice(0, column), editor.tabSize);
|
|
278
|
+
const insert = editor.insertSpaces ? " ".repeat(editor.tabSize - width % editor.tabSize) : "\t";
|
|
279
|
+
editor.dispatch({
|
|
280
|
+
changes: [{ from, to, insert }],
|
|
281
|
+
selection: { anchor: from + insert.length, head: from + insert.length },
|
|
282
|
+
kind: "indent"
|
|
283
|
+
});
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
286
|
+
const changes = [];
|
|
287
|
+
for (let index = first;index <= last; index++) {
|
|
288
|
+
const info = editor.doc.lineInfo(index);
|
|
289
|
+
if (info.text.length > 0)
|
|
290
|
+
changes.push({ from: info.from, to: info.from, insert: editor.indentUnit });
|
|
291
|
+
}
|
|
292
|
+
if (changes.length === 0)
|
|
293
|
+
return true;
|
|
294
|
+
editor.dispatch({ changes, selection: linewiseSelection(editor, changes), kind: "indent" });
|
|
295
|
+
return true;
|
|
296
|
+
};
|
|
297
|
+
var indentLess = (editor) => {
|
|
298
|
+
if (editor.readOnly)
|
|
299
|
+
return false;
|
|
300
|
+
const { first, last } = selectedLines(editor);
|
|
301
|
+
const changes = [];
|
|
302
|
+
for (let index = first;index <= last; index++) {
|
|
303
|
+
const info = editor.doc.lineInfo(index);
|
|
304
|
+
const whitespace = leadingWhitespace(info.text);
|
|
305
|
+
if (whitespace === "")
|
|
306
|
+
continue;
|
|
307
|
+
const width = indentWidth(whitespace, editor.tabSize);
|
|
308
|
+
const target = Math.max(0, width - (width % editor.tabSize || editor.tabSize));
|
|
309
|
+
let cut = whitespace.length;
|
|
310
|
+
while (cut > 0 && indentWidth(whitespace.slice(0, cut - 1), editor.tabSize) >= target)
|
|
311
|
+
cut--;
|
|
312
|
+
changes.push({ from: info.from + cut, to: info.from + whitespace.length, insert: "" });
|
|
313
|
+
}
|
|
314
|
+
if (changes.length === 0)
|
|
315
|
+
return true;
|
|
316
|
+
editor.dispatch({ changes, selection: linewiseSelection(editor, changes), kind: "indent" });
|
|
317
|
+
return true;
|
|
318
|
+
};
|
|
319
|
+
var toggleLineComment = (editor) => {
|
|
320
|
+
const token = editor.language.lineComment;
|
|
321
|
+
if (editor.readOnly || token === "")
|
|
322
|
+
return false;
|
|
323
|
+
const { first, last } = selectedLines(editor);
|
|
324
|
+
const lines = [];
|
|
325
|
+
for (let index = first;index <= last; index++)
|
|
326
|
+
lines.push(editor.doc.lineInfo(index));
|
|
327
|
+
const content = lines.filter((line) => line.text.trim() !== "");
|
|
328
|
+
if (content.length === 0)
|
|
329
|
+
return true;
|
|
330
|
+
const commented = content.every((line) => line.text.trimStart().startsWith(token));
|
|
331
|
+
const changes = [];
|
|
332
|
+
if (commented) {
|
|
333
|
+
for (const line of content) {
|
|
334
|
+
const start = line.from + leadingWhitespace(line.text).length;
|
|
335
|
+
const spaced = line.text.charAt(start - line.from + token.length) === " ";
|
|
336
|
+
changes.push({ from: start, to: start + token.length + (spaced ? 1 : 0), insert: "" });
|
|
337
|
+
}
|
|
338
|
+
} else {
|
|
339
|
+
const column = Math.min(...content.map((line) => leadingWhitespace(line.text).length));
|
|
340
|
+
for (const line of content)
|
|
341
|
+
changes.push({ from: line.from + column, to: line.from + column, insert: `${token} ` });
|
|
342
|
+
}
|
|
343
|
+
editor.dispatch({ changes, selection: linewiseSelection(editor, changes), kind: "comment" });
|
|
344
|
+
return true;
|
|
345
|
+
};
|
|
346
|
+
var deleteBackward = (editor) => {
|
|
347
|
+
if (editor.readOnly || !rangeEmpty(editor.main))
|
|
348
|
+
return false;
|
|
349
|
+
const head = editor.main.head;
|
|
350
|
+
if (head === 0)
|
|
351
|
+
return false;
|
|
352
|
+
const line = editor.doc.lineAt(head);
|
|
353
|
+
const column = head - line.from;
|
|
354
|
+
const previous = line.text.charAt(column - 1);
|
|
355
|
+
const next = line.text.charAt(column);
|
|
356
|
+
const pairs = editor.language.brackets[previous] === next && next !== "";
|
|
357
|
+
const quotes = previous === next && editor.language.quotes.includes(previous);
|
|
358
|
+
if (pairs || quotes) {
|
|
359
|
+
editor.dispatch({
|
|
360
|
+
changes: [{ from: head - 1, to: head + 1, insert: "" }],
|
|
361
|
+
selection: { anchor: head - 1, head: head - 1 },
|
|
362
|
+
kind: "delete-pair"
|
|
363
|
+
});
|
|
364
|
+
return true;
|
|
365
|
+
}
|
|
366
|
+
const before = line.text.slice(0, column);
|
|
367
|
+
if (editor.insertSpaces && column > 1 && /^ +$/.test(before)) {
|
|
368
|
+
const remove = column % editor.tabSize || editor.tabSize;
|
|
369
|
+
if (remove > 1) {
|
|
370
|
+
editor.dispatch({
|
|
371
|
+
changes: [{ from: head - remove, to: head, insert: "" }],
|
|
372
|
+
selection: { anchor: head - remove, head: head - remove },
|
|
373
|
+
kind: "delete-indent"
|
|
374
|
+
});
|
|
375
|
+
return true;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return false;
|
|
379
|
+
};
|
|
380
|
+
function handleTextInput(editor, text) {
|
|
381
|
+
if (editor.readOnly || text.length !== 1)
|
|
382
|
+
return false;
|
|
383
|
+
const from = rangeFrom(editor.main);
|
|
384
|
+
const to = rangeTo(editor.main);
|
|
385
|
+
const line = editor.doc.lineAt(from);
|
|
386
|
+
const column = from - line.from;
|
|
387
|
+
const previous = line.text.charAt(column - 1);
|
|
388
|
+
const next = editor.doc.slice(to, to + 1);
|
|
389
|
+
const { brackets, quotes } = editor.language;
|
|
390
|
+
const closers = Object.values(brackets);
|
|
391
|
+
const isQuote = quotes.includes(text);
|
|
392
|
+
const closer = brackets[text];
|
|
393
|
+
if (from !== to) {
|
|
394
|
+
const close = closer ?? (isQuote ? text : undefined);
|
|
395
|
+
if (close === undefined)
|
|
396
|
+
return false;
|
|
397
|
+
const selected = editor.doc.slice(from, to);
|
|
398
|
+
editor.dispatch({
|
|
399
|
+
changes: [{ from, to, insert: `${text}${selected}${close}` }],
|
|
400
|
+
selection: { anchor: from + 1, head: to + 1 },
|
|
401
|
+
kind: "wrap"
|
|
402
|
+
});
|
|
403
|
+
return true;
|
|
404
|
+
}
|
|
405
|
+
if ((closers.includes(text) || isQuote) && next === text) {
|
|
406
|
+
if (!isQuote || previous !== "\\") {
|
|
407
|
+
editor.dispatch({ selection: { anchor: from + 1, head: from + 1 }, kind: "type" });
|
|
408
|
+
return true;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
if (closer !== undefined) {
|
|
412
|
+
if (next !== "" && next !== `
|
|
413
|
+
` && !/\s/.test(next) && !closers.includes(next) && next !== "," && next !== ";")
|
|
414
|
+
return false;
|
|
415
|
+
editor.dispatch({
|
|
416
|
+
changes: [{ from, to, insert: `${text}${closer}` }],
|
|
417
|
+
selection: { anchor: from + 1, head: from + 1 },
|
|
418
|
+
kind: "pair"
|
|
419
|
+
});
|
|
420
|
+
return true;
|
|
421
|
+
}
|
|
422
|
+
if (isQuote) {
|
|
423
|
+
if (isWordCharacter(previous) || previous === "\\" || previous === text || isWordCharacter(next))
|
|
424
|
+
return false;
|
|
425
|
+
editor.dispatch({
|
|
426
|
+
changes: [{ from, to, insert: `${text}${text}` }],
|
|
427
|
+
selection: { anchor: from + 1, head: from + 1 },
|
|
428
|
+
kind: "pair"
|
|
429
|
+
});
|
|
430
|
+
return true;
|
|
431
|
+
}
|
|
432
|
+
if (closers.includes(text) && column > 0 && line.text.slice(0, column).trim() === "") {
|
|
433
|
+
const width = indentWidth(line.text.slice(0, column), editor.tabSize);
|
|
434
|
+
const outdented = indentString(Math.max(0, width - editor.tabSize), editor.tabSize, editor.insertSpaces);
|
|
435
|
+
const insert = `${outdented}${text}`;
|
|
436
|
+
editor.dispatch({
|
|
437
|
+
changes: [{ from: line.from, to: from, insert }],
|
|
438
|
+
selection: { anchor: line.from + insert.length, head: line.from + insert.length },
|
|
439
|
+
kind: "outdent"
|
|
440
|
+
});
|
|
441
|
+
return true;
|
|
442
|
+
}
|
|
443
|
+
return false;
|
|
444
|
+
}
|
|
445
|
+
var undo = (editor) => editor.undo();
|
|
446
|
+
var redo = (editor) => editor.redo();
|
|
447
|
+
var defaultCommands = {
|
|
448
|
+
insertNewlineAndIndent,
|
|
449
|
+
indentMore,
|
|
450
|
+
indentLess,
|
|
451
|
+
toggleLineComment,
|
|
452
|
+
deleteBackward,
|
|
453
|
+
undo,
|
|
454
|
+
redo
|
|
455
|
+
};
|
|
456
|
+
function registerDefaultCommands(editor) {
|
|
457
|
+
for (const [name, command] of Object.entries(defaultCommands))
|
|
458
|
+
editor.commands.set(name, command);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// src/ui/code-editor/core/completion.ts
|
|
462
|
+
var WORD_CHARACTER = /[\p{L}\p{N}_$]/u;
|
|
463
|
+
function wordStart(text, offset) {
|
|
464
|
+
let start = offset;
|
|
465
|
+
while (start > 0 && WORD_CHARACTER.test(text.charAt(start - 1)))
|
|
466
|
+
start--;
|
|
467
|
+
return start;
|
|
468
|
+
}
|
|
469
|
+
function matchScore(query, text) {
|
|
470
|
+
if (query === "")
|
|
471
|
+
return { score: 0, matches: [] };
|
|
472
|
+
const needle = query.toLowerCase();
|
|
473
|
+
const haystack = text.toLowerCase();
|
|
474
|
+
const matches = [];
|
|
475
|
+
let score = 0;
|
|
476
|
+
let position = 0;
|
|
477
|
+
let previous = -2;
|
|
478
|
+
for (let index = 0;index < needle.length; index++) {
|
|
479
|
+
const found = haystack.indexOf(needle.charAt(index), position);
|
|
480
|
+
if (found === -1)
|
|
481
|
+
return null;
|
|
482
|
+
matches.push(found);
|
|
483
|
+
score += 10;
|
|
484
|
+
if (found === previous + 1)
|
|
485
|
+
score += 15;
|
|
486
|
+
if (found === 0)
|
|
487
|
+
score += 30;
|
|
488
|
+
else if (!WORD_CHARACTER.test(text.charAt(found - 1)) || isHump(text, found))
|
|
489
|
+
score += 12;
|
|
490
|
+
if (text.charAt(found) === query.charAt(index))
|
|
491
|
+
score += 2;
|
|
492
|
+
previous = found;
|
|
493
|
+
position = found + 1;
|
|
494
|
+
}
|
|
495
|
+
if (haystack.startsWith(needle))
|
|
496
|
+
score += 40;
|
|
497
|
+
if (haystack === needle)
|
|
498
|
+
score += 20;
|
|
499
|
+
score -= Math.min(20, text.length - needle.length) * 0.5;
|
|
500
|
+
return { score, matches };
|
|
501
|
+
}
|
|
502
|
+
function isHump(text, index) {
|
|
503
|
+
const character = text.charAt(index);
|
|
504
|
+
const before = text.charAt(index - 1);
|
|
505
|
+
return character !== character.toLowerCase() && before === before.toLowerCase();
|
|
506
|
+
}
|
|
507
|
+
function rankCompletions(items, query) {
|
|
508
|
+
const ranked = [];
|
|
509
|
+
items.forEach((item, order) => {
|
|
510
|
+
const match = matchScore(query, item.filterText ?? item.label);
|
|
511
|
+
if (match)
|
|
512
|
+
ranked.push({ item, score: match.score + (item.boost ?? 0), matches: item.filterText ? [] : match.matches, order });
|
|
513
|
+
});
|
|
514
|
+
ranked.sort((a, b) => b.score - a.score || a.order - b.order);
|
|
515
|
+
return ranked.map(({ item, score, matches }) => ({ item, score, matches }));
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
class CompletionList {
|
|
519
|
+
items = [];
|
|
520
|
+
ranked = [];
|
|
521
|
+
index = 0;
|
|
522
|
+
start = 0;
|
|
523
|
+
opened = false;
|
|
524
|
+
get isOpen() {
|
|
525
|
+
return this.opened && this.ranked.length > 0;
|
|
526
|
+
}
|
|
527
|
+
get visible() {
|
|
528
|
+
return this.opened ? this.ranked : [];
|
|
529
|
+
}
|
|
530
|
+
get selectedIndex() {
|
|
531
|
+
return this.index;
|
|
532
|
+
}
|
|
533
|
+
get selected() {
|
|
534
|
+
return this.visible[this.index]?.item;
|
|
535
|
+
}
|
|
536
|
+
get from() {
|
|
537
|
+
return this.start;
|
|
538
|
+
}
|
|
539
|
+
open(items, from, query) {
|
|
540
|
+
this.items = items;
|
|
541
|
+
this.start = from;
|
|
542
|
+
this.opened = true;
|
|
543
|
+
this.filter(query);
|
|
544
|
+
}
|
|
545
|
+
filter(query) {
|
|
546
|
+
const previous = this.selected;
|
|
547
|
+
this.ranked = rankCompletions(this.items, query);
|
|
548
|
+
const kept = previous && this.index > 0 ? this.ranked.findIndex((entry) => entry.item === previous) : -1;
|
|
549
|
+
this.index = kept > 0 ? kept : 0;
|
|
550
|
+
if (this.ranked.length === 0)
|
|
551
|
+
this.opened = false;
|
|
552
|
+
}
|
|
553
|
+
move(delta) {
|
|
554
|
+
const count = this.ranked.length;
|
|
555
|
+
if (count === 0)
|
|
556
|
+
return;
|
|
557
|
+
this.index = ((this.index + delta) % count + count) % count;
|
|
558
|
+
}
|
|
559
|
+
select(index) {
|
|
560
|
+
if (index >= 0 && index < this.ranked.length)
|
|
561
|
+
this.index = index;
|
|
562
|
+
}
|
|
563
|
+
close() {
|
|
564
|
+
this.opened = false;
|
|
565
|
+
this.ranked = [];
|
|
566
|
+
this.items = [];
|
|
567
|
+
this.index = 0;
|
|
568
|
+
}
|
|
569
|
+
accept(head) {
|
|
570
|
+
const item = this.selected;
|
|
571
|
+
if (!this.isOpen || !item)
|
|
572
|
+
return null;
|
|
573
|
+
const insert = item.insert ?? item.label;
|
|
574
|
+
return { change: { from: Math.min(this.start, head), to: head, insert }, item };
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
// src/ui/code-editor/core/history.ts
|
|
579
|
+
function invertChanges(applied) {
|
|
580
|
+
let shift = 0;
|
|
581
|
+
return applied.map((change) => {
|
|
582
|
+
const from = change.from + shift;
|
|
583
|
+
shift += change.insert.length - (change.to - change.from);
|
|
584
|
+
return { from, to: from + change.insert.length, insert: change.removed };
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
class History {
|
|
589
|
+
done = [];
|
|
590
|
+
undone = [];
|
|
591
|
+
depth;
|
|
592
|
+
groupDelay;
|
|
593
|
+
constructor(options = {}) {
|
|
594
|
+
this.depth = options.depth ?? 500;
|
|
595
|
+
this.groupDelay = options.groupDelay ?? 600;
|
|
596
|
+
}
|
|
597
|
+
get canUndo() {
|
|
598
|
+
return this.done.length > 0;
|
|
599
|
+
}
|
|
600
|
+
get canRedo() {
|
|
601
|
+
return this.undone.length > 0;
|
|
602
|
+
}
|
|
603
|
+
get undoDepth() {
|
|
604
|
+
return this.done.length;
|
|
605
|
+
}
|
|
606
|
+
clear() {
|
|
607
|
+
this.done = [];
|
|
608
|
+
this.undone = [];
|
|
609
|
+
}
|
|
610
|
+
record(applied, before, after, kind, time) {
|
|
611
|
+
if (applied.length === 0)
|
|
612
|
+
return;
|
|
613
|
+
this.undone = [];
|
|
614
|
+
const entry = {
|
|
615
|
+
changes: applied.map((change) => ({ from: change.from, to: change.to, insert: change.insert })),
|
|
616
|
+
inverse: invertChanges(applied),
|
|
617
|
+
before,
|
|
618
|
+
after,
|
|
619
|
+
kind,
|
|
620
|
+
time
|
|
621
|
+
};
|
|
622
|
+
const previous = this.done[this.done.length - 1];
|
|
623
|
+
if (previous && this.joins(previous, entry)) {
|
|
624
|
+
this.done[this.done.length - 1] = this.join(previous, entry);
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
this.done.push(entry);
|
|
628
|
+
if (this.done.length > this.depth)
|
|
629
|
+
this.done.shift();
|
|
630
|
+
}
|
|
631
|
+
joins(previous, next) {
|
|
632
|
+
if (previous.kind !== next.kind || next.kind !== "type" && next.kind !== "delete")
|
|
633
|
+
return false;
|
|
634
|
+
if (next.time - previous.time > this.groupDelay)
|
|
635
|
+
return false;
|
|
636
|
+
if (previous.changes.length !== 1 || next.changes.length !== 1)
|
|
637
|
+
return false;
|
|
638
|
+
const a = previous.changes[0];
|
|
639
|
+
const b = next.changes[0];
|
|
640
|
+
if (next.kind === "type") {
|
|
641
|
+
if (a.from !== a.to || b.from !== b.to || b.insert.includes(`
|
|
642
|
+
`) || a.insert.includes(`
|
|
643
|
+
`))
|
|
644
|
+
return false;
|
|
645
|
+
return b.from === a.from + a.insert.length;
|
|
646
|
+
}
|
|
647
|
+
if (a.insert !== "" || b.insert !== "")
|
|
648
|
+
return false;
|
|
649
|
+
return b.to === a.from || b.from === a.from;
|
|
650
|
+
}
|
|
651
|
+
join(previous, next) {
|
|
652
|
+
const a = previous.changes[0];
|
|
653
|
+
const b = next.changes[0];
|
|
654
|
+
const removedA = previous.inverse[0].insert;
|
|
655
|
+
const removedB = next.inverse[0].insert;
|
|
656
|
+
let change;
|
|
657
|
+
let inverse;
|
|
658
|
+
if (next.kind === "type") {
|
|
659
|
+
change = { from: a.from, to: a.to, insert: a.insert + b.insert };
|
|
660
|
+
inverse = { from: a.from, to: a.from + change.insert.length, insert: removedA };
|
|
661
|
+
} else if (b.to === a.from) {
|
|
662
|
+
change = { from: b.from, to: a.to, insert: "" };
|
|
663
|
+
inverse = { from: b.from, to: b.from, insert: removedB + removedA };
|
|
664
|
+
} else {
|
|
665
|
+
change = { from: a.from, to: a.to + (b.to - b.from), insert: "" };
|
|
666
|
+
inverse = { from: a.from, to: a.from, insert: removedA + removedB };
|
|
667
|
+
}
|
|
668
|
+
return {
|
|
669
|
+
changes: [change],
|
|
670
|
+
inverse: [inverse],
|
|
671
|
+
before: previous.before,
|
|
672
|
+
after: next.after,
|
|
673
|
+
kind: next.kind,
|
|
674
|
+
time: next.time
|
|
675
|
+
};
|
|
676
|
+
}
|
|
677
|
+
breakGroup() {
|
|
678
|
+
const previous = this.done[this.done.length - 1];
|
|
679
|
+
if (previous)
|
|
680
|
+
previous.time = Number.NEGATIVE_INFINITY;
|
|
681
|
+
}
|
|
682
|
+
popUndo() {
|
|
683
|
+
const entry = this.done.pop();
|
|
684
|
+
if (entry)
|
|
685
|
+
this.undone.push(entry);
|
|
686
|
+
return entry;
|
|
687
|
+
}
|
|
688
|
+
popRedo() {
|
|
689
|
+
const entry = this.undone.pop();
|
|
690
|
+
if (entry)
|
|
691
|
+
this.done.push(entry);
|
|
692
|
+
return entry;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
// src/ui/code-editor/core/editor.ts
|
|
697
|
+
var DEFAULT_LANGUAGE = {
|
|
698
|
+
lineComment: "//",
|
|
699
|
+
brackets: { "(": ")", "[": "]", "{": "}" },
|
|
700
|
+
quotes: ['"', "'", "`"]
|
|
701
|
+
};
|
|
702
|
+
var LANGUAGE_CONFIGS = {
|
|
703
|
+
python: { lineComment: "#", quotes: ['"', "'"] },
|
|
704
|
+
ruby: { lineComment: "#" },
|
|
705
|
+
bash: { lineComment: "#" },
|
|
706
|
+
yaml: { lineComment: "#", quotes: ['"', "'"] },
|
|
707
|
+
toml: { lineComment: "#", quotes: ['"', "'"] },
|
|
708
|
+
dockerfile: { lineComment: "#", quotes: ['"', "'"] },
|
|
709
|
+
sql: { lineComment: "--", quotes: ['"', "'"] },
|
|
710
|
+
lua: { lineComment: "--", quotes: ['"', "'"] },
|
|
711
|
+
json: { lineComment: "", quotes: ['"'] },
|
|
712
|
+
html: { lineComment: "", quotes: ['"', "'"] },
|
|
713
|
+
css: { lineComment: "", quotes: ['"', "'"] },
|
|
714
|
+
markdown: { lineComment: "", brackets: { "(": ")", "[": "]" } },
|
|
715
|
+
text: { lineComment: "", brackets: {}, quotes: [] }
|
|
716
|
+
};
|
|
717
|
+
function languageConfig(language) {
|
|
718
|
+
return { ...DEFAULT_LANGUAGE, ...language ? LANGUAGE_CONFIGS[language.toLowerCase()] : undefined };
|
|
719
|
+
}
|
|
720
|
+
function isSelection(value) {
|
|
721
|
+
return Array.isArray(value.ranges);
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
class EditorCore {
|
|
725
|
+
doc;
|
|
726
|
+
history;
|
|
727
|
+
commands = new Map;
|
|
728
|
+
tabSize;
|
|
729
|
+
insertSpaces;
|
|
730
|
+
readOnly;
|
|
731
|
+
language;
|
|
732
|
+
current = singleSelection(0);
|
|
733
|
+
listeners = new Set;
|
|
734
|
+
now;
|
|
735
|
+
constructor(options = {}) {
|
|
736
|
+
this.doc = new TextDocument(options.value ?? "");
|
|
737
|
+
this.history = new History({ depth: options.historyDepth });
|
|
738
|
+
this.tabSize = Math.max(1, options.tabSize ?? 2);
|
|
739
|
+
this.insertSpaces = options.insertSpaces ?? true;
|
|
740
|
+
this.readOnly = options.readOnly ?? false;
|
|
741
|
+
this.language = { ...DEFAULT_LANGUAGE, ...options.language };
|
|
742
|
+
this.now = options.now ?? (() => Date.now());
|
|
743
|
+
}
|
|
744
|
+
get selection() {
|
|
745
|
+
return this.current;
|
|
746
|
+
}
|
|
747
|
+
get main() {
|
|
748
|
+
return mainRange(this.current);
|
|
749
|
+
}
|
|
750
|
+
get value() {
|
|
751
|
+
return this.doc.toString();
|
|
752
|
+
}
|
|
753
|
+
get indentUnit() {
|
|
754
|
+
return this.insertSpaces ? " ".repeat(this.tabSize) : "\t";
|
|
755
|
+
}
|
|
756
|
+
get cursor() {
|
|
757
|
+
return this.doc.positionAt(this.main.head);
|
|
758
|
+
}
|
|
759
|
+
get selectedText() {
|
|
760
|
+
return this.doc.slice(rangeFrom(this.main), rangeTo(this.main));
|
|
761
|
+
}
|
|
762
|
+
subscribe(listener) {
|
|
763
|
+
this.listeners.add(listener);
|
|
764
|
+
return () => {
|
|
765
|
+
this.listeners.delete(listener);
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
dispatch(spec) {
|
|
769
|
+
const origin = spec.origin ?? "command";
|
|
770
|
+
const kind = spec.kind ?? "edit";
|
|
771
|
+
const before = this.current;
|
|
772
|
+
const specs = this.readOnly && origin !== "api" ? [] : (spec.changes ?? []).filter((change) => change.from !== change.to || change.insert !== "");
|
|
773
|
+
const applied = Array.from({ length: specs.length });
|
|
774
|
+
for (let index = specs.length - 1;index >= 0; index--) {
|
|
775
|
+
const change = specs[index];
|
|
776
|
+
applied[index] = this.doc.replace(change.from, change.to, change.insert);
|
|
777
|
+
}
|
|
778
|
+
let selection;
|
|
779
|
+
if (spec.selection)
|
|
780
|
+
selection = isSelection(spec.selection) ? spec.selection : { ranges: [spec.selection], main: 0 };
|
|
781
|
+
else
|
|
782
|
+
selection = applied.length > 0 ? mapSelection(before, applied) : before;
|
|
783
|
+
selection = clampSelection(selection, this.doc.length);
|
|
784
|
+
this.current = selection;
|
|
785
|
+
const docChanged = applied.length > 0;
|
|
786
|
+
const selectionChanged = !selectionsEqual(before, selection);
|
|
787
|
+
if (docChanged && origin !== "history" && spec.addToHistory !== false)
|
|
788
|
+
this.history.record(applied, before, selection, kind, this.now());
|
|
789
|
+
else if (!docChanged && selectionChanged)
|
|
790
|
+
this.history.breakGroup();
|
|
791
|
+
const transaction = {
|
|
792
|
+
changes: applied,
|
|
793
|
+
before,
|
|
794
|
+
selection,
|
|
795
|
+
docChanged,
|
|
796
|
+
selectionChanged,
|
|
797
|
+
kind,
|
|
798
|
+
origin,
|
|
799
|
+
scrollIntoView: spec.scrollIntoView ?? (docChanged || selectionChanged)
|
|
800
|
+
};
|
|
801
|
+
if (docChanged || selectionChanged) {
|
|
802
|
+
for (const listener of this.listeners)
|
|
803
|
+
listener(transaction, this);
|
|
804
|
+
}
|
|
805
|
+
return transaction;
|
|
806
|
+
}
|
|
807
|
+
setSelection(anchor, head = anchor, origin = "api") {
|
|
808
|
+
return this.dispatch({ selection: { anchor, head }, origin, kind: "select" });
|
|
809
|
+
}
|
|
810
|
+
setValue(text, options = {}) {
|
|
811
|
+
const next = normalizeNewlines(text);
|
|
812
|
+
if (next === this.doc.toString())
|
|
813
|
+
return this.dispatch({ origin: "api" });
|
|
814
|
+
const head = Math.min(this.main.head, next.length);
|
|
815
|
+
const transaction = this.dispatch({
|
|
816
|
+
changes: [{ from: 0, to: this.doc.length, insert: next }],
|
|
817
|
+
selection: { anchor: head, head },
|
|
818
|
+
origin: "api",
|
|
819
|
+
kind: "set",
|
|
820
|
+
addToHistory: options.keepHistory === true,
|
|
821
|
+
scrollIntoView: false
|
|
822
|
+
});
|
|
823
|
+
if (!options.keepHistory)
|
|
824
|
+
this.history.clear();
|
|
825
|
+
return transaction;
|
|
826
|
+
}
|
|
827
|
+
syncInput(value, selectionStart, selectionEnd, backward = false) {
|
|
828
|
+
const next = normalizeNewlines(value);
|
|
829
|
+
const previous = this.doc.toString();
|
|
830
|
+
const selection = backward ? { anchor: selectionEnd, head: selectionStart } : { anchor: selectionStart, head: selectionEnd };
|
|
831
|
+
if (next === previous)
|
|
832
|
+
return this.dispatch({ selection, origin: "input", kind: "select" });
|
|
833
|
+
const limit = Math.min(previous.length, next.length);
|
|
834
|
+
let prefix = 0;
|
|
835
|
+
while (prefix < limit && previous.charCodeAt(prefix) === next.charCodeAt(prefix))
|
|
836
|
+
prefix++;
|
|
837
|
+
let suffix = 0;
|
|
838
|
+
while (suffix < limit - prefix && previous.charCodeAt(previous.length - 1 - suffix) === next.charCodeAt(next.length - 1 - suffix)) {
|
|
839
|
+
suffix++;
|
|
840
|
+
}
|
|
841
|
+
const insert = next.slice(prefix, next.length - suffix);
|
|
842
|
+
const removed = previous.length - suffix - prefix;
|
|
843
|
+
const kind = removed === 0 ? "type" : insert === "" ? "delete" : "replace";
|
|
844
|
+
const transaction = this.dispatch({
|
|
845
|
+
changes: [{ from: prefix, to: previous.length - suffix, insert }],
|
|
846
|
+
selection,
|
|
847
|
+
origin: "input",
|
|
848
|
+
kind
|
|
849
|
+
});
|
|
850
|
+
this.doc.adoptText(next);
|
|
851
|
+
return transaction;
|
|
852
|
+
}
|
|
853
|
+
undo() {
|
|
854
|
+
const entry = this.history.popUndo();
|
|
855
|
+
if (!entry)
|
|
856
|
+
return false;
|
|
857
|
+
this.dispatch({ changes: entry.inverse, selection: entry.before, origin: "history", kind: "undo" });
|
|
858
|
+
return true;
|
|
859
|
+
}
|
|
860
|
+
redo() {
|
|
861
|
+
const entry = this.history.popRedo();
|
|
862
|
+
if (!entry)
|
|
863
|
+
return false;
|
|
864
|
+
this.dispatch({ changes: entry.changes, selection: entry.after, origin: "history", kind: "redo" });
|
|
865
|
+
return true;
|
|
866
|
+
}
|
|
867
|
+
exec(name) {
|
|
868
|
+
const command = this.commands.get(name);
|
|
869
|
+
return command ? command(this) : false;
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
// src/ui/code-editor/core/highlight.ts
|
|
874
|
+
class LineHighlighter {
|
|
875
|
+
lines = [];
|
|
876
|
+
stale = true;
|
|
877
|
+
dirtyFrom = 0;
|
|
878
|
+
dirtyTo = 0;
|
|
879
|
+
tokenizer;
|
|
880
|
+
source;
|
|
881
|
+
constructor(tokenizer, source) {
|
|
882
|
+
this.tokenizer = tokenizer;
|
|
883
|
+
this.source = source;
|
|
884
|
+
this.reset();
|
|
885
|
+
}
|
|
886
|
+
reset(tokenizer, source) {
|
|
887
|
+
if (tokenizer)
|
|
888
|
+
this.tokenizer = tokenizer;
|
|
889
|
+
if (source)
|
|
890
|
+
this.source = source;
|
|
891
|
+
this.lines = Array.from({ length: this.source.lineCount }, () => null);
|
|
892
|
+
this.stale = true;
|
|
893
|
+
this.dirtyFrom = 0;
|
|
894
|
+
this.dirtyTo = this.lines.length;
|
|
895
|
+
}
|
|
896
|
+
get(index) {
|
|
897
|
+
return this.lines[index] ?? null;
|
|
898
|
+
}
|
|
899
|
+
get pending() {
|
|
900
|
+
return this.stale;
|
|
901
|
+
}
|
|
902
|
+
get firstDirtyLine() {
|
|
903
|
+
return this.stale ? this.dirtyFrom : this.lines.length;
|
|
904
|
+
}
|
|
905
|
+
applyChange(change) {
|
|
906
|
+
const fresh = Array.from({ length: change.insertedLines }).fill(null);
|
|
907
|
+
this.lines.splice(change.fromLine, change.removedLines, ...fresh);
|
|
908
|
+
const end = change.fromLine + change.insertedLines;
|
|
909
|
+
if (!this.stale) {
|
|
910
|
+
this.stale = true;
|
|
911
|
+
this.dirtyFrom = change.fromLine;
|
|
912
|
+
this.dirtyTo = end;
|
|
913
|
+
return;
|
|
914
|
+
}
|
|
915
|
+
const removedEnd = change.fromLine + change.removedLines;
|
|
916
|
+
let to = this.dirtyTo;
|
|
917
|
+
if (to >= removedEnd)
|
|
918
|
+
to += change.insertedLines - change.removedLines;
|
|
919
|
+
else if (to > change.fromLine)
|
|
920
|
+
to = end;
|
|
921
|
+
this.dirtyFrom = Math.min(this.dirtyFrom, change.fromLine);
|
|
922
|
+
this.dirtyTo = Math.min(this.lines.length, Math.max(to, end));
|
|
923
|
+
}
|
|
924
|
+
update(options = {}) {
|
|
925
|
+
if (!this.stale)
|
|
926
|
+
return { from: -1, to: -1, pending: false };
|
|
927
|
+
const count = this.source.lineCount;
|
|
928
|
+
const through = Math.min(count - 1, options.through ?? count - 1);
|
|
929
|
+
const maxLines = options.maxLines ?? Number.POSITIVE_INFINITY;
|
|
930
|
+
const start = Math.min(this.dirtyFrom, count);
|
|
931
|
+
const before = start > 0 ? this.lines[start - 1] : null;
|
|
932
|
+
let index = start;
|
|
933
|
+
let state = before ? before.endState : this.tokenizer.initialState();
|
|
934
|
+
let key = before ? before.endKey : this.tokenizer.stateKey(state);
|
|
935
|
+
let beyond = 0;
|
|
936
|
+
let settled = false;
|
|
937
|
+
while (index < count) {
|
|
938
|
+
if (index >= this.dirtyTo) {
|
|
939
|
+
const existing = this.lines[index];
|
|
940
|
+
if (existing && existing.startKey === key) {
|
|
941
|
+
settled = true;
|
|
942
|
+
break;
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
if (index > through && ++beyond > maxLines)
|
|
946
|
+
break;
|
|
947
|
+
const text = this.source.line(index);
|
|
948
|
+
const result = this.tokenizer.tokenizeLine(text, state);
|
|
949
|
+
const endKey = this.tokenizer.stateKey(result.endState);
|
|
950
|
+
this.lines[index] = { text, tokens: result.tokens, startKey: key, endState: result.endState, endKey };
|
|
951
|
+
state = result.endState;
|
|
952
|
+
key = endKey;
|
|
953
|
+
index++;
|
|
954
|
+
}
|
|
955
|
+
if (settled || index >= count) {
|
|
956
|
+
this.stale = false;
|
|
957
|
+
this.dirtyFrom = count;
|
|
958
|
+
this.dirtyTo = count;
|
|
959
|
+
} else {
|
|
960
|
+
this.dirtyFrom = index;
|
|
961
|
+
this.dirtyTo = Math.max(this.dirtyTo, index);
|
|
962
|
+
}
|
|
963
|
+
return { from: start, to: index, pending: this.stale };
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
// src/ui/code-editor/core/keymap.ts
|
|
968
|
+
var defaultKeymap = {
|
|
969
|
+
Enter: "insertNewlineAndIndent",
|
|
970
|
+
Tab: "indentMore",
|
|
971
|
+
"Shift-Tab": "indentLess",
|
|
972
|
+
Backspace: "deleteBackward",
|
|
973
|
+
"Mod-/": "toggleLineComment",
|
|
974
|
+
"Mod-z": "undo",
|
|
975
|
+
"Mod-Shift-z": "redo",
|
|
976
|
+
"Mod-y": "redo",
|
|
977
|
+
"Mod-Enter": "run",
|
|
978
|
+
"Mod-Shift-Enter": "runSelection",
|
|
979
|
+
"Ctrl-Space": "triggerCompletion"
|
|
980
|
+
};
|
|
981
|
+
var MODIFIER_ORDER = ["Ctrl", "Alt", "Shift", "Meta"];
|
|
982
|
+
function isApplePlatform(platform) {
|
|
983
|
+
const source = platform ?? (typeof navigator === "undefined" ? "" : navigator.platform || navigator.userAgent || "");
|
|
984
|
+
return /Mac|iP(?:hone|ad|od)/i.test(source);
|
|
985
|
+
}
|
|
986
|
+
function normalizeKey(key) {
|
|
987
|
+
if (key === " " || key === "Spacebar")
|
|
988
|
+
return "Space";
|
|
989
|
+
return key.length === 1 ? key.toLowerCase() : key;
|
|
990
|
+
}
|
|
991
|
+
function normalizeBinding(binding, apple) {
|
|
992
|
+
const parts = binding.split("-");
|
|
993
|
+
const key = parts[parts.length - 1] === "" ? "-" : parts[parts.length - 1];
|
|
994
|
+
const modifiers = new Set;
|
|
995
|
+
for (const part of parts.slice(0, parts[parts.length - 1] === "" ? -2 : -1)) {
|
|
996
|
+
const name = part.toLowerCase();
|
|
997
|
+
if (name === "mod")
|
|
998
|
+
modifiers.add(apple ? "Meta" : "Ctrl");
|
|
999
|
+
else if (name === "ctrl" || name === "control")
|
|
1000
|
+
modifiers.add("Ctrl");
|
|
1001
|
+
else if (name === "alt" || name === "option")
|
|
1002
|
+
modifiers.add("Alt");
|
|
1003
|
+
else if (name === "shift")
|
|
1004
|
+
modifiers.add("Shift");
|
|
1005
|
+
else if (name === "meta" || name === "cmd" || name === "command")
|
|
1006
|
+
modifiers.add("Meta");
|
|
1007
|
+
}
|
|
1008
|
+
return [...MODIFIER_ORDER.filter((name) => modifiers.has(name)), normalizeKey(key)].join("-");
|
|
1009
|
+
}
|
|
1010
|
+
function keyName(event) {
|
|
1011
|
+
const modifiers = [];
|
|
1012
|
+
if (event.ctrlKey)
|
|
1013
|
+
modifiers.push("Ctrl");
|
|
1014
|
+
if (event.altKey)
|
|
1015
|
+
modifiers.push("Alt");
|
|
1016
|
+
if (event.shiftKey)
|
|
1017
|
+
modifiers.push("Shift");
|
|
1018
|
+
if (event.metaKey)
|
|
1019
|
+
modifiers.push("Meta");
|
|
1020
|
+
return [...modifiers, normalizeKey(event.key)].join("-");
|
|
1021
|
+
}
|
|
1022
|
+
function createKeymap(maps = [defaultKeymap], apple = isApplePlatform()) {
|
|
1023
|
+
const bindings = new Map;
|
|
1024
|
+
for (const map of maps) {
|
|
1025
|
+
for (const [binding, command] of Object.entries(map)) {
|
|
1026
|
+
const name = normalizeBinding(binding, apple);
|
|
1027
|
+
if (command === "")
|
|
1028
|
+
bindings.delete(name);
|
|
1029
|
+
else
|
|
1030
|
+
bindings.set(name, command);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
return {
|
|
1034
|
+
bindings,
|
|
1035
|
+
lookup: (event) => bindings.get(keyName(event))
|
|
1036
|
+
};
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
// src/ui/code-editor/tokenizer.ts
|
|
1040
|
+
import { resolveGrammar, tokenClass, Tokenizer } from "ts-syntax-highlighter/browser";
|
|
1041
|
+
var MAX_TOKENIZED_LINE_LENGTH = 1e4;
|
|
1042
|
+
function tokenizerStateKey(state) {
|
|
1043
|
+
const { frames } = state;
|
|
1044
|
+
if (frames.length === 1 && frames[0].pattern === null && !frames[0].raw)
|
|
1045
|
+
return "";
|
|
1046
|
+
let key = "";
|
|
1047
|
+
for (const frame of frames)
|
|
1048
|
+
key += `${frame.pattern ?? ""}\x01${frame.raw ? frame.end ?? "" : ""}\x01${frame.scopes.join(" ")}\x02`;
|
|
1049
|
+
return key;
|
|
1050
|
+
}
|
|
1051
|
+
async function createLanguageTokenizer(language) {
|
|
1052
|
+
const grammar = await resolveGrammar(language);
|
|
1053
|
+
if (!grammar)
|
|
1054
|
+
return null;
|
|
1055
|
+
const tokenizer = new Tokenizer(grammar);
|
|
1056
|
+
const lines = [""];
|
|
1057
|
+
return {
|
|
1058
|
+
initialState: () => tokenizer.initialState(),
|
|
1059
|
+
stateKey: tokenizerStateKey,
|
|
1060
|
+
tokenizeLine(text, state) {
|
|
1061
|
+
if (text.length > MAX_TOKENIZED_LINE_LENGTH)
|
|
1062
|
+
return { tokens: [{ type: "", content: text }], endState: state };
|
|
1063
|
+
lines[0] = text;
|
|
1064
|
+
const result = tokenizer.tokenizeLinesFrom(lines, state, 1);
|
|
1065
|
+
const tokens = [];
|
|
1066
|
+
let last;
|
|
1067
|
+
for (const token of result.lines[0].tokens) {
|
|
1068
|
+
const type = tokenClass(token.type);
|
|
1069
|
+
if (last && last.type === type)
|
|
1070
|
+
last.content += token.content;
|
|
1071
|
+
else
|
|
1072
|
+
tokens.push(last = { type, content: token.content });
|
|
1073
|
+
}
|
|
1074
|
+
return { tokens, endState: result.endState };
|
|
1075
|
+
}
|
|
1076
|
+
};
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
// src/ui/code-editor/view.ts
|
|
1080
|
+
var OVERSCAN = 12;
|
|
1081
|
+
var SYNC_BUDGET = 200;
|
|
1082
|
+
var IDLE_BUDGET = 600;
|
|
1083
|
+
var SEVERITY_RANK = { error: 3, warning: 2, info: 1, hint: 0 };
|
|
1084
|
+
var PRINTABLE_ASCII = /^[\x20-\x7E]*$/;
|
|
1085
|
+
function escapeHtml(text) {
|
|
1086
|
+
if (!/[&<>]/.test(text))
|
|
1087
|
+
return text;
|
|
1088
|
+
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
1089
|
+
}
|
|
1090
|
+
function renderLineHtml(line, text) {
|
|
1091
|
+
if (!line || line.text !== text)
|
|
1092
|
+
return escapeHtml(text);
|
|
1093
|
+
let html = "";
|
|
1094
|
+
for (const token of line.tokens)
|
|
1095
|
+
html += token.type === "" ? escapeHtml(token.content) : `<span class="tok-${token.type}">${escapeHtml(token.content)}</span>`;
|
|
1096
|
+
return html;
|
|
1097
|
+
}
|
|
1098
|
+
function part(root, name) {
|
|
1099
|
+
const element = root.querySelector(`[data-ce="${name}"]`);
|
|
1100
|
+
if (!element)
|
|
1101
|
+
throw new Error(`CodeEditor: the template has no [data-ce="${name}"] element`);
|
|
1102
|
+
return element;
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
class EditorView {
|
|
1106
|
+
core;
|
|
1107
|
+
options;
|
|
1108
|
+
root;
|
|
1109
|
+
scroller;
|
|
1110
|
+
content;
|
|
1111
|
+
gutter;
|
|
1112
|
+
gutterRows;
|
|
1113
|
+
region;
|
|
1114
|
+
rows;
|
|
1115
|
+
textarea;
|
|
1116
|
+
activeLine;
|
|
1117
|
+
decorations;
|
|
1118
|
+
widgetLayer;
|
|
1119
|
+
measure;
|
|
1120
|
+
status;
|
|
1121
|
+
keymap;
|
|
1122
|
+
highlighter = null;
|
|
1123
|
+
metrics = { lineHeight: 20, charWidth: 8, padTop: 0, padBottom: 0, padLeft: 0, padRight: 0 };
|
|
1124
|
+
widthCache = new Map;
|
|
1125
|
+
rendered = [];
|
|
1126
|
+
renderedNumbers = [];
|
|
1127
|
+
firstRow = 0;
|
|
1128
|
+
gutterWidth = 0;
|
|
1129
|
+
showLineNumbers = true;
|
|
1130
|
+
contentWidth = -1;
|
|
1131
|
+
contentHeight = -1;
|
|
1132
|
+
activeRow = -1;
|
|
1133
|
+
widgets = [];
|
|
1134
|
+
diagnostics = [];
|
|
1135
|
+
ghost = null;
|
|
1136
|
+
announced = "";
|
|
1137
|
+
composing = false;
|
|
1138
|
+
focused = false;
|
|
1139
|
+
tabLeavesNext = false;
|
|
1140
|
+
frame = 0;
|
|
1141
|
+
idle;
|
|
1142
|
+
resizeObserver;
|
|
1143
|
+
disposers = [];
|
|
1144
|
+
destroyed = false;
|
|
1145
|
+
constructor(root, options) {
|
|
1146
|
+
this.root = root;
|
|
1147
|
+
this.options = options;
|
|
1148
|
+
this.core = options.core;
|
|
1149
|
+
this.keymap = options.keymap;
|
|
1150
|
+
this.scroller = part(root, "scroller");
|
|
1151
|
+
this.content = part(root, "content");
|
|
1152
|
+
this.gutter = part(root, "gutter");
|
|
1153
|
+
this.gutterRows = part(root, "gutter-rows");
|
|
1154
|
+
this.region = part(root, "region");
|
|
1155
|
+
this.rows = part(root, "rows");
|
|
1156
|
+
this.textarea = part(root, "input");
|
|
1157
|
+
this.activeLine = part(root, "active-line");
|
|
1158
|
+
this.decorations = part(root, "decorations");
|
|
1159
|
+
this.widgetLayer = part(root, "widgets");
|
|
1160
|
+
this.measure = part(root, "measure");
|
|
1161
|
+
this.status = root.querySelector('[data-ce="status"]');
|
|
1162
|
+
if (this.textarea.value !== this.core.value)
|
|
1163
|
+
this.textarea.value = this.core.value;
|
|
1164
|
+
this.listen(this.textarea, "beforeinput", (event) => this.onBeforeInput(event));
|
|
1165
|
+
this.listen(this.textarea, "input", () => this.onInput());
|
|
1166
|
+
this.listen(this.textarea, "keydown", (event) => this.onKeydown(event));
|
|
1167
|
+
this.listen(this.textarea, "compositionstart", () => {
|
|
1168
|
+
this.composing = true;
|
|
1169
|
+
});
|
|
1170
|
+
this.listen(this.textarea, "compositionend", () => {
|
|
1171
|
+
this.composing = false;
|
|
1172
|
+
this.onInput();
|
|
1173
|
+
});
|
|
1174
|
+
this.listen(this.textarea, "focus", () => {
|
|
1175
|
+
this.focused = true;
|
|
1176
|
+
this.root.setAttribute("data-focused", "");
|
|
1177
|
+
this.syncSelection();
|
|
1178
|
+
this.render();
|
|
1179
|
+
this.options.onFocus?.();
|
|
1180
|
+
});
|
|
1181
|
+
this.listen(this.textarea, "blur", () => {
|
|
1182
|
+
this.focused = false;
|
|
1183
|
+
this.tabLeavesNext = false;
|
|
1184
|
+
this.root.removeAttribute("data-focused");
|
|
1185
|
+
this.render();
|
|
1186
|
+
this.options.onBlur?.();
|
|
1187
|
+
});
|
|
1188
|
+
this.listen(this.textarea, "scroll", () => {
|
|
1189
|
+
if (this.textarea.scrollTop !== 0)
|
|
1190
|
+
this.textarea.scrollTop = 0;
|
|
1191
|
+
if (this.textarea.scrollLeft !== 0)
|
|
1192
|
+
this.textarea.scrollLeft = 0;
|
|
1193
|
+
});
|
|
1194
|
+
this.listen(this.textarea, "select", () => this.syncSelection());
|
|
1195
|
+
this.listen(this.textarea, "mouseup", () => this.syncSelection());
|
|
1196
|
+
this.listen(this.textarea, "keyup", () => this.syncSelection());
|
|
1197
|
+
this.listen(root.ownerDocument, "selectionchange", () => {
|
|
1198
|
+
if (root.ownerDocument.activeElement === this.textarea)
|
|
1199
|
+
this.syncSelection();
|
|
1200
|
+
});
|
|
1201
|
+
this.listen(this.scroller, "scroll", () => this.scheduleRender(), { passive: true });
|
|
1202
|
+
this.listen(this.gutter, "mousedown", (event) => this.onGutterMouseDown(event));
|
|
1203
|
+
if (typeof ResizeObserver !== "undefined") {
|
|
1204
|
+
this.resizeObserver = new ResizeObserver(() => this.layout());
|
|
1205
|
+
this.resizeObserver.observe(this.scroller);
|
|
1206
|
+
}
|
|
1207
|
+
const fonts = root.ownerDocument.fonts;
|
|
1208
|
+
if (fonts?.ready)
|
|
1209
|
+
fonts.ready.then(() => !this.destroyed && this.layout());
|
|
1210
|
+
this.disposers.push(this.core.subscribe((transaction) => this.onTransaction(transaction)));
|
|
1211
|
+
this.root.setAttribute("data-ce-ready", "");
|
|
1212
|
+
this.layout();
|
|
1213
|
+
}
|
|
1214
|
+
listen(target, type, handler, options) {
|
|
1215
|
+
target.addEventListener(type, handler, options);
|
|
1216
|
+
this.disposers.push(() => target.removeEventListener(type, handler, options));
|
|
1217
|
+
}
|
|
1218
|
+
setKeymap(keymap) {
|
|
1219
|
+
this.keymap = keymap;
|
|
1220
|
+
}
|
|
1221
|
+
setLineNumbers(show) {
|
|
1222
|
+
this.showLineNumbers = show;
|
|
1223
|
+
this.layout();
|
|
1224
|
+
}
|
|
1225
|
+
setReadOnly(readOnly) {
|
|
1226
|
+
this.core.readOnly = readOnly;
|
|
1227
|
+
this.textarea.readOnly = readOnly;
|
|
1228
|
+
}
|
|
1229
|
+
setTokenizer(tokenizer) {
|
|
1230
|
+
this.highlighter = tokenizer ? new LineHighlighter(tokenizer, this.core.doc) : null;
|
|
1231
|
+
this.rendered = [];
|
|
1232
|
+
this.render();
|
|
1233
|
+
}
|
|
1234
|
+
layout() {
|
|
1235
|
+
if (this.destroyed)
|
|
1236
|
+
return;
|
|
1237
|
+
const view = this.root.ownerDocument.defaultView;
|
|
1238
|
+
const style = view ? view.getComputedStyle(this.textarea) : null;
|
|
1239
|
+
const number = (value, fallback) => {
|
|
1240
|
+
const parsed = Number.parseFloat(value ?? "");
|
|
1241
|
+
return Number.isFinite(parsed) ? parsed : fallback;
|
|
1242
|
+
};
|
|
1243
|
+
this.measure.textContent = "x".repeat(64);
|
|
1244
|
+
const sample = this.measure.getBoundingClientRect().width / 64;
|
|
1245
|
+
const fontSize = number(style?.fontSize, 13);
|
|
1246
|
+
this.metrics = {
|
|
1247
|
+
lineHeight: number(style?.lineHeight, Math.round(fontSize * 1.5)) || Math.round(fontSize * 1.5),
|
|
1248
|
+
charWidth: sample > 0 ? sample : fontSize * 0.6,
|
|
1249
|
+
padTop: number(style?.paddingTop, 0),
|
|
1250
|
+
padBottom: number(style?.paddingBottom, 0),
|
|
1251
|
+
padLeft: number(style?.paddingLeft, 0),
|
|
1252
|
+
padRight: number(style?.paddingRight, 0)
|
|
1253
|
+
};
|
|
1254
|
+
this.widthCache.clear();
|
|
1255
|
+
this.activeRow = -1;
|
|
1256
|
+
this.contentWidth = -1;
|
|
1257
|
+
this.contentHeight = -1;
|
|
1258
|
+
this.render();
|
|
1259
|
+
}
|
|
1260
|
+
widthOf(text, column) {
|
|
1261
|
+
if (column <= 0)
|
|
1262
|
+
return 0;
|
|
1263
|
+
const slice = column >= text.length ? text : text.slice(0, column);
|
|
1264
|
+
if (PRINTABLE_ASCII.test(slice))
|
|
1265
|
+
return slice.length * this.metrics.charWidth;
|
|
1266
|
+
const cached = this.widthCache.get(slice);
|
|
1267
|
+
if (cached !== undefined)
|
|
1268
|
+
return cached;
|
|
1269
|
+
this.measure.textContent = slice;
|
|
1270
|
+
const width = this.measure.getBoundingClientRect().width;
|
|
1271
|
+
if (this.widthCache.size > 500)
|
|
1272
|
+
this.widthCache.clear();
|
|
1273
|
+
this.widthCache.set(slice, width);
|
|
1274
|
+
return width;
|
|
1275
|
+
}
|
|
1276
|
+
syncSelection() {
|
|
1277
|
+
if (this.destroyed)
|
|
1278
|
+
return;
|
|
1279
|
+
const start = this.textarea.selectionStart ?? 0;
|
|
1280
|
+
const end = this.textarea.selectionEnd ?? start;
|
|
1281
|
+
const backward = this.textarea.selectionDirection === "backward";
|
|
1282
|
+
const anchor = backward ? end : start;
|
|
1283
|
+
const head = backward ? start : end;
|
|
1284
|
+
const main = this.core.main;
|
|
1285
|
+
if (main.anchor === anchor && main.head === head)
|
|
1286
|
+
return;
|
|
1287
|
+
if (start === end && main.anchor === main.head && main.head === start)
|
|
1288
|
+
return;
|
|
1289
|
+
this.core.dispatch({ selection: { anchor, head }, origin: "input", kind: "select" });
|
|
1290
|
+
}
|
|
1291
|
+
onBeforeInput(event) {
|
|
1292
|
+
if (event.inputType === "historyUndo" || event.inputType === "historyRedo") {
|
|
1293
|
+
event.preventDefault();
|
|
1294
|
+
this.core.exec(event.inputType === "historyUndo" ? "undo" : "redo");
|
|
1295
|
+
return;
|
|
1296
|
+
}
|
|
1297
|
+
if (this.composing || event.isComposing)
|
|
1298
|
+
return;
|
|
1299
|
+
if (event.inputType === "insertLineBreak" || event.inputType === "insertParagraph") {
|
|
1300
|
+
this.syncSelection();
|
|
1301
|
+
if (this.core.exec("insertNewlineAndIndent"))
|
|
1302
|
+
event.preventDefault();
|
|
1303
|
+
return;
|
|
1304
|
+
}
|
|
1305
|
+
if (event.inputType === "deleteContentBackward") {
|
|
1306
|
+
this.syncSelection();
|
|
1307
|
+
if (this.core.exec("deleteBackward"))
|
|
1308
|
+
event.preventDefault();
|
|
1309
|
+
return;
|
|
1310
|
+
}
|
|
1311
|
+
if (event.inputType !== "insertText" || !event.data)
|
|
1312
|
+
return;
|
|
1313
|
+
this.syncSelection();
|
|
1314
|
+
if (handleTextInput(this.core, event.data))
|
|
1315
|
+
event.preventDefault();
|
|
1316
|
+
}
|
|
1317
|
+
onInput() {
|
|
1318
|
+
if (this.destroyed)
|
|
1319
|
+
return;
|
|
1320
|
+
const field = this.textarea;
|
|
1321
|
+
this.core.syncInput(field.value, field.selectionStart ?? 0, field.selectionEnd ?? 0, field.selectionDirection === "backward");
|
|
1322
|
+
}
|
|
1323
|
+
onKeydown(event) {
|
|
1324
|
+
if (event.isComposing || event.keyCode === 229)
|
|
1325
|
+
return;
|
|
1326
|
+
if (this.options.onKeydown?.(event)) {
|
|
1327
|
+
event.preventDefault();
|
|
1328
|
+
return;
|
|
1329
|
+
}
|
|
1330
|
+
const plain = !event.ctrlKey && !event.metaKey && !event.altKey;
|
|
1331
|
+
if (this.ghost && plain && !event.shiftKey) {
|
|
1332
|
+
if (event.key === "Tab") {
|
|
1333
|
+
event.preventDefault();
|
|
1334
|
+
this.acceptGhost();
|
|
1335
|
+
return;
|
|
1336
|
+
}
|
|
1337
|
+
if (event.key === "Escape") {
|
|
1338
|
+
event.preventDefault();
|
|
1339
|
+
event.stopPropagation();
|
|
1340
|
+
this.setGhostText(null, "dismiss");
|
|
1341
|
+
return;
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
if (event.key === "Escape" && plain) {
|
|
1345
|
+
this.tabLeavesNext = true;
|
|
1346
|
+
this.announce("Tab now moves focus out of the editor.");
|
|
1347
|
+
return;
|
|
1348
|
+
}
|
|
1349
|
+
if (event.key === "Tab" && this.tabLeavesNext) {
|
|
1350
|
+
this.tabLeavesNext = false;
|
|
1351
|
+
return;
|
|
1352
|
+
}
|
|
1353
|
+
if (event.key !== "Shift")
|
|
1354
|
+
this.tabLeavesNext = false;
|
|
1355
|
+
const name = this.keymap.lookup(event);
|
|
1356
|
+
if (!name)
|
|
1357
|
+
return;
|
|
1358
|
+
this.syncSelection();
|
|
1359
|
+
const handled = this.core.commands.has(name) ? this.core.exec(name) : this.options.onCommand?.(name) === true;
|
|
1360
|
+
if (handled || name === "undo" || name === "redo")
|
|
1361
|
+
event.preventDefault();
|
|
1362
|
+
}
|
|
1363
|
+
onGutterMouseDown(event) {
|
|
1364
|
+
event.preventDefault();
|
|
1365
|
+
const bounds = this.rows.getBoundingClientRect();
|
|
1366
|
+
const row = Math.floor((event.clientY - bounds.top) / this.metrics.lineHeight);
|
|
1367
|
+
const line = Math.max(0, Math.min(this.core.doc.lineCount - 1, this.firstRow + row));
|
|
1368
|
+
const info = this.core.doc.lineInfo(line);
|
|
1369
|
+
const end = line + 1 < this.core.doc.lineCount ? info.to + 1 : info.to;
|
|
1370
|
+
this.textarea.focus();
|
|
1371
|
+
this.core.setSelection(info.from, end);
|
|
1372
|
+
}
|
|
1373
|
+
onTransaction(transaction) {
|
|
1374
|
+
const { changes } = transaction;
|
|
1375
|
+
if (transaction.docChanged) {
|
|
1376
|
+
for (let index = changes.length - 1;index >= 0; index--)
|
|
1377
|
+
this.highlighter?.applyChange(changes[index]);
|
|
1378
|
+
for (const diagnostic of this.diagnostics) {
|
|
1379
|
+
diagnostic.from = mapOffset(diagnostic.from, changes, 1);
|
|
1380
|
+
diagnostic.to = Math.max(diagnostic.from, mapOffset(diagnostic.to, changes, -1));
|
|
1381
|
+
}
|
|
1382
|
+
for (const widget of this.widgets)
|
|
1383
|
+
widget.offset = mapOffset(widget.offset, changes, widget.atEnd ? 1 : -1);
|
|
1384
|
+
}
|
|
1385
|
+
if (transaction.origin !== "input") {
|
|
1386
|
+
if (transaction.docChanged) {
|
|
1387
|
+
if (transaction.kind === "set" || changes.length > 64) {
|
|
1388
|
+
this.textarea.value = this.core.value;
|
|
1389
|
+
} else {
|
|
1390
|
+
for (let index = changes.length - 1;index >= 0; index--) {
|
|
1391
|
+
const change = changes[index];
|
|
1392
|
+
this.textarea.setRangeText(change.insert, change.from, change.to, "preserve");
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
const main = this.core.main;
|
|
1397
|
+
const backward = main.head < main.anchor;
|
|
1398
|
+
this.textarea.setSelectionRange(rangeFrom(main), rangeTo(main), backward ? "backward" : "forward");
|
|
1399
|
+
}
|
|
1400
|
+
this.updateGhost(transaction);
|
|
1401
|
+
this.render();
|
|
1402
|
+
if (transaction.scrollIntoView && (this.focused || transaction.origin !== "api"))
|
|
1403
|
+
this.revealCaret();
|
|
1404
|
+
this.announceDiagnostic();
|
|
1405
|
+
this.options.onTransaction?.(transaction);
|
|
1406
|
+
}
|
|
1407
|
+
scheduleRender() {
|
|
1408
|
+
if (this.frame !== 0 || this.destroyed)
|
|
1409
|
+
return;
|
|
1410
|
+
const view = this.root.ownerDocument.defaultView;
|
|
1411
|
+
if (!view?.requestAnimationFrame) {
|
|
1412
|
+
this.render();
|
|
1413
|
+
return;
|
|
1414
|
+
}
|
|
1415
|
+
this.frame = view.requestAnimationFrame(() => {
|
|
1416
|
+
this.frame = 0;
|
|
1417
|
+
this.render();
|
|
1418
|
+
});
|
|
1419
|
+
}
|
|
1420
|
+
visibleRange() {
|
|
1421
|
+
const { lineHeight, padTop } = this.metrics;
|
|
1422
|
+
const count = this.core.doc.lineCount;
|
|
1423
|
+
const top = this.scroller.scrollTop - padTop;
|
|
1424
|
+
const height = this.scroller.clientHeight || 600;
|
|
1425
|
+
const first = Math.max(0, Math.floor(top / lineHeight) - OVERSCAN);
|
|
1426
|
+
const last = Math.min(count, Math.ceil((top + height) / lineHeight) + OVERSCAN);
|
|
1427
|
+
return { first: Math.min(first, Math.max(0, count - 1)), last: Math.max(last, Math.min(count, first + 1)) };
|
|
1428
|
+
}
|
|
1429
|
+
render() {
|
|
1430
|
+
if (this.destroyed)
|
|
1431
|
+
return;
|
|
1432
|
+
const doc = this.core.doc;
|
|
1433
|
+
const { lineHeight, padTop, padBottom, padLeft, padRight, charWidth } = this.metrics;
|
|
1434
|
+
const count = doc.lineCount;
|
|
1435
|
+
const digits = Math.max(3, String(count).length);
|
|
1436
|
+
const gutterWidth = this.showLineNumbers ? Math.ceil(digits * charWidth + 2 * charWidth) : 0;
|
|
1437
|
+
if (gutterWidth !== this.gutterWidth) {
|
|
1438
|
+
this.gutterWidth = gutterWidth;
|
|
1439
|
+
this.gutter.style.width = `${gutterWidth}px`;
|
|
1440
|
+
this.gutter.style.display = gutterWidth === 0 ? "none" : "";
|
|
1441
|
+
}
|
|
1442
|
+
let longest = "";
|
|
1443
|
+
for (let index = 0;index < count; index++) {
|
|
1444
|
+
const text = doc.line(index);
|
|
1445
|
+
if (text.length > longest.length)
|
|
1446
|
+
longest = text;
|
|
1447
|
+
}
|
|
1448
|
+
const width = Math.ceil(padLeft + this.widthOf(longest, longest.length) + padRight + 2 * charWidth);
|
|
1449
|
+
const height = Math.ceil(padTop + count * lineHeight + padBottom);
|
|
1450
|
+
if (width !== this.contentWidth) {
|
|
1451
|
+
this.contentWidth = width;
|
|
1452
|
+
this.region.style.minWidth = `${width}px`;
|
|
1453
|
+
}
|
|
1454
|
+
if (height !== this.contentHeight) {
|
|
1455
|
+
this.contentHeight = height;
|
|
1456
|
+
this.content.style.height = `${height}px`;
|
|
1457
|
+
}
|
|
1458
|
+
const { first, last } = this.visibleRange();
|
|
1459
|
+
if (this.highlighter?.pending) {
|
|
1460
|
+
this.highlighter.update({ through: last - 1, maxLines: SYNC_BUDGET });
|
|
1461
|
+
if (this.highlighter.pending)
|
|
1462
|
+
this.scheduleIdle();
|
|
1463
|
+
}
|
|
1464
|
+
this.renderRows(first, last);
|
|
1465
|
+
this.renderGutter(first, last);
|
|
1466
|
+
this.renderActiveLine();
|
|
1467
|
+
this.renderDecorations(first, last);
|
|
1468
|
+
this.renderWidgets(first, last);
|
|
1469
|
+
}
|
|
1470
|
+
renderRows(first, last) {
|
|
1471
|
+
const doc = this.core.doc;
|
|
1472
|
+
const wanted = last - first;
|
|
1473
|
+
while (this.rows.childElementCount < wanted) {
|
|
1474
|
+
const row = this.root.ownerDocument.createElement("div");
|
|
1475
|
+
row.className = "stx-ce-line";
|
|
1476
|
+
this.rows.appendChild(row);
|
|
1477
|
+
}
|
|
1478
|
+
while (this.rows.childElementCount > wanted)
|
|
1479
|
+
this.rows.lastElementChild.remove();
|
|
1480
|
+
if (first !== this.firstRow) {
|
|
1481
|
+
this.rendered = [];
|
|
1482
|
+
this.firstRow = first;
|
|
1483
|
+
}
|
|
1484
|
+
this.rows.style.transform = `translateY(${first * this.metrics.lineHeight}px)`;
|
|
1485
|
+
this.rendered.length = wanted;
|
|
1486
|
+
const children = this.rows.children;
|
|
1487
|
+
for (let index = 0;index < wanted; index++) {
|
|
1488
|
+
const text = doc.line(first + index);
|
|
1489
|
+
const line = this.highlighter?.get(first + index) ?? null;
|
|
1490
|
+
const source = line && line.text === text ? line : text;
|
|
1491
|
+
if (this.rendered[index] === source)
|
|
1492
|
+
continue;
|
|
1493
|
+
this.rendered[index] = source;
|
|
1494
|
+
children[index].innerHTML = typeof source === "string" ? escapeHtml(source) : renderLineHtml(source, text);
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
renderGutter(first, last) {
|
|
1498
|
+
if (this.gutterWidth === 0)
|
|
1499
|
+
return;
|
|
1500
|
+
const wanted = last - first;
|
|
1501
|
+
while (this.gutterRows.childElementCount < wanted) {
|
|
1502
|
+
const row = this.root.ownerDocument.createElement("div");
|
|
1503
|
+
row.className = "stx-ce-number";
|
|
1504
|
+
this.gutterRows.appendChild(row);
|
|
1505
|
+
}
|
|
1506
|
+
while (this.gutterRows.childElementCount > wanted)
|
|
1507
|
+
this.gutterRows.lastElementChild.remove();
|
|
1508
|
+
this.gutterRows.style.transform = `translateY(${first * this.metrics.lineHeight}px)`;
|
|
1509
|
+
this.renderedNumbers.length = wanted;
|
|
1510
|
+
const active = this.focused && rangeEmpty(this.core.main) ? this.core.cursor.line : -1;
|
|
1511
|
+
const marked = this.markedLines();
|
|
1512
|
+
const children = this.gutterRows.children;
|
|
1513
|
+
for (let index = 0;index < wanted; index++) {
|
|
1514
|
+
const line = first + index;
|
|
1515
|
+
const row = children[index];
|
|
1516
|
+
if (this.renderedNumbers[index] !== line) {
|
|
1517
|
+
this.renderedNumbers[index] = line;
|
|
1518
|
+
row.textContent = String(line + 1);
|
|
1519
|
+
}
|
|
1520
|
+
const severity = marked.get(line) ?? "";
|
|
1521
|
+
if ((row.dataset.severity ?? "") !== severity) {
|
|
1522
|
+
if (severity)
|
|
1523
|
+
row.dataset.severity = severity;
|
|
1524
|
+
else
|
|
1525
|
+
delete row.dataset.severity;
|
|
1526
|
+
}
|
|
1527
|
+
const isActive = line === active;
|
|
1528
|
+
if (row.hasAttribute("data-active") !== isActive)
|
|
1529
|
+
row.toggleAttribute("data-active", isActive);
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
renderActiveLine() {
|
|
1533
|
+
const show = this.focused && rangeEmpty(this.core.main);
|
|
1534
|
+
const line = show ? this.core.cursor.line : -1;
|
|
1535
|
+
if (line === this.activeRow)
|
|
1536
|
+
return;
|
|
1537
|
+
this.activeRow = line;
|
|
1538
|
+
if (line < 0) {
|
|
1539
|
+
this.activeLine.style.display = "none";
|
|
1540
|
+
return;
|
|
1541
|
+
}
|
|
1542
|
+
this.activeLine.style.display = "";
|
|
1543
|
+
this.activeLine.style.transform = `translateY(${this.metrics.padTop + line * this.metrics.lineHeight}px)`;
|
|
1544
|
+
}
|
|
1545
|
+
markedLines() {
|
|
1546
|
+
const marked = new Map;
|
|
1547
|
+
for (const diagnostic of this.diagnostics) {
|
|
1548
|
+
const line = this.core.doc.lineIndexAt(diagnostic.from);
|
|
1549
|
+
const existing = marked.get(line);
|
|
1550
|
+
if (!existing || SEVERITY_RANK[diagnostic.severity] > SEVERITY_RANK[existing])
|
|
1551
|
+
marked.set(line, diagnostic.severity);
|
|
1552
|
+
}
|
|
1553
|
+
return marked;
|
|
1554
|
+
}
|
|
1555
|
+
renderDecorations(first, last) {
|
|
1556
|
+
if (this.diagnostics.length === 0) {
|
|
1557
|
+
if (this.decorations.childElementCount > 0)
|
|
1558
|
+
this.decorations.replaceChildren();
|
|
1559
|
+
return;
|
|
1560
|
+
}
|
|
1561
|
+
const doc = this.core.doc;
|
|
1562
|
+
const { lineHeight, padTop, padLeft } = this.metrics;
|
|
1563
|
+
const fragment = this.root.ownerDocument.createDocumentFragment();
|
|
1564
|
+
for (const diagnostic of this.diagnostics) {
|
|
1565
|
+
const start = doc.positionAt(diagnostic.from);
|
|
1566
|
+
const end = doc.positionAt(diagnostic.to);
|
|
1567
|
+
for (let line = Math.max(first, start.line);line <= Math.min(last - 1, end.line); line++) {
|
|
1568
|
+
const text = doc.line(line);
|
|
1569
|
+
const fromColumn = line === start.line ? start.column : 0;
|
|
1570
|
+
let toColumn = line === end.line ? end.column : text.length;
|
|
1571
|
+
if (toColumn <= fromColumn)
|
|
1572
|
+
toColumn = Math.min(text.length, fromColumn + 1);
|
|
1573
|
+
const mark = this.root.ownerDocument.createElement("div");
|
|
1574
|
+
mark.className = "stx-ce-squiggle";
|
|
1575
|
+
mark.dataset.severity = diagnostic.severity;
|
|
1576
|
+
mark.textContent = text.slice(fromColumn, toColumn) || " ";
|
|
1577
|
+
mark.style.transform = `translate(${padLeft + this.widthOf(text, fromColumn)}px, ${padTop + line * lineHeight}px)`;
|
|
1578
|
+
fragment.appendChild(mark);
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
this.decorations.replaceChildren(fragment);
|
|
1582
|
+
}
|
|
1583
|
+
renderWidgets(first, last) {
|
|
1584
|
+
const doc = this.core.doc;
|
|
1585
|
+
const { lineHeight, padTop, padLeft, charWidth } = this.metrics;
|
|
1586
|
+
const fragment = this.root.ownerDocument.createDocumentFragment();
|
|
1587
|
+
const trailing = new Map;
|
|
1588
|
+
const trail = (line) => {
|
|
1589
|
+
let holder = trailing.get(line);
|
|
1590
|
+
if (!holder) {
|
|
1591
|
+
const text = doc.line(line);
|
|
1592
|
+
holder = this.root.ownerDocument.createElement("div");
|
|
1593
|
+
holder.className = "stx-ce-trailing";
|
|
1594
|
+
holder.style.transform = `translate(${padLeft + this.widthOf(text, text.length) + 2 * charWidth}px, ${padTop + line * lineHeight}px)`;
|
|
1595
|
+
trailing.set(line, holder);
|
|
1596
|
+
fragment.appendChild(holder);
|
|
1597
|
+
}
|
|
1598
|
+
return holder;
|
|
1599
|
+
};
|
|
1600
|
+
const chip = (text, kind, className) => {
|
|
1601
|
+
const element = this.root.ownerDocument.createElement("span");
|
|
1602
|
+
element.className = className ? `stx-ce-widget ${className}` : "stx-ce-widget";
|
|
1603
|
+
element.dataset.kind = kind;
|
|
1604
|
+
element.textContent = text;
|
|
1605
|
+
return element;
|
|
1606
|
+
};
|
|
1607
|
+
for (const widget of this.widgets) {
|
|
1608
|
+
const position = doc.positionAt(widget.offset);
|
|
1609
|
+
if (position.line < first || position.line >= last)
|
|
1610
|
+
continue;
|
|
1611
|
+
if (widget.atEnd) {
|
|
1612
|
+
trail(position.line).appendChild(chip(widget.text, widget.kind, widget.className));
|
|
1613
|
+
continue;
|
|
1614
|
+
}
|
|
1615
|
+
const element = chip(widget.text, widget.kind, widget.className);
|
|
1616
|
+
element.classList.add("stx-ce-widget--placed");
|
|
1617
|
+
element.style.transform = `translate(${padLeft + this.widthOf(doc.line(position.line), position.column)}px, ${padTop + position.line * lineHeight}px)`;
|
|
1618
|
+
fragment.appendChild(element);
|
|
1619
|
+
}
|
|
1620
|
+
if (this.options.inlineDiagnostics !== false) {
|
|
1621
|
+
const shown = new Set;
|
|
1622
|
+
const ordered = [...this.diagnostics].sort((a, b) => SEVERITY_RANK[b.severity] - SEVERITY_RANK[a.severity]);
|
|
1623
|
+
for (const diagnostic of ordered) {
|
|
1624
|
+
const line = doc.lineIndexAt(diagnostic.from);
|
|
1625
|
+
if (line < first || line >= last || shown.has(line))
|
|
1626
|
+
continue;
|
|
1627
|
+
shown.add(line);
|
|
1628
|
+
trail(line).appendChild(chip(diagnostic.message.split(`
|
|
1629
|
+
`)[0], diagnostic.severity, "stx-ce-widget--diagnostic"));
|
|
1630
|
+
}
|
|
1631
|
+
}
|
|
1632
|
+
if (this.ghost) {
|
|
1633
|
+
const position = doc.positionAt(this.ghost.offset);
|
|
1634
|
+
if (position.line >= first && position.line < last) {
|
|
1635
|
+
const [head, ...rest] = this.ghost.text.split(`
|
|
1636
|
+
`);
|
|
1637
|
+
const left = padLeft + this.widthOf(doc.line(position.line), position.column);
|
|
1638
|
+
const top = padTop + position.line * lineHeight;
|
|
1639
|
+
const inline = this.root.ownerDocument.createElement("span");
|
|
1640
|
+
inline.className = "stx-ce-ghost";
|
|
1641
|
+
inline.textContent = head ?? "";
|
|
1642
|
+
inline.style.transform = `translate(${left}px, ${top}px)`;
|
|
1643
|
+
fragment.appendChild(inline);
|
|
1644
|
+
if (rest.length > 0) {
|
|
1645
|
+
const block = this.root.ownerDocument.createElement("div");
|
|
1646
|
+
block.className = "stx-ce-ghost stx-ce-ghost--block";
|
|
1647
|
+
block.textContent = rest.join(`
|
|
1648
|
+
`);
|
|
1649
|
+
block.style.transform = `translate(${padLeft}px, ${top + lineHeight}px)`;
|
|
1650
|
+
fragment.appendChild(block);
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
if (fragment.childNodes.length === 0 && this.widgetLayer.childElementCount === 0)
|
|
1655
|
+
return;
|
|
1656
|
+
this.widgetLayer.replaceChildren(fragment);
|
|
1657
|
+
}
|
|
1658
|
+
scheduleIdle() {
|
|
1659
|
+
if (this.idle !== undefined || this.destroyed)
|
|
1660
|
+
return;
|
|
1661
|
+
this.idle = setTimeout(() => {
|
|
1662
|
+
this.idle = undefined;
|
|
1663
|
+
if (this.destroyed || !this.highlighter?.pending)
|
|
1664
|
+
return;
|
|
1665
|
+
const pass = this.highlighter.update({ through: -1, maxLines: IDLE_BUDGET });
|
|
1666
|
+
const { first, last } = this.visibleRange();
|
|
1667
|
+
if (pass.from < last && pass.to > first)
|
|
1668
|
+
this.renderRows(first, last);
|
|
1669
|
+
if (this.highlighter.pending)
|
|
1670
|
+
this.scheduleIdle();
|
|
1671
|
+
}, 0);
|
|
1672
|
+
}
|
|
1673
|
+
caretAnchor(offset = this.core.main.head) {
|
|
1674
|
+
const { lineHeight, padTop, padLeft } = this.metrics;
|
|
1675
|
+
const position = this.core.doc.positionAt(offset);
|
|
1676
|
+
const top = padTop + position.line * lineHeight;
|
|
1677
|
+
const visibleTop = this.scroller.scrollTop;
|
|
1678
|
+
const visibleBottom = visibleTop + this.scroller.clientHeight;
|
|
1679
|
+
return {
|
|
1680
|
+
left: padLeft + this.widthOf(this.core.doc.line(position.line), position.column),
|
|
1681
|
+
top,
|
|
1682
|
+
bottom: top + lineHeight,
|
|
1683
|
+
spaceAbove: top - visibleTop,
|
|
1684
|
+
spaceBelow: visibleBottom - (top + lineHeight)
|
|
1685
|
+
};
|
|
1686
|
+
}
|
|
1687
|
+
revealCaret() {
|
|
1688
|
+
const { lineHeight, charWidth } = this.metrics;
|
|
1689
|
+
const anchor = this.caretAnchor();
|
|
1690
|
+
const scroller = this.scroller;
|
|
1691
|
+
const marginY = lineHeight;
|
|
1692
|
+
const marginX = 4 * charWidth;
|
|
1693
|
+
if (anchor.top - marginY < scroller.scrollTop)
|
|
1694
|
+
scroller.scrollTop = Math.max(0, anchor.top - marginY);
|
|
1695
|
+
else if (anchor.bottom + marginY > scroller.scrollTop + scroller.clientHeight)
|
|
1696
|
+
scroller.scrollTop = anchor.bottom + marginY - scroller.clientHeight;
|
|
1697
|
+
const left = anchor.left;
|
|
1698
|
+
const visibleWidth = scroller.clientWidth - this.gutterWidth;
|
|
1699
|
+
if (left - marginX < scroller.scrollLeft)
|
|
1700
|
+
scroller.scrollLeft = Math.max(0, left - marginX);
|
|
1701
|
+
else if (left + marginX > scroller.scrollLeft + visibleWidth)
|
|
1702
|
+
scroller.scrollLeft = left + marginX - visibleWidth;
|
|
1703
|
+
}
|
|
1704
|
+
focus() {
|
|
1705
|
+
this.textarea.focus();
|
|
1706
|
+
}
|
|
1707
|
+
get hasFocus() {
|
|
1708
|
+
return this.focused;
|
|
1709
|
+
}
|
|
1710
|
+
setWidgets(widgets) {
|
|
1711
|
+
const doc = this.core.doc;
|
|
1712
|
+
this.widgets = widgets.map((widget) => {
|
|
1713
|
+
const atEnd = widget.column === undefined || widget.column === "end";
|
|
1714
|
+
const line = Math.max(0, Math.min(doc.lineCount - 1, widget.line));
|
|
1715
|
+
const offset = atEnd ? doc.lineInfo(line).to : doc.offsetAt({ line, column: widget.column });
|
|
1716
|
+
return { offset, atEnd, text: widget.text, kind: widget.kind ?? "result", className: widget.className ?? "" };
|
|
1717
|
+
});
|
|
1718
|
+
this.render();
|
|
1719
|
+
}
|
|
1720
|
+
setDiagnostics(diagnostics) {
|
|
1721
|
+
const doc = this.core.doc;
|
|
1722
|
+
this.diagnostics = diagnostics.map((diagnostic) => {
|
|
1723
|
+
const from = doc.offsetAt(diagnostic.from);
|
|
1724
|
+
return {
|
|
1725
|
+
from,
|
|
1726
|
+
to: Math.max(from, doc.offsetAt(diagnostic.to)),
|
|
1727
|
+
message: diagnostic.message,
|
|
1728
|
+
severity: diagnostic.severity ?? "error"
|
|
1729
|
+
};
|
|
1730
|
+
});
|
|
1731
|
+
this.announced = "";
|
|
1732
|
+
this.render();
|
|
1733
|
+
this.announceDiagnostic();
|
|
1734
|
+
}
|
|
1735
|
+
setGhostText(text, outcome = null) {
|
|
1736
|
+
const previous = this.ghost;
|
|
1737
|
+
this.ghost = text && rangeEmpty(this.core.main) ? { offset: this.core.main.head, text } : null;
|
|
1738
|
+
if (previous && !this.ghost && outcome)
|
|
1739
|
+
this.options.onGhost?.(outcome, previous.text);
|
|
1740
|
+
this.render();
|
|
1741
|
+
}
|
|
1742
|
+
get ghostText() {
|
|
1743
|
+
return this.ghost?.text ?? "";
|
|
1744
|
+
}
|
|
1745
|
+
acceptGhost() {
|
|
1746
|
+
const ghost = this.ghost;
|
|
1747
|
+
if (!ghost || this.core.readOnly)
|
|
1748
|
+
return false;
|
|
1749
|
+
this.ghost = null;
|
|
1750
|
+
const end = ghost.offset + ghost.text.length;
|
|
1751
|
+
this.core.dispatch({
|
|
1752
|
+
changes: [{ from: ghost.offset, to: ghost.offset, insert: ghost.text }],
|
|
1753
|
+
selection: { anchor: end, head: end },
|
|
1754
|
+
kind: "ghost"
|
|
1755
|
+
});
|
|
1756
|
+
this.options.onGhost?.("accept", ghost.text);
|
|
1757
|
+
return true;
|
|
1758
|
+
}
|
|
1759
|
+
updateGhost(transaction) {
|
|
1760
|
+
const ghost = this.ghost;
|
|
1761
|
+
if (!ghost)
|
|
1762
|
+
return;
|
|
1763
|
+
const [change] = transaction.changes;
|
|
1764
|
+
const typedThrough = transaction.changes.length === 1 && change.from === ghost.offset && change.to === ghost.offset && change.insert !== "" && ghost.text.startsWith(change.insert) && this.core.main.head === ghost.offset + change.insert.length;
|
|
1765
|
+
if (typedThrough) {
|
|
1766
|
+
const rest = ghost.text.slice(change.insert.length);
|
|
1767
|
+
this.ghost = rest === "" ? null : { offset: ghost.offset + change.insert.length, text: rest };
|
|
1768
|
+
if (!this.ghost)
|
|
1769
|
+
this.options.onGhost?.("accept", ghost.text);
|
|
1770
|
+
return;
|
|
1771
|
+
}
|
|
1772
|
+
this.ghost = null;
|
|
1773
|
+
this.options.onGhost?.("dismiss", ghost.text);
|
|
1774
|
+
}
|
|
1775
|
+
announce(message) {
|
|
1776
|
+
if (this.status && this.status.textContent !== message)
|
|
1777
|
+
this.status.textContent = message;
|
|
1778
|
+
}
|
|
1779
|
+
announceDiagnostic() {
|
|
1780
|
+
if (!this.status || this.diagnostics.length === 0)
|
|
1781
|
+
return;
|
|
1782
|
+
const line = this.core.cursor.line;
|
|
1783
|
+
const here = this.diagnostics.filter((diagnostic) => this.core.doc.lineIndexAt(diagnostic.from) === line);
|
|
1784
|
+
const message = here.length === 0 ? "" : `Line ${line + 1}: ${here.map((diagnostic) => `${diagnostic.severity}: ${diagnostic.message}`).join(". ")}`;
|
|
1785
|
+
if (message !== this.announced) {
|
|
1786
|
+
this.announced = message;
|
|
1787
|
+
this.announce(message);
|
|
1788
|
+
}
|
|
1789
|
+
}
|
|
1790
|
+
destroy() {
|
|
1791
|
+
if (this.destroyed)
|
|
1792
|
+
return;
|
|
1793
|
+
this.destroyed = true;
|
|
1794
|
+
for (const dispose of this.disposers)
|
|
1795
|
+
dispose();
|
|
1796
|
+
this.disposers.length = 0;
|
|
1797
|
+
this.resizeObserver?.disconnect();
|
|
1798
|
+
if (this.idle !== undefined)
|
|
1799
|
+
clearTimeout(this.idle);
|
|
1800
|
+
const view = this.root.ownerDocument.defaultView;
|
|
1801
|
+
if (this.frame !== 0 && view?.cancelAnimationFrame)
|
|
1802
|
+
view.cancelAnimationFrame(this.frame);
|
|
1803
|
+
this.root.removeAttribute("data-ce-ready");
|
|
1804
|
+
}
|
|
1805
|
+
}
|
|
1806
|
+
|
|
1807
|
+
// src/ui/code-editor/controller.ts
|
|
1808
|
+
var CLOSED_POPUP = { open: false, items: [], activeId: "", left: 0, top: 0, placement: "below" };
|
|
1809
|
+
var WORD2 = /^[\p{L}\p{N}_$]*$/u;
|
|
1810
|
+
var TRIGGER = /[\p{L}\p{N}_$.]/u;
|
|
1811
|
+
var POPUP_LIMIT = 80;
|
|
1812
|
+
var POPUP_ROOM = 220;
|
|
1813
|
+
var instances = 0;
|
|
1814
|
+
|
|
1815
|
+
class CodeEditorController {
|
|
1816
|
+
core;
|
|
1817
|
+
view;
|
|
1818
|
+
hooks;
|
|
1819
|
+
list = new CompletionList;
|
|
1820
|
+
popupId;
|
|
1821
|
+
completions;
|
|
1822
|
+
autoComplete;
|
|
1823
|
+
highlight;
|
|
1824
|
+
language = "";
|
|
1825
|
+
languageRequest = 0;
|
|
1826
|
+
userKeymap;
|
|
1827
|
+
popupWasOpen = false;
|
|
1828
|
+
destroyed = false;
|
|
1829
|
+
constructor(root, config = {}, hooks = {}) {
|
|
1830
|
+
this.hooks = hooks;
|
|
1831
|
+
this.popupId = `stx-ce-${++instances}`;
|
|
1832
|
+
this.completions = config.completions ?? [];
|
|
1833
|
+
this.autoComplete = config.autoComplete ?? true;
|
|
1834
|
+
this.highlight = config.highlight ?? true;
|
|
1835
|
+
this.userKeymap = config.keymap ?? {};
|
|
1836
|
+
this.core = new EditorCore({
|
|
1837
|
+
value: config.value ?? "",
|
|
1838
|
+
tabSize: config.tabSize,
|
|
1839
|
+
insertSpaces: config.insertSpaces,
|
|
1840
|
+
readOnly: config.readOnly,
|
|
1841
|
+
language: languageConfig(config.language)
|
|
1842
|
+
});
|
|
1843
|
+
registerDefaultCommands(this.core);
|
|
1844
|
+
this.view = new EditorView(root, {
|
|
1845
|
+
core: this.core,
|
|
1846
|
+
keymap: createKeymap([defaultKeymap, this.userKeymap]),
|
|
1847
|
+
inlineDiagnostics: config.inlineDiagnostics,
|
|
1848
|
+
onTransaction: (transaction) => this.onTransaction(transaction),
|
|
1849
|
+
onCommand: (name) => this.onCommand(name),
|
|
1850
|
+
onKeydown: (event) => this.onKeydown(event),
|
|
1851
|
+
onGhost: (outcome, text) => this.hooks.onGhost?.(outcome, text),
|
|
1852
|
+
onFocus: () => this.hooks.onFocus?.(),
|
|
1853
|
+
onBlur: () => {
|
|
1854
|
+
this.closeCompletions();
|
|
1855
|
+
this.hooks.onBlur?.();
|
|
1856
|
+
}
|
|
1857
|
+
});
|
|
1858
|
+
this.view.setReadOnly(config.readOnly ?? false);
|
|
1859
|
+
this.view.setLineNumbers(config.lineNumbers ?? true);
|
|
1860
|
+
this.setLanguage(config.language ?? "typescript");
|
|
1861
|
+
}
|
|
1862
|
+
get value() {
|
|
1863
|
+
return this.core.value;
|
|
1864
|
+
}
|
|
1865
|
+
setValue(value) {
|
|
1866
|
+
if (value !== this.core.value)
|
|
1867
|
+
this.core.setValue(value);
|
|
1868
|
+
}
|
|
1869
|
+
get selection() {
|
|
1870
|
+
const main = this.core.main;
|
|
1871
|
+
const cursor = this.core.cursor;
|
|
1872
|
+
return {
|
|
1873
|
+
anchor: main.anchor,
|
|
1874
|
+
head: main.head,
|
|
1875
|
+
from: rangeFrom(main),
|
|
1876
|
+
to: rangeTo(main),
|
|
1877
|
+
line: cursor.line,
|
|
1878
|
+
column: cursor.column,
|
|
1879
|
+
text: this.core.selectedText,
|
|
1880
|
+
empty: rangeEmpty(main)
|
|
1881
|
+
};
|
|
1882
|
+
}
|
|
1883
|
+
setSelection(anchor, head = anchor) {
|
|
1884
|
+
this.core.setSelection(anchor, head);
|
|
1885
|
+
}
|
|
1886
|
+
focus() {
|
|
1887
|
+
this.view.focus();
|
|
1888
|
+
}
|
|
1889
|
+
setLanguage(language) {
|
|
1890
|
+
if (language === this.language)
|
|
1891
|
+
return;
|
|
1892
|
+
this.language = language;
|
|
1893
|
+
this.core.language = languageConfig(language);
|
|
1894
|
+
this.loadTokenizer();
|
|
1895
|
+
}
|
|
1896
|
+
setHighlight(highlight) {
|
|
1897
|
+
if (highlight === this.highlight)
|
|
1898
|
+
return;
|
|
1899
|
+
this.highlight = highlight;
|
|
1900
|
+
this.loadTokenizer();
|
|
1901
|
+
}
|
|
1902
|
+
loadTokenizer() {
|
|
1903
|
+
const request = ++this.languageRequest;
|
|
1904
|
+
if (!this.highlight) {
|
|
1905
|
+
this.view.setTokenizer(null);
|
|
1906
|
+
return;
|
|
1907
|
+
}
|
|
1908
|
+
createLanguageTokenizer(this.language).catch(() => null).then((tokenizer) => {
|
|
1909
|
+
if (!this.destroyed && request === this.languageRequest)
|
|
1910
|
+
this.view.setTokenizer(tokenizer);
|
|
1911
|
+
});
|
|
1912
|
+
}
|
|
1913
|
+
setReadOnly(readOnly) {
|
|
1914
|
+
this.view.setReadOnly(readOnly);
|
|
1915
|
+
if (readOnly)
|
|
1916
|
+
this.closeCompletions();
|
|
1917
|
+
}
|
|
1918
|
+
setLineNumbers(show) {
|
|
1919
|
+
this.view.setLineNumbers(show);
|
|
1920
|
+
}
|
|
1921
|
+
setTabSize(tabSize) {
|
|
1922
|
+
this.core.tabSize = Math.max(1, Math.floor(tabSize) || 2);
|
|
1923
|
+
}
|
|
1924
|
+
setInsertSpaces(insertSpaces) {
|
|
1925
|
+
this.core.insertSpaces = insertSpaces;
|
|
1926
|
+
}
|
|
1927
|
+
setAutoComplete(autoComplete) {
|
|
1928
|
+
this.autoComplete = autoComplete;
|
|
1929
|
+
}
|
|
1930
|
+
setKeymap(keymap) {
|
|
1931
|
+
this.userKeymap = keymap;
|
|
1932
|
+
this.view.setKeymap(createKeymap([defaultKeymap, keymap]));
|
|
1933
|
+
}
|
|
1934
|
+
setCompletions(completions) {
|
|
1935
|
+
this.completions = completions;
|
|
1936
|
+
}
|
|
1937
|
+
setWidgets(widgets) {
|
|
1938
|
+
this.view.setWidgets(widgets);
|
|
1939
|
+
}
|
|
1940
|
+
setDiagnostics(diagnostics) {
|
|
1941
|
+
this.view.setDiagnostics(diagnostics);
|
|
1942
|
+
}
|
|
1943
|
+
setGhostText(text) {
|
|
1944
|
+
this.view.setGhostText(text);
|
|
1945
|
+
}
|
|
1946
|
+
onCommand(name) {
|
|
1947
|
+
if (name === "run") {
|
|
1948
|
+
this.hooks.onRun?.({ value: this.core.value, selection: this.selection });
|
|
1949
|
+
return true;
|
|
1950
|
+
}
|
|
1951
|
+
if (name === "runSelection") {
|
|
1952
|
+
this.hooks.onRunSelection?.(this.runSelectionDetail());
|
|
1953
|
+
return true;
|
|
1954
|
+
}
|
|
1955
|
+
if (name === "triggerCompletion") {
|
|
1956
|
+
this.requestCompletions(true, "");
|
|
1957
|
+
return true;
|
|
1958
|
+
}
|
|
1959
|
+
return false;
|
|
1960
|
+
}
|
|
1961
|
+
runSelectionDetail() {
|
|
1962
|
+
const main = this.core.main;
|
|
1963
|
+
if (!rangeEmpty(main)) {
|
|
1964
|
+
const from = rangeFrom(main);
|
|
1965
|
+
return { text: this.core.selectedText, from, to: rangeTo(main), line: this.core.doc.positionAt(from).line, selected: true };
|
|
1966
|
+
}
|
|
1967
|
+
const line = this.core.doc.lineAt(main.head);
|
|
1968
|
+
return { text: line.text, from: line.from, to: line.to, line: line.index, selected: false };
|
|
1969
|
+
}
|
|
1970
|
+
onTransaction(transaction) {
|
|
1971
|
+
if (transaction.docChanged)
|
|
1972
|
+
this.hooks.onChange?.(this.core.value, transaction);
|
|
1973
|
+
if (transaction.selectionChanged || transaction.docChanged)
|
|
1974
|
+
this.hooks.onSelection?.(this.selection);
|
|
1975
|
+
if (this.list.isOpen) {
|
|
1976
|
+
this.narrowCompletions();
|
|
1977
|
+
} else if (this.autoComplete && transaction.origin === "input" && transaction.kind === "type" && transaction.changes.length === 1) {
|
|
1978
|
+
const typed = transaction.changes[0].insert;
|
|
1979
|
+
if (typed.length === 1 && TRIGGER.test(typed))
|
|
1980
|
+
this.requestCompletions(false, typed);
|
|
1981
|
+
}
|
|
1982
|
+
this.publishPopup();
|
|
1983
|
+
}
|
|
1984
|
+
currentWord() {
|
|
1985
|
+
const head = this.core.main.head;
|
|
1986
|
+
const line = this.core.doc.lineAt(head);
|
|
1987
|
+
const textBefore = line.text.slice(0, head - line.from);
|
|
1988
|
+
const start = wordStart(textBefore, textBefore.length);
|
|
1989
|
+
return { from: line.from + start, query: textBefore.slice(start), textBefore };
|
|
1990
|
+
}
|
|
1991
|
+
requestCompletions(explicit, trigger) {
|
|
1992
|
+
if (this.core.readOnly || !rangeEmpty(this.core.main))
|
|
1993
|
+
return;
|
|
1994
|
+
const head = this.core.main.head;
|
|
1995
|
+
const cursor = this.core.cursor;
|
|
1996
|
+
const word = this.currentWord();
|
|
1997
|
+
if (this.completions.length > 0 && (explicit || word.query !== ""))
|
|
1998
|
+
this.list.open(this.completions, word.from, word.query);
|
|
1999
|
+
this.hooks.onCompleteRequest?.({
|
|
2000
|
+
query: word.query,
|
|
2001
|
+
from: word.from,
|
|
2002
|
+
to: head,
|
|
2003
|
+
line: cursor.line,
|
|
2004
|
+
column: cursor.column,
|
|
2005
|
+
textBefore: word.textBefore,
|
|
2006
|
+
explicit,
|
|
2007
|
+
trigger
|
|
2008
|
+
});
|
|
2009
|
+
this.publishPopup();
|
|
2010
|
+
}
|
|
2011
|
+
showCompletions(items, options = {}) {
|
|
2012
|
+
if (this.destroyed || this.core.readOnly || !rangeEmpty(this.core.main))
|
|
2013
|
+
return;
|
|
2014
|
+
const head = this.core.main.head;
|
|
2015
|
+
const from = Math.min(head, options.from ?? this.currentWord().from);
|
|
2016
|
+
this.list.open(items, from, this.core.doc.slice(from, head));
|
|
2017
|
+
this.publishPopup();
|
|
2018
|
+
}
|
|
2019
|
+
closeCompletions() {
|
|
2020
|
+
if (!this.list.isOpen && !this.popupWasOpen)
|
|
2021
|
+
return;
|
|
2022
|
+
this.list.close();
|
|
2023
|
+
this.publishPopup();
|
|
2024
|
+
}
|
|
2025
|
+
narrowCompletions() {
|
|
2026
|
+
const main = this.core.main;
|
|
2027
|
+
const from = this.list.from;
|
|
2028
|
+
const query = rangeEmpty(main) && main.head >= from ? this.core.doc.slice(from, main.head) : null;
|
|
2029
|
+
if (query === null || !WORD2.test(query))
|
|
2030
|
+
this.list.close();
|
|
2031
|
+
else
|
|
2032
|
+
this.list.filter(query);
|
|
2033
|
+
}
|
|
2034
|
+
acceptCompletion(index) {
|
|
2035
|
+
if (index !== undefined)
|
|
2036
|
+
this.list.select(index);
|
|
2037
|
+
const accepted = this.list.accept(this.core.main.head);
|
|
2038
|
+
if (!accepted)
|
|
2039
|
+
return false;
|
|
2040
|
+
const end = accepted.change.from + accepted.change.insert.length;
|
|
2041
|
+
this.list.close();
|
|
2042
|
+
this.core.dispatch({ changes: [accepted.change], selection: { anchor: end, head: end }, kind: "complete" });
|
|
2043
|
+
this.hooks.onCompletionAccept?.(accepted.item);
|
|
2044
|
+
this.publishPopup();
|
|
2045
|
+
return true;
|
|
2046
|
+
}
|
|
2047
|
+
selectCompletion(index) {
|
|
2048
|
+
this.list.select(index);
|
|
2049
|
+
this.publishPopup();
|
|
2050
|
+
}
|
|
2051
|
+
onKeydown(event) {
|
|
2052
|
+
if (!this.list.isOpen || event.ctrlKey || event.metaKey || event.altKey)
|
|
2053
|
+
return false;
|
|
2054
|
+
switch (event.key) {
|
|
2055
|
+
case "ArrowDown":
|
|
2056
|
+
this.list.move(1);
|
|
2057
|
+
break;
|
|
2058
|
+
case "ArrowUp":
|
|
2059
|
+
this.list.move(-1);
|
|
2060
|
+
break;
|
|
2061
|
+
case "PageDown":
|
|
2062
|
+
this.list.move(8);
|
|
2063
|
+
break;
|
|
2064
|
+
case "PageUp":
|
|
2065
|
+
this.list.move(-8);
|
|
2066
|
+
break;
|
|
2067
|
+
case "Enter":
|
|
2068
|
+
case "Tab":
|
|
2069
|
+
if (event.shiftKey)
|
|
2070
|
+
return false;
|
|
2071
|
+
return this.acceptCompletion();
|
|
2072
|
+
case "Escape":
|
|
2073
|
+
this.list.close();
|
|
2074
|
+
break;
|
|
2075
|
+
default:
|
|
2076
|
+
return false;
|
|
2077
|
+
}
|
|
2078
|
+
this.publishPopup();
|
|
2079
|
+
return true;
|
|
2080
|
+
}
|
|
2081
|
+
publishPopup() {
|
|
2082
|
+
if (!this.hooks.onPopup)
|
|
2083
|
+
return;
|
|
2084
|
+
if (!this.list.isOpen) {
|
|
2085
|
+
if (this.popupWasOpen) {
|
|
2086
|
+
this.popupWasOpen = false;
|
|
2087
|
+
this.hooks.onPopup(CLOSED_POPUP);
|
|
2088
|
+
}
|
|
2089
|
+
return;
|
|
2090
|
+
}
|
|
2091
|
+
this.popupWasOpen = true;
|
|
2092
|
+
const anchor = this.view.caretAnchor(this.list.from);
|
|
2093
|
+
const above = anchor.spaceBelow < POPUP_ROOM && anchor.spaceAbove > anchor.spaceBelow;
|
|
2094
|
+
const selected = this.list.selectedIndex;
|
|
2095
|
+
const items = this.list.visible.slice(0, POPUP_LIMIT).map((entry, index) => ({
|
|
2096
|
+
id: `${this.popupId}-${index}`,
|
|
2097
|
+
index,
|
|
2098
|
+
label: entry.item.label,
|
|
2099
|
+
detail: entry.item.detail ?? "",
|
|
2100
|
+
kind: entry.item.kind ?? "",
|
|
2101
|
+
selected: index === selected
|
|
2102
|
+
}));
|
|
2103
|
+
this.hooks.onPopup({
|
|
2104
|
+
open: true,
|
|
2105
|
+
items,
|
|
2106
|
+
activeId: items[selected]?.id ?? "",
|
|
2107
|
+
left: anchor.left,
|
|
2108
|
+
top: above ? anchor.top : anchor.bottom,
|
|
2109
|
+
placement: above ? "above" : "below"
|
|
2110
|
+
});
|
|
2111
|
+
}
|
|
2112
|
+
destroy() {
|
|
2113
|
+
this.destroyed = true;
|
|
2114
|
+
this.list.close();
|
|
2115
|
+
this.view.destroy();
|
|
2116
|
+
}
|
|
2117
|
+
}
|
|
2118
|
+
export {
|
|
2119
|
+
CodeEditorController,
|
|
2120
|
+
CompletionList,
|
|
2121
|
+
DEFAULT_LANGUAGE,
|
|
2122
|
+
EditorCore,
|
|
2123
|
+
EditorView,
|
|
2124
|
+
History,
|
|
2125
|
+
LANGUAGE_CONFIGS,
|
|
2126
|
+
LineHighlighter,
|
|
2127
|
+
MAX_TOKENIZED_LINE_LENGTH,
|
|
2128
|
+
TextDocument,
|
|
2129
|
+
clampSelection,
|
|
2130
|
+
createKeymap,
|
|
2131
|
+
createLanguageTokenizer,
|
|
2132
|
+
defaultCommands,
|
|
2133
|
+
defaultKeymap,
|
|
2134
|
+
deleteBackward,
|
|
2135
|
+
handleTextInput,
|
|
2136
|
+
indentLess,
|
|
2137
|
+
indentMore,
|
|
2138
|
+
indentString,
|
|
2139
|
+
indentWidth,
|
|
2140
|
+
insertNewlineAndIndent,
|
|
2141
|
+
invertChanges,
|
|
2142
|
+
isApplePlatform,
|
|
2143
|
+
keyName,
|
|
2144
|
+
languageConfig,
|
|
2145
|
+
leadingWhitespace,
|
|
2146
|
+
mainRange,
|
|
2147
|
+
mapOffset,
|
|
2148
|
+
mapSelection,
|
|
2149
|
+
matchScore,
|
|
2150
|
+
normalizeBinding,
|
|
2151
|
+
normalizeNewlines,
|
|
2152
|
+
range,
|
|
2153
|
+
rangeEmpty,
|
|
2154
|
+
rangeFrom,
|
|
2155
|
+
rangeTo,
|
|
2156
|
+
rankCompletions,
|
|
2157
|
+
redo,
|
|
2158
|
+
registerDefaultCommands,
|
|
2159
|
+
renderLineHtml,
|
|
2160
|
+
selectionsEqual,
|
|
2161
|
+
singleSelection,
|
|
2162
|
+
toggleLineComment,
|
|
2163
|
+
tokenizerStateKey,
|
|
2164
|
+
undo,
|
|
2165
|
+
wordStart
|
|
2166
|
+
};
|