svelte-highlight 7.21.1 → 7.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AnsiOutput.svelte +6 -30
- package/CodeWindow.svelte +35 -11
- package/CodeWindow.svelte.d.ts +7 -1
- package/FileTabs.svelte +3 -1
- package/HighlightEditable.svelte +115 -143
- package/HighlightStream.svelte +44 -48
- package/HighlightStyle.svelte +9 -21
- package/HighlightVirtual.svelte +82 -4
- package/HighlightVirtual.svelte.d.ts +19 -2
- package/LangTag.svelte +4 -0
- package/LineNumbers.svelte +1 -0
- package/MarkdownStream.svelte +209 -0
- package/MarkdownStream.svelte.d.ts +100 -0
- package/README.md +183 -9
- package/ansi-color.js +109 -22
- package/ansi-output.js +93 -0
- package/ansi.d.ts +18 -0
- package/ansi.js +85 -24
- package/css-walk.js +9 -12
- package/editable-css-paint.js +118 -0
- package/editable-dom-paint.js +217 -146
- package/editable-line-dom.js +241 -0
- package/engine.d.ts +28 -6
- package/engine.js +707 -144
- package/fence.d.ts +5 -5
- package/fence.js +196 -56
- package/highlight-theme.js +10 -4
- package/incremental-tokenize.js +95 -17
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/languages/aliases.js +1 -1
- package/languages/all.js +23 -1
- package/languages/alloy.d.ts +2 -0
- package/languages/alloy.js +2 -0
- package/languages/asymptote.d.ts +2 -0
- package/languages/asymptote.js +2 -0
- package/languages/blockdiag.d.ts +2 -0
- package/languages/blockdiag.js +2 -0
- package/languages/bsv.d.ts +2 -0
- package/languages/bsv.js +2 -0
- package/languages/casbin.d.ts +2 -0
- package/languages/casbin.js +2 -0
- package/languages/choicescript.d.ts +2 -0
- package/languages/choicescript.js +2 -0
- package/languages/dafny.d.ts +2 -0
- package/languages/dafny.js +2 -0
- package/languages/dbml.d.ts +2 -0
- package/languages/dbml.js +2 -0
- package/languages/dialog.d.ts +2 -0
- package/languages/dialog.js +2 -0
- package/languages/fstar.d.ts +2 -0
- package/languages/fstar.js +2 -0
- package/languages/gnuplot.d.ts +2 -0
- package/languages/gnuplot.js +2 -0
- package/languages/harlowe.d.ts +2 -0
- package/languages/harlowe.js +2 -0
- package/languages/index.d.ts +44 -0
- package/languages/index.js +22 -0
- package/languages/likec4.d.ts +2 -0
- package/languages/likec4.js +2 -0
- package/languages/nextflow.d.ts +2 -0
- package/languages/nextflow.js +2 -0
- package/languages/nomnoml.d.ts +2 -0
- package/languages/nomnoml.js +2 -0
- package/languages/openfga.d.ts +2 -0
- package/languages/openfga.js +2 -0
- package/languages/pikchr.d.ts +2 -0
- package/languages/pikchr.js +2 -0
- package/languages/spade.d.ts +2 -0
- package/languages/spade.js +2 -0
- package/languages/spicedb.d.ts +2 -0
- package/languages/spicedb.js +2 -0
- package/languages/tlaplus.d.ts +2 -0
- package/languages/tlaplus.js +2 -0
- package/languages/veryl.d.ts +2 -0
- package/languages/veryl.js +2 -0
- package/languages/wdl.d.ts +2 -0
- package/languages/wdl.js +2 -0
- package/package.json +1 -1
- package/preprocessor.js +16 -2
- package/scoped.js +15 -13
- package/search.d.ts +6 -4
- package/search.js +427 -85
- package/stream-final-highlight.js +52 -0
- package/stream-highlighted.js +17 -0
- package/stream-preview.js +7 -2
- package/stream-regenerate.js +262 -0
- package/style-registry.js +60 -0
- package/text-diff.js +66 -18
- package/tokenized-document.d.ts +8 -0
- package/tokenized-document.js +60 -15
- package/worker.js +14 -2
package/AnsiOutput.svelte
CHANGED
|
@@ -14,37 +14,13 @@
|
|
|
14
14
|
*/
|
|
15
15
|
export let wrap = false;
|
|
16
16
|
|
|
17
|
-
import {
|
|
18
|
-
import { classNames, inlineStyle } from "./ansi-color.js";
|
|
17
|
+
import { createAnsiOutput } from "./ansi-output.js";
|
|
19
18
|
|
|
20
|
-
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
// Re-parsing the whole string on every change is O(n^2) over a growing
|
|
27
|
-
// (live-tailed or streamed) `text`, so only feed the new suffix when
|
|
28
|
-
// `text` grows by a pure append; anything else gets a fresh session.
|
|
29
|
-
$: {
|
|
30
|
-
if (text.startsWith(fedText)) {
|
|
31
|
-
if (text.length > fedText.length) {
|
|
32
|
-
session.append(text.slice(fedText.length));
|
|
33
|
-
fedText = text;
|
|
34
|
-
}
|
|
35
|
-
} else {
|
|
36
|
-
session = createAnsiSession();
|
|
37
|
-
session.append(text);
|
|
38
|
-
fedText = text;
|
|
39
|
-
}
|
|
40
|
-
parsed = session.segments();
|
|
41
|
-
}
|
|
42
|
-
$: segments = parsed.map((segment) => ({
|
|
43
|
-
text: segment.text,
|
|
44
|
-
class: classNames(segment),
|
|
45
|
-
style: inlineStyle(segment, autoContrast),
|
|
46
|
-
link: segment.link,
|
|
47
|
-
}));
|
|
19
|
+
const output = createAnsiOutput();
|
|
20
|
+
// `update()` hands back the same array, patched in place. Reassigning it
|
|
21
|
+
// still invalidates `segments`: legacy-mode equality treats any object
|
|
22
|
+
// as changed.
|
|
23
|
+
$: segments = output.update(text, autoContrast);
|
|
48
24
|
</script>
|
|
49
25
|
|
|
50
26
|
<pre class="ansi" class:wrap {...$$restProps}><code
|
package/CodeWindow.svelte
CHANGED
|
@@ -9,17 +9,29 @@
|
|
|
9
9
|
<div class="code-window {variant}" {...$$restProps}>
|
|
10
10
|
<div class="titlebar">
|
|
11
11
|
{#if variant === "macos"}
|
|
12
|
-
<span class="
|
|
13
|
-
<span class="
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
<span class="titlebar-lead">
|
|
13
|
+
<span class="dots" aria-hidden="true">
|
|
14
|
+
<span class="dot close"></span>
|
|
15
|
+
<span class="dot minimize"></span>
|
|
16
|
+
<span class="dot maximize"></span>
|
|
17
|
+
</span>
|
|
16
18
|
</span>
|
|
17
19
|
{:else if variant === "terminal"}
|
|
18
|
-
<span class="
|
|
20
|
+
<span class="titlebar-lead">
|
|
21
|
+
<span class="prompt" aria-hidden="true">>_</span>
|
|
22
|
+
</span>
|
|
19
23
|
{/if}
|
|
20
24
|
|
|
21
25
|
{#if title}
|
|
22
|
-
<span class="title">{title}</span>
|
|
26
|
+
<span class="title" {title}>{title}</span>
|
|
27
|
+
{/if}
|
|
28
|
+
|
|
29
|
+
{#if variant !== "plain"}
|
|
30
|
+
<!-- titlebar content wider than the leading chrome will unbalance the
|
|
31
|
+
centering above -- an accepted trade-off, not a bug. -->
|
|
32
|
+
<span class="titlebar-end"><slot name="titlebar" /></span>
|
|
33
|
+
{:else}
|
|
34
|
+
<slot name="titlebar" />
|
|
23
35
|
{/if}
|
|
24
36
|
</div>
|
|
25
37
|
|
|
@@ -84,12 +96,24 @@
|
|
|
84
96
|
color: var(--prompt-color, inherit);
|
|
85
97
|
}
|
|
86
98
|
|
|
99
|
+
.titlebar-lead,
|
|
100
|
+
.titlebar-end {
|
|
101
|
+
display: flex;
|
|
102
|
+
align-items: center;
|
|
103
|
+
flex: 1 1 0;
|
|
104
|
+
min-width: 0;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
.titlebar-end {
|
|
108
|
+
justify-content: flex-end;
|
|
109
|
+
}
|
|
110
|
+
|
|
87
111
|
.title {
|
|
88
|
-
/*
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
112
|
+
/* Sandwiched between equal-width .titlebar-lead/.titlebar-end, so it
|
|
113
|
+
centers in the space actually left over instead of the full bar. */
|
|
114
|
+
flex: 1 1 0;
|
|
115
|
+
min-width: 0;
|
|
116
|
+
text-align: center;
|
|
93
117
|
overflow: hidden;
|
|
94
118
|
white-space: nowrap;
|
|
95
119
|
text-overflow: ellipsis;
|
package/CodeWindow.svelte.d.ts
CHANGED
|
@@ -9,7 +9,8 @@ export type CodeWindowProps = HTMLAttributes<HTMLDivElement> & {
|
|
|
9
9
|
variant?: "macos" | "terminal" | "plain";
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* Title bar text.
|
|
12
|
+
* Title bar text. Also becomes the title bar's native HTML `title`
|
|
13
|
+
* attribute, so a truncated title is readable as a hover/focus tooltip.
|
|
13
14
|
* @default ""
|
|
14
15
|
*/
|
|
15
16
|
title?: string;
|
|
@@ -127,6 +128,11 @@ export type CodeWindowEvents = {};
|
|
|
127
128
|
|
|
128
129
|
export type CodeWindowSlots = {
|
|
129
130
|
default: {};
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Optional content rendered in the mirrored trailing region of the title bar.
|
|
134
|
+
*/
|
|
135
|
+
titlebar: {};
|
|
130
136
|
};
|
|
131
137
|
|
|
132
138
|
export default class CodeWindow extends SvelteComponentTyped<
|
package/FileTabs.svelte
CHANGED
|
@@ -161,7 +161,9 @@
|
|
|
161
161
|
role="tabpanel"
|
|
162
162
|
id="{baseId}-panel"
|
|
163
163
|
class="tabpanel"
|
|
164
|
-
aria-labelledby={activeIndex === -1
|
|
164
|
+
aria-labelledby={activeIndex === -1
|
|
165
|
+
? undefined
|
|
166
|
+
: `${baseId}-tab-${activeIndex}`}
|
|
165
167
|
>
|
|
166
168
|
<slot {active} />
|
|
167
169
|
</div>
|
package/HighlightEditable.svelte
CHANGED
|
@@ -46,7 +46,15 @@
|
|
|
46
46
|
export let theme = undefined;
|
|
47
47
|
|
|
48
48
|
import { createEventDispatcher, onMount } from "svelte";
|
|
49
|
+
import { lineTokenRanges, retokenizedEnd } from "./editable-css-paint.js";
|
|
49
50
|
import { createDomLinePainter } from "./editable-dom-paint.js";
|
|
51
|
+
import {
|
|
52
|
+
createLineView,
|
|
53
|
+
editorNodeAtOffset,
|
|
54
|
+
lineStartOffset as lineStartIn,
|
|
55
|
+
nodeAtOffset,
|
|
56
|
+
renderLines as renderLineDom,
|
|
57
|
+
} from "./editable-line-dom.js";
|
|
50
58
|
import { toRanges } from "./engine.js";
|
|
51
59
|
import {
|
|
52
60
|
highlightRules,
|
|
@@ -75,13 +83,10 @@
|
|
|
75
83
|
// instead of indenting; cleared by any other keydown or blur.
|
|
76
84
|
let tabTrapReleased = false;
|
|
77
85
|
|
|
78
|
-
// One <span> per line, painted incrementally (see `renderLines`)
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
let lineLengths = [];
|
|
83
|
-
/** @type {string[]} Line HTML currently reflected in `lineEls`. */
|
|
84
|
-
let renderedLines = [];
|
|
86
|
+
// One <span> per line, painted incrementally (see `renderLines`): the
|
|
87
|
+
// line elements, their rendered (plain-text) lengths, and the line HTML
|
|
88
|
+
// they currently reflect.
|
|
89
|
+
const view = createLineView();
|
|
85
90
|
|
|
86
91
|
// Tracks `code` so parent updates vs local edits can be distinguished.
|
|
87
92
|
let internalCode = code;
|
|
@@ -91,9 +96,9 @@
|
|
|
91
96
|
/** @type {import("./incremental-tokenize.js").IncrementalParse | undefined} */
|
|
92
97
|
let incrementalParse;
|
|
93
98
|
|
|
94
|
-
// DOM-engine line HTML:
|
|
95
|
-
//
|
|
96
|
-
const domLinePainter = createDomLinePainter(
|
|
99
|
+
// DOM-engine line HTML: after the first paint, each edit re-renders only
|
|
100
|
+
// the lines whose events changed, not the whole document.
|
|
101
|
+
const domLinePainter = createDomLinePainter();
|
|
97
102
|
|
|
98
103
|
function getEvents() {
|
|
99
104
|
incrementalParse = incrementalParse
|
|
@@ -151,23 +156,24 @@
|
|
|
151
156
|
// Character offset (same coordinate space as lineStartOffset/setSelection)
|
|
152
157
|
// of the boundary (container, offsetInContainer) from a live Selection
|
|
153
158
|
// Range, computed from integers instead of serializing the document.
|
|
154
|
-
// Exploits the fixed editor structure — one <span> per line (lineEls)
|
|
159
|
+
// Exploits the fixed editor structure — one <span> per line (view.lineEls)
|
|
155
160
|
// joined by literal "\n" text nodes — so a boundary's line is derived
|
|
156
161
|
// from its child position rather than a document-wide walk. Returns null
|
|
157
|
-
// whenever that structure doesn't hold (DOM drifted from lineEls/
|
|
158
|
-
// lineLengths ahead of renderLines' self-heal, or an unrecognized node),
|
|
162
|
+
// whenever that structure doesn't hold (DOM drifted from view.lineEls/
|
|
163
|
+
// view.lineLengths ahead of renderLines' self-heal, or an unrecognized node),
|
|
159
164
|
// so the caller can fall back to the exact cloneRange/toString behavior.
|
|
160
165
|
function offsetOfBoundary(container, offsetInContainer) {
|
|
161
|
-
const expectedChildren =
|
|
166
|
+
const expectedChildren =
|
|
167
|
+
view.lineEls.length === 0 ? 0 : view.lineEls.length * 2 - 1;
|
|
162
168
|
if (editor.childNodes.length !== expectedChildren) return null;
|
|
163
169
|
|
|
164
170
|
if (container === editor) {
|
|
165
|
-
if (lineEls.length === 0) return offsetInContainer === 0 ? 0 : null;
|
|
171
|
+
if (view.lineEls.length === 0) return offsetInContainer === 0 ? 0 : null;
|
|
166
172
|
const lineIndex = offsetInContainer >> 1;
|
|
167
|
-
if (lineIndex >= lineEls.length) return null;
|
|
173
|
+
if (lineIndex >= view.lineEls.length) return null;
|
|
168
174
|
return offsetInContainer % 2 === 0
|
|
169
175
|
? lineStartOffset(lineIndex)
|
|
170
|
-
: lineStartOffset(lineIndex) + lineLengths[lineIndex];
|
|
176
|
+
: lineStartOffset(lineIndex) + view.lineLengths[lineIndex];
|
|
171
177
|
}
|
|
172
178
|
|
|
173
179
|
// Walk up to the top-level child of `editor` containing the boundary.
|
|
@@ -186,14 +192,18 @@
|
|
|
186
192
|
if (childIndex % 2 === 1) {
|
|
187
193
|
// The "\n" separator following line `lineIndex`; it has no children,
|
|
188
194
|
// so `container` must be the separator itself.
|
|
189
|
-
if (container !== node || lineIndex >= lineLengths.length)
|
|
195
|
+
if (container !== node || lineIndex >= view.lineLengths.length)
|
|
196
|
+
return null;
|
|
190
197
|
return (
|
|
191
|
-
lineStartOffset(lineIndex) +
|
|
198
|
+
lineStartOffset(lineIndex) +
|
|
199
|
+
view.lineLengths[lineIndex] +
|
|
200
|
+
offsetInContainer
|
|
192
201
|
);
|
|
193
202
|
}
|
|
194
203
|
|
|
195
204
|
const span = node;
|
|
196
|
-
if (lineIndex >= lineEls.length || lineEls[lineIndex] !== span)
|
|
205
|
+
if (lineIndex >= view.lineEls.length || view.lineEls[lineIndex] !== span)
|
|
206
|
+
return null;
|
|
197
207
|
|
|
198
208
|
// Character offset of (container, offsetInContainer) within `span`,
|
|
199
209
|
// walking only its ancestors up to `span` (bounded by that line's size).
|
|
@@ -235,17 +245,6 @@
|
|
|
235
245
|
return sel ? sel.end : null;
|
|
236
246
|
}
|
|
237
247
|
|
|
238
|
-
function nodeAtOffset(offset, root = editor) {
|
|
239
|
-
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, null);
|
|
240
|
-
let count = 0;
|
|
241
|
-
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
242
|
-
const next = count + node.textContent.length;
|
|
243
|
-
if (offset <= next) return { node, offset: offset - count };
|
|
244
|
-
count = next;
|
|
245
|
-
}
|
|
246
|
-
return null;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
248
|
// Firefox's contenteditable undo manager treats a boundary set exactly at
|
|
250
249
|
// a text node's start/end (setStart/setEnd with a text node + offset)
|
|
251
250
|
// differently from the equivalent boundary expressed via the parent's
|
|
@@ -253,18 +252,35 @@
|
|
|
253
252
|
// node, the former leaves Firefox unable to recognize a later native
|
|
254
253
|
// undo (execCommand/Edit menu), which then silently does nothing. Prefer
|
|
255
254
|
// the parent-anchored form whenever the offset lands on a node edge.
|
|
255
|
+
//
|
|
256
|
+
// An offset at the end of a "\n" separator (a direct editor child) is the
|
|
257
|
+
// start of the next line, so anchor it inside that line's <span> instead.
|
|
258
|
+
// After the separator but outside the span, Firefox types into a new
|
|
259
|
+
// stray text node, which renderLines' child-count check then repairs with
|
|
260
|
+
// a full rebuild of every line.
|
|
256
261
|
function setRangeBoundary(range, side, node, offset) {
|
|
257
|
-
|
|
262
|
+
const nextLine = node.nextSibling;
|
|
263
|
+
if (
|
|
264
|
+
node.parentNode === editor &&
|
|
265
|
+
offset === node.textContent.length &&
|
|
266
|
+
nextLine?.nodeType === Node.ELEMENT_NODE
|
|
267
|
+
) {
|
|
268
|
+
range[side === "Start" ? "setStart" : "setEnd"](nextLine, 0);
|
|
269
|
+
} else if (offset === 0) range[`set${side}Before`](node);
|
|
258
270
|
else if (offset === node.textContent.length) range[`set${side}After`](node);
|
|
259
271
|
else range[side === "Start" ? "setStart" : "setEnd"](node, offset);
|
|
260
272
|
}
|
|
261
273
|
|
|
262
274
|
function setSelection(start, end, root = editor) {
|
|
263
|
-
const
|
|
275
|
+
const locate = (offset) =>
|
|
276
|
+
root === editor
|
|
277
|
+
? editorNodeAtOffset(editor, view, offset)
|
|
278
|
+
: nodeAtOffset(root, offset);
|
|
279
|
+
const from = locate(start);
|
|
264
280
|
if (!from) return;
|
|
265
281
|
const range = document.createRange();
|
|
266
282
|
setRangeBoundary(range, "Start", from.node, from.offset);
|
|
267
|
-
const to = end != null && end !== start ?
|
|
283
|
+
const to = end != null && end !== start ? locate(end) : null;
|
|
268
284
|
if (to) setRangeBoundary(range, "End", to.node, to.offset);
|
|
269
285
|
else range.collapse(true);
|
|
270
286
|
const selection = window.getSelection();
|
|
@@ -275,9 +291,7 @@
|
|
|
275
291
|
// Character offset (in the same space as getSelectionRange/setSelection)
|
|
276
292
|
// where line `index` starts.
|
|
277
293
|
function lineStartOffset(index) {
|
|
278
|
-
|
|
279
|
-
for (let i = 0; i < index; i++) start += lineLengths[i];
|
|
280
|
-
return start;
|
|
294
|
+
return lineStartIn(view, index);
|
|
281
295
|
}
|
|
282
296
|
|
|
283
297
|
const setHtml = (el, line) => {
|
|
@@ -287,66 +301,33 @@
|
|
|
287
301
|
el.textContent = line;
|
|
288
302
|
};
|
|
289
303
|
|
|
290
|
-
// Patches `editor` to match `lines
|
|
291
|
-
//
|
|
292
|
-
//
|
|
293
|
-
// `setContent`: innerHTML for the "dom" engine, textContent for
|
|
294
|
-
// "css-highlights"). Returns the index of the single changed line when
|
|
295
|
-
// nothing else shifted (used to scope caret restoration), or null.
|
|
304
|
+
// Patches `editor` to match `lines`; see editable-line-dom.js. Returns
|
|
305
|
+
// the index of the single changed line when nothing else shifted (used to
|
|
306
|
+
// scope caret restoration), or null.
|
|
296
307
|
function renderLines(lines, setContent) {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
let changedIndex = null;
|
|
315
|
-
let changedCount = 0;
|
|
316
|
-
for (let i = 0; i < commonLen; i++) {
|
|
317
|
-
if (renderedLines[i] === lines[i]) continue;
|
|
318
|
-
setContent(lineEls[i], lines[i]);
|
|
319
|
-
lineLengths[i] = lineEls[i].textContent.length;
|
|
320
|
-
changedIndex = i;
|
|
321
|
-
changedCount++;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
for (let i = prevLen; i < newLen; i++) {
|
|
325
|
-
if (lineEls.length > 0) editor.appendChild(document.createTextNode("\n"));
|
|
326
|
-
const span = document.createElement("span");
|
|
327
|
-
setContent(span, lines[i]);
|
|
328
|
-
editor.appendChild(span);
|
|
329
|
-
lineEls.push(span);
|
|
330
|
-
lineLengths.push(span.textContent.length);
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
while (lineEls.length > newLen) {
|
|
334
|
-
editor.removeChild(lineEls.pop());
|
|
335
|
-
lineLengths.pop();
|
|
336
|
-
clearLineHighlights(lineHighlightRanges.length - 1);
|
|
337
|
-
lineHighlightRanges.pop();
|
|
338
|
-
// Drop the separator that used to precede the removed line.
|
|
339
|
-
if (lineEls.length > 0) editor.removeChild(editor.lastChild);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
renderedLines = lines;
|
|
343
|
-
return prevLen === newLen && changedCount === 1 ? changedIndex : null;
|
|
344
|
-
}
|
|
308
|
+
return renderLineDom(editor, view, lines, setContent, lineHooks);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const lineHooks = {
|
|
312
|
+
onReset: () => clearCssHighlights(),
|
|
313
|
+
// Keeps css-highlights' per-line ranges aligned with the line elements
|
|
314
|
+
// (empty under the "dom" engine).
|
|
315
|
+
onSplice: (index, removed, inserted) => {
|
|
316
|
+
if (index >= lineHighlightRanges.length) return;
|
|
317
|
+
for (let i = index; i < index + removed; i++) clearLineHighlights(i);
|
|
318
|
+
lineHighlightRanges = lineHighlightRanges.slice(0, index).concat(
|
|
319
|
+
Array.from({ length: inserted }, () => []),
|
|
320
|
+
lineHighlightRanges.slice(index + removed),
|
|
321
|
+
);
|
|
322
|
+
},
|
|
323
|
+
};
|
|
345
324
|
|
|
346
325
|
/** @type {Map<string, InstanceType<typeof Highlight>>} scope -> registered Highlight. */
|
|
347
326
|
let cssHighlights = new Map();
|
|
348
327
|
/** @type {{ scope: string; range: Range }[][]} Ranges registered per line. */
|
|
349
328
|
let lineHighlightRanges = [];
|
|
329
|
+
/** @type {import("./engine.d.ts").ScopeEvent[] | undefined} The events the registered ranges reflect. */
|
|
330
|
+
let cssPaintedEvents;
|
|
350
331
|
|
|
351
332
|
function cssHighlightFor(scope) {
|
|
352
333
|
let highlight = cssHighlights.get(scope);
|
|
@@ -372,60 +353,28 @@
|
|
|
372
353
|
}
|
|
373
354
|
cssHighlights = new Map();
|
|
374
355
|
lineHighlightRanges = [];
|
|
356
|
+
cssPaintedEvents = undefined;
|
|
375
357
|
}
|
|
376
358
|
|
|
377
|
-
// Rebuilds
|
|
378
|
-
//
|
|
379
|
-
//
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
const textNode = lineEls[index].firstChild;
|
|
383
|
-
const next = [];
|
|
384
|
-
if (textNode) {
|
|
385
|
-
const lineStart = lineStartOffset(index);
|
|
386
|
-
const lineEnd = lineStart + lineLengths[index];
|
|
387
|
-
// `tokenRanges` is sorted by, and disjoint on, `start` (see the
|
|
388
|
-
// comment on `paintAllLineHighlights`), so `end` is monotonically
|
|
389
|
-
// increasing too. Binary-search the first range that can possibly
|
|
390
|
-
// intersect the line instead of scanning the whole document.
|
|
391
|
-
let lo = 0;
|
|
392
|
-
let hi = tokenRanges.length;
|
|
393
|
-
while (lo < hi) {
|
|
394
|
-
const mid = (lo + hi) >>> 1;
|
|
395
|
-
if (tokenRanges[mid].end <= lineStart) lo = mid + 1;
|
|
396
|
-
else hi = mid;
|
|
397
|
-
}
|
|
398
|
-
for (
|
|
399
|
-
let j = lo;
|
|
400
|
-
j < tokenRanges.length && tokenRanges[j].start < lineEnd;
|
|
401
|
-
j++
|
|
402
|
-
) {
|
|
403
|
-
const token = tokenRanges[j];
|
|
404
|
-
const start = Math.max(token.start, lineStart) - lineStart;
|
|
405
|
-
const end = Math.min(token.end, lineEnd) - lineStart;
|
|
406
|
-
if (start === end) continue;
|
|
407
|
-
const range = new Range();
|
|
408
|
-
range.setStart(textNode, start);
|
|
409
|
-
range.setEnd(textNode, end);
|
|
410
|
-
cssHighlightFor(token.scope).add(range);
|
|
411
|
-
next.push({ scope: token.scope, range });
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
lineHighlightRanges[index] = next;
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
// Repaints every line from `tokenRanges` in a single forward sweep rather
|
|
418
|
-
// than rescanning the full (document-sized) range array per line: since
|
|
359
|
+
// Rebuilds the Highlight Range registrations of the lines from `first`
|
|
360
|
+
// (starting at document offset `base`) through the one ending at offset
|
|
361
|
+
// `end`, from `tokenRanges` (relative to `base`); the lines' text nodes
|
|
362
|
+
// are untouched by this, so it never disturbs the caret. One forward
|
|
363
|
+
// sweep rather than rescanning the range array per line: since
|
|
419
364
|
// `toRanges` emits ranges sorted by, and disjoint on, document offset,
|
|
420
365
|
// `tokenIndex` only ever advances, so total work is O(lines + tokens)
|
|
421
366
|
// instead of O(lines * tokens).
|
|
422
|
-
function
|
|
367
|
+
function paintLineHighlights(first, base, end, tokenRanges) {
|
|
423
368
|
let tokenIndex = 0;
|
|
424
369
|
let lineStart = 0;
|
|
425
|
-
for (
|
|
370
|
+
for (
|
|
371
|
+
let i = first;
|
|
372
|
+
i < view.lineEls.length && base + lineStart <= end;
|
|
373
|
+
i++
|
|
374
|
+
) {
|
|
426
375
|
clearLineHighlights(i);
|
|
427
|
-
const lineEnd = lineStart + lineLengths[i];
|
|
428
|
-
const textNode = lineEls[i].firstChild;
|
|
376
|
+
const lineEnd = lineStart + view.lineLengths[i];
|
|
377
|
+
const textNode = view.lineEls[i].firstChild;
|
|
429
378
|
const next = [];
|
|
430
379
|
if (textNode) {
|
|
431
380
|
while (
|
|
@@ -458,12 +407,30 @@
|
|
|
458
407
|
|
|
459
408
|
function paintCssHighlights() {
|
|
460
409
|
const changedIndex = renderLines(code.split("\n"), setText);
|
|
461
|
-
const
|
|
410
|
+
const previousEvents = cssPaintedEvents;
|
|
411
|
+
const events = getEvents();
|
|
412
|
+
cssPaintedEvents = events;
|
|
462
413
|
|
|
463
414
|
if (changedIndex == null) {
|
|
464
|
-
|
|
415
|
+
paintLineHighlights(0, 0, code.length, toRanges(events));
|
|
465
416
|
} else {
|
|
466
|
-
|
|
417
|
+
// Only one line's text changed, but its tokens can change the lines
|
|
418
|
+
// after it too (typing `/*`), so repaint through the line where the
|
|
419
|
+
// re-parse converged with the last paint's.
|
|
420
|
+
const base = lineStartOffset(changedIndex);
|
|
421
|
+
const end = retokenizedEnd(
|
|
422
|
+
code,
|
|
423
|
+
events,
|
|
424
|
+
previousEvents,
|
|
425
|
+
incrementalParse?.reuse,
|
|
426
|
+
base,
|
|
427
|
+
);
|
|
428
|
+
paintLineHighlights(
|
|
429
|
+
changedIndex,
|
|
430
|
+
base,
|
|
431
|
+
end,
|
|
432
|
+
lineTokenRanges(events, base, end),
|
|
433
|
+
);
|
|
467
434
|
}
|
|
468
435
|
return changedIndex;
|
|
469
436
|
}
|
|
@@ -472,7 +439,12 @@
|
|
|
472
439
|
if (resolvedEngineValue === "css-highlights") return paintCssHighlights();
|
|
473
440
|
const events = getEvents();
|
|
474
441
|
return renderLines(
|
|
475
|
-
domLinePainter.paint(
|
|
442
|
+
domLinePainter.paint(
|
|
443
|
+
events,
|
|
444
|
+
code,
|
|
445
|
+
language.name,
|
|
446
|
+
incrementalParse?.reuse,
|
|
447
|
+
),
|
|
476
448
|
setHtml,
|
|
477
449
|
);
|
|
478
450
|
}
|
|
@@ -483,14 +455,14 @@
|
|
|
483
455
|
restoringSelection = true;
|
|
484
456
|
if (changedIndex != null) {
|
|
485
457
|
const lineStart = lineStartOffset(changedIndex);
|
|
486
|
-
const lineEnd = lineStart + lineLengths[changedIndex];
|
|
458
|
+
const lineEnd = lineStart + view.lineLengths[changedIndex];
|
|
487
459
|
const inLine = (offset) =>
|
|
488
460
|
offset == null || (offset >= lineStart && offset <= lineEnd);
|
|
489
461
|
if (start >= lineStart && start <= lineEnd && inLine(end)) {
|
|
490
462
|
setSelection(
|
|
491
463
|
start - lineStart,
|
|
492
464
|
end == null ? end : end - lineStart,
|
|
493
|
-
lineEls[changedIndex],
|
|
465
|
+
view.lineEls[changedIndex],
|
|
494
466
|
);
|
|
495
467
|
restoringSelection = false;
|
|
496
468
|
return;
|