svelte-highlight 7.16.1 → 7.16.2

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.
@@ -39,7 +39,8 @@
39
39
  export let theme = undefined;
40
40
 
41
41
  import { createEventDispatcher, onMount } from "svelte";
42
- import { renderHtml, toRanges } from "./engine.js";
42
+ import { createDomLinePainter } from "./editable-dom-paint.js";
43
+ import { toRanges } from "./engine.js";
43
44
  import {
44
45
  highlightRules,
45
46
  highlightRulesFromPalette,
@@ -49,7 +50,6 @@
49
50
  reparseIncremental,
50
51
  } from "./incremental-tokenize.js";
51
52
  import { ensureRegistered, registry } from "./registry.js";
52
- import { splitLines } from "./split-lines.js";
53
53
  import { diffText } from "./text-diff.js";
54
54
 
55
55
  const dispatch = createEventDispatcher();
@@ -80,6 +80,10 @@
80
80
  /** @type {import("./incremental-tokenize.js").IncrementalParse | undefined} */
81
81
  let incrementalParse;
82
82
 
83
+ // DOM-engine line HTML: pure appends feed a stream session so renderHtml is
84
+ // not re-run over the whole document on every keystroke.
85
+ const domLinePainter = createDomLinePainter({ registry });
86
+
83
87
  function getEvents() {
84
88
  incrementalParse = incrementalParse
85
89
  ? reparseIncremental(registry, language.name, incrementalParse, code)
@@ -455,10 +459,11 @@
455
459
 
456
460
  function paint() {
457
461
  if (resolvedEngineValue === "css-highlights") return paintCssHighlights();
458
- const html = renderHtml(getEvents());
459
- // Trailing empty line needs a phantom `\n` for caret placement.
460
- const paintHtml = code === "" || code.endsWith("\n") ? `${html}\n` : html;
461
- return renderLines(splitLines(paintHtml), setHtml);
462
+ const events = getEvents();
463
+ return renderLines(
464
+ domLinePainter.paint(events, code, language.name),
465
+ setHtml,
466
+ );
462
467
  }
463
468
 
464
469
  function renderAt(start, end) {
@@ -485,6 +490,8 @@
485
490
  }
486
491
 
487
492
  function repaintForLanguageChange() {
493
+ incrementalParse = undefined;
494
+ domLinePainter.reset();
488
495
  const caret = document.activeElement === editor ? getCaretOffset() : null;
489
496
  if (caret == null) paint();
490
497
  else renderAt(caret, caret);
@@ -31,6 +31,7 @@
31
31
  import { createEventDispatcher, onMount, tick } from "svelte";
32
32
  import { extendLines } from "./engine.js";
33
33
  import { ensureRegistered, registry } from "./registry.js";
34
+ import { createCompletedHtmlBuffer } from "./stream-highlighted.js";
34
35
 
35
36
  // Lines between sealed chunks. Once a chunk fills, its line spans are
36
37
  // joined into one immutable HTML string and never touched again - keyed
@@ -75,13 +76,15 @@
75
76
  // Sealed (finished, immutable) chunks of `SEAL_CHUNK_LINES` line spans
76
77
  // each, pre-joined into one HTML string apiece - `sealedChunks` is never
77
78
  // mutated in place, only appended to, and past entries are never rebuilt.
78
- // `sealedHtml` mirrors the same sealed prefix as plain (wrapper-free) HTML,
79
- // appended once per sealed chunk, so `highlighted` below is O(tail) per
80
- // repaint instead of rejoining every line streamed so far.
81
79
  /** @type {string[]} */
82
80
  let sealedChunks = [];
83
- let sealedHtml = "";
84
81
  let sealedLineCount = 0;
82
+ // Append-only completed line HTML for the `highlight` event payload.
83
+ // Completed lines are concatenated once as they finalize (O(n) over the
84
+ // stream). Each repaint still assembles `highlighted = completed + preview`
85
+ // so `on:highlight` stays live mid-line; that concat copies the completed
86
+ // string but avoids rebuilding it from sealed DOM chunks.
87
+ const completedHtml = createCompletedHtmlBuffer();
85
88
  // Completed lines not yet folded into a sealed chunk - bounded by
86
89
  // `SEAL_CHUNK_LINES`, so touching it every repaint stays O(1).
87
90
  /** @type {string[]} */
@@ -106,8 +109,8 @@
106
109
  finalizedOpenScopes = [];
107
110
  renderedCommittedCount = 0;
108
111
  sealedChunks = [];
109
- sealedHtml = "";
110
112
  sealedLineCount = 0;
113
+ completedHtml.reset();
111
114
  unsealedLines = [];
112
115
  tailLines = [];
113
116
  }
@@ -119,15 +122,13 @@
119
122
  function sealChunk() {
120
123
  const chunkLines = unsealedLines.slice(0, SEAL_CHUNK_LINES);
121
124
  let domHtml = "";
122
- let plainHtml = "";
123
125
  for (let li = 0; li < chunkLines.length; li++) {
124
126
  const i = sealedLineCount + li;
125
127
  const sep = i > 0 ? "\n" : "";
126
128
  domHtml += `${sep}<span class="highlight-stream-line" data-line="${i}">${chunkLines[li]}</span>`;
127
- plainHtml += sep + chunkLines[li];
128
129
  }
129
- sealedChunks = [...sealedChunks, domHtml];
130
- sealedHtml += plainHtml;
130
+ sealedChunks.push(domHtml);
131
+ sealedChunks = sealedChunks;
131
132
  sealedLineCount += chunkLines.length;
132
133
  unsealedLines = unsealedLines.slice(SEAL_CHUNK_LINES);
133
134
  }
@@ -152,6 +153,7 @@
152
153
  finalizedPendingHtml,
153
154
  );
