svelte-highlight 7.23.2 → 7.24.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.
Files changed (94) hide show
  1. package/AnsiOutput.svelte +33 -6
  2. package/AnsiOutput.svelte.d.ts +8 -0
  3. package/CodeToolbar.svelte +144 -0
  4. package/CodeToolbar.svelte.d.ts +125 -0
  5. package/CodeWindow.svelte +19 -0
  6. package/CodeWindow.svelte.d.ts +10 -0
  7. package/CopyButton.svelte +107 -3
  8. package/CopyButton.svelte.d.ts +17 -1
  9. package/DiffFileList.svelte +523 -0
  10. package/DiffFileList.svelte.d.ts +94 -0
  11. package/DiffFileListItem.svelte +44 -0
  12. package/DiffMinimap.svelte +99 -0
  13. package/DiffMinimap.svelte.d.ts +17 -0
  14. package/DiffStats.svelte +43 -0
  15. package/DiffStats.svelte.d.ts +17 -0
  16. package/DiffView.svelte +1213 -0
  17. package/DiffView.svelte.d.ts +76 -0
  18. package/FileTabs.svelte +2 -5
  19. package/Highlight.svelte +158 -2
  20. package/Highlight.svelte.d.ts +50 -2
  21. package/HighlightAuto.svelte.d.ts +3 -2
  22. package/HighlightAutoLazy.svelte +112 -0
  23. package/HighlightAutoLazy.svelte.d.ts +66 -0
  24. package/HighlightDiff.svelte +162 -0
  25. package/HighlightDiff.svelte.d.ts +102 -0
  26. package/HighlightEditable.svelte +65 -756
  27. package/HighlightEditable.svelte.d.ts +15 -5
  28. package/HighlightStream.svelte +117 -218
  29. package/HighlightStream.svelte.d.ts +8 -0
  30. package/HighlightSvelte.svelte.d.ts +3 -2
  31. package/HighlightVirtual.svelte +41 -8
  32. package/HighlightVirtual.svelte.d.ts +8 -0
  33. package/LangTag.svelte +33 -4
  34. package/LangTag.svelte.d.ts +6 -2
  35. package/LineNumbers.svelte +25 -4
  36. package/LineNumbers.svelte.d.ts +8 -0
  37. package/MarkdownStream.svelte +1 -0
  38. package/README.md +735 -18
  39. package/ToolbarButton.svelte +90 -0
  40. package/ToolbarButton.svelte.d.ts +106 -0
  41. package/Typewriter.svelte +59 -10
  42. package/Typewriter.svelte.d.ts +7 -0
  43. package/WrapToggle.svelte +34 -0
  44. package/WrapToggle.svelte.d.ts +39 -0
  45. package/action.js +7 -3
  46. package/ansi.d.ts +1 -3
  47. package/auto-detect.d.ts +52 -0
  48. package/auto-detect.js +208 -0
  49. package/code-region.d.ts +7 -0
  50. package/code-region.js +20 -0
  51. package/css-highlights.js +115 -0
  52. package/css-walk.js +50 -0
  53. package/diff-controller.d.ts +120 -0
  54. package/diff-controller.js +721 -0
  55. package/diff-edits.d.ts +101 -0
  56. package/diff-edits.js +890 -0
  57. package/diff-html.d.ts +8 -0
  58. package/diff-html.js +60 -0
  59. package/diff-languages.d.ts +4 -0
  60. package/diff-languages.js +73 -0
  61. package/diff.d.ts +161 -0
  62. package/diff.js +1111 -0
  63. package/editable-commands.js +81 -0
  64. package/editable-css-paint.js +5 -3
  65. package/editable-dom-paint.js +1 -1
  66. package/editable-history.js +199 -0
  67. package/editable-line-dom.js +176 -1
  68. package/editable-painter.js +356 -0
  69. package/editable.d.ts +118 -0
  70. package/editable.js +513 -0
  71. package/engine.d.ts +1 -1
  72. package/fence.js +1 -1
  73. package/highlight-cascade.js +679 -0
  74. package/highlight-theme.js +6 -47
  75. package/incremental-tokenize.js +1 -1
  76. package/index.d.ts +39 -6
  77. package/index.js +30 -0
  78. package/languages/detect-index.d.ts +16 -0
  79. package/languages/detect-index.js +6 -0
  80. package/languages/loaders.d.ts +8 -0
  81. package/languages/loaders.js +370 -0
  82. package/package.json +50 -2
  83. package/preprocessor.js +20 -5
  84. package/search.js +2 -2
  85. package/stream-highlighter.js +198 -0
  86. package/stream.css +16 -0
  87. package/stream.d.ts +60 -0
  88. package/stream.js +283 -0
  89. package/style-registry.js +34 -3
  90. package/themes/_base-css.d.ts +1 -0
  91. package/themes/_base-css.js +1 -0
  92. package/typewriter-units.d.ts +3 -0
  93. package/typewriter-units.js +43 -0
  94. package/virtual-window.js +9 -4
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Editing commands for HighlightEditable, as pure functions from code and
3
+ * selection to the next code and selection. Framework-free and DOM-free.
4
+ */
5
+
6
+ /**
7
+ * @typedef {{ start: number, end: number }} SelectionRange
8
+ * @typedef {{ code: string, selection: SelectionRange }} Edit
9
+ */
10
+
11
+ /**
12
+ * Replaces the selection with `text` and puts the caret after it.
13
+ * @param {string} code
14
+ * @param {SelectionRange} selection
15
+ * @param {string} text
16
+ * @returns {Edit}
17
+ */
18
+ export function replaceSelection(code, selection, text) {
19
+ const caret = selection.start + text.length;
20
+ return {
21
+ code: code.slice(0, selection.start) + text + code.slice(selection.end),
22
+ selection: { start: caret, end: caret },
23
+ };
24
+ }
25
+
26
+ /**
27
+ * Indents or outdents every line the selection touches by `tabSize` spaces.
28
+ * Indent skips blank lines. Null when nothing changes.
29
+ * @param {string} code
30
+ * @param {SelectionRange} selection
31
+ * @param {number} tabSize
32
+ * @param {boolean} outdent
33
+ * @returns {Edit | null}
34
+ */
35
+ export function shiftLines(code, selection, tabSize, outdent) {
36
+ const indentUnit = " ".repeat(tabSize);
37
+ const blockStart = code.lastIndexOf("\n", selection.start - 1) + 1;
38
+ const lines = code.slice(blockStart, selection.end).split("\n");
39
+
40
+ let firstDelta = 0;
41
+ let totalDelta = 0;
42
+ const next = lines
43
+ .map((line, index) => {
44
+ if (outdent) {
45
+ const leading = line.length - line.trimStart().length;
46
+ const remove = Math.min(tabSize, leading);
47
+ if (index === 0) firstDelta = -remove;
48
+ totalDelta -= remove;
49
+ return line.slice(remove);
50
+ }
51
+ if (line.length === 0) return line;
52
+ if (index === 0) firstDelta = tabSize;
53
+ totalDelta += tabSize;
54
+ return indentUnit + line;
55
+ })
56
+ .join("\n");
57
+
58
+ if (totalDelta === 0) return null;
59
+ return {
60
+ code: code.slice(0, blockStart) + next + code.slice(selection.end),
61
+ selection: {
62
+ start: Math.max(blockStart, selection.start + firstDelta),
63
+ end: selection.end + totalDelta,
64
+ },
65
+ };
66
+ }
67
+
68
+ /**
69
+ * Tab: indents every selected line when the selection spans lines, else
70
+ * replaces the selection with `tabSize` spaces.
71
+ * @param {string} code
72
+ * @param {SelectionRange} selection
73
+ * @param {number} tabSize
74
+ * @returns {Edit | null}
75
+ */
76
+ export function indentSelection(code, selection, tabSize) {
77
+ if (code.slice(selection.start, selection.end).includes("\n")) {
78
+ return shiftLines(code, selection, tabSize, false);
79
+ }
80
+ return replaceSelection(code, selection, " ".repeat(tabSize));
81
+ }
@@ -10,14 +10,16 @@ import { sharedEvents } from "./incremental-tokenize.js";
10
10
  */
