svelte-highlight 7.22.0 → 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/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/MarkdownStream.svelte +16 -9
- package/README.md +27 -7
- 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/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/search.js
CHANGED
|
@@ -11,29 +11,73 @@ const TAG_RE = /<[^>]*>/g;
|
|
|
11
11
|
const REGEX_SOURCE_LIMIT = 256;
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* HTML parser.
|
|
14
|
+
* Strips tag markup, then decodes the five entities `escapeHtml`
|
|
15
|
+
* (`./engine.js`) produces - exact for this engine's rendered HTML, not a
|
|
16
|
+
* general HTML parser. Tags go first: decoding first would turn escaped
|
|
17
|
+
* source text like `Array<string>` back into `<string>`, which the
|
|
18
|
+
* tag pass would then delete.
|
|
17
19
|
* @param {string} html
|
|
18
20
|
* @returns {string}
|
|
19
21
|
*/
|
|
20
22
|
function toPlainText(html) {
|
|
21
23
|
return html
|
|
22
|
-
.replace(
|
|
23
|
-
.replace(
|
|
24
|
+
.replace(TAG_RE, "")
|
|
25
|
+
.replace(ENTITY_RE, (entity) => ENTITY_MAP[entity] ?? entity);
|
|
24
26
|
}
|
|
25
27
|
|
|
28
|
+
/**
|
|
29
|
+
* @param {string} text
|
|
30
|
+
* @returns {number}
|
|
31
|
+
*/
|
|
32
|
+
function countLines(text) {
|
|
33
|
+
let count = 1;
|
|
34
|
+
for (let i = text.indexOf("\n"); i !== -1; i = text.indexOf("\n", i + 1)) {
|
|
35
|
+
count += 1;
|
|
36
|
+
}
|
|
37
|
+
return count;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @typedef {{
|
|
42
|
+
* kind: "string" | "array" | "tokenized";
|
|
43
|
+
* text?: string;
|
|
44
|
+
* ensureLines?: () => unknown;
|
|
45
|
+
* lineCount(): number;
|
|
46
|
+
* lineRange(start: number, end: number): string[];
|
|
47
|
+
* }} Adapter
|
|
48
|
+
*/
|
|
49
|
+
|
|
26
50
|
/**
|
|
27
51
|
* @param {import("./search.d.ts").SearchSource} source
|
|
28
|
-
* @returns {
|
|
52
|
+
* @returns {Adapter}
|
|
29
53
|
*/
|
|
30
54
|
function createAdapter(source) {
|
|
31
55
|
if (typeof source === "string") {
|
|
32
|
-
|
|
56
|
+
// Split lazily: literal queries scan `text` whole (see `scanText`), and
|
|
57
|
+
// splitting was ~70% of a full rescan's CPU profile in search.bench.ts.
|
|
58
|
+
/** @type {string[] | undefined} */
|
|
59
|
+
let lines;
|
|
60
|
+
let lineCount = -1;
|
|
61
|
+
const ensureLines = () => {
|
|
62
|
+
if (!lines) lines = source.split("\n");
|
|
63
|
+
return lines;
|
|
64
|
+
};
|
|
33
65
|
return {
|
|
34
66
|
kind: "string",
|
|
35
|
-
|
|
36
|
-
|
|
67
|
+
text: source,
|
|
68
|
+
ensureLines,
|
|
69
|
+
lineCount: () => {
|
|
70
|
+
if (lines) return lines.length;
|
|
71
|
+
if (lineCount < 0) lineCount = countLines(source);
|
|
72
|
+
return lineCount;
|
|
73
|
+
},
|
|
74
|
+
lineRange: (start, end) => {
|
|
75
|
+
const lines = ensureLines();
|
|
76
|
+
// Callers only read the result, so a full range can share `lines`.
|
|
77
|
+
return start === 0 && end >= lines.length
|
|
78
|
+
? lines
|
|
79
|
+
: lines.slice(start, end);
|
|
80
|
+
},
|
|
37
81
|
};
|
|
38
82
|
}
|
|
39
83
|
|
|
@@ -45,6 +89,19 @@ function createAdapter(source) {
|
|
|
45
89
|
};
|
|
46
90
|
}
|
|
47
91
|
|
|
92
|
+
// A real TokenizedDocument can hand back its plain text directly. Going
|
|
93
|
+
// through lineRange() instead tokenizes every scanned line only to strip
|
|
94
|
+
// the markup again: in search.bench.ts's incremental group, tokenizing
|
|
95
|
+
// was ~95% of the scan's CPU profile.
|
|
96
|
+
const { textRange } = /** @type {{ textRange?: unknown }} */ (source);
|
|
97
|
+
if (typeof textRange === "function") {
|
|
98
|
+
return {
|
|
99
|
+
kind: "tokenized",
|
|
100
|
+
lineCount: () => source.lineCount(),
|
|
101
|
+
lineRange: (start, end) => textRange.call(source, start, end),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
48
105
|
return {
|
|
49
106
|
kind: "tokenized",
|
|
50
107
|
lineCount: () => source.lineCount(),
|
|
@@ -111,6 +168,64 @@ function optionsEqual(a, b) {
|
|
|
111
168
|
);
|
|
112
169
|
}
|
|
113
170
|
|
|
171
|
+
/**
|
|
172
|
+
* Finds a literal `query`'s matches with one pass of `pattern` over the
|
|
173
|
+
* whole `source` string instead of one per split line, mapping each match
|
|
174
|
+
* back to its line as it goes. Same result as per-line scanning: the
|
|
175
|
+
* escaped literal can't match across a "\n" (one containing "\n" never
|
|
176
|
+
* matches a single line, so it returns nothing), and `\b` sees the "\n"
|
|
177
|
+
* before a line as the same non-word boundary as the line's start.
|
|
178
|
+
*
|
|
179
|
+
* A case-sensitive, non-whole-word query is a plain substring search, so
|
|
180
|
+
* it skips the regex for `indexOf` (non-overlapping, like the `g` regex).
|
|
181
|
+
* @param {string} source
|
|
182
|
+
* @param {string} query
|
|
183
|
+
* @param {RegExp} pattern
|
|
184
|
+
* @param {boolean} exact case-sensitive and not whole-word
|
|
185
|
+
* @returns {import("./search.d.ts").SearchMatch[]}
|
|
186
|
+
*/
|
|
187
|
+
function scanText(source, query, pattern, exact) {
|
|
188
|
+
/** @type {import("./search.d.ts").SearchMatch[]} */
|
|
189
|
+
const found = [];
|
|
190
|
+
if (query.includes("\n")) return found;
|
|
191
|
+
let line = 0;
|
|
192
|
+
let lineStart = 0;
|
|
193
|
+
let nextBreak = source.indexOf("\n");
|
|
194
|
+
/**
|
|
195
|
+
* @param {number} index
|
|
196
|
+
* @param {number} length
|
|
197
|
+
*/
|
|
198
|
+
const record = (index, length) => {
|
|
199
|
+
while (nextBreak !== -1 && nextBreak < index) {
|
|
200
|
+
line += 1;
|
|
201
|
+
lineStart = nextBreak + 1;
|
|
202
|
+
nextBreak = source.indexOf("\n", lineStart);
|
|
203
|
+
}
|
|
204
|
+
const start = index - lineStart;
|
|
205
|
+
found.push({ line, start, end: start + length });
|
|
206
|
+
};
|
|
207
|
+
if (exact) {
|
|
208
|
+
const length = query.length;
|
|
209
|
+
for (
|
|
210
|
+
let index = source.indexOf(query);
|
|
211
|
+
index !== -1;
|
|
212
|
+
index = source.indexOf(query, index + length)
|
|
213
|
+
) {
|
|
214
|
+
record(index, length);
|
|
215
|
+
}
|
|
216
|
+
return found;
|
|
217
|
+
}
|
|
218
|
+
pattern.lastIndex = 0;
|
|
219
|
+
for (
|
|
220
|
+
let match = pattern.exec(source);
|
|
221
|
+
match !== null;
|
|
222
|
+
match = pattern.exec(source)
|
|
223
|
+
) {
|
|
224
|
+
record(match.index, match[0].length);
|
|
225
|
+
}
|
|
226
|
+
return found;
|
|
227
|
+
}
|
|
228
|
+
|
|
114
229
|
/**
|
|
115
230
|
* @param {import("./search.d.ts").SearchSource} source
|
|
116
231
|
* @returns {import("./search.d.ts").Search}
|
|
@@ -223,6 +338,10 @@ export function createSearch(source) {
|
|
|
223
338
|
lastText = text;
|
|
224
339
|
lastOptions = normalized;
|
|
225
340
|
hasQueried = true;
|
|
341
|
+
const scanWhole = adapter.text !== undefined && !normalized.regex;
|
|
342
|
+
// A regex scan splits a string source into lines anyway; splitting
|
|
343
|
+
// first makes lineCount() free instead of a second full pass.
|
|
344
|
+
if (pattern !== null && !scanWhole) adapter.ensureLines?.();
|
|
226
345
|
lastScannedLineCount = adapter.lineCount();
|
|
227
346
|
|
|
228
347
|
if (pattern === null) {
|
|
@@ -234,7 +353,15 @@ export function createSearch(source) {
|
|
|
234
353
|
}
|
|
235
354
|
|
|
236
355
|
errorMessage = undefined;
|
|
237
|
-
matches =
|
|
356
|
+
matches =
|
|
357
|
+
scanWhole && adapter.text !== undefined
|
|
358
|
+
? scanText(
|
|
359
|
+
adapter.text,
|
|
360
|
+
text,
|
|
361
|
+
pattern,
|
|
362
|
+
normalized.caseSensitive && !normalized.wholeWord,
|
|
363
|
+
)
|
|
364
|
+
: scanRange(0, lastScannedLineCount, pattern);
|
|
238
365
|
currentIndex = matches.length > 0 ? 0 : undefined;
|
|
239
366
|
notify();
|
|
240
367
|
}
|
|
@@ -316,81 +443,131 @@ export function createSearch(source) {
|
|
|
316
443
|
};
|
|
317
444
|
}
|
|
318
445
|
|
|
446
|
+
/** @typedef {{ container: Element; baseOffset: number }} LineScope */
|
|
447
|
+
|
|
319
448
|
/**
|
|
320
|
-
*
|
|
321
|
-
* `line`-th `.line` element (0-indexed), then the whole
|
|
322
|
-
* treated as absolute into its full `textContent`, split
|
|
449
|
+
* Returns a resolver for the rendered row of each `line`: `[data-line]`
|
|
450
|
+
* first, then the `line`-th `.line` element (0-indexed), then the whole
|
|
451
|
+
* `<code>` (offsets treated as absolute into its full `textContent`, split
|
|
452
|
+
* on `"\n"`).
|
|
453
|
+
*
|
|
454
|
+
* Each query runs at most once per paint and is then looked up per line.
|
|
455
|
+
* Running all three per matched line made a paint O(matched lines x DOM
|
|
456
|
+
* size): a missing `[data-line]` or the `.line` list scans the whole tree
|
|
457
|
+
* (bench/search.bench.ts, "highlightMatches()"). The `<mark>` fallback
|
|
458
|
+
* only adds `<mark>`s and splits text, so nothing it does between lines
|
|
459
|
+
* changes what these queries return.
|
|
323
460
|
* @param {Element} root
|
|
324
|
-
* @
|
|
325
|
-
* @param {Map<Element, number[]>} codeLineOffsets
|
|
326
|
-
* @returns {{ container: Element; baseOffset: number } | undefined}
|
|
461
|
+
* @returns {(line: number) => LineScope | undefined}
|
|
327
462
|
*/
|
|
328
|
-
function
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
463
|
+
function createLineScopeResolver(root) {
|
|
464
|
+
/** @type {Map<string, Element> | undefined} */
|
|
465
|
+
let byDataLine;
|
|
466
|
+
/** @type {NodeListOf<Element> | undefined} */
|
|
467
|
+
let lineElements;
|
|
468
|
+
/** @type {{ code: Element; offsets: number[] } | null | undefined} */
|
|
469
|
+
let fallback;
|
|
470
|
+
|
|
471
|
+
return (line) => {
|
|
472
|
+
if (!byDataLine) {
|
|
473
|
+
byDataLine = new Map();
|
|
474
|
+
for (const element of root.querySelectorAll("[data-line]")) {
|
|
475
|
+
const value = element.getAttribute("data-line");
|
|
476
|
+
// Keep the first in document order, as querySelector() would.
|
|
477
|
+
if (value !== null && !byDataLine.has(value)) {
|
|
478
|
+
byDataLine.set(value, element);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
345
481
|
}
|
|
346
|
-
|
|
347
|
-
|
|
482
|
+
const row = byDataLine.get(`${line}`);
|
|
483
|
+
if (row) return { container: row, baseOffset: 0 };
|
|
484
|
+
|
|
485
|
+
lineElements ??= root.querySelectorAll(".line");
|
|
486
|
+
const byClass = lineElements[line];
|
|
487
|
+
if (byClass) return { container: byClass, baseOffset: 0 };
|
|
488
|
+
|
|
489
|
+
if (fallback === undefined) {
|
|
490
|
+
const code = root.querySelector("code");
|
|
491
|
+
fallback = code && { code, offsets: lineOffsets(code) };
|
|
492
|
+
}
|
|
493
|
+
if (!fallback) return undefined;
|
|
494
|
+
const baseOffset = fallback.offsets[line];
|
|
495
|
+
return baseOffset === undefined
|
|
496
|
+
? undefined
|
|
497
|
+
: { container: fallback.code, baseOffset };
|
|
498
|
+
};
|
|
499
|
+
}
|
|
348
500
|
|
|
349
|
-
|
|
350
|
-
|
|
501
|
+
/**
|
|
502
|
+
* Start offset of each `"\n"`-separated line in `code`'s `textContent`.
|
|
503
|
+
* @param {Element} code
|
|
504
|
+
* @returns {number[]}
|
|
505
|
+
*/
|
|
506
|
+
function lineOffsets(code) {
|
|
507
|
+
const offsets = [];
|
|
508
|
+
let consumed = 0;
|
|
509
|
+
for (const text of (code.textContent ?? "").split("\n")) {
|
|
510
|
+
offsets.push(consumed);
|
|
511
|
+
consumed += text.length + 1;
|
|
512
|
+
}
|
|
513
|
+
return offsets;
|
|
351
514
|
}
|
|
352
515
|
|
|
353
516
|
/**
|
|
354
|
-
*
|
|
355
|
-
*
|
|
517
|
+
* Resolves absolute character offsets into `container`'s text content to
|
|
518
|
+
* (text node, local offset) boundaries in one walk. An offset on the seam
|
|
519
|
+
* between two text nodes lands at the end of the earlier one. Offsets past
|
|
520
|
+
* the end of the text are left out of the result.
|
|
356
521
|
* @param {Node} container
|
|
357
|
-
* @param {number}
|
|
358
|
-
* @returns {{ node: Text; offset: number }
|
|
522
|
+
* @param {number[]} offsets Ascending.
|
|
523
|
+
* @returns {Map<number, { node: Text; offset: number }>}
|
|
359
524
|
*/
|
|
360
|
-
function
|
|
525
|
+
function resolveTextPositions(container, offsets) {
|
|
526
|
+
/** @type {Map<number, { node: Text; offset: number }>} */
|
|
527
|
+
const positions = new Map();
|
|
361
528
|
const walker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT);
|
|
362
529
|
let consumed = 0;
|
|
363
530
|
let node = /** @type {Text | null} */ (walker.nextNode());
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
531
|
+
for (const offset of offsets) {
|
|
532
|
+
while (node !== null && offset > consumed + node.data.length) {
|
|
533
|
+
consumed += node.data.length;
|
|
534
|
+
node = /** @type {Text | null} */ (walker.nextNode());
|
|
535
|
+
}
|
|
536
|
+
if (node === null) break;
|
|
537
|
+
positions.set(offset, { node, offset: offset - consumed });
|
|
369
538
|
}
|
|
370
|
-
return
|
|
539
|
+
return positions;
|
|
371
540
|
}
|
|
372
541
|
|
|
373
542
|
/**
|
|
374
|
-
*
|
|
375
|
-
*
|
|
376
|
-
* @param {
|
|
377
|
-
* @
|
|
543
|
+
* Wraps `node`'s `[localStart, localEnd)` in a `<mark>`, splitting it as
|
|
544
|
+
* needed. When `localStart > 0`, `node` keeps the text before it.
|
|
545
|
+
* @param {Text} node
|
|
546
|
+
* @param {number} localStart
|
|
547
|
+
* @param {number} localEnd
|
|
548
|
+
* @param {boolean} isCurrent
|
|
549
|
+
* @returns {HTMLElement}
|
|
378
550
|
*/
|
|
379
|
-
function
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
if (
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
551
|
+
function wrapInMark(node, localStart, localEnd, isCurrent) {
|
|
552
|
+
let middle = node;
|
|
553
|
+
if (localEnd < node.data.length) middle.splitText(localEnd);
|
|
554
|
+
if (localStart > 0) middle = middle.splitText(localStart);
|
|
555
|
+
|
|
556
|
+
const mark = document.createElement("mark");
|
|
557
|
+
mark.dataset.shlSearch = "";
|
|
558
|
+
if (isCurrent) mark.dataset.shlSearchCurrent = "";
|
|
559
|
+
middle.replaceWith(mark);
|
|
560
|
+
mark.appendChild(middle);
|
|
561
|
+
return mark;
|
|
387
562
|
}
|
|
388
563
|
|
|
389
564
|
/**
|
|
390
565
|
* Wraps a match's text in one `<mark>` per text node it spans, splitting
|
|
391
566
|
* nodes as needed. Re-walks `scope.container` fresh each call, so callers
|
|
392
|
-
* must process matches within a scope in descending `start` order.
|
|
393
|
-
*
|
|
567
|
+
* must process matches within a scope in descending `start` order. Only
|
|
568
|
+
* used when the one-pass `wrapSpansInMarks` can't be (see
|
|
569
|
+
* `highlightMatches`).
|
|
570
|
+
* @param {LineScope} scope
|
|
394
571
|
* @param {number} start
|
|
395
572
|
* @param {number} end
|
|
396
573
|
* @param {boolean} isCurrent
|
|
@@ -425,28 +602,108 @@ function wrapMatchInMarks(scope, start, end, isCurrent) {
|
|
|
425
602
|
const overlapEnd = Math.min(globalEnd, nodeEnd);
|
|
426
603
|
if (overlapStart >= overlapEnd) continue;
|
|
427
604
|
|
|
428
|
-
|
|
429
|
-
|
|
605
|
+
marks.push(
|
|
606
|
+
wrapInMark(
|
|
607
|
+
textNode,
|
|
608
|
+
overlapStart - nodeStart,
|
|
609
|
+
overlapEnd - nodeStart,
|
|
610
|
+
isCurrent,
|
|
611
|
+
),
|
|
612
|
+
);
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
return marks;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/** @typedef {{ start: number; end: number; isCurrent: boolean }} MarkSpan */
|
|
430
619
|
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
620
|
+
/**
|
|
621
|
+
* Wraps every span in `container` in `<mark>`s with one walk of its text
|
|
622
|
+
* nodes, where `wrapMatchInMarks` re-walks the container per match. Spans
|
|
623
|
+
* are absolute offsets into the container's text, sorted by descending
|
|
624
|
+
* `start`, non-empty, and non-overlapping. Going back to front, a split
|
|
625
|
+
* only ever touches text after every span still to come, and the node a
|
|
626
|
+
* split starts from keeps its own start offset, so the offsets indexed up
|
|
627
|
+
* front stay valid for the rest of the pass.
|
|
628
|
+
* @param {Element} container
|
|
629
|
+
* @param {MarkSpan[]} spans
|
|
630
|
+
* @param {HTMLElement[]} marks Receives the created `<mark>`s.
|
|
631
|
+
*/
|
|
632
|
+
function wrapSpansInMarks(container, spans, marks) {
|
|
633
|
+
const walker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT);
|
|
634
|
+
/** @type {Text[]} */
|
|
635
|
+
const nodes = [];
|
|
636
|
+
/** @type {number[]} */
|
|
637
|
+
const starts = [];
|
|
638
|
+
let consumed = 0;
|
|
639
|
+
let node = /** @type {Text | null} */ (walker.nextNode());
|
|
640
|
+
while (node !== null) {
|
|
641
|
+
nodes.push(node);
|
|
642
|
+
starts.push(consumed);
|
|
643
|
+
consumed += node.data.length;
|
|
644
|
+
node = /** @type {Text | null} */ (walker.nextNode());
|
|
645
|
+
}
|
|
434
646
|
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
647
|
+
// Last node that starts before the current span's end.
|
|
648
|
+
let last = nodes.length - 1;
|
|
649
|
+
for (const { start, end, isCurrent } of spans) {
|
|
650
|
+
while (last >= 0 && /** @type {number} */ (starts[last]) >= end) last -= 1;
|
|
651
|
+
let first = last;
|
|
652
|
+
while (
|
|
653
|
+
first >= 0 &&
|
|
654
|
+
/** @type {number} */ (starts[first]) +
|
|
655
|
+
/** @type {Text} */ (nodes[first]).data.length >
|
|
656
|
+
start
|
|
657
|
+
) {
|
|
658
|
+
first -= 1;
|
|
659
|
+
}
|
|
660
|
+
for (let i = first + 1; i <= last; i += 1) {
|
|
661
|
+
const textNode = /** @type {Text} */ (nodes[i]);
|
|
662
|
+
const nodeStart = /** @type {number} */ (starts[i]);
|
|
663
|
+
const overlapStart = Math.max(start, nodeStart);
|
|
664
|
+
const overlapEnd = Math.min(end, nodeStart + textNode.data.length);
|
|
665
|
+
if (overlapStart >= overlapEnd) continue;
|
|
666
|
+
marks.push(
|
|
667
|
+
wrapInMark(
|
|
668
|
+
textNode,
|
|
669
|
+
overlapStart - nodeStart,
|
|
670
|
+
overlapEnd - nodeStart,
|
|
671
|
+
isCurrent,
|
|
672
|
+
),
|
|
673
|
+
);
|
|
674
|
+
}
|
|
441
675
|
}
|
|
676
|
+
}
|
|
442
677
|
|
|
443
|
-
|
|
678
|
+
/**
|
|
679
|
+
* Whether any of `containers` sits inside another one.
|
|
680
|
+
* @param {Iterable<Element>} containers
|
|
681
|
+
* @param {Element} root
|
|
682
|
+
* @returns {boolean}
|
|
683
|
+
*/
|
|
684
|
+
function anyNested(containers, root) {
|
|
685
|
+
const set = new Set(containers);
|
|
686
|
+
for (const container of set) {
|
|
687
|
+
for (
|
|
688
|
+
let p = container.parentElement;
|
|
689
|
+
p && p !== root;
|
|
690
|
+
p = p.parentElement
|
|
691
|
+
) {
|
|
692
|
+
if (set.has(p)) return true;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
return false;
|
|
444
696
|
}
|
|
445
697
|
|
|
446
698
|
/**
|
|
447
699
|
* Paints `matches` into `root` (the currently-rendered rows only): the CSS
|
|
448
700
|
* Custom Highlight API when available, else `<mark data-shl-search>`
|
|
449
701
|
* wrapping. One full paint per call - repaint-on-change is the caller's job.
|
|
702
|
+
*
|
|
703
|
+
* Text positions are resolved with one walk per row container, not one
|
|
704
|
+
* per match: with the `<code>` fallback every line shares one container,
|
|
705
|
+
* and a walk from its first text node per match made a paint O(matches x
|
|
706
|
+
* text nodes) (bench/search.bench.ts, "highlightMatches()").
|
|
450
707
|
* @param {Element} root
|
|
451
708
|
* @param {readonly import("./search.d.ts").SearchMatch[]} matches
|
|
452
709
|
* @param {{ current?: number; name?: string }} [options]
|
|
@@ -467,8 +724,13 @@ export function highlightMatches(
|
|
|
467
724
|
byLine.set(match.line, list);
|
|
468
725
|
});
|
|
469
726
|
|
|
470
|
-
|
|
471
|
-
|
|
727
|
+
const resolveScope = createLineScopeResolver(root);
|
|
728
|
+
/** @type {Map<number, LineScope>} */
|
|
729
|
+
const scopes = new Map();
|
|
730
|
+
for (const line of byLine.keys()) {
|
|
731
|
+
const scope = resolveScope(line);
|
|
732
|
+
if (scope) scopes.set(line, scope);
|
|
733
|
+
}
|
|
472
734
|
|
|
473
735
|
if ("highlights" in CSS) {
|
|
474
736
|
const highlight = new Highlight();
|
|
@@ -476,12 +738,42 @@ export function highlightMatches(
|
|
|
476
738
|
CSS.highlights.set(name, highlight);
|
|
477
739
|
CSS.highlights.set(`${name}-current`, currentHighlight);
|
|
478
740
|
|
|
741
|
+
/** @type {Map<Element, number[]>} */
|
|
742
|
+
const offsetsByContainer = new Map();
|
|
743
|
+
for (const [line, lineMatches] of byLine) {
|
|
744
|
+
const scope = scopes.get(line);
|
|
745
|
+
if (!scope) continue;
|
|
746
|
+
let offsets = offsetsByContainer.get(scope.container);
|
|
747
|
+
if (!offsets) {
|
|
748
|
+
offsets = [];
|
|
749
|
+
offsetsByContainer.set(scope.container, offsets);
|
|
750
|
+
}
|
|
751
|
+
for (const { start, end } of lineMatches) {
|
|
752
|
+
offsets.push(scope.baseOffset + start, scope.baseOffset + end);
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
/** @type {Map<Element, Map<number, { node: Text; offset: number }>>} */
|
|
756
|
+
const positionsByContainer = new Map();
|
|
757
|
+
for (const [container, offsets] of offsetsByContainer) {
|
|
758
|
+
offsets.sort((a, b) => a - b);
|
|
759
|
+
positionsByContainer.set(
|
|
760
|
+
container,
|
|
761
|
+
resolveTextPositions(container, offsets),
|
|
762
|
+
);
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
// Ranges go in the same order as before: by line, then match order.
|
|
479
766
|
for (const [line, lineMatches] of byLine) {
|
|
480
|
-
const scope =
|
|
767
|
+
const scope = scopes.get(line);
|
|
481
768
|
if (!scope) continue;
|
|
769
|
+
const positions = positionsByContainer.get(scope.container);
|
|
482
770
|
for (const { start, end, index } of lineMatches) {
|
|
483
|
-
const
|
|
484
|
-
|
|
771
|
+
const from = positions?.get(scope.baseOffset + start);
|
|
772
|
+
const to = positions?.get(scope.baseOffset + end);
|
|
773
|
+
if (!from || !to) continue;
|
|
774
|
+
const range = new Range();
|
|
775
|
+
range.setStart(from.node, from.offset);
|
|
776
|
+
range.setEnd(to.node, to.offset);
|
|
485
777
|
if (index === current) currentHighlight.add(range);
|
|
486
778
|
else highlight.add(range);
|
|
487
779
|
}
|
|
@@ -498,14 +790,64 @@ export function highlightMatches(
|
|
|
498
790
|
/** @type {HTMLElement[]} */
|
|
499
791
|
const createdMarks = [];
|
|
500
792
|
|
|
793
|
+
/** @type {Map<Element, MarkSpan[]>} */
|
|
794
|
+
const spansByContainer = new Map();
|
|
501
795
|
for (const [line, lineMatches] of byLine) {
|
|
502
|
-
const scope =
|
|
796
|
+
const scope = scopes.get(line);
|
|
503
797
|
if (!scope) continue;
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
798
|
+
let spans = spansByContainer.get(scope.container);
|
|
799
|
+
if (!spans) {
|
|
800
|
+
spans = [];
|
|
801
|
+
spansByContainer.set(scope.container, spans);
|
|
802
|
+
}
|
|
803
|
+
for (const { start, end, index } of lineMatches) {
|
|
804
|
+
// An empty or inverted span never overlaps any text, so it never
|
|
805
|
+
// split or wrapped anything.
|
|
806
|
+
if (end <= start) continue;
|
|
807
|
+
spans.push({
|
|
808
|
+
start: scope.baseOffset + start,
|
|
809
|
+
end: scope.baseOffset + end,
|
|
810
|
+
isCurrent: index === current,
|
|
811
|
+
});
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
// The one-pass wrap gives the same DOM as the per-match wrap only when
|
|
816
|
+
// no two spans cover the same text, so the order they're wrapped in
|
|
817
|
+
// can't matter. Within one container that means disjoint spans, which
|
|
818
|
+
// is what `createSearch` returns. Spans in nested containers (a `<code>`
|
|
819
|
+
// fallback line around rendered `[data-line]` rows) aren't cheap to
|
|
820
|
+
// compare, so those paints, like any with overlapping spans, keep the
|
|
821
|
+
// per-match wrap in its old order.
|
|
822
|
+
let onePass = !anyNested(spansByContainer.keys(), root);
|
|
823
|
+
for (const spans of spansByContainer.values()) {
|
|
824
|
+
if (!onePass) break;
|
|
825
|
+
spans.sort((a, b) => b.start - a.start);
|
|
826
|
+
for (let i = 1; i < spans.length; i += 1) {
|
|
827
|
+
if (
|
|
828
|
+
/** @type {MarkSpan} */ (spans[i]).end >
|
|
829
|
+
/** @type {MarkSpan} */ (spans[i - 1]).start
|
|
830
|
+
) {
|
|
831
|
+
onePass = false;
|
|
832
|
+
break;
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
if (onePass) {
|
|
838
|
+
for (const [container, spans] of spansByContainer) {
|
|
839
|
+
wrapSpansInMarks(container, spans, createdMarks);
|
|
840
|
+
}
|
|
841
|
+
} else {
|
|
842
|
+
for (const [line, lineMatches] of byLine) {
|
|
843
|
+
const scope = scopes.get(line);
|
|
844
|
+
if (!scope) continue;
|
|
845
|
+
const descending = [...lineMatches].sort((a, b) => b.start - a.start);
|
|
846
|
+
for (const { start, end, index } of descending) {
|
|
847
|
+
createdMarks.push(
|
|
848
|
+
...wrapMatchInMarks(scope, start, end, index === current),
|
|
849
|
+
);
|
|
850
|
+
}
|
|
509
851
|
}
|
|
510
852
|
}
|
|
511
853
|
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HighlightStream's final pass once `done`: one full, canonicalizing
|
|
3
|
+
* re-parse of the whole buffer, for the multi-line lookahead (heredocs,
|
|
4
|
+
* etc.) the streaming parse can't resolve.
|
|
5
|
+
*
|
|
6
|
+
* It's a one-shot `registry.highlight` - exactly what
|
|
7
|
+
* `session.finish({ canonicalize: true })` runs - so it needs no session.
|
|
8
|
+
* HighlightStream drops its streaming session once the pass is done, and a
|
|
9
|
+
* repeat pass doesn't build a new one.
|
|
10
|
+
*
|
|
11
|
+
* The result is memoized on the code and language. The done pass can
|
|
12
|
+
* re-run with neither changed (a parent re-renders and hands over its
|
|
13
|
+
* `language` object again, which Svelte's legacy-mode equality always
|
|
14
|
+
* treats as changed). Without the memo, each re-run re-parsed the whole
|
|
15
|
+
* code - bench/stream-final-highlight.bench.ts.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @typedef {{ highlight: (code: string, options: { language: string }) => { value: string } }} FinalRegistry
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @returns {{
|
|
24
|
+
* highlight: (registry: FinalRegistry, code: string, language: string) => string,
|
|
25
|
+
* }}
|
|
26
|
+
*/
|
|
27
|
+
export function createFinalHighlighter() {
|
|
28
|
+
/** @type {string | undefined} */
|
|
29
|
+
let lastCode;
|
|
30
|
+
/** @type {string | undefined} */
|
|
31
|
+
let lastLanguage;
|
|
32
|
+
let lastHtml = "";
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
/**
|
|
36
|
+
* @param {FinalRegistry} registry Has `language` registered.
|
|
37
|
+
* @param {string} code
|
|
38
|
+
* @param {string} language
|
|
39
|
+
* @returns {string}
|
|
40
|
+
*/
|
|
41
|
+
highlight(registry, code, language) {
|
|
42
|
+
// A pure function of the code and language, so an unchanged pair can
|
|
43
|
+
// reuse the last result.
|
|
44
|
+
if (code !== lastCode || language !== lastLanguage) {
|
|
45
|
+
lastHtml = registry.highlight(code, { language }).value;
|
|
46
|
+
lastCode = code;
|
|
47
|
+
lastLanguage = language;
|
|
48
|
+
}
|
|
49
|
+
return lastHtml;
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|