154
155
  if (result.completedLines.length > 0) {
156
+ completedHtml.appendLines(result.completedLines);
155
157
  unsealedLines = unsealedLines.concat(result.completedLines);
156
158
  while (unsealedLines.length >= SEAL_CHUNK_LINES) sealChunk();
157
159
  }
@@ -174,8 +176,14 @@
174
176
  }
175
177
 
176
178
  tailLines = [...unsealedLines, ...previewLines];
179
+
180
+ // Always assemble a live event payload (including mid-line preview).
181
+ // Trailing empty preview keeps a final `\n` when the stream ends a line.
182
+ const completed = completedHtml.toString();
177
183
  highlighted =
178
- sealedHtml + (sealedLineCount > 0 ? "\n" : "") + tailLines.join("\n");
184
+ completedHtml.lineCount === 0
185
+ ? previewLines.join("\n")
186
+ : `${completed}\n${previewLines.join("\n")}`;
179
187
  }
180
188
 
181
189
  dispatch("highlight", { highlighted });
@@ -0,0 +1,216 @@
1
+ /**
2
+ * Incremental DOM-engine paint helpers for HighlightEditable.
3
+ *
4
+ * Tokenization via `reparseIncremental` is already incremental, but the
5
+ * default `"dom"` engine still ran `renderHtml` + `splitLines` over the full
6
+ * event stream every keystroke. For pure appends (typing at the end — the
7
+ * common path when growing a document), a dedicated stream session feeds only
8
+ * the new suffix through `extendLines`, so HTML work is O(delta) per keystroke
9
+ * and O(n) total to type a document of size n.
10
+ */
11
+
12
+ import { extendLines, renderHtml } from "./engine.js";
13
+ import { splitLines } from "./split-lines.js";
14
+
15
+ /**
16
+ * @param {string} previousCode
17
+ * @param {string} nextCode
18
+ */
19
+ export function isPureAppend(previousCode, nextCode) {
20
+ return (
21
+ nextCode.length >= previousCode.length && nextCode.startsWith(previousCode)
22
+ );
23
+ }
24
+
25
+ /**
26
+ * Full line-HTML paint via `renderHtml` + `splitLines` (the historical path).
27
+ * @param {import("./engine.d.ts").ScopeEvent[]} events
28
+ * @param {string} code
29
+ * @returns {string[]}
30
+ */
31
+ export function lineHtmlFromEvents(events, code) {
32
+ const html = renderHtml(events);
33
+ const paintHtml = code === "" || code.endsWith("\n") ? `${html}\n` : html;
34
+ return splitLines(paintHtml);
35
+ }
36
+
37
+ /**
38
+ * Build the visible line list from incremental extendLines state, matching
39
+ * `lineHtmlFromEvents` (including the historical phantom trailing newline
40
+ * when `code` is empty or ends in `\n`).
41
+ *
42
+ * @param {string[]} completedLines
43
+ * @param {string[]} previewLines unsealed + pending preview from resume
44
+ * @param {string} code
45
+ * @returns {string[]}
46
+ */
47
+ export function linesFromStreamState(completedLines, previewLines, code) {
48
+ /** @type {string[]} */
49
+ const lines = completedLines.concat(previewLines);
50
+ // Historical paint does `html + "\n"` before splitLines when the source ends
51
+ // on a line boundary, which yields one extra empty caret line beyond the
52
+ // stream-style completed+pending list.
53
+ if (code === "" || code.endsWith("\n")) {
54
+ lines.push("");
55
+ }
56
+ return lines;
57
+ }
58
+
59
+ /**
60
+ * @typedef {import("./engine.d.ts").Registry} Registry
61
+ * @typedef {import("./engine.d.ts").ScopeEvent} ScopeEvent
62
+ * @typedef {import("./engine.d.ts").StreamSession} StreamSession
63
+ */
64
+
65
+ /**
66
+ * Stateful incremental painter driven by pure-append stream sessions.
67
+ *
68
+ * @param {{
69
+ * registry: Registry,
70
+ * }} options
71
+ */
72
+ export function createDomLinePainter({ registry }) {
73
+ /** @type {StreamSession | undefined} */
74
+ let session;
75
+ let sessionLanguage = "";
76
+ let fedCode = "";
77
+ let renderedEventCount = 0;
78
+ /** @type {string[]} */
79
+ let openScopes = [];
80
+ let pendingHtml = "";
81
+ /** @type {string[]} */
82
+ let completedLines = [];
83
+ let lastUsedIncremental = false;
84
+ /** Whether the current session has already accepted at least one append. */
85
+ let hasAppended = false;
86
+ /**
87
+ * Mid-document edits mark the stream session dirty instead of eagerly
88
+ * re-tokenizing. The next pure append pays one full resync, then resumes
89
+ * O(delta) painting.
90
+ */
91
+ let needsResync = false;
92
+
93
+ /**
94
+ * @param {string} nextLanguage
95
+ * @returns {StreamSession}
96
+ */
97
+ function resetSession(nextLanguage) {
98
+ session = registry.createSession(nextLanguage);
99
+ sessionLanguage = nextLanguage;
100
+ fedCode = "";
101
+ renderedEventCount = 0;
102
+ openScopes = [];
103
+ pendingHtml = "";
104
+ completedLines = [];
105
+ hasAppended = false;
106
+ needsResync = false;
107
+ return session;
108
+ }
109
+
110
+ /**
111
+ * Rebuild the stream session from `base` so a subsequent delta append can
112
+ * continue incrementally. Preserves `fedCode` across the reset.
113
+ * @param {string} languageName
114
+ * @param {string} base
115
+ */
116
+ function resyncSession(languageName, base) {
117
+ session = resetSession(languageName);
118
+ if (base.length > 0) {
119
+ session.append(base);
120
+ hasAppended = true;
121
+ consumeCommitted(session.events());
122
+ }
123
+ fedCode = base;
124
+ }
125
+
126
+ /**
127
+ * @param {ScopeEvent[]} committed
128
+ */
129
+ function consumeCommitted(committed) {
130
+ if (!session || committed.length <= renderedEventCount) return;
131
+ const result = extendLines(
132
+ committed.slice(renderedEventCount),
133
+ openScopes,
134
+ pendingHtml,
135
+ );
136
+ if (result.completedLines.length > 0) {
137
+ completedLines = completedLines.concat(result.completedLines);
138
+ }
139
+ openScopes = result.openScopes;
140
+ pendingHtml = result.pendingHtml;
141
+ renderedEventCount = committed.length;
142
+ }
143
+
144
+ /**
145
+ * @param {string} code
146
+ * @returns {string[]}
147
+ */
148
+ function linesForCurrentCode(code) {
149
+ if (!session) return linesFromStreamState([], [""], code);
150
+ const snapshot = session.snapshot();
151
+ /** @type {string[]} */
152
+ let previewLines = [pendingHtml];
153
+ if (snapshot.pos < fedCode.length) {
154
+ const preview = registry.resume(fedCode, sessionLanguage, snapshot);
155
+ const result = extendLines(preview.events, openScopes, pendingHtml);
156
+ previewLines = result.completedLines.concat(result.pendingHtml);
157
+ }
158
+ return linesFromStreamState(completedLines, previewLines, code);
159
+ }
160
+
161
+ return {
162
+ reset() {
163
+ session = undefined;
164
+ sessionLanguage = "";
165
+ fedCode = "";
166
+ renderedEventCount = 0;
167
+ openScopes = [];
168
+ pendingHtml = "";
169
+ completedLines = [];
170
+ lastUsedIncremental = false;
171
+ hasAppended = false;
172
+ needsResync = false;
173
+ },
174
+ lastUsedIncremental() {
175
+ return lastUsedIncremental;
176
+ },
177
+ /**
178
+ * @param {ScopeEvent[]} events Full events from getEvents() — used only
179
+ * for the non-append fallback path (mid-document edits).
180
+ * @param {string} code
181
+ * @param {string} languageName
182
+ * @returns {string[]}
183
+ */
184
+ paint(events, code, languageName) {
185
+ if (session === undefined || languageName !== sessionLanguage) {
186
+ session = resetSession(languageName);
187
+ }
188
+
189
+ if (!isPureAppend(fedCode, code)) {
190
+ lastUsedIncremental = false;
191
+ const lines = lineHtmlFromEvents(events, code);
192
+ // Defer the O(n) stream resync until the next pure append.
193
+ needsResync = true;
194
+ fedCode = code;
195
+ return lines;
196
+ }
197
+
198
+ if (needsResync) {
199
+ resyncSession(languageName, fedCode);
200
+ }
201
+
202
+ const hadContent = hasAppended;
203
+ if (code.length > fedCode.length) {
204
+ session.append(code.slice(fedCode.length));
205
+ fedCode = code;
206
+ hasAppended = true;
207
+ consumeCommitted(session.events());
208
+ } else if (code.length === 0 && fedCode.length === 0) {
209
+ // Empty document paint.
210
+ }
211
+
212
+ lastUsedIncremental = hadContent;
213
+ return linesForCurrentCode(code);
214
+ },
215
+ };
216
+ }
@@ -94,8 +94,16 @@ function stateConverges(a, b) {
94
94
  }