11
11
 
12
12
  /**
13
- * `toRanges(events)` clipped to `[lineStart, lineEnd)`, relative to
13
+ * `ranges(events)` clipped to `[lineStart, lineEnd)`, relative to
14
14
  * `lineStart`, with empty clips dropped.
15
15
  * @param {ScopeEvent[]} events
16
16
  * @param {number} lineStart
17
17
  * @param {number} lineEnd
18
+ * @param {(events: ScopeEvent[]) => TokenRange[]} [ranges] Turns the
19
+ * clipped events, with the scopes open at `lineStart`, into ranges.
18
20
  * @returns {TokenRange[]}
19
21
  */
20
- export function lineTokenRanges(events, lineStart, lineEnd) {
22
+ export function lineTokenRanges(events, lineStart, lineEnd, ranges = toRanges) {
21
23
  // Perf: walk to the line tracking only open scopes, then run `toRanges`
22
24
  // over just the line's events instead of over the whole document.
23
25
  /** @type {ScopeEvent[]} */
@@ -51,7 +53,7 @@ export function lineTokenRanges(events, lineStart, lineEnd) {
51
53
  );
52
54
  offset += length;
53
55
  }
54
- return toRanges(line);
56
+ return ranges(line);
55
57
  }
56
58
 
57
59
  /**
@@ -29,7 +29,7 @@ export function lineHtmlFromEvents(events, code) {
29
29
  */
