@nibling/editor 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +53 -2
- package/dist/commands/autoclose.d.ts +13 -0
- package/dist/commands/autoclose.js +78 -0
- package/dist/commands/commands.d.ts +104 -0
- package/dist/commands/commands.js +442 -0
- package/dist/commands/comments.d.ts +3 -0
- package/dist/commands/comments.js +9 -0
- package/dist/commands/fold.d.ts +18 -0
- package/dist/commands/fold.js +75 -0
- package/dist/commands/keymap.d.ts +40 -0
- package/dist/commands/keymap.js +118 -0
- package/dist/complete/autocomplete.d.ts +35 -0
- package/dist/complete/autocomplete.js +386 -0
- package/dist/complete/source.d.ts +72 -0
- package/dist/complete/source.js +52 -0
- package/dist/complete/words.d.ts +53 -0
- package/dist/complete/words.js +203 -0
- package/dist/element.d.ts +119 -0
- package/dist/element.js +1011 -0
- package/dist/expand/expansions.d.ts +68 -0
- package/dist/expand/expansions.js +143 -0
- package/dist/expand/projection.d.ts +73 -0
- package/dist/expand/projection.js +189 -0
- package/dist/expand/resolver.d.ts +42 -0
- package/dist/expand/resolver.js +585 -0
- package/dist/expand/slice-tokens.d.ts +25 -0
- package/dist/expand/slice-tokens.js +79 -0
- package/dist/extension.d.ts +58 -0
- package/dist/extension.js +1 -0
- package/dist/find/find.d.ts +12 -0
- package/dist/find/find.js +469 -0
- package/dist/find/search.d.ts +81 -0
- package/dist/find/search.js +308 -0
- package/dist/headless.d.ts +16 -0
- package/dist/headless.js +19 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/inline-expansion.d.ts +89 -0
- package/dist/inline-expansion.js +324 -0
- package/dist/input/edit-context.d.ts +23 -0
- package/dist/input/edit-context.js +102 -0
- package/dist/input/surface.d.ts +101 -0
- package/dist/input/surface.js +84 -0
- package/dist/input/textarea.d.ts +26 -0
- package/dist/input/textarea.js +125 -0
- package/dist/sheet.d.ts +6 -0
- package/dist/sheet.js +12 -0
- package/dist/state/core.d.ts +73 -0
- package/dist/state/core.js +86 -0
- package/dist/state/fold-ranges.d.ts +41 -0
- package/dist/state/fold-ranges.js +167 -0
- package/dist/state/folds.d.ts +45 -0
- package/dist/state/folds.js +240 -0
- package/dist/state/history.d.ts +25 -0
- package/dist/state/history.js +127 -0
- package/dist/state/indent.d.ts +33 -0
- package/dist/state/indent.js +121 -0
- package/dist/state/selection.d.ts +45 -0
- package/dist/state/selection.js +106 -0
- package/dist/state/whitespace.d.ts +16 -0
- package/dist/state/whitespace.js +54 -0
- package/dist/text/changeset.d.ts +56 -0
- package/dist/text/changeset.js +321 -0
- package/dist/text/text.d.ts +30 -0
- package/dist/text/text.js +321 -0
- package/dist/tokens/bridge.d.ts +107 -0
- package/dist/tokens/bridge.js +461 -0
- package/dist/tokens/engine.d.ts +39 -0
- package/dist/tokens/engine.js +360 -0
- package/dist/tokens/protocol.d.ts +113 -0
- package/dist/tokens/protocol.js +38 -0
- package/dist/tokens/textmate/ascii-regex.d.ts +5 -0
- package/dist/tokens/textmate/ascii-regex.js +156 -0
- package/dist/tokens/textmate/index.d.ts +7 -0
- package/dist/tokens/textmate/index.js +19 -0
- package/dist/tokens/textmate/linker.d.ts +67 -0
- package/dist/tokens/textmate/linker.js +238 -0
- package/dist/tokens/textmate/scopes.d.ts +14 -0
- package/dist/tokens/textmate/scopes.js +54 -0
- package/dist/tokens/textmate/theme.d.ts +54 -0
- package/dist/tokens/textmate/theme.js +138 -0
- package/dist/tokens/textmate/tokenizer.d.ts +29 -0
- package/dist/tokens/textmate/tokenizer.js +684 -0
- package/dist/tokens/textmate.d.ts +49 -0
- package/dist/tokens/textmate.js +4 -0
- package/dist/view/decorations.d.ts +183 -0
- package/dist/view/decorations.js +51 -0
- package/dist/view/guides.d.ts +58 -0
- package/dist/view/guides.js +174 -0
- package/dist/view/heightmap.d.ts +97 -0
- package/dist/view/heightmap.js +229 -0
- package/dist/view/line-dom.d.ts +54 -0
- package/dist/view/line-dom.js +168 -0
- package/dist/view/measure.d.ts +37 -0
- package/dist/view/measure.js +136 -0
- package/dist/view/overlays.d.ts +32 -0
- package/dist/view/overlays.js +135 -0
- package/dist/view/rail.d.ts +27 -0
- package/dist/view/rail.js +232 -0
- package/dist/view/remote.d.ts +24 -0
- package/dist/view/remote.js +143 -0
- package/dist/view/scope-focus.d.ts +27 -0
- package/dist/view/scope-focus.js +255 -0
- package/dist/view/view.d.ts +114 -0
- package/dist/view/view.js +1231 -0
- package/dist/vite.d.ts +12 -0
- package/dist/vite.js +64 -0
- package/dist/worker.d.ts +1 -0
- package/dist/worker.js +64 -0
- package/grammars/astro.mjs +2 -0
- package/grammars/chunks/r-90cb351dba.mjs +1 -0
- package/grammars/chunks/r-b92a500aab.mjs +1 -0
- package/grammars/chunks/r-ced5311992.mjs +1 -0
- package/grammars/chunks/r-ebae91d4ec.mjs +1 -0
- package/grammars/chunks/s-1821e66170.mjs +1 -0
- package/grammars/css.mjs +1 -0
- package/grammars/diff.mjs +1 -0
- package/grammars/docker.mjs +1 -0
- package/grammars/dotenv.mjs +1 -0
- package/grammars/graphql.mjs +1 -0
- package/grammars/html.mjs +2 -0
- package/grammars/ini.mjs +1 -0
- package/grammars/javascript.mjs +5 -0
- package/grammars/json.mjs +1 -0
- package/grammars/json5.mjs +1 -0
- package/grammars/jsonc.mjs +1 -0
- package/grammars/less.mjs +1 -0
- package/grammars/manifest.json +58 -0
- package/grammars/markdown.mjs +1 -0
- package/grammars/mdx.mjs +1 -0
- package/grammars/pug.mjs +1 -0
- package/grammars/scss.mjs +1 -0
- package/grammars/shellscript.mjs +1 -0
- package/grammars/sql.mjs +1 -0
- package/grammars/stylus.mjs +1 -0
- package/grammars/svelte.mjs +1 -0
- package/grammars/toml.mjs +1 -0
- package/grammars/tsx.mjs +5 -0
- package/grammars/typescript.mjs +2 -0
- package/grammars/vue.mjs +1 -0
- package/grammars/yaml.mjs +1 -0
- package/package.json +71 -3
- package/themes/dark-plus.json +1 -0
- package/themes/light-plus.json +1 -0
- package/themes/monokai.json +1 -0
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
// Ported verbatim from research/text-core/ts/text.ts (S2). Signatures only here.
|
|
2
|
+
const LEAF_TARGET = 512;
|
|
3
|
+
const LEAF_MAX = 1024;
|
|
4
|
+
const LEAF_MIN = 256;
|
|
5
|
+
const BRANCH_MAX = 16;
|
|
6
|
+
const BRANCH_MIN = 4;
|
|
7
|
+
function leafWidths(s) {
|
|
8
|
+
const first = s.indexOf('\n');
|
|
9
|
+
if (first < 0)
|
|
10
|
+
return { head: s.length, tail: s.length, max: 0 };
|
|
11
|
+
let max = 0;
|
|
12
|
+
let prev = first;
|
|
13
|
+
for (let i = s.indexOf('\n', first + 1); i >= 0; i = s.indexOf('\n', i + 1)) {
|
|
14
|
+
if (i - prev - 1 > max)
|
|
15
|
+
max = i - prev - 1;
|
|
16
|
+
prev = i;
|
|
17
|
+
}
|
|
18
|
+
return { head: first, tail: s.length - prev - 1, max };
|
|
19
|
+
}
|
|
20
|
+
/** Computed on first ask and kept: nodes are immutable, so an edit recomputes only its new path. */
|
|
21
|
+
function widthsOf(node) {
|
|
22
|
+
if (node.widths)
|
|
23
|
+
return node.widths;
|
|
24
|
+
let w;
|
|
25
|
+
if (node instanceof Leaf)
|
|
26
|
+
w = leafWidths(node.text);
|
|
27
|
+
else {
|
|
28
|
+
let acc = null;
|
|
29
|
+
let accBreaks = 0;
|
|
30
|
+
for (const c of node.children) {
|
|
31
|
+
const cw = widthsOf(c);
|
|
32
|
+
if (!acc)
|
|
33
|
+
acc = cw;
|
|
34
|
+
else if (accBreaks === 0 && c.breaks === 0)
|
|
35
|
+
acc = { head: acc.head + cw.head, tail: acc.head + cw.head, max: 0 };
|
|
36
|
+
else if (accBreaks === 0)
|
|
37
|
+
acc = { head: acc.head + cw.head, tail: cw.tail, max: cw.max };
|
|
38
|
+
else if (c.breaks === 0)
|
|
39
|
+
acc = { head: acc.head, tail: acc.tail + cw.tail, max: acc.max };
|
|
40
|
+
else
|
|
41
|
+
acc = { head: acc.head, tail: cw.tail, max: Math.max(acc.max, cw.max, acc.tail + cw.head) };
|
|
42
|
+
accBreaks += c.breaks;
|
|
43
|
+
}
|
|
44
|
+
w = acc ?? { head: 0, tail: 0, max: 0 };
|
|
45
|
+
}
|
|
46
|
+
node.widths = w;
|
|
47
|
+
return w;
|
|
48
|
+
}
|
|
49
|
+
class Leaf {
|
|
50
|
+
text;
|
|
51
|
+
length;
|
|
52
|
+
breaks;
|
|
53
|
+
widths = null;
|
|
54
|
+
constructor(text, breaks) {
|
|
55
|
+
this.text = text;
|
|
56
|
+
this.length = text.length;
|
|
57
|
+
this.breaks = breaks;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
class Branch {
|
|
61
|
+
children;
|
|
62
|
+
length;
|
|
63
|
+
breaks;
|
|
64
|
+
widths = null;
|
|
65
|
+
constructor(children) {
|
|
66
|
+
let length = 0;
|
|
67
|
+
let breaks = 0;
|
|
68
|
+
for (const c of children) {
|
|
69
|
+
length += c.length;
|
|
70
|
+
breaks += c.breaks;
|
|
71
|
+
}
|
|
72
|
+
this.children = children;
|
|
73
|
+
this.length = length;
|
|
74
|
+
this.breaks = breaks;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function countBreaks(s, from, to) {
|
|
78
|
+
let n = 0;
|
|
79
|
+
let i = s.indexOf('\n', from);
|
|
80
|
+
while (i >= 0 && i < to) {
|
|
81
|
+
n++;
|
|
82
|
+
i = s.indexOf('\n', i + 1);
|
|
83
|
+
}
|
|
84
|
+
return n;
|
|
85
|
+
}
|
|
86
|
+
function chunkString(s, out) {
|
|
87
|
+
const count = Math.ceil(s.length / LEAF_TARGET);
|
|
88
|
+
const size = Math.ceil(s.length / count);
|
|
89
|
+
for (let i = 0; i < s.length; i += size) {
|
|
90
|
+
const part = s.slice(i, i + size);
|
|
91
|
+
out.push(new Leaf(part, countBreaks(part, 0, part.length)));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function makeLeaves(s, breaks, out) {
|
|
95
|
+
if (s.length === 0)
|
|
96
|
+
return;
|
|
97
|
+
if (s.length <= LEAF_MAX)
|
|
98
|
+
out.push(new Leaf(s, breaks));
|
|
99
|
+
else
|
|
100
|
+
chunkString(s, out);
|
|
101
|
+
}
|
|
102
|
+
function groupNodes(nodes, out) {
|
|
103
|
+
if (nodes.length <= BRANCH_MAX) {
|
|
104
|
+
out.push(new Branch(nodes));
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const count = Math.ceil(nodes.length / BRANCH_MAX);
|
|
108
|
+
const size = Math.ceil(nodes.length / count);
|
|
109
|
+
for (let i = 0; i < nodes.length; i += size)
|
|
110
|
+
out.push(new Branch(nodes.slice(i, i + size)));
|
|
111
|
+
}
|
|
112
|
+
function isUnderfull(n) {
|
|
113
|
+
return n instanceof Leaf ? n.length < LEAF_MIN : n.children.length < BRANCH_MIN;
|
|
114
|
+
}
|
|
115
|
+
function mergePair(a, b, out) {
|
|
116
|
+
if (a instanceof Leaf) {
|
|
117
|
+
const bl = b;
|
|
118
|
+
makeLeaves(a.text + bl.text, a.breaks + bl.breaks, out);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
groupNodes(a.children.concat(b.children), out);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// Merges each underfull node in `nodes` with a neighbor. All nodes share one height.
|
|
125
|
+
function rebalance(nodes) {
|
|
126
|
+
if (nodes.length < 2)
|
|
127
|
+
return nodes;
|
|
128
|
+
let needs = false;
|
|
129
|
+
for (const n of nodes)
|
|
130
|
+
if (isUnderfull(n)) {
|
|
131
|
+
needs = true;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
if (!needs)
|
|
135
|
+
return nodes;
|
|
136
|
+
const out = [];
|
|
137
|
+
let i = 0;
|
|
138
|
+
while (i < nodes.length) {
|
|
139
|
+
const n = nodes[i];
|
|
140
|
+
if (isUnderfull(n)) {
|
|
141
|
+
if (i + 1 < nodes.length) {
|
|
142
|
+
mergePair(n, nodes[i + 1], out);
|
|
143
|
+
i += 2;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const last = out.pop();
|
|
147
|
+
if (last !== undefined) {
|
|
148
|
+
mergePair(last, n, out);
|
|
149
|
+
i++;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
out.push(n);
|
|
154
|
+
i++;
|
|
155
|
+
}
|
|
156
|
+
return out;
|
|
157
|
+
}
|
|
158
|
+
// Returns the nodes, all at `node`'s height, that replace `node` after the edit.
|
|
159
|
+
function replaceNode(node, from, to, insert, insBreaks, out) {
|
|
160
|
+
if (node instanceof Leaf) {
|
|
161
|
+
const s = node.text;
|
|
162
|
+
const breaks = node.breaks - countBreaks(s, from, to) + insBreaks;
|
|
163
|
+
makeLeaves(s.slice(0, from) + insert + s.slice(to), breaks, out);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const kids = node.children;
|
|
167
|
+
let i = 0;
|
|
168
|
+
let start = 0;
|
|
169
|
+
while (i < kids.length - 1 && from > start + kids[i].length) {
|
|
170
|
+
start += kids[i].length;
|
|
171
|
+
i++;
|
|
172
|
+
}
|
|
173
|
+
let j = i;
|
|
174
|
+
let startJ = start;
|
|
175
|
+
while (j < kids.length - 1 && to > startJ + kids[j].length) {
|
|
176
|
+
startJ += kids[j].length;
|
|
177
|
+
j++;
|
|
178
|
+
}
|
|
179
|
+
const next = kids.slice(0, i);
|
|
180
|
+
if (i === j) {
|
|
181
|
+
replaceNode(kids[i], from - start, to - start, insert, insBreaks, next);
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
replaceNode(kids[i], from - start, kids[i].length, insert, insBreaks, next);
|
|
185
|
+
replaceNode(kids[j], 0, to - startJ, '', 0, next);
|
|
186
|
+
}
|
|
187
|
+
for (let k = j + 1; k < kids.length; k++)
|
|
188
|
+
next.push(kids[k]);
|
|
189
|
+
const balanced = rebalance(next);
|
|
190
|
+
if (balanced.length === 0)
|
|
191
|
+
return;
|
|
192
|
+
groupNodes(balanced, out);
|
|
193
|
+
}
|
|
194
|
+
function rootOf(nodes) {
|
|
195
|
+
while (nodes.length > 1) {
|
|
196
|
+
const up = [];
|
|
197
|
+
groupNodes(nodes, up);
|
|
198
|
+
nodes = up;
|
|
199
|
+
}
|
|
200
|
+
let root = nodes.length === 0 ? EMPTY_LEAF : nodes[0];
|
|
201
|
+
while (root instanceof Branch && root.children.length === 1)
|
|
202
|
+
root = root.children[0];
|
|
203
|
+
return root;
|
|
204
|
+
}
|
|
205
|
+
const EMPTY_LEAF = new Leaf('', 0);
|
|
206
|
+
function sliceNode(node, from, to) {
|
|
207
|
+
if (node instanceof Leaf)
|
|
208
|
+
return from === 0 && to === node.length ? node.text : node.text.slice(from, to);
|
|
209
|
+
let out = '';
|
|
210
|
+
let start = 0;
|
|
211
|
+
for (const c of node.children) {
|
|
212
|
+
const end = start + c.length;
|
|
213
|
+
if (end > from && start < to)
|
|
214
|
+
out += sliceNode(c, Math.max(0, from - start), Math.min(c.length, to - start));
|
|
215
|
+
if (end >= to)
|
|
216
|
+
break;
|
|
217
|
+
start = end;
|
|
218
|
+
}
|
|
219
|
+
return out;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Immutable persistent rope. Positions are UTF-16 code units. Only "\n" breaks lines.
|
|
223
|
+
* `replace` shares untouched subtrees, so keeping old versions alive is cheap; the token bridge
|
|
224
|
+
* relies on that to hold the Text of every in-flight version.
|
|
225
|
+
*/
|
|
226
|
+
export class Text {
|
|
227
|
+
root;
|
|
228
|
+
constructor(root) {
|
|
229
|
+
this.root = root;
|
|
230
|
+
}
|
|
231
|
+
static empty = new Text(EMPTY_LEAF);
|
|
232
|
+
static of(s) {
|
|
233
|
+
const leaves = [];
|
|
234
|
+
makeLeaves(s, countBreaks(s, 0, s.length), leaves);
|
|
235
|
+
return new Text(rootOf(leaves));
|
|
236
|
+
}
|
|
237
|
+
get length() {
|
|
238
|
+
return this.root.length;
|
|
239
|
+
}
|
|
240
|
+
get lines() {
|
|
241
|
+
return this.root.breaks + 1;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Length in code units of the longest line. Nodes cache their part, so after an edit this costs
|
|
245
|
+
* O(log n) node combines on the edited path; the first call on a fresh document scans it once.
|
|
246
|
+
*/
|
|
247
|
+
get maxLineLength() {
|
|
248
|
+
const w = widthsOf(this.root);
|
|
249
|
+
return Math.max(w.head, w.tail, w.max);
|
|
250
|
+
}
|
|
251
|
+
replace(from, to, insert) {
|
|
252
|
+
if (from < 0 || to < from || to > this.root.length)
|
|
253
|
+
throw new RangeError(`Invalid replace ${from}-${to} in ${this.root.length}`);
|
|
254
|
+
if (from === to && insert.length === 0)
|
|
255
|
+
return this;
|
|
256
|
+
const out = [];
|
|
257
|
+
replaceNode(this.root, from, to, insert, countBreaks(insert, 0, insert.length), out);
|
|
258
|
+
return new Text(rootOf(out));
|
|
259
|
+
}
|
|
260
|
+
slice(from, to = this.root.length) {
|
|
261
|
+
if (from < 0 || to < from || to > this.root.length)
|
|
262
|
+
throw new RangeError(`Invalid slice ${from}-${to} in ${this.root.length}`);
|
|
263
|
+
return sliceNode(this.root, from, to);
|
|
264
|
+
}
|
|
265
|
+
// Offset of the `k`th `\n`, 1-based. Requires 1 <= k <= breaks.
|
|
266
|
+
breakOffset(k) {
|
|
267
|
+
let node = this.root;
|
|
268
|
+
let base = 0;
|
|
269
|
+
while (node instanceof Branch) {
|
|
270
|
+
for (const c of node.children) {
|
|
271
|
+
if (k <= c.breaks) {
|
|
272
|
+
node = c;
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
k -= c.breaks;
|
|
276
|
+
base += c.length;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
const s = node.text;
|
|
280
|
+
let i = s.indexOf('\n');
|
|
281
|
+
while (--k > 0)
|
|
282
|
+
i = s.indexOf('\n', i + 1);
|
|
283
|
+
return base + i;
|
|
284
|
+
}
|
|
285
|
+
lineAt(pos) {
|
|
286
|
+
if (pos < 0 || pos > this.root.length)
|
|
287
|
+
throw new RangeError(`Invalid position ${pos} in ${this.root.length}`);
|
|
288
|
+
let node = this.root;
|
|
289
|
+
let base = 0;
|
|
290
|
+
let before = 0;
|
|
291
|
+
let rel = pos;
|
|
292
|
+
while (node instanceof Branch) {
|
|
293
|
+
const kids = node.children;
|
|
294
|
+
let i = 0;
|
|
295
|
+
while (i < kids.length - 1 && rel > kids[i].length) {
|
|
296
|
+
rel -= kids[i].length;
|
|
297
|
+
base += kids[i].length;
|
|
298
|
+
before += kids[i].breaks;
|
|
299
|
+
i++;
|
|
300
|
+
}
|
|
301
|
+
node = kids[i];
|
|
302
|
+
}
|
|
303
|
+
const s = node.text;
|
|
304
|
+
const n = before + countBreaks(s, 0, rel);
|
|
305
|
+
const prev = rel > 0 ? s.lastIndexOf('\n', rel - 1) : -1;
|
|
306
|
+
const from = prev >= 0 ? base + prev + 1 : n === 0 ? 0 : this.breakOffset(n) + 1;
|
|
307
|
+
const nextLocal = s.indexOf('\n', rel);
|
|
308
|
+
const to = nextLocal >= 0 ? base + nextLocal : n === this.root.breaks ? this.root.length : this.breakOffset(n + 1);
|
|
309
|
+
return { number: n + 1, from, to };
|
|
310
|
+
}
|
|
311
|
+
line(n) {
|
|
312
|
+
if (n < 1 || n > this.root.breaks + 1)
|
|
313
|
+
throw new RangeError(`Invalid line ${n} in ${this.root.breaks + 1}`);
|
|
314
|
+
const from = n === 1 ? 0 : this.breakOffset(n - 1) + 1;
|
|
315
|
+
const to = n === this.root.breaks + 1 ? this.root.length : this.breakOffset(n);
|
|
316
|
+
return { number: n, from, to };
|
|
317
|
+
}
|
|
318
|
+
toString() {
|
|
319
|
+
return this.slice(0, this.root.length);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { ChangeSet } from '../text/changeset.ts';
|
|
2
|
+
import type { Text } from '../text/text.ts';
|
|
3
|
+
import type { EditorCore, Version } from '../state/core.ts';
|
|
4
|
+
import type { EditorTheme, FromWorker, GrammarManifest, LineTokens, StyleDef, ToWorker } from './protocol.ts';
|
|
5
|
+
/** Anything with postMessage/onmessage: a Worker, a MessagePort, or a fake in tests. */
|
|
6
|
+
export interface WorkerLike {
|
|
7
|
+
postMessage(message: ToWorker, transfer?: Transferable[]): void;
|
|
8
|
+
addEventListener(type: 'message', listener: (e: MessageEvent<FromWorker>) => void): void;
|
|
9
|
+
terminate?(): void;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* What a view reads tokens from. TokenBridge is the one for a whole document; an inline
|
|
13
|
+
* expansion's view reads a slice of its source's bridge through the same three calls.
|
|
14
|
+
*/
|
|
15
|
+
export interface LineTokenSource {
|
|
16
|
+
lineTokens(line: number): LineTokens | undefined;
|
|
17
|
+
setViewport(from: number, to: number): void;
|
|
18
|
+
onChange(listener: (c: TokenChange) => void): () => void;
|
|
19
|
+
}
|
|
20
|
+
/** What the view hears. `lines` are 1-based in the current document. */
|
|
21
|
+
export interface TokenChange {
|
|
22
|
+
readonly lines: readonly number[];
|
|
23
|
+
/** New style definitions; when `reset` is set, they replace every previous one (theme swap). */
|
|
24
|
+
readonly styles: readonly StyleDef[];
|
|
25
|
+
readonly reset: boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Main-thread side of the tokenizer (D6, D10). A follower of EditorCore.
|
|
29
|
+
*
|
|
30
|
+
* It owns the only main-thread copy of syntax tokens, and only for the mounted window
|
|
31
|
+
* [#from, #from + #rows.length). The worker owns tokens for the whole document. That keeps the
|
|
32
|
+
* main thread's per-keystroke work proportional to the window (about 100 lines), never to the
|
|
33
|
+
* document.
|
|
34
|
+
*
|
|
35
|
+
* Staleness rule (the answer to "version N tokens reaching a view at N+k"):
|
|
36
|
+
* A row from a worker batch is only ever attached to a line whose text is identical to the
|
|
37
|
+
* text it was computed for. Batches carry their version; the bridge keeps the Text of every version from
|
|
38
|
+
* the newest batch's version to the present (persistent, so this costs only the edited paths),
|
|
39
|
+
* composes the changes since, and for each line in the batch:
|
|
40
|
+
* - touched by those changes -> dropped (a newer batch is already on its way);
|
|
41
|
+
* - untouched -> moved to its current line number and accepted.
|
|
42
|
+
* Lines below an edit whose *start state* changed keep their old, correct-for-their-text-at-N
|
|
43
|
+
* tokens until the worker's newer batch replaces them. That is bounded staleness, never
|
|
44
|
+
* misalignment.
|
|
45
|
+
*
|
|
46
|
+
* Local edits: on every Update the window is re-keyed. Untouched rows shift to their new line
|
|
47
|
+
* numbers. A touched row is mapped through the change: retained characters keep their runs,
|
|
48
|
+
* inserted characters get style 0. That row is marked provisional; the next batch for that line
|
|
49
|
+
* replaces it. Typing never repaints a colored line as plain text.
|
|
50
|
+
*/
|
|
51
|
+
export declare class TokenBridge implements LineTokenSource {
|
|
52
|
+
#private;
|
|
53
|
+
/**
|
|
54
|
+
* `prefetch`: lines kept and requested beyond each side of the mounted range, so scrolling lands
|
|
55
|
+
* on colored lines; default, the mounted range's own length. `cacheLines`: how many rows outside
|
|
56
|
+
* that range are kept for scrolling back.
|
|
57
|
+
*/
|
|
58
|
+
constructor(worker: WorkerLike, core: EditorCore, init: {
|
|
59
|
+
language: string;
|
|
60
|
+
grammars: GrammarManifest;
|
|
61
|
+
theme: EditorTheme;
|
|
62
|
+
prefetch?: number;
|
|
63
|
+
cacheLines?: number;
|
|
64
|
+
}, signal?: AbortSignal);
|
|
65
|
+
/** Tokens for a line, or undefined to paint it plain. O(1). */
|
|
66
|
+
lineTokens(line: number): LineTokens | undefined;
|
|
67
|
+
/** The styles every row of the shown epoch may use. */
|
|
68
|
+
get styles(): readonly StyleDef[];
|
|
69
|
+
/**
|
|
70
|
+
* Keep rows for lines [from, to) on behalf of `owner`, wherever the mounted window is: the worker
|
|
71
|
+
* sends them, and edits re-key them provisionally as they do the window, so a line typed into
|
|
72
|
+
* through an expansion never flashes plain. Calling it again moves the pin; `from >= to` removes
|
|
73
|
+
* it. Pins also move themselves through each edit, approximately, until their owner moves them.
|
|
74
|
+
*/
|
|
75
|
+
pin(owner: object, from: number, to: number): void;
|
|
76
|
+
/** Called by the view when its mounted line range changes. The worker is asked for the range
|
|
77
|
+
* plus the prefetch margin; rows leaving that span move to the cache, and rows entering it come
|
|
78
|
+
* back from the cache. */
|
|
79
|
+
setViewport(from: number, to: number): void;
|
|
80
|
+
setTheme(theme: EditorTheme): void;
|
|
81
|
+
/** Like a theme change, a new epoch: the old grammar's rows keep painting the same text until
|
|
82
|
+
* the first batch of the new epoch replaces them. */
|
|
83
|
+
setLanguage(language: string, version: Version): void;
|
|
84
|
+
onChange(listener: (c: TokenChange) => void): () => void;
|
|
85
|
+
dispose(): void;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Pure. Re-key a token window through one ChangeSet. Exported for tests.
|
|
89
|
+
* Rows for lines the change does not touch move to their new line numbers unchanged (same
|
|
90
|
+
* Uint32Array). Touched rows are rebuilt by `mapLineTokens`. Lines created by the change get
|
|
91
|
+
* undefined. Cost: O(window + changed sections).
|
|
92
|
+
*/
|
|
93
|
+
export declare function rekeyWindow(rows: readonly (LineTokens | undefined)[], from: number, changes: ChangeSet, before: Text, after: Text): (LineTokens | undefined)[];
|
|
94
|
+
/** Pure. rekeyWindow for rows kept by line number rather than in a window (the bridge's pins). */
|
|
95
|
+
export declare function rekeyRows(rows: ReadonlyMap<number, LineTokens>, changes: ChangeSet, before: Text, after: Text): Map<number, LineTokens>;
|
|
96
|
+
/**
|
|
97
|
+
* Pure. Map one line's runs through an in-line edit: retained characters keep their style,
|
|
98
|
+
* inserted characters take style 0, empty runs vanish, equal neighbours merge.
|
|
99
|
+
*/
|
|
100
|
+
export declare function mapLineTokens(tokens: LineTokens, lineFrom: number, changes: ChangeSet, newLineFrom: number, newLength: number): LineTokens;
|
|
101
|
+
/**
|
|
102
|
+
* Pure. Re-key cached rows through one ChangeSet with the rule worker batches follow: a row whose
|
|
103
|
+
* line the change touches is dropped, and every other row moves to its line's new number. Cached
|
|
104
|
+
* rows are never mapped provisionally, so each one stays exactly what the worker computed for its
|
|
105
|
+
* text. Order is kept, so the oldest entry stays first. O(cache).
|
|
106
|
+
*/
|
|
107
|
+
export declare function rekeyCache(cache: ReadonlyMap<number, LineTokens>, changes: ChangeSet, before: Text, after: Text): Map<number, LineTokens>;
|