95
95
 
96
96
  /**
97
- * Full parse with a line checkpoint after every line. First paint or language
98
- * change.
97
+ * Default gap between line checkpoints. Matches the spirit of
98
+ * `tokenized-document`'s interval: denser than its windowed default (100)
99
+ * so mid-document edits still resume nearby, sparse enough that a 10k-line
100
+ * file stores O(hundreds) of snapshots rather than O(lines).
101
+ */
102
+ export const CHECKPOINT_INTERVAL = 32;
103
+
104
+ /**
105
+ * Full parse with a line checkpoint every `CHECKPOINT_INTERVAL` lines (and
106
+ * always at the document end). First paint or language change.
99
107
  * @param {Registry} registry
100
108
  * @param {string} language
101
109
  * @param {string} code
@@ -104,8 +112,18 @@ function stateConverges(a, b) {
104
112
  export function parseIncremental(registry, language, code) {
105
113
  const session = registry.createSession(language);
106
114
  const checkpoints = [session.snapshot()];
115
+ let linesSinceCheckpoint = 0;
107
116
  for (const line of splitKeepEnds(code)) {
108
117
  session.append(line);
118
+ linesSinceCheckpoint++;
119
+ if (linesSinceCheckpoint >= CHECKPOINT_INTERVAL) {
120
+ checkpoints.push(session.snapshot());
121
+ linesSinceCheckpoint = 0;
122
+ }
123
+ }
124
+ // Always retain an end checkpoint so resume can land on the final state
125
+ // even when the last interval is incomplete.
126
+ if (linesSinceCheckpoint > 0 || checkpoints.length === 1) {
109
127
  checkpoints.push(session.snapshot());
110
128
  }
111
129
  const { events } = session.finish();
@@ -173,16 +191,19 @@ export function reparseIncremental(registry, language, previous, code) {
173
191
  const newLines = splitKeepEnds(code.slice(resumeCheckpoint.pos));
174
192
  let convergedAtOldIndex = -1;
175
193
  let oldIndex = resumeIndex;
194
+ let linesSinceCheckpoint = 0;
176
195
 
177
- for (const line of newLines) {
178
- session.append(line);
196
+ for (let li = 0; li < newLines.length; li++) {
197
+ session.append(/** @type {string} */ (newLines[li]));
179
198
  const snap = session.snapshot();
