@astryxdesign/richtext 0.7.6 → 0.7.7-canary.6442361
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/CodeSyntaxPlugin.d.ts +1 -8
- package/dist/CodeSyntaxPlugin.d.ts.map +1 -1
- package/dist/CodeSyntaxPlugin.js +295 -66
- package/dist/codeLineTokens.d.ts +53 -0
- package/dist/codeLineTokens.d.ts.map +1 -0
- package/dist/codeLineTokens.js +188 -0
- package/package.json +5 -5
- package/src/CodeSyntaxPlugin.tsx +367 -84
- package/src/codeLineTokens.ts +241 -0
|
@@ -1,13 +1,6 @@
|
|
|
1
|
-
import { type CodeLanguage
|
|
1
|
+
import { type CodeLanguage } from '@astryxdesign/core/CodeBlock';
|
|
2
2
|
/** The class the theme adds to every code block (editorTheme.ts). */
|
|
3
3
|
export declare const CODE_SYNTAX_CLASS = "astryx-richtext-code";
|
|
4
|
-
/**
|
|
5
|
-
* The tokens of `code`, laid out for the rendered text of its block: line by
|
|
6
|
-
* line when the text keeps the line breaks, or as one line of offsets when
|
|
7
|
-
* each line break is an element with no text. Null when the rendered text is
|
|
8
|
-
* not the code (it is mid-update), so nothing is colored out of place.
|
|
9
|
-
*/
|
|
10
|
-
export declare function renderedTokens(code: string, language: string | CodeLanguage, rendered: string): TokenLine[] | null;
|
|
11
4
|
export declare function CodeSyntaxPlugin({ codeLanguages, }?: {
|
|
12
5
|
/** Language values a fence may name (component:Markdown FR30). */
|
|
13
6
|
codeLanguages?: ReadonlyArray<CodeLanguage>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CodeSyntaxPlugin.d.ts","sourceRoot":"","sources":["../src/CodeSyntaxPlugin.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"CodeSyntaxPlugin.d.ts","sourceRoot":"","sources":["../src/CodeSyntaxPlugin.tsx"],"names":[],"mappings":"AAqCA,OAAO,EAGL,KAAK,YAAY,EAClB,MAAM,8BAA8B,CAAC;AAItC,qEAAqE;AACrE,eAAO,MAAM,iBAAiB,yBAAyB,CAAC;AAgMxD,wBAAgB,gBAAgB,CAAC,EAC/B,aAAa,GACd,GAAE;IACD,kEAAkE;IAClE,aAAa,CAAC,EAAE,aAAa,CAAC,YAAY,CAAC,CAAC;CACxC,GAAG,IAAI,CAyPZ"}
|
package/dist/CodeSyntaxPlugin.js
CHANGED
|
@@ -5,27 +5,32 @@
|
|
|
5
5
|
/**
|
|
6
6
|
* @file CodeSyntaxPlugin.tsx
|
|
7
7
|
* @input Uses @lexical/react (composer context), @lexical/code (CodeNode),
|
|
8
|
-
* lexical,
|
|
9
|
-
*
|
|
8
|
+
* lexical, core CodeBlock's highlight styles and token types, and
|
|
9
|
+
* CodeLineTokens (codeLineTokens.ts), core CodeBlock's tokens kept line by
|
|
10
|
+
* line.
|
|
10
11
|
* @output Exports CodeSyntaxPlugin, which colors the code in every fenced code
|
|
11
|
-
* block with core CodeBlock's tokenizer and syntax color tokens;
|
|
12
|
-
* CODE_SYNTAX_CLASS, the class the theme gives code blocks for it
|
|
13
|
-
* renderedTokens, which lays tokens out over a block's rendered text.
|
|
12
|
+
* block with core CodeBlock's tokenizer and syntax color tokens; and
|
|
13
|
+
* CODE_SYNTAX_CLASS, the class the theme gives code blocks for it.
|
|
14
14
|
* @position Rendered by RichTextEditor and RichTextView. A fenced code block
|
|
15
15
|
* with a language core CodeBlock knows is colored as core Markdown colors
|
|
16
16
|
* it: the same tokenizer finds the same tokens, and each token type takes
|
|
17
17
|
* its `--color-syntax-*` token (spec:AST-061 FR1) — no palette of its own.
|
|
18
18
|
* The colors are CSS Custom Highlight ranges over the text Lexical renders,
|
|
19
19
|
* the way core CodeBlock colors editable code, so the document and its DOM
|
|
20
|
-
* are untouched
|
|
21
|
-
*
|
|
20
|
+
* are untouched. A block is colored in full when it appears, or when its
|
|
21
|
+
* language, its element, or its set of text nodes changes. An edit that
|
|
22
|
+
* only rewrites one text node's text re-tokenizes just the lines
|
|
23
|
+
* CodeLineTokens says it can change, and replaces only the ranges in that
|
|
24
|
+
* text node, reading no other. Browsers without the Highlight API show
|
|
25
|
+
* plain code.
|
|
22
26
|
*/
|
|
23
27
|
import { useEffect, useState } from 'react';
|
|
24
28
|
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
|
25
29
|
import { $isCodeNode, CodeNode } from '@lexical/code';
|
|
26
|
-
import { $getNodeByKey } from 'lexical';
|
|
27
|
-
import {
|
|
30
|
+
import { $getNodeByKey, $isTextNode } from 'lexical';
|
|
31
|
+
import { ensureHighlightStyles, TOKEN_TYPES } from '@astryxdesign/core/CodeBlock';
|
|
28
32
|
import { listedLanguage, sameLanguages } from "./codeLanguages.js";
|
|
33
|
+
import { CodeLineTokens } from "./codeLineTokens.js";
|
|
29
34
|
|
|
30
35
|
/** The class the theme adds to every code block (editorTheme.ts). */
|
|
31
36
|
export const CODE_SYNTAX_CLASS = 'astryx-richtext-code';
|
|
@@ -49,33 +54,140 @@ function supportsHighlights() {
|
|
|
49
54
|
return typeof CSS !== 'undefined' && 'highlights' in CSS && typeof Highlight !== 'undefined';
|
|
50
55
|
}
|
|
51
56
|
|
|
57
|
+
/** A colored token: its range, and the highlight that holds it. */
|
|
58
|
+
|
|
52
59
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
60
|
+
* A colored code block, with what an edit needs to recolor part of it.
|
|
61
|
+
* Offsets in the rendered text are offsets in the block's DOM text nodes
|
|
62
|
+
* laid end to end, which is the code itself when its line breaks render as
|
|
63
|
+
* text, and the code without them when each renders as an element (`flat`).
|
|
57
64
|
*/
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
65
|
+
|
|
66
|
+
function highlightFor(type) {
|
|
67
|
+
const name = `astryx-${type}`;
|
|
68
|
+
let highlight = CSS.highlights.get(name);
|
|
69
|
+
if (highlight == null) {
|
|
70
|
+
highlight = new Highlight();
|
|
71
|
+
CSS.highlights.set(name, highlight);
|
|
62
72
|
}
|
|
63
|
-
|
|
73
|
+
return highlight;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The DOM position of `offset` in a block's rendered text: in the text node
|
|
78
|
+
* that holds the character there when `inside`, else in the first text node
|
|
79
|
+
* that reaches it. Null past the end of the text.
|
|
80
|
+
*/
|
|
81
|
+
function pointAt(block, offset, inside) {
|
|
82
|
+
const {
|
|
83
|
+
texts,
|
|
84
|
+
starts,
|
|
85
|
+
length
|
|
86
|
+
} = block;
|
|
87
|
+
if (texts.length === 0 || offset > length) {
|
|
64
88
|
return null;
|
|
65
89
|
}
|
|
66
|
-
|
|
67
|
-
let
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
90
|
+
let low = 0;
|
|
91
|
+
let high = texts.length - 1;
|
|
92
|
+
while (low < high) {
|
|
93
|
+
const mid = low + high >>> 1;
|
|
94
|
+
const end = mid + 1 < texts.length ? starts[mid + 1] : length;
|
|
95
|
+
if (inside ? end > offset : end >= offset) {
|
|
96
|
+
high = mid;
|
|
97
|
+
} else {
|
|
98
|
+
low = mid + 1;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return [texts[low], offset - starts[low]];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Colors one line's tokens. */
|
|
105
|
+
function paintLine(block, line) {
|
|
106
|
+
// A flat block's rendered text has no line breaks before this line.
|
|
107
|
+
const base = block.lines.starts[line] - (block.flat ? line : 0);
|
|
108
|
+
const painted = [];
|
|
109
|
+
for (const token of block.lines.tokens[line] ?? []) {
|
|
110
|
+
const start = base + token.start;
|
|
111
|
+
const end = base + token.end;
|
|
112
|
+
const from = pointAt(block, start, start < end);
|
|
113
|
+
const to = pointAt(block, end, false);
|
|
114
|
+
if (from == null || to == null) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const highlight = highlightFor(token.type);
|
|
118
|
+
try {
|
|
119
|
+
const range = new Range();
|
|
120
|
+
range.setStart(...from);
|
|
121
|
+
range.setEnd(...to);
|
|
122
|
+
highlight.add(range);
|
|
123
|
+
painted.push({
|
|
124
|
+
range,
|
|
125
|
+
highlight
|
|
126
|
+
});
|
|
127
|
+
} catch {
|
|
128
|
+
// A range the DOM refuses colors nothing.
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
block.ranges[line] = painted;
|
|
132
|
+
}
|
|
133
|
+
function unpaintLine(block, line) {
|
|
134
|
+
for (const {
|
|
135
|
+
range,
|
|
136
|
+
highlight
|
|
137
|
+
} of block.ranges[line] ?? []) {
|
|
138
|
+
highlight.delete(range);
|
|
139
|
+
}
|
|
140
|
+
block.ranges[line] = [];
|
|
141
|
+
}
|
|
142
|
+
function unpaintAll(block) {
|
|
143
|
+
for (let line = 0; line < block.ranges.length; line++) {
|
|
144
|
+
unpaintLine(block, line);
|
|
145
|
+
}
|
|
146
|
+
block.ranges = [];
|
|
147
|
+
}
|
|
148
|
+
function repaintAll(block) {
|
|
149
|
+
unpaintAll(block);
|
|
150
|
+
for (let line = 0; line < block.lines.tokens.length; line++) {
|
|
151
|
+
paintLine(block, line);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Lists a block's DOM text nodes in order with their offsets, and the
|
|
157
|
+
* Lexical text nodes that render as exactly one of them.
|
|
158
|
+
*/
|
|
159
|
+
function $indexText(editor, node, block) {
|
|
160
|
+
const walker = document.createTreeWalker(block.element, NodeFilter.SHOW_TEXT);
|
|
161
|
+
while (walker.nextNode() != null) {
|
|
162
|
+
const text = walker.currentNode;
|
|
163
|
+
block.texts.push(text);
|
|
164
|
+
block.starts.push(block.length);
|
|
165
|
+
block.codeStarts.push(NaN);
|
|
166
|
+
block.length += text.length;
|
|
167
|
+
}
|
|
168
|
+
const indexes = new Map(block.texts.map((text, index) => [text, index]));
|
|
169
|
+
let codeStart = 0;
|
|
170
|
+
for (const child of node.getChildren()) {
|
|
171
|
+
const dom = $isTextNode(child) ? editor.getElementByKey(child.getKey()) : null;
|
|
172
|
+
const index = dom?.childNodes.length === 1 ? indexes.get(dom.firstChild) : undefined;
|
|
173
|
+
if (index != null) {
|
|
174
|
+
block.leaves.set(child.getKey(), {
|
|
175
|
+
index,
|
|
176
|
+
text: child.getTextContent()
|
|
74
177
|
});
|
|
178
|
+
block.codeStarts[index] = codeStart;
|
|
75
179
|
}
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
|
|
180
|
+
codeStart += child.getTextContentSize();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** The key of the code block that holds the node `key`, if any. */
|
|
185
|
+
function $codeBlockOf(key) {
|
|
186
|
+
let node = $getNodeByKey(key)?.getParent() ?? null;
|
|
187
|
+
while (node != null && !$isCodeNode(node)) {
|
|
188
|
+
node = node.getParent();
|
|
189
|
+
}
|
|
190
|
+
return node?.getKey() ?? null;
|
|
79
191
|
}
|
|
80
192
|
export function CodeSyntaxPlugin({
|
|
81
193
|
codeLanguages
|
|
@@ -90,70 +202,187 @@ export function CodeSyntaxPlugin({
|
|
|
90
202
|
}
|
|
91
203
|
|
|
92
204
|
// Synchronizes the editor's code blocks with the CSS highlight registry:
|
|
93
|
-
// Lexical
|
|
205
|
+
// Lexical mutation and update listeners colour each block, and the cleanup
|
|
94
206
|
// removes the listeners and every highlight range they added.
|
|
95
207
|
useEffect(() => {
|
|
96
208
|
if (!supportsHighlights()) {
|
|
97
209
|
return undefined;
|
|
98
210
|
}
|
|
99
211
|
ensureRules();
|
|
100
|
-
// Every code block in the editor,
|
|
212
|
+
// Every code block in the editor, the ones colored, and the ones colored
|
|
213
|
+
// in full during the current update.
|
|
101
214
|
const blocks = new Set();
|
|
102
215
|
const colored = new Map();
|
|
216
|
+
const repainted = new Set();
|
|
103
217
|
const uncolor = key => {
|
|
104
|
-
colored.get(key)
|
|
105
|
-
|
|
218
|
+
const block = colored.get(key);
|
|
219
|
+
if (block != null) {
|
|
220
|
+
unpaintAll(block);
|
|
221
|
+
colored.delete(key);
|
|
222
|
+
}
|
|
106
223
|
};
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
code: node.getTextContent(),
|
|
112
|
-
language: node.getLanguage() ?? ''
|
|
113
|
-
} : null;
|
|
114
|
-
});
|
|
224
|
+
// Colors a block in full, unless `force` is false and its element,
|
|
225
|
+
// language, and code are as last colored.
|
|
226
|
+
const $color = (key, force) => {
|
|
227
|
+
const node = $getNodeByKey(key);
|
|
115
228
|
const element = editor.getElementByKey(key);
|
|
116
|
-
if (
|
|
229
|
+
if (!$isCodeNode(node) || element == null) {
|
|
117
230
|
uncolor(key);
|
|
118
231
|
return;
|
|
119
232
|
}
|
|
233
|
+
const code = node.getTextContent();
|
|
234
|
+
const language = node.getLanguage() ?? '';
|
|
120
235
|
const previous = colored.get(key);
|
|
121
|
-
if (previous != null && previous.element === element && previous.
|
|
236
|
+
if (!force && previous != null && previous.element === element && previous.language === language && previous.lines.text === code) {
|
|
122
237
|
return;
|
|
123
238
|
}
|
|
124
|
-
|
|
125
|
-
const
|
|
126
|
-
if (
|
|
127
|
-
// Not rendered yet; the next
|
|
128
|
-
colored.delete(key);
|
|
239
|
+
uncolor(key);
|
|
240
|
+
const rendered = element.textContent ?? '';
|
|
241
|
+
if (rendered !== code && rendered !== code.replaceAll('\n', '')) {
|
|
242
|
+
// Not rendered yet; the block's next change tries again.
|
|
129
243
|
return;
|
|
130
244
|
}
|
|
131
|
-
|
|
245
|
+
ensureHighlightStyles();
|
|
246
|
+
const block = {
|
|
132
247
|
element,
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
248
|
+
language,
|
|
249
|
+
lines: new CodeLineTokens(code, listedLanguage(languages, language) ?? language),
|
|
250
|
+
flat: rendered !== code,
|
|
251
|
+
texts: [],
|
|
252
|
+
starts: [],
|
|
253
|
+
codeStarts: [],
|
|
254
|
+
length: 0,
|
|
255
|
+
leaves: new Map(),
|
|
256
|
+
ranges: []
|
|
257
|
+
};
|
|
258
|
+
$indexText(editor, node, block);
|
|
259
|
+
repaintAll(block);
|
|
260
|
+
colored.set(key, block);
|
|
261
|
+
repainted.add(key);
|
|
262
|
+
};
|
|
263
|
+
// Recolors a block after an update that rewrote the text of its leaf
|
|
264
|
+
// `leafKey` and changed nothing else in it. False when the block must
|
|
265
|
+
// be colored in full instead.
|
|
266
|
+
const $recolorLeaf = (key, leafKey) => {
|
|
267
|
+
const block = colored.get(key);
|
|
268
|
+
const leaf = block?.leaves.get(leafKey);
|
|
269
|
+
const node = $getNodeByKey(leafKey);
|
|
270
|
+
const parent = node?.getParent();
|
|
271
|
+
if (block == null || leaf == null || !$isTextNode(node) || !$isCodeNode(parent) || parent.getKey() !== key || (parent.getLanguage() ?? '') !== block.language || editor.getElementByKey(key) !== block.element) {
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
const text = block.texts[leaf.index];
|
|
275
|
+
const dom = editor.getElementByKey(leafKey);
|
|
276
|
+
const after = node.getTextContent();
|
|
277
|
+
if (dom?.childNodes.length !== 1 || dom.firstChild !== text || text.data !== after) {
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
const before = leaf.text;
|
|
281
|
+
if (after === before) {
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
let first = 0;
|
|
285
|
+
while (first < before.length && first < after.length && before[first] === after[first]) {
|
|
286
|
+
first++;
|
|
287
|
+
}
|
|
288
|
+
let beforeEnd = before.length;
|
|
289
|
+
let afterEnd = after.length;
|
|
290
|
+
while (beforeEnd > first && afterEnd > first && before[beforeEnd - 1] === after[afterEnd - 1]) {
|
|
291
|
+
beforeEnd--;
|
|
292
|
+
afterEnd--;
|
|
293
|
+
}
|
|
294
|
+
const inserted = after.slice(first, afterEnd);
|
|
295
|
+
// A flat block's text holds no line breaks; one typed in would no
|
|
296
|
+
// longer lay the code over its rendered text.
|
|
297
|
+
if (block.flat && inserted.includes('\n')) {
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
const leafStart = block.codeStarts[leaf.index];
|
|
301
|
+
const lines = block.lines.replace(leafStart + first, beforeEnd - first, inserted);
|
|
302
|
+
const delta = after.length - before.length;
|
|
303
|
+
for (let i = leaf.index + 1; i < block.texts.length; i++) {
|
|
304
|
+
block.starts[i] += delta;
|
|
305
|
+
block.codeStarts[i] += delta;
|
|
306
|
+
}
|
|
307
|
+
block.length += delta;
|
|
308
|
+
leaf.text = after;
|
|
309
|
+
if (lines == null) {
|
|
310
|
+
repaintAll(block);
|
|
311
|
+
return true;
|
|
312
|
+
}
|
|
313
|
+
// Lexical rewrites a changed DOM text node whole, which leaves every
|
|
314
|
+
// range in it stale: recolor each line the node holds, and any other
|
|
315
|
+
// line the edit re-tokenized. Every other range sits in a text node of
|
|
316
|
+
// its own line and is still right.
|
|
317
|
+
const recolor = new Set(lines);
|
|
318
|
+
const last = block.lines.lineAt(leafStart + Math.max(after.length - 1, 0));
|
|
319
|
+
for (let i = block.lines.lineAt(leafStart); i <= last; i++) {
|
|
320
|
+
recolor.add(i);
|
|
321
|
+
}
|
|
322
|
+
for (const i of recolor) {
|
|
323
|
+
unpaintLine(block, i);
|
|
324
|
+
paintLine(block, i);
|
|
325
|
+
}
|
|
326
|
+
return true;
|
|
136
327
|
};
|
|
137
328
|
const unregisterMutations = editor.registerMutationListener(CodeNode, mutations => {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
329
|
+
editor.getEditorState().read(() => {
|
|
330
|
+
for (const [key, mutation] of mutations) {
|
|
331
|
+
if (mutation === 'destroyed') {
|
|
332
|
+
blocks.delete(key);
|
|
333
|
+
uncolor(key);
|
|
334
|
+
} else {
|
|
335
|
+
blocks.add(key);
|
|
336
|
+
$color(key, false);
|
|
337
|
+
}
|
|
145
338
|
}
|
|
146
|
-
}
|
|
339
|
+
});
|
|
147
340
|
}, {
|
|
148
341
|
skipInitialization: false
|
|
149
342
|
});
|
|
150
|
-
|
|
151
|
-
//
|
|
152
|
-
//
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
343
|
+
repainted.clear();
|
|
344
|
+
// Typing in a block changes its text nodes, not the block itself: after
|
|
345
|
+
// each update, recolor the blocks whose text nodes changed. A block whose
|
|
346
|
+
// only change is one leaf's text takes the partial path; any other
|
|
347
|
+
// change colors it in full, unless the block listener just did.
|
|
348
|
+
const unregisterUpdates = editor.registerUpdateListener(({
|
|
349
|
+
editorState,
|
|
350
|
+
mutatedNodes,
|
|
351
|
+
prevEditorState
|
|
352
|
+
}) => {
|
|
353
|
+
if (mutatedNodes != null && blocks.size > 0) {
|
|
354
|
+
const mutations = [];
|
|
355
|
+
for (const nodes of mutatedNodes.values()) {
|
|
356
|
+
for (const mutation of nodes) {
|
|
357
|
+
mutations.push(mutation);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
const before = prevEditorState.read(() => mutations.map(([key, mutation]) => mutation === 'created' ? null : $codeBlockOf(key)));
|
|
361
|
+
editorState.read(() => {
|
|
362
|
+
// Each changed block, with the one leaf whose text an update
|
|
363
|
+
// rewrote, or null when anything else in it changed.
|
|
364
|
+
const changed = new Map();
|
|
365
|
+
const note = (key, leafKey) => {
|
|
366
|
+
if (key != null && blocks.has(key)) {
|
|
367
|
+
changed.set(key, changed.has(key) ? null : leafKey);
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
mutations.forEach(([key, mutation], i) => {
|
|
371
|
+
const after = mutation === 'destroyed' ? null : $codeBlockOf(key);
|
|
372
|
+
const rewritten = mutation === 'updated' && after === before[i] && $isTextNode($getNodeByKey(key));
|
|
373
|
+
note(after, rewritten ? key : null);
|
|
374
|
+
if (before[i] !== after) {
|
|
375
|
+
note(before[i], null);
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
for (const [key, leafKey] of changed) {
|
|
379
|
+
if (!repainted.has(key) && (leafKey == null || !$recolorLeaf(key, leafKey))) {
|
|
380
|
+
$color(key, true);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
});
|
|
156
384
|
}
|
|
385
|
+
repainted.clear();
|
|
157
386
|
});
|
|
158
387
|
return () => {
|
|
159
388
|
unregisterMutations();
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file codeLineTokens.ts
|
|
3
|
+
* @input Core CodeBlock's tokenizer (`tokenize`) and token line type.
|
|
4
|
+
* @output Exports CodeLineTokens, which keeps core CodeBlock's tokens for a
|
|
5
|
+
* block of code line by line and re-tokenizes only the lines an edit can
|
|
6
|
+
* change.
|
|
7
|
+
* @position Used by CodeSyntaxPlugin. Its tokens always equal what `tokenize`
|
|
8
|
+
* returns for the whole code. An edit re-tokenizes only its own line, and
|
|
9
|
+
* at most the line above it, for the built-in TypeScript, JavaScript,
|
|
10
|
+
* Python, and shell grammars while every line passes `standsAlone`;
|
|
11
|
+
* anything else re-tokenizes all of it. The checks follow those grammars'
|
|
12
|
+
* patterns in core CodeBlock's tokenizer.ts, and codeLineTokens.test.ts
|
|
13
|
+
* compares edits against a full `tokenize`, so a grammar change that
|
|
14
|
+
* breaks them fails there.
|
|
15
|
+
*/
|
|
16
|
+
import { type CodeLanguage, type TokenLine } from '@astryxdesign/core/CodeBlock';
|
|
17
|
+
/**
|
|
18
|
+
* Core CodeBlock's tokens for a block of code, kept line by line with
|
|
19
|
+
* line-relative offsets, exactly as `tokenize` returns them for the whole
|
|
20
|
+
* code.
|
|
21
|
+
*/
|
|
22
|
+
export declare class CodeLineTokens {
|
|
23
|
+
readonly language: string | CodeLanguage;
|
|
24
|
+
/** Where each line starts in the code. */
|
|
25
|
+
starts: number[];
|
|
26
|
+
/** Each line's tokens. */
|
|
27
|
+
tokens: TokenLine[];
|
|
28
|
+
private lines;
|
|
29
|
+
private readonly family;
|
|
30
|
+
/** Whether every line stands alone, so an edit re-tokenizes lines alone. */
|
|
31
|
+
private linesStandAlone;
|
|
32
|
+
constructor(text: string, language: string | CodeLanguage);
|
|
33
|
+
/** The code. */
|
|
34
|
+
get text(): string;
|
|
35
|
+
/** The line that holds `offset` in the code. */
|
|
36
|
+
lineAt(offset: number): number;
|
|
37
|
+
/**
|
|
38
|
+
* Replaces `removed` characters at `offset` in the code with `inserted`.
|
|
39
|
+
* Returns the lines it re-tokenized when the edit stays inside one line
|
|
40
|
+
* and every line still stands alone; otherwise it re-tokenizes all the
|
|
41
|
+
* code and returns null.
|
|
42
|
+
*/
|
|
43
|
+
replace(offset: number, removed: number, inserted: string): number[] | null;
|
|
44
|
+
/**
|
|
45
|
+
* A line's tokens as `tokenize` finds them in the whole code. A function
|
|
46
|
+
* name is a name followed by `(` past any whitespace, line breaks
|
|
47
|
+
* included, so when the line ends in a name, the code up to the next
|
|
48
|
+
* visible character is tokenized with it.
|
|
49
|
+
*/
|
|
50
|
+
private tokenizeLine;
|
|
51
|
+
private tokenizeAll;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=codeLineTokens.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codeLineTokens.d.ts","sourceRoot":"","sources":["../src/codeLineTokens.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,SAAS,EACf,MAAM,8BAA8B,CAAC;AAkFtC;;;;GAIG;AACH,qBAAa,cAAc;IACzB,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,YAAY,CAAC;IACzC,0CAA0C;IAC1C,MAAM,EAAE,MAAM,EAAE,CAAM;IACtB,0BAA0B;IAC1B,MAAM,EAAE,SAAS,EAAE,CAAM;IACzB,OAAO,CAAC,KAAK,CAAgB;IAC7B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgB;IACvC,4EAA4E;IAC5E,OAAO,CAAC,eAAe,CAAS;gBAEpB,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,YAAY;IAOzD,gBAAgB;IAChB,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,gDAAgD;IAChD,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IAc9B;;;;;OAKG;IACH,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI;IA+C3E;;;;;OAKG;IACH,OAAO,CAAC,YAAY;IAkBpB,OAAO,CAAC,WAAW;CAgBpB"}
|