@pierre/diffs 1.3.0-beta.9 → 1.3.0-rc.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.
- package/README.md +9 -0
- package/dist/components/CodeView.d.ts +12 -13
- package/dist/components/CodeView.d.ts.map +1 -1
- package/dist/components/CodeView.js +50 -58
- package/dist/components/CodeView.js.map +1 -1
- package/dist/components/File.d.ts +10 -3
- package/dist/components/File.d.ts.map +1 -1
- package/dist/components/File.js +41 -10
- package/dist/components/File.js.map +1 -1
- package/dist/components/FileDiff.d.ts +37 -6
- package/dist/components/FileDiff.d.ts.map +1 -1
- package/dist/components/FileDiff.js +202 -19
- package/dist/components/FileDiff.js.map +1 -1
- package/dist/components/UnresolvedFile.d.ts.map +1 -1
- package/dist/components/UnresolvedFile.js +7 -2
- package/dist/components/UnresolvedFile.js.map +1 -1
- package/dist/components/VirtualizedFile.d.ts +2 -1
- package/dist/components/VirtualizedFile.d.ts.map +1 -1
- package/dist/components/VirtualizedFile.js +9 -5
- package/dist/components/VirtualizedFile.js.map +1 -1
- package/dist/components/VirtualizedFileDiff.d.ts +14 -0
- package/dist/components/VirtualizedFileDiff.d.ts.map +1 -1
- package/dist/components/VirtualizedFileDiff.js +61 -3
- package/dist/components/VirtualizedFileDiff.js.map +1 -1
- package/dist/components/Virtualizer.d.ts +14 -2
- package/dist/components/Virtualizer.d.ts.map +1 -1
- package/dist/components/Virtualizer.js +27 -2
- package/dist/components/Virtualizer.js.map +1 -1
- package/dist/edit/index.d.ts +5 -0
- package/dist/edit/index.js +3 -0
- package/dist/editor/command.d.ts +1 -1
- package/dist/editor/command.d.ts.map +1 -1
- package/dist/editor/command.js +11 -0
- package/dist/editor/command.js.map +1 -1
- package/dist/editor/editStack.d.ts +6 -2
- package/dist/editor/editStack.d.ts.map +1 -1
- package/dist/editor/editStack.js +53 -12
- package/dist/editor/editStack.js.map +1 -1
- package/dist/editor/editor.d.ts +51 -23
- package/dist/editor/editor.d.ts.map +1 -1
- package/dist/editor/editor.js +977 -241
- package/dist/editor/editor.js.map +1 -1
- package/dist/editor/editor2.js +1 -1
- package/dist/editor/languages.d.ts +26 -0
- package/dist/editor/languages.d.ts.map +1 -0
- package/dist/editor/languages.js +363 -0
- package/dist/editor/languages.js.map +1 -0
- package/dist/editor/lineAnnotations.d.ts.map +1 -1
- package/dist/editor/lineAnnotations.js +25 -26
- package/dist/editor/lineAnnotations.js.map +1 -1
- package/dist/editor/marker.d.ts +5 -3
- package/dist/editor/marker.d.ts.map +1 -1
- package/dist/editor/marker.js +111 -91
- package/dist/editor/marker.js.map +1 -1
- package/dist/editor/matchBrackets.d.ts +3 -2
- package/dist/editor/matchBrackets.d.ts.map +1 -1
- package/dist/editor/matchBrackets.js +2 -10
- package/dist/editor/matchBrackets.js.map +1 -1
- package/dist/editor/pieceTable.d.ts +2 -1
- package/dist/editor/pieceTable.d.ts.map +1 -1
- package/dist/editor/pieceTable.js +103 -39
- package/dist/editor/pieceTable.js.map +1 -1
- package/dist/editor/platform.js +2 -0
- package/dist/editor/platform.js.map +1 -1
- package/dist/editor/popover.d.ts +25 -5
- package/dist/editor/popover.d.ts.map +1 -1
- package/dist/editor/popover.js +60 -14
- package/dist/editor/popover.js.map +1 -1
- package/dist/editor/searchPanel.d.ts.map +1 -1
- package/dist/editor/searchPanel.js +9 -2
- package/dist/editor/searchPanel.js.map +1 -1
- package/dist/editor/selection.d.ts +31 -11
- package/dist/editor/selection.d.ts.map +1 -1
- package/dist/editor/selection.js +198 -70
- package/dist/editor/selection.js.map +1 -1
- package/dist/editor/selectionAction.d.ts +4 -3
- package/dist/editor/selectionAction.d.ts.map +1 -1
- package/dist/editor/selectionAction.js +9 -5
- package/dist/editor/selectionAction.js.map +1 -1
- package/dist/editor/stateStorage.d.ts +15 -0
- package/dist/editor/stateStorage.d.ts.map +1 -0
- package/dist/editor/stateStorage.js +65 -0
- package/dist/editor/stateStorage.js.map +1 -0
- package/dist/editor/textDocument.d.ts +12 -77
- package/dist/editor/textDocument.d.ts.map +1 -1
- package/dist/editor/textDocument.js +76 -34
- package/dist/editor/textDocument.js.map +1 -1
- package/dist/editor/textMeasure.d.ts +7 -12
- package/dist/editor/textMeasure.d.ts.map +1 -1
- package/dist/editor/textMeasure.js +40 -32
- package/dist/editor/textMeasure.js.map +1 -1
- package/dist/editor/{tokenzier.d.ts → tokenizer.d.ts} +3 -3
- package/dist/editor/tokenizer.d.ts.map +1 -0
- package/dist/editor/{tokenzier.js → tokenizer.js} +170 -50
- package/dist/editor/tokenizer.js.map +1 -0
- package/dist/editor/utils.js +1 -1
- package/dist/editor/utils.js.map +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +6 -5
- package/dist/managers/InteractionManager.d.ts +18 -7
- package/dist/managers/InteractionManager.d.ts.map +1 -1
- package/dist/managers/InteractionManager.js +154 -99
- package/dist/managers/InteractionManager.js.map +1 -1
- package/dist/managers/UniversalRenderingManager.d.ts.map +1 -1
- package/dist/managers/UniversalRenderingManager.js +1 -2
- package/dist/managers/UniversalRenderingManager.js.map +1 -1
- package/dist/react/CodeView.d.ts +13 -13
- package/dist/react/CodeView.d.ts.map +1 -1
- package/dist/react/CodeView.js +37 -28
- package/dist/react/CodeView.js.map +1 -1
- package/dist/react/EditContext.d.ts +20 -0
- package/dist/react/EditContext.d.ts.map +1 -0
- package/dist/react/EditContext.js +20 -0
- package/dist/react/EditContext.js.map +1 -0
- package/dist/react/File.d.ts +2 -1
- package/dist/react/File.d.ts.map +1 -1
- package/dist/react/File.js +3 -2
- package/dist/react/File.js.map +1 -1
- package/dist/react/FileDiff.d.ts +2 -2
- package/dist/react/FileDiff.d.ts.map +1 -1
- package/dist/react/FileDiff.js +3 -2
- package/dist/react/FileDiff.js.map +1 -1
- package/dist/react/MultiFileDiff.d.ts +2 -2
- package/dist/react/MultiFileDiff.d.ts.map +1 -1
- package/dist/react/MultiFileDiff.js +3 -2
- package/dist/react/MultiFileDiff.js.map +1 -1
- package/dist/react/PatchDiff.d.ts +2 -2
- package/dist/react/PatchDiff.d.ts.map +1 -1
- package/dist/react/PatchDiff.js +3 -2
- package/dist/react/PatchDiff.js.map +1 -1
- package/dist/react/UnresolvedFile.d.ts +1 -1
- package/dist/react/UnresolvedFile.js.map +1 -1
- package/dist/react/index.d.ts +4 -4
- package/dist/react/index.js +3 -3
- package/dist/react/types.d.ts +9 -1
- package/dist/react/types.d.ts.map +1 -1
- package/dist/react/utils/useFileDiffInstance.d.ts +5 -3
- package/dist/react/utils/useFileDiffInstance.d.ts.map +1 -1
- package/dist/react/utils/useFileDiffInstance.js +24 -34
- package/dist/react/utils/useFileDiffInstance.js.map +1 -1
- package/dist/react/utils/useFileInstance.d.ts +5 -3
- package/dist/react/utils/useFileInstance.d.ts.map +1 -1
- package/dist/react/utils/useFileInstance.js +23 -33
- package/dist/react/utils/useFileInstance.js.map +1 -1
- package/dist/react/utils/useUnresolvedFileInstance.js +4 -4
- package/dist/react/utils/useUnresolvedFileInstance.js.map +1 -1
- package/dist/renderers/DiffHunksRenderer.d.ts +41 -1
- package/dist/renderers/DiffHunksRenderer.d.ts.map +1 -1
- package/dist/renderers/DiffHunksRenderer.js +167 -29
- package/dist/renderers/DiffHunksRenderer.js.map +1 -1
- package/dist/renderers/FileRenderer.d.ts +20 -1
- package/dist/renderers/FileRenderer.d.ts.map +1 -1
- package/dist/renderers/FileRenderer.js +119 -29
- package/dist/renderers/FileRenderer.js.map +1 -1
- package/dist/ssr/index.d.ts +2 -2
- package/dist/ssr/preloadDiffs.d.ts.map +1 -1
- package/dist/ssr/preloadDiffs.js +3 -1
- package/dist/ssr/preloadDiffs.js.map +1 -1
- package/dist/ssr/preloadFile.d.ts.map +1 -1
- package/dist/ssr/preloadFile.js +2 -0
- package/dist/ssr/preloadFile.js.map +1 -1
- package/dist/style.js +1 -1
- package/dist/types.d.ts +192 -42
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/annotationHelpers.d.ts +20 -0
- package/dist/utils/annotationHelpers.d.ts.map +1 -0
- package/dist/utils/annotationHelpers.js +29 -0
- package/dist/utils/annotationHelpers.js.map +1 -0
- package/dist/utils/computeFileOffsets.d.ts.map +1 -1
- package/dist/utils/computeFileOffsets.js +34 -5
- package/dist/utils/computeFileOffsets.js.map +1 -1
- package/dist/utils/editSessionHunks.d.ts +66 -0
- package/dist/utils/editSessionHunks.d.ts.map +1 -0
- package/dist/utils/editSessionHunks.js +437 -0
- package/dist/utils/editSessionHunks.js.map +1 -0
- package/dist/utils/getDiffHunksRendererOptions.d.ts.map +1 -1
- package/dist/utils/getDiffHunksRendererOptions.js +2 -1
- package/dist/utils/getDiffHunksRendererOptions.js.map +1 -1
- package/dist/utils/getFileRendererOptions.d.ts.map +1 -1
- package/dist/utils/getFileRendererOptions.js +2 -1
- package/dist/utils/getFileRendererOptions.js.map +1 -1
- package/dist/utils/getHunkSideBoundaries.d.ts +7 -0
- package/dist/utils/getHunkSideBoundaries.d.ts.map +1 -0
- package/dist/utils/getHunkSideBoundaries.js +12 -0
- package/dist/utils/getHunkSideBoundaries.js.map +1 -0
- package/dist/utils/getTotalLineCountFromHunks.d.ts.map +1 -1
- package/dist/utils/getTotalLineCountFromHunks.js +2 -1
- package/dist/utils/getTotalLineCountFromHunks.js.map +1 -1
- package/dist/utils/hydratePartialDiff.d.ts.map +1 -1
- package/dist/utils/hydratePartialDiff.js +4 -3
- package/dist/utils/hydratePartialDiff.js.map +1 -1
- package/dist/utils/isGutterUtilityPath.d.ts +5 -0
- package/dist/utils/isGutterUtilityPath.d.ts.map +1 -0
- package/dist/utils/isGutterUtilityPath.js +12 -0
- package/dist/utils/isGutterUtilityPath.js.map +1 -0
- package/dist/utils/iterateOverDiff.d.ts.map +1 -1
- package/dist/utils/iterateOverDiff.js +17 -12
- package/dist/utils/iterateOverDiff.js.map +1 -1
- package/dist/utils/parseMergeConflictDiffFromFile.d.ts.map +1 -1
- package/dist/utils/parseMergeConflictDiffFromFile.js +9 -6
- package/dist/utils/parseMergeConflictDiffFromFile.js.map +1 -1
- package/dist/utils/parsePatchFiles.d.ts.map +1 -1
- package/dist/utils/parsePatchFiles.js +6 -3
- package/dist/utils/parsePatchFiles.js.map +1 -1
- package/dist/utils/realignChangeContent.d.ts +33 -0
- package/dist/utils/realignChangeContent.d.ts.map +1 -0
- package/dist/utils/realignChangeContent.js +148 -0
- package/dist/utils/realignChangeContent.js.map +1 -0
- package/dist/utils/renderDiffWithHighlighter.js +1 -1
- package/dist/utils/resolveRegion.d.ts.map +1 -1
- package/dist/utils/resolveRegion.js +18 -2
- package/dist/utils/resolveRegion.js.map +1 -1
- package/dist/utils/shouldUseTokenTransformer.d.ts +7 -3
- package/dist/utils/shouldUseTokenTransformer.d.ts.map +1 -1
- package/dist/utils/shouldUseTokenTransformer.js +1 -1
- package/dist/utils/shouldUseTokenTransformer.js.map +1 -1
- package/dist/utils/updateDiffHunks.d.ts +8 -2
- package/dist/utils/updateDiffHunks.d.ts.map +1 -1
- package/dist/utils/updateDiffHunks.js +9 -7
- package/dist/utils/updateDiffHunks.js.map +1 -1
- package/dist/utils/virtualDiffLayout.d.ts +42 -2
- package/dist/utils/virtualDiffLayout.d.ts.map +1 -1
- package/dist/utils/virtualDiffLayout.js +101 -7
- package/dist/utils/virtualDiffLayout.js.map +1 -1
- package/dist/worker/WorkerPoolManager.d.ts +2 -1
- package/dist/worker/WorkerPoolManager.d.ts.map +1 -1
- package/dist/worker/WorkerPoolManager.js +3 -0
- package/dist/worker/WorkerPoolManager.js.map +1 -1
- package/dist/worker/worker-portable.js +60 -20
- package/dist/worker/worker-portable.js.map +1 -1
- package/dist/worker/worker.js +61 -19
- package/dist/worker/worker.js.map +1 -1
- package/package.json +10 -7
- package/dist/editor/index.d.ts +0 -3
- package/dist/editor/index.js +0 -3
- package/dist/editor/tokenzier.d.ts.map +0 -1
- package/dist/editor/tokenzier.js.map +0 -1
- package/dist/react/EditorContext.d.ts +0 -15
- package/dist/react/EditorContext.d.ts.map +0 -1
- package/dist/react/EditorContext.js +0 -23
- package/dist/react/EditorContext.js.map +0 -1
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { DEFAULT_THEMES } from "../constants.js";
|
|
2
2
|
import { addEventListener, debounce, h } from "./utils.js";
|
|
3
|
+
import { colorUtils } from "@pierre/theming/color";
|
|
3
4
|
import { EncodedTokenMetadata, INITIAL } from "shiki/textmate";
|
|
4
|
-
//#region src/editor/
|
|
5
|
+
//#region src/editor/tokenizer.ts
|
|
5
6
|
const TOKENIZE_TIME_LIMIT = 500;
|
|
7
|
+
let nextTokenizerId = 0;
|
|
6
8
|
/** Stoppable code tokenizer for the editor */
|
|
7
9
|
var EditorTokenizer = class {
|
|
8
10
|
#highlighter;
|
|
9
11
|
#grammar;
|
|
10
12
|
#mediaQueryList;
|
|
11
|
-
#themeType;
|
|
13
|
+
#themeType = "dark";
|
|
12
14
|
#themeName = "";
|
|
13
15
|
#colorMap;
|
|
14
16
|
#textDocument;
|
|
@@ -19,28 +21,39 @@ var EditorTokenizer = class {
|
|
|
19
21
|
#matchBrackets;
|
|
20
22
|
#debug;
|
|
21
23
|
#disposes;
|
|
24
|
+
#isCleanedUp = false;
|
|
22
25
|
#stateStack = [INITIAL];
|
|
26
|
+
#comparisonStateStack = [];
|
|
27
|
+
#comparisonStateStackStart = 0;
|
|
28
|
+
#comparisonLineChanges = [];
|
|
23
29
|
#lastLine = -1;
|
|
24
30
|
#isStopped = true;
|
|
25
31
|
#isPaused = false;
|
|
26
32
|
#backgroundJobId = 0;
|
|
33
|
+
#tokenizerId = ++nextTokenizerId;
|
|
34
|
+
#backgroundPrebuildEndLine = -1;
|
|
35
|
+
#pendingPrebuildEndLine = -1;
|
|
27
36
|
#backgroundChangedLineRanges;
|
|
28
37
|
#backgroundChangedRangeIndex = 0;
|
|
29
|
-
#bracketIgnoredRanges =
|
|
38
|
+
#bracketIgnoredRanges = [];
|
|
30
39
|
#isMessageListenerAttached = false;
|
|
31
40
|
#prebuildStateStack = debounce(async (renderRange) => {
|
|
41
|
+
if (this.#isCleanedUp) return;
|
|
32
42
|
const { startingLine = 0, totalLines = Infinity } = renderRange ?? {};
|
|
33
43
|
const endLine = Math.min(totalLines === Infinity ? Infinity : startingLine + totalLines, this.#textDocument.lineCount);
|
|
34
44
|
if (this.#grammar === void 0 && !isGrammarlessLanguage(this.#textDocument.languageId)) {
|
|
35
45
|
await this.#highlighter.loadLanguage(this.#textDocument.languageId);
|
|
46
|
+
if (this.#isCleanedUp) return;
|
|
36
47
|
this.#grammar = this.#highlighter.getLanguage(this.#textDocument.languageId);
|
|
37
48
|
}
|
|
38
|
-
this.#
|
|
49
|
+
this.#ensureActiveTheme();
|
|
50
|
+
this.#scheduleStatePrebuild(endLine);
|
|
39
51
|
}, 500);
|
|
40
52
|
#onMessage = ({ data }) => {
|
|
41
53
|
if (typeof data !== "object" || data === null) return;
|
|
42
|
-
const { type, jobId } = data;
|
|
43
|
-
if (type === "tokenize" && typeof jobId === "number" && jobId === this.#backgroundJobId) this.#
|
|
54
|
+
const { type, tokenizerId, jobId } = data;
|
|
55
|
+
if (type === "tokenize" && tokenizerId === this.#tokenizerId && typeof jobId === "number" && jobId === this.#backgroundJobId) if (this.#backgroundPrebuildEndLine >= 0) this.#backgroundPrebuild(jobId);
|
|
56
|
+
else this.#backgroundTokenize(jobId);
|
|
44
57
|
};
|
|
45
58
|
get themeType() {
|
|
46
59
|
return this.#themeType;
|
|
@@ -49,31 +62,32 @@ var EditorTokenizer = class {
|
|
|
49
62
|
if (!this.#matchBrackets || lineIndex < 0 || lineIndex >= this.#textDocument.lineCount) return null;
|
|
50
63
|
this.#ensureGrammar();
|
|
51
64
|
if (this.#grammar === void 0) return null;
|
|
52
|
-
if (
|
|
65
|
+
if (this.#bracketIgnoredRanges[lineIndex] === void 0) {
|
|
53
66
|
this.#buildStateStack(lineIndex);
|
|
54
67
|
const state = this.#stateStack[lineIndex] ?? INITIAL;
|
|
55
68
|
const result = this.#tokenizeLineAt(lineIndex, state);
|
|
56
69
|
this.#stateStack[lineIndex + 1] = result.state;
|
|
57
70
|
}
|
|
58
|
-
return this.#bracketIgnoredRanges
|
|
71
|
+
return this.#bracketIgnoredRanges[lineIndex] ?? null;
|
|
59
72
|
}
|
|
60
73
|
constructor({ codeOptions, highlighter, textDocument, matchBrackets, setStyle, onDeferTokenize, onThemeChange, __debug }) {
|
|
61
|
-
const { themeType = "system", theme = DEFAULT_THEMES, tokenizeMaxLineLength = 1e3 } = codeOptions;
|
|
74
|
+
const { themeType: themeTypeOption = "system", theme = DEFAULT_THEMES, tokenizeMaxLineLength = 1e3 } = codeOptions;
|
|
62
75
|
this.#mediaQueryList = window.matchMedia("(prefers-color-scheme: dark)");
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
76
|
+
let themeType;
|
|
77
|
+
if (themeTypeOption === "system") themeType = this.#resolveSystemThemeType();
|
|
78
|
+
else themeType = themeTypeOption;
|
|
79
|
+
if (typeof theme !== "string" && themeTypeOption === "system") {
|
|
66
80
|
const observer = new MutationObserver((mutations) => {
|
|
67
81
|
for (const { type, attributeName } of mutations) if (type === "attributes" && attributeName !== null && (attributeName === "class" || attributeName.startsWith("data-"))) {
|
|
68
|
-
const themeType =
|
|
82
|
+
const themeType = this.#resolveSystemThemeType();
|
|
69
83
|
this.#emitThemeChange(theme[themeType], themeType);
|
|
70
84
|
break;
|
|
71
85
|
}
|
|
72
86
|
});
|
|
73
87
|
observer.observe(document.documentElement, { attributes: true });
|
|
74
88
|
observer.observe(document.body, { attributes: true });
|
|
75
|
-
this.#disposes = [addEventListener(this.#mediaQueryList, "change", (
|
|
76
|
-
const themeType =
|
|
89
|
+
this.#disposes = [addEventListener(this.#mediaQueryList, "change", () => {
|
|
90
|
+
const themeType = this.#resolveSystemThemeType();
|
|
77
91
|
this.#emitThemeChange(theme[themeType], themeType);
|
|
78
92
|
}), () => observer.disconnect()];
|
|
79
93
|
}
|
|
@@ -87,29 +101,44 @@ var EditorTokenizer = class {
|
|
|
87
101
|
this.#debug = __debug ?? false;
|
|
88
102
|
this.#ensureGrammar();
|
|
89
103
|
this.#colorMap = [];
|
|
90
|
-
this.#setTheme(typeof theme === "string" ? theme : theme[
|
|
104
|
+
this.#setTheme(typeof theme === "string" ? theme : theme[themeType], themeType);
|
|
91
105
|
}
|
|
92
106
|
#emitThemeChange(themeName, themeType) {
|
|
93
|
-
this.#themeType
|
|
94
|
-
this.#setTheme(themeName);
|
|
107
|
+
if (themeName === this.#themeName && themeType === this.#themeType) return;
|
|
108
|
+
this.#setTheme(themeName, themeType);
|
|
95
109
|
this.stopBackgroundTokenize();
|
|
96
110
|
this.#stateStack = [INITIAL];
|
|
111
|
+
this.#comparisonStateStack = [];
|
|
112
|
+
this.#comparisonStateStackStart = 0;
|
|
113
|
+
this.#comparisonLineChanges = [];
|
|
97
114
|
if (this.#grammar !== void 0 && this.#textDocument.lineCount > 0) this.#scheduleBackgroundTokenize(0);
|
|
98
115
|
this.#onThemeChange?.();
|
|
99
116
|
}
|
|
117
|
+
#resolveSystemThemeType() {
|
|
118
|
+
try {
|
|
119
|
+
if (typeof document !== "undefined" && typeof getComputedStyle === "function" && document.body != null) {
|
|
120
|
+
const colorSchemes = getComputedStyle(document.body).colorScheme.split(/\s+/);
|
|
121
|
+
const supportsDark = colorSchemes.includes("dark");
|
|
122
|
+
if (supportsDark !== colorSchemes.includes("light")) return supportsDark ? "dark" : "light";
|
|
123
|
+
}
|
|
124
|
+
} catch {}
|
|
125
|
+
return this.#mediaQueryList.matches ? "dark" : "light";
|
|
126
|
+
}
|
|
100
127
|
syncTheme(codeOptions) {
|
|
101
128
|
const { themeType = "system", theme = DEFAULT_THEMES } = codeOptions;
|
|
102
|
-
const nextThemeType = themeType === "system" ? this.#
|
|
129
|
+
const nextThemeType = themeType === "system" ? this.#resolveSystemThemeType() : themeType;
|
|
103
130
|
const nextThemeName = typeof theme === "string" ? theme : theme[nextThemeType];
|
|
104
131
|
if (nextThemeType === this.#themeType && nextThemeName === this.#themeName) return;
|
|
105
132
|
this.#emitThemeChange(nextThemeName, nextThemeType);
|
|
106
133
|
}
|
|
107
|
-
#setTheme(themeName) {
|
|
108
|
-
|
|
109
|
-
this.#colorMap = this.#highlighter.setTheme(themeName).colorMap;
|
|
134
|
+
#setTheme(themeName, themeType) {
|
|
135
|
+
const { theme, colorMap } = this.#highlighter.setTheme(themeName);
|
|
110
136
|
const { colors = {} } = this.#highlighter.getTheme(themeName);
|
|
111
137
|
const selectionBackground = colors["editor.selectionBackground"];
|
|
112
|
-
const
|
|
138
|
+
const themeLineHighlightBackground = colors["editor.lineHighlightBackground"];
|
|
139
|
+
const lineHighlightBackground = themeLineHighlightBackground != null && themeLineHighlightBackground.trim() !== "" && !colorUtils.isFullyTransparent(themeLineHighlightBackground) ? themeLineHighlightBackground : void 0;
|
|
140
|
+
const lineHighlightBorder = colors["editor.lineHighlightBorder"] ?? (lineHighlightBackground == null ? "color-mix(in lab, var(--diffs-bg) 70%, var(--diffs-fg))" : "transparent");
|
|
141
|
+
const activeLineSourceMix = lineHighlightBackground == null ? "100%" : "85%";
|
|
113
142
|
const cursorForeground = colors["editorCursor.foreground"];
|
|
114
143
|
const findMatchBackground = colors["editor.findMatchBackground"];
|
|
115
144
|
const findMatchHighlightBackground = colors["editor.findMatchHighlightBackground"];
|
|
@@ -121,7 +150,8 @@ var EditorTokenizer = class {
|
|
|
121
150
|
const errorForeground = colors["editorError.foreground"];
|
|
122
151
|
this.#setStyle(`:host {
|
|
123
152
|
--diffs-editor-selection-bg: ${selectionBackground ?? "var(--diffs-line-bg)"};
|
|
124
|
-
--diffs-editor-line-highlight-
|
|
153
|
+
--diffs-editor-line-highlight-border: ${lineHighlightBorder};
|
|
154
|
+
--diffs-editor-active-line-source-mix: ${activeLineSourceMix};
|
|
125
155
|
--diffs-editor-match-bg: ${findMatchBackground ?? "initial"};
|
|
126
156
|
--diffs-editor-match-highlight-bg: ${findMatchHighlightBackground ?? "initial"};
|
|
127
157
|
--diffs-editor-bracket-match-bg: ${bracketMatchBackground ?? "initial"};
|
|
@@ -132,8 +162,17 @@ var EditorTokenizer = class {
|
|
|
132
162
|
--diffs-editor-warning-fg: ${warningForeground ?? "initial"};
|
|
133
163
|
--diffs-editor-error-fg: ${errorForeground ?? "initial"};
|
|
134
164
|
}`);
|
|
165
|
+
this.#themeName = themeName;
|
|
166
|
+
this.#themeType = themeType ?? theme.type;
|
|
167
|
+
this.#colorMap = colorMap;
|
|
168
|
+
}
|
|
169
|
+
#ensureActiveTheme() {
|
|
170
|
+
if (this.#themeName === "") return;
|
|
171
|
+
const { colorMap } = this.#highlighter.setTheme(this.#themeName);
|
|
172
|
+
this.#colorMap = colorMap;
|
|
135
173
|
}
|
|
136
174
|
cleanUp() {
|
|
175
|
+
this.#isCleanedUp = true;
|
|
137
176
|
this.stopBackgroundTokenize();
|
|
138
177
|
this.#detachMessageListener();
|
|
139
178
|
this.#disposes?.forEach((dispose) => dispose());
|
|
@@ -141,19 +180,21 @@ var EditorTokenizer = class {
|
|
|
141
180
|
}
|
|
142
181
|
tokenize(change, renderRange) {
|
|
143
182
|
this.#ensureGrammar();
|
|
183
|
+
this.#ensureActiveTheme();
|
|
144
184
|
if (this.#grammar === void 0 && !isGrammarlessLanguage(this.#textDocument.languageId)) throw new Error(`Grammar for language "${this.#textDocument.languageId}" not loaded`);
|
|
145
|
-
if (this.#matchBrackets)
|
|
146
|
-
for (const line of this.#bracketIgnoredRanges.keys()) if (line >= change.startLine) this.#bracketIgnoredRanges.delete(line);
|
|
147
|
-
}
|
|
185
|
+
if (this.#matchBrackets) this.#bracketIgnoredRanges.length = Math.min(this.#bracketIgnoredRanges.length, change.startLine);
|
|
148
186
|
const { lineCount } = this.#textDocument;
|
|
149
187
|
const { startingLine = 0, totalLines = Infinity } = renderRange ?? {};
|
|
150
188
|
const renderRangeEndLine = totalLines === Infinity ? lineCount : Math.min(startingLine + totalLines, lineCount);
|
|
151
189
|
const dirtyStart = change.startLine;
|
|
152
190
|
const viewStart = Math.max(startingLine, dirtyStart);
|
|
153
191
|
const crossesRenderRangeEnd = renderRange !== void 0 && totalLines !== Infinity && change.lineDelta > 0 && dirtyStart < renderRangeEndLine && change.endLine >= renderRangeEndLine;
|
|
154
|
-
const canReuseCachedStates = change.lineDelta === 0;
|
|
192
|
+
const canReuseCachedStates = change.lineDelta === 0 && (change.changedLineChanges?.every(([, , lineDelta]) => lineDelta === 0) ?? true);
|
|
155
193
|
const canCacheTokenizedStates = canReuseCachedStates || renderRange === void 0 || dirtyStart >= viewStart;
|
|
156
|
-
const changedLineRanges =
|
|
194
|
+
const changedLineRanges = change.changedLineRanges ?? [[dirtyStart, change.endLine]];
|
|
195
|
+
this.#comparisonStateStack = [];
|
|
196
|
+
this.#comparisonStateStackStart = 0;
|
|
197
|
+
this.#comparisonLineChanges = [];
|
|
157
198
|
let offscreenSyncEnd = -1;
|
|
158
199
|
if (dirtyStart < viewStart) {
|
|
159
200
|
for (const [rangeStart, rangeEnd] of changedLineRanges) if (rangeStart < viewStart) offscreenSyncEnd = Math.max(offscreenSyncEnd, Math.min(rangeEnd, viewStart - 1));
|
|
@@ -161,7 +202,7 @@ var EditorTokenizer = class {
|
|
|
161
202
|
const shouldFlushOffscreenLines = offscreenSyncEnd >= dirtyStart && (canReuseCachedStates || change.lineDelta < 0);
|
|
162
203
|
if (canReuseCachedStates) this.#buildStateStack(dirtyStart);
|
|
163
204
|
else {
|
|
164
|
-
this.#
|
|
205
|
+
this.#shiftComparisonStateStack(change);
|
|
165
206
|
if (renderRange === void 0 || dirtyStart >= viewStart) this.#buildStateStack(viewStart);
|
|
166
207
|
}
|
|
167
208
|
let changedRangeIndex = 0;
|
|
@@ -183,7 +224,7 @@ var EditorTokenizer = class {
|
|
|
183
224
|
offscreenState = resolved.state;
|
|
184
225
|
offscreenDirtyLines.set(offscreenLine, resolved.resolvedTokens);
|
|
185
226
|
}
|
|
186
|
-
|
|
227
|
+
this.#stateStack[offscreenEnd] = offscreenState;
|
|
187
228
|
}
|
|
188
229
|
}
|
|
189
230
|
let state = this.#stateStack[line] ?? INITIAL;
|
|
@@ -224,7 +265,7 @@ var EditorTokenizer = class {
|
|
|
224
265
|
if (backgroundStartLine !== void 0) this.#scheduleBackgroundTokenize(backgroundStartLine, changedLineRanges, backgroundChangedRangeIndex);
|
|
225
266
|
else if (!settled && line < lineCount) {
|
|
226
267
|
const backgroundLine = crossesRenderRangeEnd && dirtyStart >= viewStart ? renderRangeEndLine : dirtyStart < viewStart && !canReuseCachedStates ? dirtyStart : line;
|
|
227
|
-
this.#scheduleBackgroundTokenize(backgroundLine,
|
|
268
|
+
this.#scheduleBackgroundTokenize(backgroundLine, changedLineRanges, changedRangeIndex);
|
|
228
269
|
}
|
|
229
270
|
return dirtyLines;
|
|
230
271
|
}
|
|
@@ -233,12 +274,17 @@ var EditorTokenizer = class {
|
|
|
233
274
|
this.#prebuildStateStack(renderRange);
|
|
234
275
|
}
|
|
235
276
|
stopBackgroundTokenize() {
|
|
277
|
+
this.#pendingPrebuildEndLine = -1;
|
|
236
278
|
if (this.#isStopped) return;
|
|
237
279
|
this.#isStopped = true;
|
|
238
280
|
this.#isPaused = false;
|
|
239
281
|
this.#lastLine = -1;
|
|
282
|
+
this.#backgroundPrebuildEndLine = -1;
|
|
240
283
|
this.#backgroundChangedLineRanges = void 0;
|
|
241
284
|
this.#backgroundChangedRangeIndex = 0;
|
|
285
|
+
this.#comparisonStateStack = [];
|
|
286
|
+
this.#comparisonStateStackStart = 0;
|
|
287
|
+
this.#comparisonLineChanges = [];
|
|
242
288
|
this.#detachMessageListener();
|
|
243
289
|
}
|
|
244
290
|
pauseBackgroundTokenize() {
|
|
@@ -268,6 +314,7 @@ var EditorTokenizer = class {
|
|
|
268
314
|
#postTokenizeMessage(jobId) {
|
|
269
315
|
globalThis.postMessage({
|
|
270
316
|
type: "tokenize",
|
|
317
|
+
tokenizerId: this.#tokenizerId,
|
|
271
318
|
jobId
|
|
272
319
|
});
|
|
273
320
|
}
|
|
@@ -283,11 +330,31 @@ var EditorTokenizer = class {
|
|
|
283
330
|
this.#isStopped = false;
|
|
284
331
|
this.#isPaused = false;
|
|
285
332
|
this.#lastLine = startLine;
|
|
333
|
+
if (this.#backgroundPrebuildEndLine >= 0) this.#pendingPrebuildEndLine = Math.max(this.#pendingPrebuildEndLine, this.#backgroundPrebuildEndLine);
|
|
334
|
+
this.#backgroundPrebuildEndLine = -1;
|
|
286
335
|
this.#backgroundChangedLineRanges = changedLineRanges;
|
|
287
336
|
this.#backgroundChangedRangeIndex = changedRangeIndex;
|
|
288
337
|
this.#attachMessageListener();
|
|
289
338
|
this.#postTokenizeMessage(jobId);
|
|
290
339
|
}
|
|
340
|
+
#scheduleStatePrebuild(endLine) {
|
|
341
|
+
if (this.#grammar === void 0 || this.#stateStack.length > endLine) return;
|
|
342
|
+
if (!this.#isStopped) {
|
|
343
|
+
if (this.#backgroundPrebuildEndLine >= 0) this.#backgroundPrebuildEndLine = Math.max(this.#backgroundPrebuildEndLine, endLine);
|
|
344
|
+
else this.#pendingPrebuildEndLine = Math.max(this.#pendingPrebuildEndLine, endLine);
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
const jobId = ++this.#backgroundJobId;
|
|
348
|
+
this.#isStopped = false;
|
|
349
|
+
this.#isPaused = false;
|
|
350
|
+
this.#lastLine = this.#stateStack.length - 1;
|
|
351
|
+
this.#backgroundPrebuildEndLine = endLine;
|
|
352
|
+
this.#pendingPrebuildEndLine = -1;
|
|
353
|
+
this.#backgroundChangedLineRanges = void 0;
|
|
354
|
+
this.#backgroundChangedRangeIndex = 0;
|
|
355
|
+
this.#attachMessageListener();
|
|
356
|
+
this.#postTokenizeMessage(jobId);
|
|
357
|
+
}
|
|
291
358
|
#tokenizeLineAt(line, state) {
|
|
292
359
|
const lineText = this.#textDocument.getLineText(line);
|
|
293
360
|
if (lineText.length > this.#tokenizeMaxLineLength) {
|
|
@@ -321,26 +388,71 @@ var EditorTokenizer = class {
|
|
|
321
388
|
};
|
|
322
389
|
}
|
|
323
390
|
#cacheBracketIgnoredRanges(line, ranges) {
|
|
324
|
-
if (this.#matchBrackets) this.#bracketIgnoredRanges
|
|
391
|
+
if (this.#matchBrackets) this.#bracketIgnoredRanges[line] = ranges;
|
|
392
|
+
}
|
|
393
|
+
#shiftComparisonStateStack(change) {
|
|
394
|
+
const lineChanges = change.changedLineChanges ?? [[
|
|
395
|
+
change.startLine,
|
|
396
|
+
change.endLine,
|
|
397
|
+
change.lineDelta
|
|
398
|
+
]];
|
|
399
|
+
const comparisonStart = change.startLine + 1;
|
|
400
|
+
if (comparisonStart <= this.#stateStack.length - comparisonStart) {
|
|
401
|
+
this.#comparisonStateStack = this.#stateStack;
|
|
402
|
+
this.#comparisonStateStackStart = 0;
|
|
403
|
+
this.#stateStack = this.#stateStack.slice(0, comparisonStart);
|
|
404
|
+
} else {
|
|
405
|
+
this.#comparisonStateStackStart = comparisonStart;
|
|
406
|
+
this.#comparisonStateStack = this.#stateStack.slice(comparisonStart);
|
|
407
|
+
this.#stateStack.length = Math.min(this.#stateStack.length, comparisonStart);
|
|
408
|
+
}
|
|
409
|
+
this.#comparisonLineChanges = lineChanges;
|
|
410
|
+
}
|
|
411
|
+
#getPreviousEndState(line) {
|
|
412
|
+
let previousLine = line;
|
|
413
|
+
for (let index = this.#comparisonLineChanges.length - 1; index >= 0; index--) {
|
|
414
|
+
const [startLine, endLine, lineDelta] = this.#comparisonLineChanges[index];
|
|
415
|
+
if (lineDelta === 0) continue;
|
|
416
|
+
if (previousLine > endLine) previousLine -= lineDelta;
|
|
417
|
+
else if (previousLine > startLine) return this.#stateStack[line];
|
|
418
|
+
}
|
|
419
|
+
return this.#comparisonStateStack[previousLine - this.#comparisonStateStackStart] ?? this.#stateStack[line];
|
|
325
420
|
}
|
|
326
|
-
#buildStateStack(endAt) {
|
|
421
|
+
#buildStateStack(endAt, timeBudget) {
|
|
327
422
|
const boundedEndAt = Math.min(Math.max(0, endAt), this.#textDocument.lineCount);
|
|
328
|
-
if (this.#stateStack.length > boundedEndAt || this.#grammar === void 0) return;
|
|
423
|
+
if (this.#stateStack.length > boundedEndAt || this.#grammar === void 0) return true;
|
|
424
|
+
const startedAt = timeBudget === void 0 ? 0 : performance.now();
|
|
329
425
|
let line = this.#stateStack.length - 1;
|
|
330
426
|
let state = this.#stateStack[line] ?? INITIAL;
|
|
331
|
-
|
|
427
|
+
while (line < boundedEndAt) {
|
|
332
428
|
this.#stateStack[line] = state;
|
|
333
429
|
const lineText = this.#textDocument.getLineText(line);
|
|
334
430
|
if (lineText.length <= this.#tokenizeMaxLineLength && lineText !== "" && lineText.trim() !== "") {
|
|
335
|
-
const result = tokenizeLine(this.#grammar, this.#colorMap, lineText, state, TOKENIZE_TIME_LIMIT, this.#matchBrackets);
|
|
431
|
+
const result = tokenizeLine(this.#grammar, this.#colorMap, lineText, state, TOKENIZE_TIME_LIMIT, this.#matchBrackets, false);
|
|
336
432
|
this.#cacheBracketIgnoredRanges(line, result.bracketIgnoredRanges);
|
|
337
433
|
state = result.ruleStack;
|
|
338
434
|
} else this.#cacheBracketIgnoredRanges(line, null);
|
|
435
|
+
line++;
|
|
436
|
+
this.#stateStack[line] = state;
|
|
437
|
+
if (timeBudget !== void 0 && performance.now() - startedAt > timeBudget) break;
|
|
339
438
|
}
|
|
340
|
-
|
|
439
|
+
return line >= boundedEndAt;
|
|
440
|
+
}
|
|
441
|
+
#backgroundPrebuild(jobId) {
|
|
442
|
+
if (this.#isStopped || this.#isPaused || this.#grammar === void 0 || jobId !== this.#backgroundJobId) return;
|
|
443
|
+
this.#ensureActiveTheme();
|
|
444
|
+
const complete = this.#buildStateStack(this.#backgroundPrebuildEndLine, 1);
|
|
445
|
+
if (this.#isStopped || this.#isPaused || jobId !== this.#backgroundJobId) return;
|
|
446
|
+
if (complete) {
|
|
447
|
+
this.stopBackgroundTokenize();
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
this.#lastLine = this.#stateStack.length - 1;
|
|
451
|
+
this.#postTokenizeMessage(jobId);
|
|
341
452
|
}
|
|
342
453
|
#backgroundTokenize(jobId) {
|
|
343
454
|
if (this.#isStopped || this.#isPaused || this.#grammar === void 0 || jobId !== this.#backgroundJobId) return;
|
|
455
|
+
this.#ensureActiveTheme();
|
|
344
456
|
const t = performance.now();
|
|
345
457
|
const lines = /* @__PURE__ */ new Map();
|
|
346
458
|
const totalLines = this.#textDocument.lineCount;
|
|
@@ -352,7 +464,7 @@ var EditorTokenizer = class {
|
|
|
352
464
|
let currentChangedRangeEnd = changedLineRanges?.[changedRangeIndex]?.[1];
|
|
353
465
|
for (; line < totalLines;) {
|
|
354
466
|
this.#stateStack[line] = state;
|
|
355
|
-
const previousNextState = currentChangedRangeEnd !== void 0 ? this.#
|
|
467
|
+
const previousNextState = currentChangedRangeEnd !== void 0 ? this.#getPreviousEndState(line + 1) : void 0;
|
|
356
468
|
const lineText = this.#textDocument.getLineText(line);
|
|
357
469
|
if (lineText.length > this.#tokenizeMaxLineLength) {
|
|
358
470
|
console.warn(`[diffs] Line(${line}) too long to tokenize: ${lineText.length}`);
|
|
@@ -396,7 +508,9 @@ var EditorTokenizer = class {
|
|
|
396
508
|
this.#onDeferTokenize(lines, this.#themeType);
|
|
397
509
|
if (this.#isStopped || this.#isPaused || jobId !== this.#backgroundJobId) return;
|
|
398
510
|
if (settled || line >= totalLines) {
|
|
511
|
+
const pendingPrebuildEndLine = this.#pendingPrebuildEndLine;
|
|
399
512
|
this.stopBackgroundTokenize();
|
|
513
|
+
if (pendingPrebuildEndLine >= 0) this.#scheduleStatePrebuild(pendingPrebuildEndLine);
|
|
400
514
|
return;
|
|
401
515
|
}
|
|
402
516
|
this.#lastLine = line;
|
|
@@ -404,25 +518,31 @@ var EditorTokenizer = class {
|
|
|
404
518
|
this.#postTokenizeMessage(jobId);
|
|
405
519
|
}
|
|
406
520
|
};
|
|
407
|
-
function tokenizeLine(grammar, colorMap, lineText, stateStack, timeLimit, collectBracketIgnoredRanges = true) {
|
|
521
|
+
function tokenizeLine(grammar, colorMap, lineText, stateStack, timeLimit, collectBracketIgnoredRanges = true, resolveTokens = true) {
|
|
408
522
|
const result = grammar.tokenizeLine2(lineText, stateStack, timeLimit);
|
|
409
523
|
if (result.stoppedEarly) console.warn(`[diffs] Time limit reached when tokenizing line: ${lineText.substring(0, 100)}`);
|
|
410
524
|
const rawTokens = result.tokens;
|
|
411
525
|
const tokensLength = rawTokens.length / 2;
|
|
412
526
|
const resolvedTokens = [];
|
|
413
527
|
const bracketIgnoredRanges = [];
|
|
528
|
+
if (!resolveTokens && !collectBracketIgnoredRanges) return {
|
|
529
|
+
ruleStack: result.ruleStack,
|
|
530
|
+
resolvedTokens,
|
|
531
|
+
bracketIgnoredRanges
|
|
532
|
+
};
|
|
414
533
|
for (let j = 0; j < tokensLength; j++) {
|
|
415
534
|
const offset = rawTokens[2 * j];
|
|
416
535
|
const nextOffset = j + 1 < tokensLength ? rawTokens[2 * j + 2] : lineText.length;
|
|
417
536
|
if (offset === nextOffset) continue;
|
|
418
537
|
const metadata = rawTokens[2 * j + 1];
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
538
|
+
if (resolveTokens) {
|
|
539
|
+
const fg = EncodedTokenMetadata.getForeground(metadata);
|
|
540
|
+
resolvedTokens.push([
|
|
541
|
+
offset,
|
|
542
|
+
colorMap[fg],
|
|
543
|
+
lineText.slice(offset, nextOffset)
|
|
544
|
+
]);
|
|
545
|
+
}
|
|
426
546
|
if (collectBracketIgnoredRanges && EncodedTokenMetadata.getTokenType(metadata) > 0) bracketIgnoredRanges.push([offset, nextOffset]);
|
|
427
547
|
}
|
|
428
548
|
return {
|
|
@@ -431,7 +551,7 @@ function tokenizeLine(grammar, colorMap, lineText, stateStack, timeLimit, collec
|
|
|
431
551
|
bracketIgnoredRanges
|
|
432
552
|
};
|
|
433
553
|
}
|
|
434
|
-
function renderLineTokens(tokens
|
|
554
|
+
function renderLineTokens(tokens) {
|
|
435
555
|
return tokens.map(([char, fg, textContent]) => {
|
|
436
556
|
if (char === 0 && fg === "") {
|
|
437
557
|
if (textContent === "") return h("br");
|
|
@@ -439,7 +559,7 @@ function renderLineTokens(tokens, themeType) {
|
|
|
439
559
|
}
|
|
440
560
|
return h("span", {
|
|
441
561
|
dataset: { char: char.toString() },
|
|
442
|
-
style:
|
|
562
|
+
style: `color:${fg};`,
|
|
443
563
|
textContent
|
|
444
564
|
});
|
|
445
565
|
});
|
|
@@ -450,4 +570,4 @@ function isGrammarlessLanguage(languageId) {
|
|
|
450
570
|
//#endregion
|
|
451
571
|
export { EditorTokenizer, renderLineTokens };
|
|
452
572
|
|
|
453
|
-
//# sourceMappingURL=
|
|
573
|
+
//# sourceMappingURL=tokenizer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokenizer.js","names":["#isCleanedUp","#textDocument","#grammar","#highlighter","#ensureActiveTheme","#scheduleStatePrebuild","#tokenizerId","#backgroundJobId","#backgroundPrebuildEndLine","#backgroundPrebuild","#backgroundTokenize","#themeType","#matchBrackets","#ensureGrammar","#bracketIgnoredRanges","#buildStateStack","#stateStack","#tokenizeLineAt","#mediaQueryList","#resolveSystemThemeType","#emitThemeChange","#disposes","#tokenizeMaxLineLength","#setStyle","#onDeferTokenize","#onThemeChange","#debug","#colorMap","#setTheme","#themeName","#comparisonStateStack","#comparisonStateStackStart","#comparisonLineChanges","#scheduleBackgroundTokenize","#detachMessageListener","#shiftComparisonStateStack","#prebuildStateStack","#pendingPrebuildEndLine","#isStopped","#isPaused","#lastLine","#backgroundChangedLineRanges","#backgroundChangedRangeIndex","#postTokenizeMessage","#isMessageListenerAttached","#onMessage","#attachMessageListener","#cacheBracketIgnoredRanges","#getPreviousEndState"],"sources":["../../src/editor/tokenizer.ts"],"sourcesContent":["import { colorUtils } from '@pierre/theming/color';\nimport {\n EncodedTokenMetadata,\n type IGrammar,\n INITIAL,\n type StateStack,\n} from 'shiki/textmate';\n\nimport { DEFAULT_THEMES } from '../constants';\nimport type {\n BaseCodeOptions,\n DiffsHighlighter,\n HighlightedToken,\n RenderRange,\n} from '../types';\nimport type { TextDocument, TextDocumentChange } from './textDocument';\nimport { addEventListener, debounce, h } from './utils';\n\nconst TOKENIZE_TIME_LIMIT = 500;\nlet nextTokenizerId = 0;\n\nexport interface EditorTokenizerProps {\n highlighter: DiffsHighlighter;\n textDocument: TextDocument<unknown>;\n codeOptions: BaseCodeOptions;\n matchBrackets?: boolean;\n setStyle: (style: string) => void;\n onDeferTokenize: (\n lines: Map<number, Array<HighlightedToken>>,\n themeType: 'dark' | 'light'\n ) => void;\n // Fired after the active theme (light/dark mode or theme name) changes and the\n // new theme CSS has been applied. Lets the editor recompute overlay pieces\n // that captured a resolved theme color, e.g. rounded selection corner masks.\n onThemeChange?: () => void;\n __debug?: boolean;\n}\n\n/** Stoppable code tokenizer for the editor */\nexport class EditorTokenizer {\n #highlighter: DiffsHighlighter;\n #grammar: IGrammar | undefined;\n #mediaQueryList: MediaQueryList;\n #themeType: 'light' | 'dark' = 'dark';\n // The resolved name of the theme currently applied to the editor (e.g.\n // `github-light`). Tracked so `syncTheme` can detect a host-driven theme swap\n // even when the light/dark mode itself is unchanged.\n #themeName = '';\n #colorMap: string[];\n #textDocument: TextDocument<unknown>;\n #tokenizeMaxLineLength: number;\n #setStyle: EditorTokenizerProps['setStyle'];\n #onDeferTokenize: EditorTokenizerProps['onDeferTokenize'];\n #onThemeChange: EditorTokenizerProps['onThemeChange'];\n #matchBrackets: boolean;\n #debug: boolean;\n #disposes?: (() => void)[];\n #isCleanedUp = false;\n\n // state\n #stateStack: StateStack[] = [INITIAL]; // cached state stack by line index\n #comparisonStateStack: StateStack[] = [];\n #comparisonStateStackStart = 0;\n #comparisonLineChanges: NonNullable<\n TextDocumentChange['changedLineChanges']\n > = [];\n #lastLine: number = -1;\n #isStopped: boolean = true;\n #isPaused: boolean = false;\n #backgroundJobId: number = 0;\n #tokenizerId = ++nextTokenizerId;\n #backgroundPrebuildEndLine = -1;\n #pendingPrebuildEndLine = -1;\n #backgroundChangedLineRanges: readonly [number, number][] | undefined;\n #backgroundChangedRangeIndex: number = 0;\n #bracketIgnoredRanges: ([number, number][] | null | undefined)[] = [];\n #isMessageListenerAttached: boolean = false;\n\n #prebuildStateStack = debounce(async (renderRange?: RenderRange) => {\n // Drop work scheduled before cleanUp; a late timer must not call setTheme\n // on a highlighter that tests (or hosts) have already disposed.\n if (this.#isCleanedUp) {\n return;\n }\n const { startingLine = 0, totalLines = Infinity } = renderRange ?? {};\n const endLine = Math.min(\n totalLines === Infinity ? Infinity : startingLine + totalLines,\n this.#textDocument.lineCount\n );\n if (\n this.#grammar === undefined &&\n !isGrammarlessLanguage(this.#textDocument.languageId)\n ) {\n await this.#highlighter.loadLanguage(this.#textDocument.languageId);\n if (this.#isCleanedUp) {\n return;\n }\n this.#grammar = this.#highlighter.getLanguage(\n this.#textDocument.languageId\n );\n }\n this.#ensureActiveTheme();\n this.#scheduleStatePrebuild(endLine);\n }, 500);\n\n #onMessage = ({ data }: MessageEvent<unknown>) => {\n if (typeof data !== 'object' || data === null) {\n return;\n }\n const { type, tokenizerId, jobId } = data as {\n type?: unknown;\n tokenizerId?: unknown;\n jobId?: unknown;\n };\n if (\n type === 'tokenize' &&\n tokenizerId === this.#tokenizerId &&\n typeof jobId === 'number' &&\n jobId === this.#backgroundJobId\n ) {\n if (this.#backgroundPrebuildEndLine >= 0) {\n this.#backgroundPrebuild(jobId);\n } else {\n this.#backgroundTokenize(jobId);\n }\n }\n };\n\n get themeType(): 'light' | 'dark' {\n return this.#themeType;\n }\n\n getStringCommentRegexpRangesInLine(\n lineIndex: number\n ): [number, number][] | null {\n if (\n !this.#matchBrackets ||\n lineIndex < 0 ||\n lineIndex >= this.#textDocument.lineCount\n ) {\n return null;\n }\n this.#ensureGrammar();\n if (this.#grammar === undefined) {\n return null;\n }\n if (this.#bracketIgnoredRanges[lineIndex] === undefined) {\n this.#buildStateStack(lineIndex);\n const state = this.#stateStack[lineIndex] ?? INITIAL;\n const result = this.#tokenizeLineAt(lineIndex, state);\n this.#stateStack[lineIndex + 1] = result.state;\n }\n return this.#bracketIgnoredRanges[lineIndex] ?? null;\n }\n\n constructor({\n codeOptions,\n highlighter,\n textDocument,\n matchBrackets,\n setStyle,\n onDeferTokenize,\n onThemeChange,\n __debug,\n }: EditorTokenizerProps) {\n const {\n themeType: themeTypeOption = 'system',\n theme = DEFAULT_THEMES,\n tokenizeMaxLineLength = 1000,\n } = codeOptions;\n this.#mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)');\n let themeType: 'light' | 'dark' | undefined;\n if (themeTypeOption === 'system') {\n // Prefer the host document's computed color-scheme (page CSS/classes can\n // force light/dark while the OS media query differs) over matchMedia.\n themeType = this.#resolveSystemThemeType();\n } else {\n themeType = themeTypeOption;\n }\n // Only track the document/system color scheme when the surface follows it\n // (`themeType: 'system'`). A surface pinned to an explicit 'dark'/'light'\n // theme keeps that theme regardless of the page, so re-tokenizing after an\n // edit must emit the same `--diffs-token-{theme}` variable the SSR markup\n // used; otherwise the edited tokens fall back to the default foreground.\n if (typeof theme !== 'string' && themeTypeOption === 'system') {\n const observer = new MutationObserver((mutations) => {\n for (const { type, attributeName } of mutations) {\n if (\n type === 'attributes' &&\n attributeName !== null &&\n (attributeName === 'class' || attributeName.startsWith('data-'))\n ) {\n const themeType = this.#resolveSystemThemeType();\n this.#emitThemeChange(theme[themeType], themeType);\n break;\n }\n }\n });\n observer.observe(document.documentElement, { attributes: true });\n observer.observe(document.body, { attributes: true });\n this.#disposes = [\n addEventListener(this.#mediaQueryList, 'change', () => {\n // Re-read computed color-scheme so a host-forced scheme still wins\n // when the OS preference changes underneath it.\n const themeType = this.#resolveSystemThemeType();\n this.#emitThemeChange(theme[themeType], themeType);\n }),\n () => observer.disconnect(),\n ];\n }\n this.#highlighter = highlighter;\n this.#textDocument = textDocument;\n this.#tokenizeMaxLineLength = tokenizeMaxLineLength;\n this.#setStyle = setStyle;\n this.#onDeferTokenize = onDeferTokenize;\n this.#onThemeChange = onThemeChange;\n this.#matchBrackets = matchBrackets !== false;\n this.#debug = __debug ?? false;\n this.#ensureGrammar();\n this.#colorMap = [];\n this.#setTheme(\n typeof theme === 'string' ? theme : theme[themeType],\n themeType\n );\n }\n\n // By default, diffs components support dual themes, but the tokenizer only renders\n // the preferred theme. When the theme type is changed, the tokenizer will re-tokenize the document.\n #emitThemeChange(themeName: string, themeType: 'light' | 'dark') {\n if (themeName === this.#themeName && themeType === this.#themeType) {\n return;\n }\n this.#setTheme(themeName, themeType);\n this.stopBackgroundTokenize();\n this.#stateStack = [INITIAL];\n this.#comparisonStateStack = [];\n this.#comparisonStateStackStart = 0;\n this.#comparisonLineChanges = [];\n if (this.#grammar !== undefined && this.#textDocument.lineCount > 0) {\n this.#scheduleBackgroundTokenize(0);\n }\n // The theme CSS is now applied, so overlay pieces that captured a resolved\n // theme color (e.g. rounded selection corner masks) can recompute against\n // the new colors instead of keeping the old light/dark value.\n this.#onThemeChange?.();\n }\n\n // Respect an explicit host color-scheme override. When the computed value\n // advertises support for both schemes, let the OS preference choose one.\n #resolveSystemThemeType(): 'light' | 'dark' {\n try {\n if (\n typeof document !== 'undefined' &&\n typeof getComputedStyle === 'function' &&\n document.body != null\n ) {\n const colorSchemes = getComputedStyle(document.body).colorScheme.split(\n /\\s+/\n );\n const supportsDark = colorSchemes.includes('dark');\n const supportsLight = colorSchemes.includes('light');\n // A single host-forced scheme wins. `light dark` only declares support\n // for both schemes, so the media query still selects the active one.\n if (supportsDark !== supportsLight) {\n return supportsDark ? 'dark' : 'light';\n }\n }\n } catch {\n // jsdom and similar harnesses may lack getComputedStyle or throw; fall\n // through to the OS media query.\n }\n return this.#mediaQueryList.matches ? 'dark' : 'light';\n }\n\n // Re-apply the editor's theme from the surface's current code options. Edit\n // mode reuses a single tokenizer across re-renders, so when the host swaps the\n // theme — a theme picker, a light/dark toggle, etc. — we must recompute the\n // active theme and re-tokenize. Without this the editor keeps rendering the\n // theme it captured when it first attached (stale line-highlight background\n // and token colors). System-driven changes are still handled by the\n // observers wired up in the constructor; this covers explicit `themeType`/\n // `theme` option changes that those observers don't see.\n syncTheme(codeOptions: BaseCodeOptions): void {\n const { themeType = 'system', theme = DEFAULT_THEMES } = codeOptions;\n const nextThemeType =\n themeType === 'system' ? this.#resolveSystemThemeType() : themeType;\n const nextThemeName =\n typeof theme === 'string' ? theme : theme[nextThemeType];\n if (\n nextThemeType === this.#themeType &&\n nextThemeName === this.#themeName\n ) {\n return;\n }\n this.#emitThemeChange(nextThemeName, nextThemeType);\n }\n\n #setTheme(themeName: string, themeType?: 'light' | 'dark') {\n const { theme, colorMap } = this.#highlighter.setTheme(themeName);\n const { colors = {} } = this.#highlighter.getTheme(themeName);\n const selectionBackground = colors['editor.selectionBackground'];\n const themeLineHighlightBackground =\n colors['editor.lineHighlightBackground'];\n const lineHighlightBackground =\n themeLineHighlightBackground != null &&\n themeLineHighlightBackground.trim() !== '' &&\n !colorUtils.isFullyTransparent(themeLineHighlightBackground)\n ? themeLineHighlightBackground\n : undefined;\n // A usable theme background opts into the semantic active-line mix.\n // Missing backgrounds retain the resolved row color and rely on a border.\n const lineHighlightBorder =\n colors['editor.lineHighlightBorder'] ??\n (lineHighlightBackground == null\n ? 'color-mix(in lab, var(--diffs-bg) 70%, var(--diffs-fg))'\n : 'transparent');\n const activeLineSourceMix =\n lineHighlightBackground == null ? '100%' : '85%';\n const cursorForeground = colors['editorCursor.foreground'];\n const findMatchBackground = colors['editor.findMatchBackground'];\n const findMatchHighlightBackground =\n colors['editor.findMatchHighlightBackground'];\n const bracketMatchBackground = colors['editorBracketMatch.background'];\n const bracketMatchBorder = colors['editorBracketMatch.border'];\n const hintForeground = colors['editorHint.foreground'];\n const infoForeground = colors['editorInfo.foreground'];\n const warningForeground = colors['editorWarning.foreground'];\n const errorForeground = colors['editorError.foreground'];\n this.#setStyle(`:host {\n --diffs-editor-selection-bg: ${selectionBackground ?? 'var(--diffs-line-bg)'};\n --diffs-editor-line-highlight-border: ${lineHighlightBorder};\n --diffs-editor-active-line-source-mix: ${activeLineSourceMix};\n --diffs-editor-match-bg: ${findMatchBackground ?? 'initial'};\n --diffs-editor-match-highlight-bg: ${findMatchHighlightBackground ?? 'initial'};\n --diffs-editor-bracket-match-bg: ${bracketMatchBackground ?? 'initial'};\n --diffs-editor-bracket-match-border: ${bracketMatchBorder ?? 'initial'};\n --diffs-editor-cursor-fg: ${cursorForeground ?? 'initial'};\n --diffs-editor-hint-fg: ${hintForeground ?? 'initial'};\n --diffs-editor-info-fg: ${infoForeground ?? 'initial'};\n --diffs-editor-warning-fg: ${warningForeground ?? 'initial'};\n --diffs-editor-error-fg: ${errorForeground ?? 'initial'};\n }`);\n this.#themeName = themeName;\n this.#themeType = themeType ?? theme.type;\n this.#colorMap = colorMap;\n }\n\n // The shared highlighter is also used for dual-theme SSR (`themes: {dark,light}`),\n // which leaves its active theme on whichever pass finished last (usually light).\n // The tokenizer caches a single-theme colorMap from construction; if we tokenize\n // without re-activating that theme, grammar color indices are looked up in the\n // wrong map — property names resolve to a near-foreground gray while types and\n // comments (stable across maps) still look correct. Re-apply before every\n // tokenize path so a first edit after load matches a later file-switch re-attach.\n #ensureActiveTheme(): void {\n if (this.#themeName === '') {\n return;\n }\n const { colorMap } = this.#highlighter.setTheme(this.#themeName);\n this.#colorMap = colorMap;\n }\n\n cleanUp(): void {\n this.#isCleanedUp = true;\n this.stopBackgroundTokenize();\n this.#detachMessageListener();\n this.#disposes?.forEach((dispose) => dispose());\n this.#disposes = undefined;\n }\n\n // to use `tokenize`, call `prebuildStateStackMap` first to prebuild\n // the state stack map for the given render range.\n tokenize(\n change: TextDocumentChange,\n renderRange?: RenderRange\n ): Map<number, Array<HighlightedToken>> {\n this.#ensureGrammar();\n this.#ensureActiveTheme();\n if (\n this.#grammar === undefined &&\n !isGrammarlessLanguage(this.#textDocument.languageId)\n ) {\n throw new Error(\n `Grammar for language \"${this.#textDocument.languageId}\" not loaded`\n );\n }\n\n if (this.#matchBrackets) {\n // Clear ignored token ranges for lines invalidated by the edit.\n this.#bracketIgnoredRanges.length = Math.min(\n this.#bracketIgnoredRanges.length,\n change.startLine\n );\n }\n\n const { lineCount } = this.#textDocument;\n const { startingLine = 0, totalLines = Infinity } = renderRange ?? {};\n const renderRangeEndLine =\n totalLines === Infinity\n ? lineCount\n : Math.min(startingLine + totalLines, lineCount);\n\n const dirtyStart = change.startLine;\n const viewStart = Math.max(startingLine, dirtyStart);\n const crossesRenderRangeEnd =\n renderRange !== undefined &&\n totalLines !== Infinity &&\n change.lineDelta > 0 &&\n dirtyStart < renderRangeEndLine &&\n change.endLine >= renderRangeEndLine;\n const canReuseCachedStates =\n change.lineDelta === 0 &&\n (change.changedLineChanges?.every(([, , lineDelta]) => lineDelta === 0) ??\n true);\n const canCacheTokenizedStates =\n canReuseCachedStates ||\n renderRange === undefined ||\n dirtyStart >= viewStart;\n const changedLineRanges: readonly [number, number][] =\n change.changedLineRanges ?? [[dirtyStart, change.endLine]];\n this.#comparisonStateStack = [];\n this.#comparisonStateStackStart = 0;\n this.#comparisonLineChanges = [];\n let offscreenSyncEnd = -1;\n if (dirtyStart < viewStart) {\n for (const [rangeStart, rangeEnd] of changedLineRanges) {\n if (rangeStart < viewStart) {\n offscreenSyncEnd = Math.max(\n offscreenSyncEnd,\n Math.min(rangeEnd, viewStart - 1)\n );\n }\n }\n }\n const shouldFlushOffscreenLines =\n offscreenSyncEnd >= dirtyStart &&\n (canReuseCachedStates || change.lineDelta < 0);\n if (canReuseCachedStates) {\n this.#buildStateStack(dirtyStart);\n } else {\n this.#shiftComparisonStateStack(change);\n if (renderRange === undefined || dirtyStart >= viewStart) {\n this.#buildStateStack(viewStart);\n }\n }\n\n let changedRangeIndex = 0;\n let currentChangedRangeEnd = changedLineRanges[changedRangeIndex][1];\n let backgroundStartLine: number | undefined;\n let backgroundChangedRangeIndex = 0;\n let line = canReuseCachedStates\n ? changedLineRanges[changedRangeIndex][0]\n : viewStart;\n let settled = false;\n const dirtyLines: Map<number, Array<HighlightedToken>> = new Map();\n const offscreenDirtyLines:\n | Map<number, Array<HighlightedToken>>\n | undefined = shouldFlushOffscreenLines ? new Map() : undefined;\n if (offscreenDirtyLines !== undefined && !canReuseCachedStates) {\n const offscreenEnd = Math.min(\n offscreenSyncEnd + 1,\n viewStart,\n renderRangeEndLine\n );\n if (offscreenEnd > dirtyStart) {\n this.#buildStateStack(offscreenEnd);\n let offscreenLine = dirtyStart;\n let offscreenState = this.#stateStack[offscreenLine] ?? INITIAL;\n for (; offscreenLine < offscreenEnd; offscreenLine++) {\n const resolved = this.#tokenizeLineAt(offscreenLine, offscreenState);\n offscreenState = resolved.state;\n offscreenDirtyLines.set(offscreenLine, resolved.resolvedTokens);\n }\n this.#stateStack[offscreenEnd] = offscreenState;\n }\n }\n // Seed the loop's grammar state after the offscreen flush, not before it.\n // When a delete's removed lines reach the viewport's first line, the flush\n // rebuilds the cached state up to `line`; reading it earlier would capture\n // the truncated INITIAL state and color the viewport as if outside an open\n // construct (block comment, template literal) it is actually inside.\n let state = this.#stateStack[line] ?? INITIAL;\n for (; line < renderRangeEndLine; ) {\n const previousNextState = canReuseCachedStates\n ? this.#stateStack[line + 1]\n : undefined;\n if (canCacheTokenizedStates) {\n this.#stateStack[line] = state;\n }\n\n const { resolvedTokens, state: nextState } = this.#tokenizeLineAt(\n line,\n state\n );\n state = nextState;\n\n if (line >= viewStart) {\n dirtyLines.set(line, resolvedTokens);\n } else {\n offscreenDirtyLines?.set(line, resolvedTokens);\n }\n\n if (canCacheTokenizedStates) {\n this.#stateStack[line + 1] = state;\n }\n settled =\n line >= currentChangedRangeEnd &&\n canReuseCachedStates &&\n previousNextState !== undefined &&\n state.equals(previousNextState);\n if (settled) {\n changedRangeIndex++;\n const nextRange = changedLineRanges[changedRangeIndex];\n if (nextRange === undefined) {\n break;\n }\n if (nextRange[0] >= renderRangeEndLine) {\n backgroundStartLine = nextRange[0];\n backgroundChangedRangeIndex = changedRangeIndex;\n break;\n }\n if (this.#stateStack[nextRange[0]] === undefined) {\n currentChangedRangeEnd = nextRange[1];\n line++;\n } else {\n line = nextRange[0];\n state = this.#stateStack[line] ?? state;\n currentChangedRangeEnd = nextRange[1];\n }\n settled = false;\n continue;\n }\n line++;\n }\n\n if (canCacheTokenizedStates) {\n if (line < renderRangeEndLine) {\n this.#stateStack[line + 1] = state;\n } else {\n this.#stateStack[line] = state;\n }\n }\n\n if (offscreenDirtyLines !== undefined && offscreenDirtyLines.size > 0) {\n this.#onDeferTokenize(offscreenDirtyLines, this.#themeType);\n }\n\n if (backgroundStartLine !== undefined) {\n this.#scheduleBackgroundTokenize(\n backgroundStartLine,\n changedLineRanges,\n backgroundChangedRangeIndex\n );\n } else if (!settled && line < lineCount) {\n const backgroundLine =\n crossesRenderRangeEnd && dirtyStart >= viewStart\n ? renderRangeEndLine\n : dirtyStart < viewStart && !canReuseCachedStates\n ? dirtyStart\n : line;\n this.#scheduleBackgroundTokenize(\n backgroundLine,\n changedLineRanges,\n changedRangeIndex\n );\n }\n\n return dirtyLines;\n }\n\n prebuildStateStack(renderRange?: RenderRange): void {\n this.#ensureGrammar();\n this.#prebuildStateStack(renderRange);\n }\n\n stopBackgroundTokenize(): void {\n this.#pendingPrebuildEndLine = -1;\n if (this.#isStopped) {\n return;\n }\n this.#isStopped = true;\n this.#isPaused = false;\n this.#lastLine = -1;\n this.#backgroundPrebuildEndLine = -1;\n this.#backgroundChangedLineRanges = undefined;\n this.#backgroundChangedRangeIndex = 0;\n this.#comparisonStateStack = [];\n this.#comparisonStateStackStart = 0;\n this.#comparisonLineChanges = [];\n this.#detachMessageListener();\n }\n\n pauseBackgroundTokenize(): void {\n if (this.#isStopped || this.#isPaused) {\n return;\n }\n if (this.#debug) {\n console.log('[diffs/editor] background tokenization paused', {\n jobId: this.#backgroundJobId,\n });\n }\n this.#isPaused = true;\n }\n\n resumeBackgroundTokenize(): void {\n if (\n this.#isStopped ||\n !this.#isPaused ||\n this.#grammar === undefined ||\n this.#lastLine < 0\n ) {\n return;\n }\n if (this.#debug) {\n console.log('[diffs/editor] background tokenization resumed', {\n jobId: this.#backgroundJobId,\n });\n }\n this.#isPaused = false;\n this.#postTokenizeMessage(this.#backgroundJobId);\n }\n\n #ensureGrammar(): void {\n if (\n this.#grammar === undefined &&\n !isGrammarlessLanguage(this.#textDocument.languageId) &&\n this.#highlighter\n .getLoadedLanguages()\n .includes(this.#textDocument.languageId)\n ) {\n this.#grammar = this.#highlighter.getLanguage(\n this.#textDocument.languageId\n );\n }\n }\n\n #attachMessageListener(): void {\n if (this.#isMessageListenerAttached) {\n return;\n }\n globalThis.addEventListener('message', this.#onMessage);\n this.#isMessageListenerAttached = true;\n }\n\n #detachMessageListener(): void {\n if (!this.#isMessageListenerAttached) {\n return;\n }\n globalThis.removeEventListener('message', this.#onMessage);\n this.#isMessageListenerAttached = false;\n }\n\n #postTokenizeMessage(jobId: number): void {\n // use `postMessage` instead of `setTimeout(fn, 0)` to avoid 4ms delay\n globalThis.postMessage({\n type: 'tokenize',\n tokenizerId: this.#tokenizerId,\n jobId,\n });\n }\n\n #scheduleBackgroundTokenize(\n startLine: number,\n changedLineRanges?: readonly [number, number][],\n changedRangeIndex = 0\n ): void {\n if (isGrammarlessLanguage(this.#textDocument.languageId)) {\n return;\n }\n\n const jobId = ++this.#backgroundJobId;\n\n if (this.#debug) {\n console.log('[diffs/editor] background tokenization scheduled', {\n jobId,\n startLine,\n changedLineRanges,\n changedRangeIndex,\n });\n }\n\n this.#isStopped = false;\n this.#isPaused = false;\n this.#lastLine = startLine;\n if (this.#backgroundPrebuildEndLine >= 0) {\n this.#pendingPrebuildEndLine = Math.max(\n this.#pendingPrebuildEndLine,\n this.#backgroundPrebuildEndLine\n );\n }\n this.#backgroundPrebuildEndLine = -1;\n this.#backgroundChangedLineRanges = changedLineRanges;\n this.#backgroundChangedRangeIndex = changedRangeIndex;\n this.#attachMessageListener();\n this.#postTokenizeMessage(jobId);\n }\n\n #scheduleStatePrebuild(endLine: number): void {\n if (this.#grammar === undefined || this.#stateStack.length > endLine) {\n return;\n }\n // Extend an active prebuild, or retain the target until the foreground\n // edit job reconverges and releases the state cache.\n if (!this.#isStopped) {\n if (this.#backgroundPrebuildEndLine >= 0) {\n this.#backgroundPrebuildEndLine = Math.max(\n this.#backgroundPrebuildEndLine,\n endLine\n );\n } else {\n this.#pendingPrebuildEndLine = Math.max(\n this.#pendingPrebuildEndLine,\n endLine\n );\n }\n return;\n }\n\n const jobId = ++this.#backgroundJobId;\n this.#isStopped = false;\n this.#isPaused = false;\n this.#lastLine = this.#stateStack.length - 1;\n this.#backgroundPrebuildEndLine = endLine;\n this.#pendingPrebuildEndLine = -1;\n this.#backgroundChangedLineRanges = undefined;\n this.#backgroundChangedRangeIndex = 0;\n this.#attachMessageListener();\n this.#postTokenizeMessage(jobId);\n }\n\n #tokenizeLineAt(\n line: number,\n state: StateStack\n ): { resolvedTokens: Array<HighlightedToken>; state: StateStack } {\n const lineText = this.#textDocument.getLineText(line);\n if (lineText.length > this.#tokenizeMaxLineLength) {\n console.warn(\n `[diffs] Line(${line}) too long to tokenize: ${lineText.length}`\n );\n this.#cacheBracketIgnoredRanges(line, null);\n return { resolvedTokens: [[0, '', lineText]], state };\n }\n if (\n this.#grammar === undefined ||\n lineText === '' ||\n lineText.trim() === ''\n ) {\n this.#cacheBracketIgnoredRanges(line, null);\n return { resolvedTokens: [[0, '', lineText]], state };\n }\n const result = tokenizeLine(\n this.#grammar,\n this.#colorMap,\n lineText,\n state,\n TOKENIZE_TIME_LIMIT,\n this.#matchBrackets\n );\n this.#cacheBracketIgnoredRanges(line, result.bracketIgnoredRanges);\n return {\n resolvedTokens: result.resolvedTokens,\n state: result.ruleStack,\n };\n }\n\n #cacheBracketIgnoredRanges(\n line: number,\n ranges: [number, number][] | null\n ): void {\n if (this.#matchBrackets) {\n this.#bracketIgnoredRanges[line] = ranges;\n }\n }\n\n // Preserve old end states as comparison-only sentinels, copying whichever\n // side of the edit is smaller. Index shifts are resolved lazily on lookup.\n #shiftComparisonStateStack(change: TextDocumentChange): void {\n const lineChanges =\n change.changedLineChanges ??\n ([[change.startLine, change.endLine, change.lineDelta]] as const);\n const comparisonStart = change.startLine + 1;\n const comparisonLength = this.#stateStack.length - comparisonStart;\n if (comparisonStart <= comparisonLength) {\n this.#comparisonStateStack = this.#stateStack;\n this.#comparisonStateStackStart = 0;\n this.#stateStack = this.#stateStack.slice(0, comparisonStart);\n } else {\n this.#comparisonStateStackStart = comparisonStart;\n this.#comparisonStateStack = this.#stateStack.slice(comparisonStart);\n this.#stateStack.length = Math.min(\n this.#stateStack.length,\n comparisonStart\n );\n }\n this.#comparisonLineChanges = lineChanges;\n }\n\n #getPreviousEndState(line: number): StateStack | undefined {\n let previousLine = line;\n for (\n let index = this.#comparisonLineChanges.length - 1;\n index >= 0;\n index--\n ) {\n const [startLine, endLine, lineDelta] =\n this.#comparisonLineChanges[index];\n if (lineDelta === 0) {\n continue;\n }\n if (previousLine > endLine) {\n previousLine -= lineDelta;\n } else if (previousLine > startLine) {\n return this.#stateStack[line];\n }\n }\n return (\n this.#comparisonStateStack[\n previousLine - this.#comparisonStateStackStart\n ] ?? this.#stateStack[line]\n );\n }\n\n #buildStateStack(endAt: number, timeBudget?: number): boolean {\n const boundedEndAt = Math.min(\n Math.max(0, endAt),\n this.#textDocument.lineCount\n );\n if (this.#stateStack.length > boundedEndAt || this.#grammar === undefined) {\n return true;\n }\n const startedAt = timeBudget === undefined ? 0 : performance.now();\n let line = this.#stateStack.length - 1;\n let state = this.#stateStack[line] ?? INITIAL;\n while (line < boundedEndAt) {\n this.#stateStack[line] = state;\n const lineText = this.#textDocument.getLineText(line);\n if (\n lineText.length <= this.#tokenizeMaxLineLength &&\n lineText !== '' &&\n lineText.trim() !== ''\n ) {\n const result = tokenizeLine(\n this.#grammar,\n this.#colorMap,\n lineText,\n state,\n TOKENIZE_TIME_LIMIT,\n this.#matchBrackets,\n false\n );\n this.#cacheBracketIgnoredRanges(line, result.bracketIgnoredRanges);\n state = result.ruleStack;\n } else {\n this.#cacheBracketIgnoredRanges(line, null);\n }\n line++;\n this.#stateStack[line] = state;\n if (\n timeBudget !== undefined &&\n performance.now() - startedAt > timeBudget\n ) {\n break;\n }\n }\n return line >= boundedEndAt;\n }\n\n #backgroundPrebuild(jobId: number): void {\n if (\n this.#isStopped ||\n this.#isPaused ||\n this.#grammar === undefined ||\n jobId !== this.#backgroundJobId\n ) {\n return;\n }\n\n this.#ensureActiveTheme();\n // State prebuilds intentionally omit rendered tokens and yield between\n // short chunks so a deep viewport does not monopolize the main thread.\n const complete = this.#buildStateStack(this.#backgroundPrebuildEndLine, 1);\n if (this.#isStopped || this.#isPaused || jobId !== this.#backgroundJobId) {\n return;\n }\n if (complete) {\n this.stopBackgroundTokenize();\n return;\n }\n this.#lastLine = this.#stateStack.length - 1;\n this.#postTokenizeMessage(jobId);\n }\n\n #backgroundTokenize(jobId: number) {\n if (\n this.#isStopped ||\n this.#isPaused ||\n this.#grammar === undefined ||\n jobId !== this.#backgroundJobId\n ) {\n return;\n }\n\n this.#ensureActiveTheme();\n\n const t = performance.now();\n const lines = new Map<number, Array<HighlightedToken>>();\n const totalLines = this.#textDocument.lineCount;\n const changedLineRanges = this.#backgroundChangedLineRanges;\n\n let line = this.#lastLine;\n let state = this.#stateStack[line] ?? INITIAL;\n let settled = false;\n let changedRangeIndex = this.#backgroundChangedRangeIndex;\n let currentChangedRangeEnd = changedLineRanges?.[changedRangeIndex]?.[1];\n for (; line < totalLines; ) {\n this.#stateStack[line] = state;\n\n const previousNextState =\n currentChangedRangeEnd !== undefined\n ? this.#getPreviousEndState(line + 1)\n : undefined;\n const lineText = this.#textDocument.getLineText(line);\n if (lineText.length > this.#tokenizeMaxLineLength) {\n console.warn(\n `[diffs] Line(${line}) too long to tokenize: ${lineText.length}`\n );\n lines.set(line, [[0, '', lineText]]);\n this.#cacheBracketIgnoredRanges(line, null);\n } else if (lineText === '' || lineText.trim() === '') {\n lines.set(line, [[0, '', lineText]]);\n this.#cacheBracketIgnoredRanges(line, null);\n } else {\n const ret = tokenizeLine(\n this.#grammar,\n this.#colorMap,\n lineText,\n state,\n TOKENIZE_TIME_LIMIT,\n this.#matchBrackets\n );\n lines.set(line, ret.resolvedTokens);\n this.#cacheBracketIgnoredRanges(line, ret.bracketIgnoredRanges);\n state = ret.ruleStack;\n }\n\n this.#stateStack[line + 1] = state;\n settled =\n currentChangedRangeEnd !== undefined &&\n line >= currentChangedRangeEnd &&\n previousNextState !== undefined &&\n state.equals(previousNextState);\n line++;\n if (settled) {\n changedRangeIndex++;\n const nextRange = changedLineRanges?.[changedRangeIndex];\n if (nextRange === undefined) {\n break;\n }\n currentChangedRangeEnd = nextRange[1];\n if (this.#stateStack[nextRange[0]] === undefined) {\n settled = false;\n } else {\n line = nextRange[0];\n state = this.#stateStack[line] ?? state;\n settled = false;\n continue;\n }\n }\n\n // limit the time of partial tokenize to 1ms\n if (performance.now() - t > 1) {\n break;\n }\n }\n\n this.#onDeferTokenize(lines, this.#themeType);\n if (this.#isStopped || this.#isPaused || jobId !== this.#backgroundJobId) {\n return;\n }\n\n if (settled || line >= totalLines) {\n const pendingPrebuildEndLine = this.#pendingPrebuildEndLine;\n this.stopBackgroundTokenize();\n if (pendingPrebuildEndLine >= 0) {\n this.#scheduleStatePrebuild(pendingPrebuildEndLine);\n }\n return;\n }\n\n this.#lastLine = line;\n this.#backgroundChangedRangeIndex = changedRangeIndex;\n this.#postTokenizeMessage(jobId);\n }\n}\n\nfunction tokenizeLine(\n grammar: IGrammar,\n colorMap: string[],\n lineText: string,\n stateStack: StateStack,\n timeLimit?: number,\n collectBracketIgnoredRanges = true,\n resolveTokens = true\n): {\n ruleStack: StateStack;\n resolvedTokens: Array<HighlightedToken>;\n bracketIgnoredRanges: [number, number][];\n} {\n const result = grammar.tokenizeLine2(lineText, stateStack, timeLimit);\n if (result.stoppedEarly) {\n console.warn(\n `[diffs] Time limit reached when tokenizing line: ${lineText.substring(0, 100)}`\n );\n }\n const rawTokens = result.tokens;\n const tokensLength = rawTokens.length / 2;\n const resolvedTokens: Array<HighlightedToken> = [];\n const bracketIgnoredRanges: [number, number][] = [];\n if (!resolveTokens && !collectBracketIgnoredRanges) {\n return {\n ruleStack: result.ruleStack,\n resolvedTokens,\n bracketIgnoredRanges,\n };\n }\n for (let j = 0; j < tokensLength; j++) {\n const offset = rawTokens[2 * j];\n const nextOffset =\n j + 1 < tokensLength ? rawTokens[2 * j + 2] : lineText.length;\n if (offset === nextOffset) {\n // should never reach here, skip if happens anyway\n continue;\n }\n const metadata = rawTokens[2 * j + 1];\n if (resolveTokens) {\n const fg = EncodedTokenMetadata.getForeground(metadata);\n resolvedTokens.push([\n offset,\n colorMap[fg],\n lineText.slice(offset, nextOffset),\n ]);\n }\n if (\n collectBracketIgnoredRanges &&\n EncodedTokenMetadata.getTokenType(metadata) > 0\n ) {\n bracketIgnoredRanges.push([offset, nextOffset]);\n }\n }\n return {\n ruleStack: result.ruleStack,\n resolvedTokens,\n bracketIgnoredRanges,\n };\n}\n\nexport function renderLineTokens(\n tokens: Array<HighlightedToken>\n): (HTMLElement | string)[] {\n return tokens.map(([char, fg, textContent]) => {\n if (char === 0 && fg === '') {\n if (textContent === '') {\n return h('br');\n }\n return textContent;\n }\n return h('span', {\n dataset: {\n char: char.toString(),\n },\n style: `color:${fg};`,\n textContent: textContent,\n });\n });\n}\n\n// Shiki special-cases `text` and `ansi` in codeToHast but does not expose grammars.\nfunction isGrammarlessLanguage(languageId: string): boolean {\n return languageId === 'text' || languageId === 'ansi';\n}\n"],"mappings":";;;;;AAkBA,MAAM,sBAAsB;AAC5B,IAAI,kBAAkB;;AAoBtB,IAAa,kBAAb,MAA6B;CAC3B;CACA;CACA;CACA,aAA+B;CAI/B,aAAa;CACb;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA,eAAe;CAGf,cAA4B,CAAC,OAAO;CACpC,wBAAsC,CAAC;CACvC,6BAA6B;CAC7B,yBAEI,CA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AY,SAAS;CACpE,IAAI,OAAO,cACT,QAAQ,KACN,oDAAoD,SAAS,UAAU,GAAG,GAAG,GAC/E;CAEF,MAAM,YAAY,OAAO;CACzB,MAAM,eAAe,UAAU,SAAS;CACxC,MAAM,iBAA0C,CAAC;CACjD,MAAM,uBAA2C,CAAC;CAClD,IAAI,CAAC,iBAAiB,CAAC,6BACrB,OAAO;EACL,WAAW,OAAO;EAClB;EACA;CACF;CAEF,KAAK,IAAI,IAAI,GAAG,IAAI,cAAc,KAAK;EACrC,MAAM,SAAS,UAAU,IAAI;EAC7B,MAAM,aACJ,IAAI,IAAI,eAAe,UAAU,IAAI,IAAI,KAAK,SAAS;EACzD,IAAI,WAAW,YAEb;EAEF,MAAM,WAAW,UAAU,IAAI,IAAI;EACnC,IAAI,eAAe;GACjB,MAAM,KAAK,qBAAqB,cAAc,QAAQ;GACtD,eAAe,KAAK;IAClB;IACA,SAAS;IACT,SAAS,MAAM,QAAQ,UAAU;GACnC,CAAC;EACH;EACA,IACE,+BACA,qBAAqB,aAAa,QAAQ,IAAI,GAE9C,qBAAqB,KAAK,CAAC,QAAQ,UAAU,CAAC;CAElD;CACA,OAAO;EACL,WAAW,OAAO;EAClB;EACA;CACF;AACF;AAEA,SAAgB,iBACd,QAC0B;CAC1B,OAAO,OAAO,KAAK,CAAC,MAAM,IAAI,iBAAiB;EAC7C,IAAI,SAAS,KAAK,OAAO,IAAI;GAC3B,IAAI,gBAAgB,IAClB,OAAO,EAAE,IAAI;GAEf,OAAO;EACT;EACA,OAAO,EAAE,QAAQ;GACf,SAAS,EACP,MAAM,KAAK,SAAS,EACtB;GACA,OAAO,SAAS,GAAG;GACN;EACf,CAAC;CACH,CAAC;AACH;AAGA,SAAS,sBAAsB,YAA6B;CAC1D,OAAO,eAAe,UAAU,eAAe;AACjD"}
|
package/dist/editor/utils.js
CHANGED
|
@@ -16,7 +16,7 @@ function h(tagName, props, parent) {
|
|
|
16
16
|
}
|
|
17
17
|
function addEventListener(el, event, listener, options) {
|
|
18
18
|
el.addEventListener(event, listener, options);
|
|
19
|
-
return () => el.removeEventListener(event, listener);
|
|
19
|
+
return () => el.removeEventListener(event, listener, options);
|
|
20
20
|
}
|
|
21
21
|
function getLineNumberAttr(el, key = "line") {
|
|
22
22
|
const value = el.dataset[key];
|
package/dist/editor/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","names":[],"sources":["../../src/editor/utils.ts"],"sourcesContent":["export function h<K extends keyof HTMLElementTagNameMap>(\n tagName: K,\n props?: {\n style?: string | Partial<CSSStyleDeclaration>;\n dataset?: DOMStringMap | string[] | string;\n children?: (Node | string)[];\n } & Partial<Omit<HTMLElementTagNameMap[K], 'style' | 'dataset' | 'children'>>,\n parent?: Element | ShadowRoot | DocumentFragment\n): HTMLElementTagNameMap[K] {\n const { style, dataset, children, ...attrs } = props ?? {};\n const el = document.createElement(tagName);\n Object.assign(el, attrs);\n if (style !== undefined) {\n if (typeof style === 'string') {\n el.style.cssText = style;\n } else {\n Object.assign(el.style, style);\n }\n }\n if (dataset !== undefined) {\n if (typeof dataset === 'string') {\n el.dataset[dataset] = '';\n } else if (Array.isArray(dataset)) {\n dataset.forEach((key) => {\n el.dataset[key] = '';\n });\n } else {\n Object.assign(el.dataset, dataset);\n }\n }\n if (children !== undefined) {\n el.replaceChildren(...children);\n }\n if (parent !== undefined) {\n parent.appendChild(el);\n }\n return el;\n}\n\nexport function addEventListener<K extends keyof HTMLElementEventMap>(\n el: HTMLElement,\n event: K,\n listener: (this: HTMLElement, evt: HTMLElementEventMap[K]) => void,\n options?: AddEventListenerOptions\n): () => void;\nexport function addEventListener<K extends keyof DocumentEventMap>(\n el: Document,\n event: K,\n listener: (this: Document, evt: DocumentEventMap[K]) => void,\n options?: AddEventListenerOptions\n): () => void;\nexport function addEventListener<K extends keyof WindowEventMap>(\n el: Window,\n event: K,\n listener: (this: Window, evt: WindowEventMap[K]) => void,\n options?: AddEventListenerOptions\n): () => void;\nexport function addEventListener<K extends keyof MediaQueryListEventMap>(\n el: MediaQueryList,\n event: K,\n listener: (this: MediaQueryList, evt: MediaQueryListEventMap[K]) => void,\n options?: AddEventListenerOptions\n): () => void;\nexport function addEventListener(\n el: HTMLElement | Document | ShadowRoot | Window | MediaQueryList,\n event: string,\n listener: EventListener,\n options?: AddEventListenerOptions\n) {\n el.addEventListener(event, listener, options);\n return () => el.removeEventListener(event, listener);\n}\n\nexport function getLineNumberAttr(\n el: HTMLElement,\n key = 'line'\n): number | undefined {\n const value = el.dataset[key];\n if (value === undefined) {\n return undefined;\n }\n const lineNumber = parseInt(value, 10);\n if (Number.isNaN(lineNumber)) {\n return undefined;\n }\n return lineNumber;\n}\n\nexport function clampDomOffset(node: Node, offset: number): number {\n if (node.nodeType === 3) {\n const length = (node as Text).textContent?.length ?? 0;\n return Math.max(0, Math.min(offset, length));\n }\n if (node.nodeType === 1) {\n return Math.max(0, Math.min(offset, node.childNodes.length));\n }\n return 0;\n}\n\nexport function extend<T extends object>(obj: T, attrs: Partial<T>): T {\n return Object.assign(obj, attrs);\n}\n\n// oxlint-disable-next-line typescript/no-explicit-any\nexport function debounce<T extends (...args: any[]) => void>(\n func: T,\n wait: number\n): (...args: Parameters<T>) => void {\n let timeout: ReturnType<typeof setTimeout>;\n return function (this: ThisType<T>, ...args: Parameters<T>) {\n clearTimeout(timeout);\n timeout = setTimeout(() => func.apply(this, args), wait);\n };\n}\n\nexport function round(value: number, precision: number = 1000): number {\n return Math.round(value * precision) / precision;\n}\n\nexport function endsWithLineBreak(text: string): boolean {\n return text.endsWith('\\n') || text.endsWith('\\r');\n}\n\n// Creates an Intl.Segmenter, or undefined when the engine lacks support.\n// Callers must fall back to a degraded path when this returns undefined.\nexport function createSegmenter(\n options: Intl.SegmenterOptions\n): Intl.Segmenter | undefined {\n if (typeof Intl === 'undefined' || typeof Intl.Segmenter !== 'function') {\n return undefined;\n }\n return new Intl.Segmenter(undefined, options);\n}\n\n// Lazily created and cached grapheme segmenter. Avoids constructing\n// Intl.Segmenter at import time so importing the editor never throws on\n// engines that lack it. Undefined when the engine has no Intl.Segmenter.\nlet graphemeSegmenter: Intl.Segmenter | undefined;\nlet graphemeSegmenterInit = false;\nexport function getGraphemeSegmenter(): Intl.Segmenter | undefined {\n if (!graphemeSegmenterInit) {\n graphemeSegmenter = createSegmenter({ granularity: 'grapheme' });\n graphemeSegmenterInit = true;\n }\n return graphemeSegmenter;\n}\n"],"mappings":";AAAA,SAAgB,EACd,SACA,OAKA,QAC0B;CAC1B,MAAM,EAAE,OAAO,SAAS,UAAU,GAAG,UAAU,SAAS,CAAC;CACzD,MAAM,KAAK,SAAS,cAAc,OAAO;CACzC,OAAO,OAAO,IAAI,KAAK;CACvB,IAAI,UAAU,KAAA,GACZ,IAAI,OAAO,UAAU,UACnB,GAAG,MAAM,UAAU;MAEnB,OAAO,OAAO,GAAG,OAAO,KAAK;CAGjC,IAAI,YAAY,KAAA,GACd,IAAI,OAAO,YAAY,UACrB,GAAG,QAAQ,WAAW;MACjB,IAAI,MAAM,QAAQ,OAAO,GAC9B,QAAQ,SAAS,QAAQ;EACvB,GAAG,QAAQ,OAAO;CACpB,CAAC;MAED,OAAO,OAAO,GAAG,SAAS,OAAO;CAGrC,IAAI,aAAa,KAAA,GACf,GAAG,gBAAgB,GAAG,QAAQ;CAEhC,IAAI,WAAW,KAAA,GACb,OAAO,YAAY,EAAE;CAEvB,OAAO;AACT;AA0BA,SAAgB,iBACd,IACA,OACA,UACA,SACA;CACA,GAAG,iBAAiB,OAAO,UAAU,OAAO;CAC5C,aAAa,GAAG,oBAAoB,OAAO,
|
|
1
|
+
{"version":3,"file":"utils.js","names":[],"sources":["../../src/editor/utils.ts"],"sourcesContent":["export function h<K extends keyof HTMLElementTagNameMap>(\n tagName: K,\n props?: {\n style?: string | Partial<CSSStyleDeclaration>;\n dataset?: DOMStringMap | string[] | string;\n children?: (Node | string)[];\n } & Partial<Omit<HTMLElementTagNameMap[K], 'style' | 'dataset' | 'children'>>,\n parent?: Element | ShadowRoot | DocumentFragment\n): HTMLElementTagNameMap[K] {\n const { style, dataset, children, ...attrs } = props ?? {};\n const el = document.createElement(tagName);\n Object.assign(el, attrs);\n if (style !== undefined) {\n if (typeof style === 'string') {\n el.style.cssText = style;\n } else {\n Object.assign(el.style, style);\n }\n }\n if (dataset !== undefined) {\n if (typeof dataset === 'string') {\n el.dataset[dataset] = '';\n } else if (Array.isArray(dataset)) {\n dataset.forEach((key) => {\n el.dataset[key] = '';\n });\n } else {\n Object.assign(el.dataset, dataset);\n }\n }\n if (children !== undefined) {\n el.replaceChildren(...children);\n }\n if (parent !== undefined) {\n parent.appendChild(el);\n }\n return el;\n}\n\nexport function addEventListener<K extends keyof HTMLElementEventMap>(\n el: HTMLElement,\n event: K,\n listener: (this: HTMLElement, evt: HTMLElementEventMap[K]) => void,\n options?: AddEventListenerOptions\n): () => void;\nexport function addEventListener<K extends keyof DocumentEventMap>(\n el: Document,\n event: K,\n listener: (this: Document, evt: DocumentEventMap[K]) => void,\n options?: AddEventListenerOptions\n): () => void;\nexport function addEventListener<K extends keyof WindowEventMap>(\n el: Window,\n event: K,\n listener: (this: Window, evt: WindowEventMap[K]) => void,\n options?: AddEventListenerOptions\n): () => void;\nexport function addEventListener<K extends keyof MediaQueryListEventMap>(\n el: MediaQueryList,\n event: K,\n listener: (this: MediaQueryList, evt: MediaQueryListEventMap[K]) => void,\n options?: AddEventListenerOptions\n): () => void;\nexport function addEventListener(\n el: HTMLElement | Document | ShadowRoot | Window | MediaQueryList,\n event: string,\n listener: EventListener,\n options?: AddEventListenerOptions\n) {\n el.addEventListener(event, listener, options);\n return () => el.removeEventListener(event, listener, options);\n}\n\nexport function getLineNumberAttr(\n el: HTMLElement,\n key = 'line'\n): number | undefined {\n const value = el.dataset[key];\n if (value === undefined) {\n return undefined;\n }\n const lineNumber = parseInt(value, 10);\n if (Number.isNaN(lineNumber)) {\n return undefined;\n }\n return lineNumber;\n}\n\nexport function clampDomOffset(node: Node, offset: number): number {\n if (node.nodeType === 3) {\n const length = (node as Text).textContent?.length ?? 0;\n return Math.max(0, Math.min(offset, length));\n }\n if (node.nodeType === 1) {\n return Math.max(0, Math.min(offset, node.childNodes.length));\n }\n return 0;\n}\n\nexport function extend<T extends object>(obj: T, attrs: Partial<T>): T {\n return Object.assign(obj, attrs);\n}\n\n// oxlint-disable-next-line typescript/no-explicit-any\nexport function debounce<T extends (...args: any[]) => void>(\n func: T,\n wait: number\n): (...args: Parameters<T>) => void {\n let timeout: ReturnType<typeof setTimeout>;\n return function (this: ThisType<T>, ...args: Parameters<T>) {\n clearTimeout(timeout);\n timeout = setTimeout(() => func.apply(this, args), wait);\n };\n}\n\nexport function round(value: number, precision: number = 1000): number {\n return Math.round(value * precision) / precision;\n}\n\nexport function endsWithLineBreak(text: string): boolean {\n return text.endsWith('\\n') || text.endsWith('\\r');\n}\n\n// Creates an Intl.Segmenter, or undefined when the engine lacks support.\n// Callers must fall back to a degraded path when this returns undefined.\nexport function createSegmenter(\n options: Intl.SegmenterOptions\n): Intl.Segmenter | undefined {\n if (typeof Intl === 'undefined' || typeof Intl.Segmenter !== 'function') {\n return undefined;\n }\n return new Intl.Segmenter(undefined, options);\n}\n\n// Lazily created and cached grapheme segmenter. Avoids constructing\n// Intl.Segmenter at import time so importing the editor never throws on\n// engines that lack it. Undefined when the engine has no Intl.Segmenter.\nlet graphemeSegmenter: Intl.Segmenter | undefined;\nlet graphemeSegmenterInit = false;\nexport function getGraphemeSegmenter(): Intl.Segmenter | undefined {\n if (!graphemeSegmenterInit) {\n graphemeSegmenter = createSegmenter({ granularity: 'grapheme' });\n graphemeSegmenterInit = true;\n }\n return graphemeSegmenter;\n}\n"],"mappings":";AAAA,SAAgB,EACd,SACA,OAKA,QAC0B;CAC1B,MAAM,EAAE,OAAO,SAAS,UAAU,GAAG,UAAU,SAAS,CAAC;CACzD,MAAM,KAAK,SAAS,cAAc,OAAO;CACzC,OAAO,OAAO,IAAI,KAAK;CACvB,IAA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