180
- // snap.eventCount is session-local; shift to index the combined array.
181
- checkpoints.push({
182
- ...snap,
183
- eventCount: snap.eventCount + prefixEvents.length,
184
- });
185
-
199
+ linesSinceCheckpoint++;
200
+ // Check every line for convergence against previous checkpoints.
201
+ // Store every line for a window right after the edit (typing tends to
202
+ // stay near the cursor, so a follow-up edit here resumes in O(1)
203
+ // instead of walking to the next interval boundary), then fall back to
204
+ // the sparse interval so density doesn't stay O(lines) further out.
205
+ let shouldStore =
206
+ li < CHECKPOINT_INTERVAL || linesSinceCheckpoint >= CHECKPOINT_INTERVAL;
186
207
  if (snap.pos >= newSuffixStart) {
187
208
  const targetOldPos = snap.pos - posOffset;
188
209
  while (
@@ -201,9 +222,19 @@ export function reparseIncremental(registry, language, previous, code) {
201
222
  stateConverges(snap, oldCheckpoint)
202
223
  ) {
203
224
  convergedAtOldIndex = oldIndex;
204
- break;
225
+ shouldStore = true;
205
226
  }
206
227
  }
228
+ if (!shouldStore && li === newLines.length - 1) shouldStore = true;
229
+ if (shouldStore) {
230
+ // snap.eventCount is session-local; shift to index the combined array.
231
+ checkpoints.push({
232
+ ...snap,
233
+ eventCount: snap.eventCount + prefixEvents.length,
234
+ });
235
+ linesSinceCheckpoint = 0;
236
+ }
237
+ if (convergedAtOldIndex >= 0) break;
207
238
  }