30
30
  function countNewlines(code, from, end) {
31
31
  let count = 0;
32
- for (let i = code.indexOf("\n", from); i !== -1 && i < end; ) {
32
+ for (let i = code.indexOf("\n", from); i !== -1 && i < end;) {
33
33
  count++;
34
34
  i = code.indexOf("\n", i + 1);
35
35
  }
@@ -0,0 +1,199 @@
1
+ /**
2
+ * Undo history for HighlightEditable. Each entry stores only the changed
3
+ * span, so memory grows with edits, not with document size. Framework-free:
4
+ * the caller owns the code, the DOM, and the events.
5
+ */
6
+ import { diffText } from "./text-diff.js";
7
+
8
+ /** Typing within this many ms of the last edit joins one undo step. */
9
+ export const COALESCE_MS = 250;
10
+
11
+ /**
12
+ * @typedef {{ start: number, end: number }} SelectionRange
13
+ * @typedef {{
14
+ * start: number,
15
+ * removed: string,
16
+ * inserted: string,
17
+ * resultLength: number,
18
+ * selectionBefore: SelectionRange,
19
+ * selectionAfter: SelectionRange,
20
+ * }} HistoryEntry
21
+ * @typedef {{ code: string, selection: SelectionRange }} HistoryMove
22
+ * @typedef {{
23
+ * entries: { size: number }[],
24
+ * index: number,
25
+ * canUndo: boolean,
26
+ * canRedo: boolean,
27
+ * }} HistorySnapshot
28
+ */
29
+
30
+ /**
31
+ * @param {string} before
32
+ * @param {string} after
33
+ * @param {SelectionRange} selectionBefore
34
+ * @param {SelectionRange} selectionAfter
35
+ * @returns {HistoryEntry}
36
+ */
37
+ function makeEntry(before, after, selectionBefore, selectionAfter) {
38
+ const { start, removed, inserted } = diffText(before, after);
39
+ return {
40
+ start,
41
+ removed,
42
+ inserted,
43
+ resultLength: after.length,
44
+ selectionBefore,
45
+ selectionAfter,
46
+ };
47
+ }
48
+
49
+ /**
50
+ * The code an entry transformed *from*, given the code it produced.
51
+ * @param {HistoryEntry} entry
52
+ * @param {string} resultCode
53
+ */
54
+ function beforeCodeOf(entry, resultCode) {
55
+ return (
56
+ resultCode.slice(0, entry.start) +
57
+ entry.removed +
58
+ resultCode.slice(entry.start + entry.inserted.length)
59
+ );
60
+ }
61
+
62
+ /**
63
+ * @param {{ limit: () => number, now?: () => number }} options `limit` is
64
+ * read on each record, so a changed prop applies to the next edit.
65
+ */
66
+ export function createEditHistory({ limit, now = Date.now }) {
67
+ /** @type {HistoryEntry[]} */
68
+ let undoStack = [];
69
+ /** @type {HistoryEntry[]} */
70
+ let redoStack = [];
71
+ let lastRecord = 0;
72
+
73
+ return {
74
+ /**
75
+ * Starts over with `code` as the only, un-undoable state.
76
+ * @param {string} code
77
+ */
78
+ reset(code) {
79
+ undoStack = [
80
+ {
81
+ start: 0,
82
+ removed: "",
83
+ inserted: code,
84
+ resultLength: code.length,
85
+ selectionBefore: { start: 0, end: 0 },
86
+ selectionAfter: { start: 0, end: 0 },
87
+ },
88
+ ];
89
+ redoStack = [];
90
+ lastRecord = 0;
91
+ },
92
+
93
+ /**
94
+ * Records an edit. With `coalesce`, typing within `COALESCE_MS` joins
95
+ * the previous step; Enter, Tab and paste pass `false`.
96
+ * @param {string} previousCode
97
+ * @param {string} newCode
98
+ * @param {SelectionRange} selectionAfter
99
+ * @param {boolean} coalesce
100
+ */
101
+ record(previousCode, newCode, selectionAfter, coalesce) {
102
+ redoStack = [];
103
+ const time = now();
104
+ const top = /** @type {HistoryEntry} */ (undoStack.at(-1));
105
+
106
+ if (coalesce && undoStack.length > 1 && time - lastRecord < COALESCE_MS) {
107
+ const burstStart = beforeCodeOf(top, previousCode);
108
+ undoStack[undoStack.length - 1] = makeEntry(
109
+ burstStart,
110
+ newCode,
111
+ top.selectionBefore,
112
+ selectionAfter,
113
+ );
114
+ } else {
115
+ undoStack.push(
116
+ makeEntry(previousCode, newCode, top.selectionAfter, selectionAfter),
117
+ );
118
+ const max = Math.max(1, limit());
119
+ if (undoStack.length > max) {
120
+ undoStack.splice(0, undoStack.length - max);
121
+ }
122
+ }
123
+ lastRecord = coalesce ? time : 0;
124
+ },
125
+
126
+ /**
127
+ * Steps back from `code`. Null when there is nothing to undo.
128
+ * @param {string} code
129
+ * @returns {HistoryMove | null}
130
+ */
131
+ undo(code) {
132
+ if (undoStack.length <= 1) return null;
133
+ const entry = /** @type {HistoryEntry} */ (undoStack.pop());
134
+ redoStack.push(entry);
135
+ return {
136
+ code: beforeCodeOf(entry, code),
137
+ selection: entry.selectionBefore,
138
+ };
139
+ },
140
+
141
+ /**
142
+ * Steps forward from `code`. Null when there is nothing to redo.
143
+ * @param {string} code
144
+ * @returns {HistoryMove | null}
145
+ */
146
+ redo(code) {
147
+ const entry = redoStack.pop();
148
+ if (!entry) return null;
149
+ undoStack.push(entry);
150
+ return {
151
+ code:
152
+ code.slice(0, entry.start) +
153
+ entry.inserted +
154
+ code.slice(entry.start + entry.removed.length),
155
+ selection: entry.selectionAfter,
156
+ };
157
+ },
158
+
159
+ /**
160
+ * Moves the latest step's caret to where the user put it, so undoing
161
+ * the next edit restores it. Skipped when `codeLength` shows the top
162
+ * entry is stale.
163
+ * @param {number} codeLength
164
+ * @param {SelectionRange} selection
165
+ */
166
+ moveCaret(codeLength, selection) {
167
+ const top = undoStack.at(-1);
168
+ if (!top || top.resultLength !== codeLength) return;
169
+ undoStack[undoStack.length - 1] = {
170
+ ...top,
171
+ selectionAfter: { start: selection.start, end: selection.end },
172
+ };
173
+ },
174
+
175
+ get isEmpty() {
176
+ return undoStack.length === 0;
177
+ },
178
+
179
+ canUndo() {
180
+ return undoStack.length > 1;
181
+ },
182
+
183
+ canRedo() {
184
+ return redoStack.length > 0;
185
+ },
186
+
187
+ /** @returns {HistorySnapshot} */
188
+ snapshot() {
189
+ return {
190
+ entries: [...undoStack, ...redoStack.slice().reverse()].map(
191
+ (entry) => ({ size: entry.resultLength }),
192
+ ),
193
+ index: undoStack.length - 1,
194
+ canUndo: undoStack.length > 1,
195
+ canRedo: redoStack.length > 0,
196
+ };
197
+ },
198
+ };
199
+ }
@@ -6,6 +6,10 @@
6
6
  /** `NodeFilter.SHOW_TEXT`, inlined so this module loads outside a browser. */
7
7
  const SHOW_TEXT = 4;
8
8
 
9
+ /** `Node.TEXT_NODE` and `Node.ELEMENT_NODE`, inlined for the same reason. */
10
+ const TEXT_NODE = 3;
11
+ const ELEMENT_NODE = 1;
12
+
9
13
  /**
10
14
  * @typedef {{
11
15
  * lineEls: HTMLElement[],
@@ -206,7 +210,7 @@ export function editorNodeAtOffset(editor, view, offset) {
206
210
  const walker = doc.createTreeWalker(editor, SHOW_TEXT, null);
207
211
  walker.currentNode = separator;
208
212
  let count = lineStart - 1;
209
- for (let node = /** @type {Node | null} */ (separator); node; ) {
213
+ for (let node = /** @type {Node | null} */ (separator); node;) {
210
214
  const next = count + /** @type {string} */ (node.textContent).length;
211
215
  if (offset <= next) return { node, offset: offset - count };
212
216
  count = next;
@@ -214,3 +218,174 @@ export function editorNodeAtOffset(editor, view, offset) {
214
218
  }
215
219
  return null;
216
220
  }
221
+
222
+ /**
223
+ * @param {Node} el
224
+ * @param {number} count
225
+ */
226
+ function sumChildLengths(el, count) {
227
+ let total = 0;
228
+ for (let i = 0; i < count && i < el.childNodes.length; i++) {
229
+ const child = /** @type {Node} */ (el.childNodes[i]);
230
+ total += /** @type {string} */ (child.textContent).length;
231
+ }
232
+ return total;
233
+ }
234
+
235
+ /**
236
+ * Character offset of a Selection boundary, derived from the line structure
237
+ * instead of serializing the document. Null when the DOM has drifted from
238
+ * `view`, so the caller falls back to cloneRange/toString.
239
+ * @param {HTMLElement} editor
240
+ * @param {LineView} view
241
+ * @param {Node} container
242
+ * @param {number} offsetInContainer
243
+ * @returns {number | null}
244
+ */
245
+ export function offsetOfBoundary(editor, view, container, offsetInContainer) {
246
+ const { lineEls, lineLengths } = view;
247
+ const expectedChildren = lineEls.length === 0 ? 0 : lineEls.length * 2 - 1;
248
+ if (editor.childNodes.length !== expectedChildren) return null;
249
+
250
+ if (container === editor) {
251
+ if (lineEls.length === 0) return offsetInContainer === 0 ? 0 : null;
252
+ const lineIndex = offsetInContainer >> 1;
253
+ if (lineIndex >= lineEls.length) return null;
254
+ return offsetInContainer % 2 === 0
255
+ ? lineStartOffset(view, lineIndex)
256
+ : lineStartOffset(view, lineIndex) +
257
+ /** @type {number} */ (lineLengths[lineIndex]);
258
+ }
259
+
260
+ let node = container;
261
+ while (node.parentNode !== editor) {
262
+ if (!node.parentNode) return null;
263
+ node = node.parentNode;
264
+ }
265
+
266
+ let childIndex = 0;
267
+ for (let sib = node.previousSibling; sib; sib = sib.previousSibling) {
268
+ childIndex++;
269
+ }
270
+ const lineIndex = childIndex >> 1;
271
+
272
+ if (childIndex % 2 === 1) {
273
+ // A "\n" separator has no children, so it must be `container` itself.
274
+ if (container !== node || lineIndex >= lineLengths.length) return null;
275
+ return (
276
+ lineStartOffset(view, lineIndex) +
277
+ /** @type {number} */ (lineLengths[lineIndex]) +
278
+ offsetInContainer
279
+ );
280
+ }
281
+
282
+ if (lineIndex >= lineEls.length || lineEls[lineIndex] !== node) return null;
283
+
284
+ let charOffset =
285
+ container.nodeType === TEXT_NODE
286
+ ? offsetInContainer
287
+ : sumChildLengths(container, offsetInContainer);
288
+ let n = container;
289
+ while (n !== node) {
290
+ for (let sib = n.previousSibling; sib; sib = sib.previousSibling) {
291
+ charOffset += /** @type {string} */ (sib.textContent).length;
292
+ }
293
+ if (!n.parentNode) return null;
294
+ n = n.parentNode;
295
+ }
296
+ return lineStartOffset(view, lineIndex) + charOffset;
297
+ }
298
+
299
+ /**
300
+ * The editor's selection as character offsets, or null when it lies outside
301
+ * `editor`.
302
+ * @param {HTMLElement} editor
303
+ * @param {LineView} view
304
+ * @param {Selection | null} selection
305
+ * @returns {{ start: number, end: number } | null}
306
+ */
307
+ export function readSelection(editor, view, selection) {
308
+ if (!selection || selection.rangeCount === 0) return null;
309
+ const range = selection.getRangeAt(0);
310
+ if (!editor.contains(range.commonAncestorContainer)) return null;
311
+ const start = offsetOfBoundary(
312
+ editor,
313
+ view,
314
+ range.startContainer,
315
+ range.startOffset,
316
+ );
317
+ const end = offsetOfBoundary(
318
+ editor,
319
+ view,
320
+ range.endContainer,
321
+ range.endOffset,
322
+ );
323
+ if (start != null && end != null) return { start, end };
324
+ const pre = range.cloneRange();
325
+ pre.selectNodeContents(editor);
326
+ pre.setEnd(range.startContainer, range.startOffset);
327
+ const fallbackStart = pre.toString().length;
328
+ return { start: fallbackStart, end: fallbackStart + range.toString().length };
329
+ }
330
+
331
+ /**
332
+ * Firefox: a boundary at a text node's edge breaks its native undo after a
333
+ * repaint, so anchor edges via setStartBefore/After. A boundary at a
334
+ * separator's end goes inside the next line's `<span>`, else Firefox types
335
+ * into a stray text node (forcing a full rebuild).
336
+ * @param {HTMLElement} editor
337
+ * @param {Range} range
338
+ * @param {"Start" | "End"} side
339
+ * @param {Node} node
340
+ * @param {number} offset
341
+ */
342
+ function setRangeBoundary(editor, range, side, node, offset) {
343
+ const length = /** @type {string} */ (node.textContent).length;
344
+ const nextLine = node.nextSibling;
345
+ if (
346
+ node.parentNode === editor &&
347
+ offset === length &&
348
+ nextLine?.nodeType === ELEMENT_NODE
349
+ ) {
350
+ range[side === "Start" ? "setStart" : "setEnd"](nextLine, 0);
351
+ } else if (offset === 0) range[`set${side}Before`](node);
352
+ else if (offset === length) range[`set${side}After`](node);
353
+ else range[side === "Start" ? "setStart" : "setEnd"](node, offset);
354
+ }
355
+
356
+ /**
357
+ * Selects characters `[start, end)` of `root`, or puts the caret at `start`
358
+ * when `end` is null or equal. `root` defaults to the editor; pass another
359
+ * element (such as a cloned preview) to select inside it.
360
+ * @param {HTMLElement} editor
361
+ * @param {LineView} view
362
+ * @param {Selection | null} selection
363
+ * @param {number} start
364
+ * @param {number | null} [end]
365
+ * @param {Node} [root]
366
+ */
367
+ export function writeSelection(
368
+ editor,
369
+ view,
370
+ selection,
371
+ start,
372
+ end,
373
+ root = editor,
374
+ ) {
375
+ if (!selection) return;
376
+ /** @param {number} offset */
377
+ const locate = (offset) =>
378
+ root === editor
379
+ ? editorNodeAtOffset(editor, view, offset)
380
+ : nodeAtOffset(root, offset);
381
+ const from = locate(start);
382
+ if (!from) return;
383
+ const doc = /** @type {Document} */ (editor.ownerDocument);
384
+ const range = doc.createRange();
385
+ setRangeBoundary(editor, range, "Start", from.node, from.offset);
386
+ const to = end != null && end !== start ? locate(end) : null;
387
+ if (to) setRangeBoundary(editor, range, "End", to.node, to.offset);
388
+ else range.collapse(true);
389
+ selection.removeAllRanges();
390
+ selection.addRange(range);
391
+ }