208
239
 
209
240
  if (convergedAtOldIndex >= 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-highlight",
3
- "version": "7.16.1",
3
+ "version": "7.16.2",
4
4
  "license": "MIT",
5
5
  "description": "Svelte component library for highlighting code using highlight.js",
6
6
  "author": "Eric Liu (https://github.com/metonym)",
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Append-only buffer for completed (line-finalized) stream HTML.
3
+ *
4
+ * Appending newly completed line HTML is O(n) over the whole stream. Callers
5
+ * still assemble `highlighted = completed + preview` each repaint so the
6
+ * `highlight` event stays live mid-line; that assembly copies the completed
7
+ * string but does not rebuild it from sealed DOM chunks.
8
+ */
9
+
10
+ /**
11
+ * @returns {{
12
+ * appendLines: (lines: string[]) => void,
13
+ * reset: () => void,
14
+ * toString: () => string,
15
+ * lineCount: number,
16
+ * }}
17
+ */
18
+ export function createCompletedHtmlBuffer() {
19
+ let html = "";
20
+ let lineCount = 0;
21
+ return {
22
+ /** @param {string[]} lines */
23
+ appendLines(lines) {
24
+ for (let i = 0; i < lines.length; i++) {
25
+ if (lineCount > 0) html += "\n";
26
+ html += lines[i];
27
+ lineCount++;
28
+ }
29
+ },
30
+ reset() {
31
+ html = "";
32
+ lineCount = 0;
33
+ },
34
+ toString() {
35
+ return html;
36
+ },
37
+ get lineCount() {
38
+ return lineCount;
39
+ },
40
+ };
41
+ }