@pierre/diffs 1.5.2 → 1.6.0-beta.1
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 +4 -3
- package/dist/components/CodeView.d.ts.map +1 -1
- package/dist/components/CodeView.js +6 -5
- package/dist/components/CodeView.js.map +1 -1
- package/dist/components/File.d.ts.map +1 -1
- package/dist/components/File.js +11 -6
- package/dist/components/File.js.map +1 -1
- package/dist/components/FileDiff.d.ts.map +1 -1
- package/dist/components/FileDiff.js +12 -7
- package/dist/components/FileDiff.js.map +1 -1
- package/dist/components/FileStream.d.ts +6 -4
- package/dist/components/FileStream.d.ts.map +1 -1
- package/dist/components/FileStream.js +49 -36
- package/dist/components/FileStream.js.map +1 -1
- package/dist/constants.d.ts +3 -2
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +2 -1
- package/dist/constants.js.map +1 -1
- package/dist/editor/editor.d.ts +2 -1
- package/dist/editor/editor.d.ts.map +1 -1
- package/dist/editor/editor.js +5 -1
- package/dist/editor/editor.js.map +1 -1
- package/dist/editor/tokenizer.d.ts +6 -4
- package/dist/editor/tokenizer.d.ts.map +1 -1
- package/dist/editor/tokenizer.js +25 -470
- package/dist/editor/tokenizer.js.map +1 -1
- package/dist/highlighter/DiffsEditorTokenizer.d.ts +16 -0
- package/dist/highlighter/DiffsEditorTokenizer.d.ts.map +1 -0
- package/dist/highlighter/DiffsEditorTokenizer.js +0 -0
- package/dist/highlighter/DiffsHighlighter.d.ts +41 -0
- package/dist/highlighter/DiffsHighlighter.d.ts.map +1 -0
- package/dist/highlighter/DiffsHighlighter.js +39 -0
- package/dist/highlighter/DiffsHighlighter.js.map +1 -0
- package/dist/highlighter/backends/highlights.d.ts +26 -0
- package/dist/highlighter/backends/highlights.d.ts.map +1 -0
- package/dist/highlighter/backends/highlights.js +71 -0
- package/dist/highlighter/backends/highlights.js.map +1 -0
- package/dist/highlighter/backends/shiki.d.ts +27 -0
- package/dist/highlighter/backends/shiki.d.ts.map +1 -0
- package/dist/highlighter/backends/shiki.js +107 -0
- package/dist/highlighter/backends/shiki.js.map +1 -0
- package/dist/highlighter/highlighterType.d.ts +13 -0
- package/dist/highlighter/highlighterType.d.ts.map +1 -0
- package/dist/highlighter/highlighterType.js +32 -0
- package/dist/highlighter/highlighterType.js.map +1 -0
- package/dist/highlighter/highlights-editor.d.ts +24 -0
- package/dist/highlighter/highlights-editor.d.ts.map +1 -0
- package/dist/highlighter/highlights-editor.js +166 -0
- package/dist/highlighter/highlights-editor.js.map +1 -0
- package/dist/highlighter/highlights-stream.d.ts +20 -0
- package/dist/highlighter/highlights-stream.d.ts.map +1 -0
- package/dist/highlighter/highlights-stream.js +93 -0
- package/dist/highlighter/highlights-stream.js.map +1 -0
- package/dist/highlighter/languages/areLanguagesAttached.d.ts +2 -1
- package/dist/highlighter/languages/areLanguagesAttached.d.ts.map +1 -1
- package/dist/highlighter/languages/areLanguagesAttached.js +4 -6
- package/dist/highlighter/languages/areLanguagesAttached.js.map +1 -1
- package/dist/highlighter/languages/attachResolvedLanguages.d.ts +6 -1
- package/dist/highlighter/languages/attachResolvedLanguages.d.ts.map +1 -1
- package/dist/highlighter/languages/attachResolvedLanguages.js +6 -11
- package/dist/highlighter/languages/attachResolvedLanguages.js.map +1 -1
- package/dist/highlighter/languages/constants.d.ts +1 -1
- package/dist/highlighter/languages/getResolvedLanguages.d.ts +1 -1
- package/dist/highlighter/languages/getResolvedOrResolveLanguage.d.ts +1 -1
- package/dist/highlighter/languages/resolveLanguage.d.ts +1 -1
- package/dist/highlighter/languages/resolveLanguage.d.ts.map +1 -1
- package/dist/highlighter/languages/resolveLanguage.js +19 -16
- package/dist/highlighter/languages/resolveLanguage.js.map +1 -1
- package/dist/highlighter/languages/resolveLanguages.d.ts +1 -1
- package/dist/highlighter/shared_highlighter.d.ts +23 -9
- package/dist/highlighter/shared_highlighter.d.ts.map +1 -1
- package/dist/highlighter/shared_highlighter.js +59 -47
- package/dist/highlighter/shared_highlighter.js.map +1 -1
- package/dist/highlighter/shiki-editor.d.ts +25 -0
- package/dist/highlighter/shiki-editor.d.ts.map +1 -0
- package/dist/highlighter/shiki-editor.js +483 -0
- package/dist/highlighter/shiki-editor.js.map +1 -0
- package/dist/highlighter/shiki-stream.d.ts +20 -0
- package/dist/highlighter/shiki-stream.d.ts.map +1 -0
- package/dist/highlighter/shiki-stream.js +60 -0
- package/dist/highlighter/shiki-stream.js.map +1 -0
- package/dist/highlighter/stream-tokenizer.d.ts +40 -0
- package/dist/highlighter/stream-tokenizer.d.ts.map +1 -0
- package/dist/highlighter/stream-tokenizer.js +70 -0
- package/dist/highlighter/stream-tokenizer.js.map +1 -0
- package/dist/highlighter/themes/areThemesAttached.d.ts +3 -2
- package/dist/highlighter/themes/areThemesAttached.d.ts.map +1 -1
- package/dist/highlighter/themes/areThemesAttached.js +5 -4
- package/dist/highlighter/themes/areThemesAttached.js.map +1 -1
- package/dist/highlighter/themes/attachResolvedThemes.d.ts +5 -2
- package/dist/highlighter/themes/attachResolvedThemes.d.ts.map +1 -1
- package/dist/highlighter/themes/attachResolvedThemes.js +4 -16
- package/dist/highlighter/themes/attachResolvedThemes.js.map +1 -1
- package/dist/highlighter/themes/cleanUpResolvedThemes.d.ts +3 -1
- package/dist/highlighter/themes/cleanUpResolvedThemes.d.ts.map +1 -1
- package/dist/highlighter/themes/cleanUpResolvedThemes.js +8 -5
- package/dist/highlighter/themes/cleanUpResolvedThemes.js.map +1 -1
- package/dist/highlighter/themes/createCSSVariablesTheme.d.ts +7 -0
- package/dist/highlighter/themes/createCSSVariablesTheme.d.ts.map +1 -0
- package/dist/highlighter/themes/createCSSVariablesTheme.js +89 -0
- package/dist/highlighter/themes/createCSSVariablesTheme.js.map +1 -0
- package/dist/highlighter/themes/createCachedLoader.d.ts +5 -0
- package/dist/highlighter/themes/createCachedLoader.d.ts.map +1 -0
- package/dist/highlighter/themes/createCachedLoader.js +15 -0
- package/dist/highlighter/themes/createCachedLoader.js.map +1 -0
- package/dist/highlighter/themes/getResolvedOrResolveTheme.d.ts +3 -2
- package/dist/highlighter/themes/getResolvedOrResolveTheme.d.ts.map +1 -1
- package/dist/highlighter/themes/getResolvedOrResolveTheme.js +4 -4
- package/dist/highlighter/themes/getResolvedOrResolveTheme.js.map +1 -1
- package/dist/highlighter/themes/getResolvedThemes.d.ts +3 -2
- package/dist/highlighter/themes/getResolvedThemes.d.ts.map +1 -1
- package/dist/highlighter/themes/getResolvedThemes.js +4 -3
- package/dist/highlighter/themes/getResolvedThemes.js.map +1 -1
- package/dist/highlighter/themes/hasResolvedThemes.d.ts +2 -2
- package/dist/highlighter/themes/hasResolvedThemes.d.ts.map +1 -1
- package/dist/highlighter/themes/hasResolvedThemes.js +4 -3
- package/dist/highlighter/themes/hasResolvedThemes.js.map +1 -1
- package/dist/highlighter/themes/registerCustomCSSVariableTheme.d.ts.map +1 -1
- package/dist/highlighter/themes/registerCustomCSSVariableTheme.js +3 -3
- package/dist/highlighter/themes/registerCustomCSSVariableTheme.js.map +1 -1
- package/dist/highlighter/themes/registerCustomTheme.d.ts +9 -4
- package/dist/highlighter/themes/registerCustomTheme.d.ts.map +1 -1
- package/dist/highlighter/themes/registerCustomTheme.js +8 -16
- package/dist/highlighter/themes/registerCustomTheme.js.map +1 -1
- package/dist/highlighter/themes/resolveTheme.d.ts +3 -2
- package/dist/highlighter/themes/resolveTheme.d.ts.map +1 -1
- package/dist/highlighter/themes/resolveTheme.js +4 -7
- package/dist/highlighter/themes/resolveTheme.js.map +1 -1
- package/dist/highlighter/themes/resolveThemes.d.ts +3 -2
- package/dist/highlighter/themes/resolveThemes.d.ts.map +1 -1
- package/dist/highlighter/themes/resolveThemes.js +4 -7
- package/dist/highlighter/themes/resolveThemes.js.map +1 -1
- package/dist/highlighter/themes/themeResolver.d.ts +10 -4
- package/dist/highlighter/themes/themeResolver.d.ts.map +1 -1
- package/dist/highlighter/themes/themeResolver.js +141 -2
- package/dist/highlighter/themes/themeResolver.js.map +1 -1
- package/dist/highlighter/themes/types.d.ts +23 -0
- package/dist/highlighter/themes/types.d.ts.map +1 -0
- package/dist/highlighter/themes/types.js +0 -0
- package/dist/highlighter/tokenizer-types.d.ts +35 -0
- package/dist/highlighter/tokenizer-types.d.ts.map +1 -0
- package/dist/highlighter/tokenizer-types.js +0 -0
- package/dist/highlighter/types.d.ts +55 -0
- package/dist/highlighter/types.d.ts.map +1 -0
- package/dist/highlighter/types.js +0 -0
- package/dist/index.d.ts +14 -10
- package/dist/index.js +16 -18
- package/dist/managers/InteractionManager.d.ts.map +1 -1
- package/dist/managers/InteractionManager.js +2 -2
- package/dist/managers/InteractionManager.js.map +1 -1
- package/dist/node_modules/.pnpm/@types_hast@3.0.4/node_modules/@types/hast/index.d.ts +9 -1
- package/dist/node_modules/.pnpm/@types_hast@3.0.4/node_modules/@types/hast/index.d.ts.map +1 -1
- package/dist/react/CodeView.js +1 -1
- package/dist/react/index.d.ts +8 -3
- package/dist/renderers/DiffHunksRenderer.d.ts +2 -1
- package/dist/renderers/DiffHunksRenderer.d.ts.map +1 -1
- package/dist/renderers/DiffHunksRenderer.js +31 -22
- package/dist/renderers/DiffHunksRenderer.js.map +1 -1
- package/dist/renderers/FileRenderer.d.ts +2 -1
- package/dist/renderers/FileRenderer.d.ts.map +1 -1
- package/dist/renderers/FileRenderer.js +33 -24
- package/dist/renderers/FileRenderer.js.map +1 -1
- package/dist/shiki-stream/index.d.ts +3 -3
- package/dist/shiki-stream/index.js +1 -2
- package/dist/shiki-stream/stream.d.ts +4 -4
- package/dist/shiki-stream/stream.d.ts.map +1 -1
- package/dist/shiki-stream/stream.js +20 -9
- package/dist/shiki-stream/stream.js.map +1 -1
- package/dist/shiki-stream/types.d.ts +7 -38
- package/dist/shiki-stream/types.d.ts.map +1 -1
- package/dist/ssr/FileDiffReact.js +1 -1
- package/dist/ssr/index.d.ts +7 -2
- package/dist/ssr/renderHTML.js +2 -2
- package/dist/ssr/renderHTML.js.map +1 -1
- package/dist/style.js +1 -1
- package/dist/style.js.map +1 -1
- package/dist/types.d.ts +10 -11
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/createSpanNodeFromToken.d.ts +3 -2
- package/dist/utils/createSpanNodeFromToken.d.ts.map +1 -1
- package/dist/utils/createSpanNodeFromToken.js +4 -5
- package/dist/utils/createSpanNodeFromToken.js.map +1 -1
- package/dist/utils/cssWrappers.d.ts.map +1 -1
- package/dist/utils/cssWrappers.js +12 -0
- package/dist/utils/cssWrappers.js.map +1 -1
- package/dist/utils/getHighlighterOptions.d.ts.map +1 -1
- package/dist/utils/getHighlighterOptions.js +3 -2
- package/dist/utils/getHighlighterOptions.js.map +1 -1
- package/dist/utils/getHighlighterThemeStyles.d.ts +2 -1
- package/dist/utils/getHighlighterThemeStyles.d.ts.map +1 -1
- package/dist/utils/getHighlighterThemeStyles.js +13 -3
- package/dist/utils/getHighlighterThemeStyles.js.map +1 -1
- package/dist/utils/getTokenOptions.d.ts +7 -0
- package/dist/utils/getTokenOptions.d.ts.map +1 -0
- package/dist/utils/getTokenOptions.js +16 -0
- package/dist/utils/getTokenOptions.js.map +1 -0
- package/dist/utils/hastToHtml.d.ts +10 -0
- package/dist/utils/hastToHtml.d.ts.map +1 -0
- package/dist/utils/hastToHtml.js +125 -0
- package/dist/utils/hastToHtml.js.map +1 -0
- package/dist/utils/parseDiffDecorations.d.ts +2 -1
- package/dist/utils/parseDiffDecorations.d.ts.map +1 -1
- package/dist/utils/renderDiffWithHighlighter.d.ts +3 -2
- package/dist/utils/renderDiffWithHighlighter.d.ts.map +1 -1
- package/dist/utils/renderDiffWithHighlighter.js +33 -48
- package/dist/utils/renderDiffWithHighlighter.js.map +1 -1
- package/dist/utils/renderFileWithHighlighter.d.ts +3 -2
- package/dist/utils/renderFileWithHighlighter.d.ts.map +1 -1
- package/dist/utils/renderFileWithHighlighter.js +10 -26
- package/dist/utils/renderFileWithHighlighter.js.map +1 -1
- package/dist/utils/renderTokenLines.d.ts +17 -0
- package/dist/utils/renderTokenLines.d.ts.map +1 -0
- package/dist/utils/renderTokenLines.js +246 -0
- package/dist/utils/renderTokenLines.js.map +1 -0
- package/dist/utils/resolvePreferredHighlighter.d.ts +13 -0
- package/dist/utils/resolvePreferredHighlighter.d.ts.map +1 -0
- package/dist/utils/resolvePreferredHighlighter.js +11 -0
- package/dist/utils/resolvePreferredHighlighter.js.map +1 -0
- package/dist/utils/setDeferredArrayItem.d.ts +5 -0
- package/dist/utils/setDeferredArrayItem.d.ts.map +1 -0
- package/dist/utils/setDeferredArrayItem.js +39 -0
- package/dist/utils/setDeferredArrayItem.js.map +1 -0
- package/dist/utils/tokenStyle.d.ts +7 -0
- package/dist/utils/tokenStyle.d.ts.map +1 -0
- package/dist/utils/tokenStyle.js +28 -0
- package/dist/utils/tokenStyle.js.map +1 -0
- package/dist/utils/tokensToHtml.d.ts +8 -0
- package/dist/utils/tokensToHtml.d.ts.map +1 -0
- package/dist/utils/tokensToHtml.js +45 -0
- package/dist/utils/tokensToHtml.js.map +1 -0
- package/dist/utils/wrapTokenFragments.js.map +1 -1
- package/dist/worker/WorkerPoolManager.d.ts +2 -2
- package/dist/worker/WorkerPoolManager.d.ts.map +1 -1
- package/dist/worker/WorkerPoolManager.js +56 -46
- package/dist/worker/WorkerPoolManager.js.map +1 -1
- package/dist/worker/hastLinesTransport.d.ts +14 -0
- package/dist/worker/hastLinesTransport.d.ts.map +1 -0
- package/dist/worker/hastLinesTransport.js +156 -0
- package/dist/worker/hastLinesTransport.js.map +1 -0
- package/dist/worker/highlights-sAHlxqlf.js +317 -0
- package/dist/worker/highlights-sAHlxqlf.js.map +1 -0
- package/dist/worker/index.d.ts +1 -1
- package/dist/worker/shiki-Bx2JpdqZ.js +697 -0
- package/dist/worker/shiki-Bx2JpdqZ.js.map +1 -0
- package/dist/worker/stream-tokenizer-YZ6M1qSx.js +424 -0
- package/dist/worker/stream-tokenizer-YZ6M1qSx.js.map +1 -0
- package/dist/worker/types.d.ts +5 -3
- package/dist/worker/types.d.ts.map +1 -1
- package/dist/worker/worker-portable.js +41 -13337
- package/dist/worker/worker-portable.js.map +1 -1
- package/dist/worker/worker.js +672 -349
- package/dist/worker/worker.js.map +1 -1
- package/dist/worker/workerMessage.d.ts +24 -0
- package/dist/worker/workerMessage.d.ts.map +1 -0
- package/dist/worker/workerMessage.js +0 -0
- package/package.json +33 -29
- package/dist/highlighter/themes/constants.d.ts +0 -5
- package/dist/highlighter/themes/constants.d.ts.map +0 -1
- package/dist/highlighter/themes/constants.js +0 -6
- package/dist/highlighter/themes/constants.js.map +0 -1
- package/dist/highlighter/themes/themeResolution.d.ts +0 -7
- package/dist/highlighter/themes/themeResolution.d.ts.map +0 -1
- package/dist/highlighter/themes/themeResolution.js +0 -21
- package/dist/highlighter/themes/themeResolution.js.map +0 -1
- package/dist/shiki-stream/tokenizer.d.ts +0 -23
- package/dist/shiki-stream/tokenizer.d.ts.map +0 -1
- package/dist/shiki-stream/tokenizer.js +0 -72
- package/dist/shiki-stream/tokenizer.js.map +0 -1
- package/dist/utils/createTransformerWithState.d.ts +0 -12
- package/dist/utils/createTransformerWithState.d.ts.map +0 -1
- package/dist/utils/createTransformerWithState.js +0 -89
- package/dist/utils/createTransformerWithState.js.map +0 -1
- package/dist/worker/wasm-B9ZqxnKj.js +0 -8
- package/dist/worker/wasm-B9ZqxnKj.js.map +0 -1
package/dist/worker/worker.js
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import { createHighlighterCore } from "shiki/core";
|
|
2
|
-
import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
|
|
3
|
-
import { createOnigurumaEngine } from "shiki/engine/oniguruma";
|
|
4
|
-
import { createThemeResolver } from "@pierre/theming";
|
|
5
1
|
import { diffChars, diffWordsWithSpace } from "diff";
|
|
6
|
-
import { transformerStyleToClass } from "@shikijs/transformers";
|
|
7
2
|
import { normalizeThemeColors } from "@pierre/theming/color";
|
|
8
3
|
(() => {
|
|
9
4
|
try {
|
|
@@ -23,52 +18,78 @@ Object.freeze({
|
|
|
23
18
|
//#endregion
|
|
24
19
|
//#region src/highlighter/languages/constants.ts
|
|
25
20
|
const ResolvedLanguages = /* @__PURE__ */ new Map();
|
|
21
|
+
const ResolvingLanguages = /* @__PURE__ */ new Map();
|
|
22
|
+
const RegisteredCustomLanguages = /* @__PURE__ */ new Map();
|
|
26
23
|
const AttachedLanguages = /* @__PURE__ */ new Set();
|
|
27
24
|
//#endregion
|
|
28
25
|
//#region src/highlighter/languages/attachResolvedLanguages.ts
|
|
26
|
+
/**
|
|
27
|
+
* Record resolved grammars in the shared language cache and attach them to a
|
|
28
|
+
* highlighter. Highlights bundles its lexers and ignores the grammars.
|
|
29
|
+
*/
|
|
29
30
|
function attachResolvedLanguages(resolvedLanguages, highlighter) {
|
|
30
|
-
|
|
31
|
-
for (const resolvedLang of resolvedLanguages) {
|
|
32
|
-
if (AttachedLanguages.has(resolvedLang.name)) continue;
|
|
31
|
+
for (const resolvedLang of Array.isArray(resolvedLanguages) ? resolvedLanguages : [resolvedLanguages]) {
|
|
33
32
|
let lang = ResolvedLanguages.get(resolvedLang.name);
|
|
34
33
|
if (lang == null) {
|
|
35
34
|
lang = resolvedLang;
|
|
36
35
|
ResolvedLanguages.set(resolvedLang.name, lang);
|
|
37
36
|
}
|
|
38
|
-
|
|
39
|
-
if (grammar == null) throw new Error(`attachResolvedLanguages: No returned grammar declares "${lang.name}" as its name or an alias.`);
|
|
40
|
-
highlighter.loadLanguageSync(lang.data);
|
|
41
|
-
try {
|
|
42
|
-
highlighter.getLanguage(lang.name);
|
|
43
|
-
} catch {
|
|
44
|
-
throw new Error(`attachResolvedLanguages: "${grammar.name}" is already loaded without alias "${lang.name}". Load the alias first or give the grammar a unique name.`);
|
|
45
|
-
}
|
|
37
|
+
highlighter.attachLanguages([lang]);
|
|
46
38
|
AttachedLanguages.add(lang.name);
|
|
47
39
|
}
|
|
48
40
|
}
|
|
49
41
|
//#endregion
|
|
50
|
-
//#region src/highlighter/
|
|
51
|
-
|
|
42
|
+
//#region src/highlighter/highlighterType.ts
|
|
43
|
+
let activeType;
|
|
44
|
+
let holds = 0;
|
|
45
|
+
var HighlighterDisposedError = class extends Error {
|
|
46
|
+
constructor() {
|
|
47
|
+
super("Highlighter is disposed");
|
|
48
|
+
this.name = "HighlighterDisposedError";
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
function resolveHighlighterType(type) {
|
|
52
|
+
return type ?? activeType ?? "shiki-js";
|
|
53
|
+
}
|
|
54
|
+
function assertHighlighterType(type) {
|
|
55
|
+
if (activeType != null && activeType !== type) throw new Error(`Cannot load the "${type}" highlighter while "${activeType}" is in use. Only one highlighter type can be active at a time; dispose every "${activeType}" instance (disposeHighlighter() disposes the shared one) before switching.`);
|
|
56
|
+
}
|
|
57
|
+
function acquireHighlighterType(type) {
|
|
58
|
+
assertHighlighterType(type);
|
|
59
|
+
activeType = type;
|
|
60
|
+
holds++;
|
|
61
|
+
}
|
|
62
|
+
function releaseHighlighterType() {
|
|
63
|
+
holds--;
|
|
64
|
+
if (holds === 0) activeType = void 0;
|
|
65
|
+
}
|
|
52
66
|
//#endregion
|
|
53
|
-
//#region src/highlighter/
|
|
54
|
-
|
|
67
|
+
//#region src/highlighter/shared_highlighter.ts
|
|
68
|
+
/**
|
|
69
|
+
* Creates an instance the caller must dispose. Defaults to the active type
|
|
70
|
+
* or DEFAULT_HIGHLIGHTER; rejects if a different type is already active.
|
|
71
|
+
*/
|
|
72
|
+
async function createHighlighter(type) {
|
|
73
|
+
const resolvedType = resolveHighlighterType(type);
|
|
74
|
+
acquireHighlighterType(resolvedType);
|
|
75
|
+
try {
|
|
76
|
+
if (resolvedType === "highlights") {
|
|
77
|
+
const { HighlightsHighlighter } = await import("./highlights-sAHlxqlf.js");
|
|
78
|
+
return new HighlightsHighlighter();
|
|
79
|
+
}
|
|
80
|
+
const { ShikiHighlighter } = await import("./shiki-Bx2JpdqZ.js");
|
|
81
|
+
return await ShikiHighlighter.create(resolvedType);
|
|
82
|
+
} finally {
|
|
83
|
+
releaseHighlighterType();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
55
86
|
//#endregion
|
|
56
87
|
//#region src/highlighter/themes/attachResolvedThemes.ts
|
|
88
|
+
/** Seed themes received from the main thread into the worker's backend resolver. */
|
|
57
89
|
function attachResolvedThemes(themes, highlighter) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (typeof themeRef === "string") {
|
|
62
|
-
resolvedTheme = themeResolver.getResolvedTheme(themeRef);
|
|
63
|
-
if (resolvedTheme == null) throw new Error(`loadResolvedThemes: ${themeRef} is not resolved, you must resolve it before calling loadResolvedThemes`);
|
|
64
|
-
} else {
|
|
65
|
-
resolvedTheme = themeRef;
|
|
66
|
-
themeRef = themeRef.name;
|
|
67
|
-
if (themeResolver.getResolvedTheme(themeRef) == null) themeResolver.seedResolvedTheme(themeRef, resolvedTheme);
|
|
68
|
-
}
|
|
69
|
-
if (AttachedThemes.has(themeRef)) continue;
|
|
70
|
-
AttachedThemes.add(themeRef);
|
|
71
|
-
highlighter.loadThemeSync(resolvedTheme);
|
|
90
|
+
for (const theme of Array.isArray(themes) ? themes : [themes]) {
|
|
91
|
+
const resolved = typeof theme === "string" ? highlighter.themeResolver.getResolvedThemes([theme])[0] : theme;
|
|
92
|
+
highlighter.themeResolver.seedResolvedTheme(resolved.name, resolved);
|
|
72
93
|
}
|
|
73
94
|
}
|
|
74
95
|
//#endregion
|
|
@@ -478,210 +499,6 @@ function cleanLastNewline(contents) {
|
|
|
478
499
|
return contents.slice(0, end);
|
|
479
500
|
}
|
|
480
501
|
//#endregion
|
|
481
|
-
//#region src/utils/hast_utils.ts
|
|
482
|
-
function createTextNodeElement(value) {
|
|
483
|
-
return {
|
|
484
|
-
type: "text",
|
|
485
|
-
value
|
|
486
|
-
};
|
|
487
|
-
}
|
|
488
|
-
function createHastElement({ tagName, children = [], properties = {} }) {
|
|
489
|
-
return {
|
|
490
|
-
type: "element",
|
|
491
|
-
tagName,
|
|
492
|
-
properties,
|
|
493
|
-
children
|
|
494
|
-
};
|
|
495
|
-
}
|
|
496
|
-
function findCodeElement(nodes) {
|
|
497
|
-
let firstChild = nodes.children[0];
|
|
498
|
-
while (firstChild != null) {
|
|
499
|
-
if (firstChild.type === "element" && firstChild.tagName === "code") return firstChild;
|
|
500
|
-
if ("children" in firstChild) firstChild = firstChild.children[0];
|
|
501
|
-
else firstChild = null;
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
//#endregion
|
|
505
|
-
//#region src/utils/processLine.ts
|
|
506
|
-
function processLine(node, line, state) {
|
|
507
|
-
const lineInfo = typeof state.lineInfo === "function" ? state.lineInfo(line) : state.lineInfo[line - 1];
|
|
508
|
-
if (lineInfo == null) {
|
|
509
|
-
const errorMessage = `processLine: line ${line}, contains no state.lineInfo`;
|
|
510
|
-
console.error(errorMessage, {
|
|
511
|
-
node,
|
|
512
|
-
line,
|
|
513
|
-
state
|
|
514
|
-
});
|
|
515
|
-
throw new Error(errorMessage);
|
|
516
|
-
}
|
|
517
|
-
node.tagName = "div";
|
|
518
|
-
node.properties["data-line"] = lineInfo.lineNumber;
|
|
519
|
-
node.properties["data-alt-line"] = lineInfo.altLineNumber;
|
|
520
|
-
node.properties["data-line-type"] = lineInfo.type;
|
|
521
|
-
node.properties["data-line-index"] = lineInfo.lineIndex;
|
|
522
|
-
if (node.children.length === 0) node.children.push(createTextNodeElement("\n"));
|
|
523
|
-
return node;
|
|
524
|
-
}
|
|
525
|
-
//#endregion
|
|
526
|
-
//#region src/utils/wrapTokenFragments.ts
|
|
527
|
-
const NO_TOKEN = Symbol("no-token");
|
|
528
|
-
const MULTIPLE_TOKENS = Symbol("multiple-tokens");
|
|
529
|
-
function wrapTokenFragments(container) {
|
|
530
|
-
const ownTokenChar = getTokenChar(container);
|
|
531
|
-
if (ownTokenChar != null) return ownTokenChar;
|
|
532
|
-
let containerTokenState = NO_TOKEN;
|
|
533
|
-
const wrappedChildren = [];
|
|
534
|
-
let currentTokenChildren = [];
|
|
535
|
-
let currentTokenChar;
|
|
536
|
-
const flushTokenChildren = () => {
|
|
537
|
-
if (currentTokenChildren.length === 0 || currentTokenChar == null) {
|
|
538
|
-
currentTokenChildren = [];
|
|
539
|
-
currentTokenChar = void 0;
|
|
540
|
-
return;
|
|
541
|
-
}
|
|
542
|
-
if (currentTokenChildren.length === 1) {
|
|
543
|
-
const child = currentTokenChildren[0];
|
|
544
|
-
if (child?.type === "element") {
|
|
545
|
-
setTokenChar(child, currentTokenChar);
|
|
546
|
-
for (const grandChild of child.children) stripTokenChar(grandChild);
|
|
547
|
-
} else stripTokenChar(child);
|
|
548
|
-
wrappedChildren.push(child);
|
|
549
|
-
currentTokenChildren = [];
|
|
550
|
-
currentTokenChar = void 0;
|
|
551
|
-
return;
|
|
552
|
-
}
|
|
553
|
-
for (const child of currentTokenChildren) stripTokenChar(child);
|
|
554
|
-
wrappedChildren.push(createHastElement({
|
|
555
|
-
tagName: "span",
|
|
556
|
-
properties: { "data-char": currentTokenChar },
|
|
557
|
-
children: currentTokenChildren
|
|
558
|
-
}));
|
|
559
|
-
currentTokenChildren = [];
|
|
560
|
-
currentTokenChar = void 0;
|
|
561
|
-
};
|
|
562
|
-
const mergeContainerTokenState = (childTokenState) => {
|
|
563
|
-
if (childTokenState === NO_TOKEN) return;
|
|
564
|
-
if (childTokenState === MULTIPLE_TOKENS) {
|
|
565
|
-
containerTokenState = MULTIPLE_TOKENS;
|
|
566
|
-
return;
|
|
567
|
-
}
|
|
568
|
-
if (containerTokenState === NO_TOKEN) {
|
|
569
|
-
containerTokenState = childTokenState;
|
|
570
|
-
return;
|
|
571
|
-
}
|
|
572
|
-
if (containerTokenState !== childTokenState) containerTokenState = MULTIPLE_TOKENS;
|
|
573
|
-
};
|
|
574
|
-
for (const child of container.children) {
|
|
575
|
-
const childTokenState = child.type === "element" ? wrapTokenFragments(child) : NO_TOKEN;
|
|
576
|
-
mergeContainerTokenState(childTokenState);
|
|
577
|
-
if (typeof childTokenState !== "number") {
|
|
578
|
-
flushTokenChildren();
|
|
579
|
-
wrappedChildren.push(child);
|
|
580
|
-
continue;
|
|
581
|
-
}
|
|
582
|
-
if (currentTokenChar != null && currentTokenChar !== childTokenState) flushTokenChildren();
|
|
583
|
-
currentTokenChar ??= childTokenState;
|
|
584
|
-
currentTokenChildren.push(child);
|
|
585
|
-
}
|
|
586
|
-
flushTokenChildren();
|
|
587
|
-
container.children = wrappedChildren;
|
|
588
|
-
return containerTokenState;
|
|
589
|
-
}
|
|
590
|
-
function getTokenChar(node) {
|
|
591
|
-
const value = node.properties["data-char"];
|
|
592
|
-
if (typeof value === "number") return value;
|
|
593
|
-
}
|
|
594
|
-
function stripTokenChar(node) {
|
|
595
|
-
if (node.type !== "element") return;
|
|
596
|
-
node.properties["data-char"] = void 0;
|
|
597
|
-
for (const child of node.children) stripTokenChar(child);
|
|
598
|
-
}
|
|
599
|
-
function setTokenChar(node, char) {
|
|
600
|
-
node.properties["data-char"] = char;
|
|
601
|
-
}
|
|
602
|
-
//#endregion
|
|
603
|
-
//#region src/utils/createTransformerWithState.ts
|
|
604
|
-
function createTransformerWithState(useTokenTransformer = false, useCSSClasses = false) {
|
|
605
|
-
const state = { lineInfo: [] };
|
|
606
|
-
const transformers = [{
|
|
607
|
-
line(node) {
|
|
608
|
-
delete node.properties.class;
|
|
609
|
-
return node;
|
|
610
|
-
},
|
|
611
|
-
pre(pre) {
|
|
612
|
-
const code = findCodeElement(pre);
|
|
613
|
-
const children = [];
|
|
614
|
-
if (code != null) {
|
|
615
|
-
let index = 1;
|
|
616
|
-
for (const node of code.children) {
|
|
617
|
-
if (node.type !== "element") continue;
|
|
618
|
-
if (useTokenTransformer) wrapTokenFragments(node);
|
|
619
|
-
children.push(processLine(node, index, state));
|
|
620
|
-
index++;
|
|
621
|
-
}
|
|
622
|
-
code.children = children;
|
|
623
|
-
}
|
|
624
|
-
return pre;
|
|
625
|
-
},
|
|
626
|
-
...useTokenTransformer ? {
|
|
627
|
-
tokens(lines) {
|
|
628
|
-
for (const line of lines) {
|
|
629
|
-
let col = 0;
|
|
630
|
-
for (const token of line) {
|
|
631
|
-
const tokenWithOriginalRange = token;
|
|
632
|
-
tokenWithOriginalRange.__lineChar ??= col;
|
|
633
|
-
col += token.content.length;
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
},
|
|
637
|
-
preprocess(_code, options) {
|
|
638
|
-
options.mergeWhitespaces = "never";
|
|
639
|
-
},
|
|
640
|
-
span(hast, _line, _char, _lineElement, token) {
|
|
641
|
-
if (token?.offset != null && token.content != null) {
|
|
642
|
-
const tokenChar = token.__lineChar;
|
|
643
|
-
if (tokenChar != null) hast.properties["data-char"] = tokenChar;
|
|
644
|
-
return hast;
|
|
645
|
-
}
|
|
646
|
-
return hast;
|
|
647
|
-
}
|
|
648
|
-
} : null
|
|
649
|
-
}];
|
|
650
|
-
if (useCSSClasses) transformers.push(tokenStyleNormalizer, toClass);
|
|
651
|
-
if (useTokenTransformer) transformers.push({ line: (node) => {
|
|
652
|
-
if (node.type === "element" && node.children.length === 0) node.children.push({
|
|
653
|
-
type: "element",
|
|
654
|
-
tagName: "br",
|
|
655
|
-
properties: {},
|
|
656
|
-
children: []
|
|
657
|
-
});
|
|
658
|
-
return node;
|
|
659
|
-
} });
|
|
660
|
-
return {
|
|
661
|
-
state,
|
|
662
|
-
transformers,
|
|
663
|
-
toClass
|
|
664
|
-
};
|
|
665
|
-
}
|
|
666
|
-
const toClass = transformerStyleToClass({ classPrefix: "hl-" });
|
|
667
|
-
const tokenStyleNormalizer = {
|
|
668
|
-
name: "token-style-normalizer",
|
|
669
|
-
tokens(lines) {
|
|
670
|
-
for (const line of lines) for (const token of line) {
|
|
671
|
-
if (token.htmlStyle != null) continue;
|
|
672
|
-
const style = {};
|
|
673
|
-
if (token.color != null) style.color = token.color;
|
|
674
|
-
if (token.bgColor != null) style["background-color"] = token.bgColor;
|
|
675
|
-
if (token.fontStyle != null && token.fontStyle !== 0) {
|
|
676
|
-
if ((token.fontStyle & 1) !== 0) style["font-style"] = "italic";
|
|
677
|
-
if ((token.fontStyle & 2) !== 0) style["font-weight"] = "bold";
|
|
678
|
-
if ((token.fontStyle & 4) !== 0) style["text-decoration"] = "underline";
|
|
679
|
-
}
|
|
680
|
-
if (Object.keys(style).length > 0) token.htmlStyle = style;
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
};
|
|
684
|
-
//#endregion
|
|
685
502
|
//#region src/utils/formatCSSVariablePrefix.ts
|
|
686
503
|
function formatCSSVariablePrefix(type) {
|
|
687
504
|
return `--${type === "token" ? "diffs-token" : "diffs"}-`;
|
|
@@ -692,7 +509,7 @@ function getHighlighterThemeStyles({ theme = DEFAULT_THEMES, highlighter, prefix
|
|
|
692
509
|
let styles = "";
|
|
693
510
|
if (typeof theme === "string") {
|
|
694
511
|
const themeData = highlighter.getTheme(theme);
|
|
695
|
-
const normalized =
|
|
512
|
+
const normalized = getThemeColors(themeData, highlighter);
|
|
696
513
|
styles += `color:${normalized.fg};`;
|
|
697
514
|
styles += `background-color:${normalized.bg};`;
|
|
698
515
|
styles += `${formatCSSVariablePrefix("global")}fg:${normalized.fg};`;
|
|
@@ -700,18 +517,28 @@ function getHighlighterThemeStyles({ theme = DEFAULT_THEMES, highlighter, prefix
|
|
|
700
517
|
styles += getGitVariables(themeData, prefix);
|
|
701
518
|
} else {
|
|
702
519
|
let themeData = highlighter.getTheme(theme.dark);
|
|
703
|
-
let normalized =
|
|
520
|
+
let normalized = getThemeColors(themeData, highlighter);
|
|
704
521
|
styles += `${formatCSSVariablePrefix("global")}dark:${normalized.fg};`;
|
|
705
522
|
styles += `${formatCSSVariablePrefix("global")}dark-bg:${normalized.bg};`;
|
|
706
523
|
styles += getGitVariables(themeData, "dark");
|
|
707
524
|
themeData = highlighter.getTheme(theme.light);
|
|
708
|
-
normalized =
|
|
525
|
+
normalized = getThemeColors(themeData, highlighter);
|
|
709
526
|
styles += `${formatCSSVariablePrefix("global")}light:${normalized.fg};`;
|
|
710
527
|
styles += `${formatCSSVariablePrefix("global")}light-bg:${normalized.bg};`;
|
|
711
528
|
styles += getGitVariables(themeData, "light");
|
|
712
529
|
}
|
|
713
530
|
return styles;
|
|
714
531
|
}
|
|
532
|
+
function getThemeColors(theme, highlighter) {
|
|
533
|
+
if (highlighter.name === "highlights" && theme.zed?.cssVariables === true) {
|
|
534
|
+
const prefix = formatCSSVariablePrefix("token");
|
|
535
|
+
return {
|
|
536
|
+
fg: `var(${prefix}foreground)`,
|
|
537
|
+
bg: `var(${prefix}background)`
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
return normalizeThemeColors(theme);
|
|
541
|
+
}
|
|
715
542
|
function getGitVariables(themeData, modePrefix) {
|
|
716
543
|
modePrefix = modePrefix != null ? `${modePrefix}-` : "";
|
|
717
544
|
let styles = "";
|
|
@@ -724,16 +551,16 @@ function getGitVariables(themeData, modePrefix) {
|
|
|
724
551
|
return styles;
|
|
725
552
|
}
|
|
726
553
|
//#endregion
|
|
727
|
-
//#region src/utils/
|
|
728
|
-
function
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
554
|
+
//#region src/utils/getTokenOptions.ts
|
|
555
|
+
function getTokenOptions(lang, theme, tokenizeMaxLineLength) {
|
|
556
|
+
return {
|
|
557
|
+
lang,
|
|
558
|
+
...typeof theme === "string" ? { theme } : { themes: theme },
|
|
559
|
+
defaultColor: false,
|
|
560
|
+
cssVariablePrefix: formatCSSVariablePrefix("token"),
|
|
561
|
+
tokenizeMaxLineLength,
|
|
562
|
+
tokenizeTimeLimit: 0
|
|
563
|
+
};
|
|
737
564
|
}
|
|
738
565
|
//#endregion
|
|
739
566
|
//#region src/utils/getHunkSideBoundaries.ts
|
|
@@ -1327,9 +1154,423 @@ function pushOrJoinSpan({ item, arr, enableJoin, isNeutral = false, isLastItem =
|
|
|
1327
1154
|
arr.push([isNeutral ? 0 : 1, item.value]);
|
|
1328
1155
|
}
|
|
1329
1156
|
//#endregion
|
|
1157
|
+
//#region src/utils/hast_utils.ts
|
|
1158
|
+
function createTextNodeElement(value) {
|
|
1159
|
+
return {
|
|
1160
|
+
type: "text",
|
|
1161
|
+
value
|
|
1162
|
+
};
|
|
1163
|
+
}
|
|
1164
|
+
function createHastElement({ tagName, children = [], properties = {} }) {
|
|
1165
|
+
return {
|
|
1166
|
+
type: "element",
|
|
1167
|
+
tagName,
|
|
1168
|
+
properties,
|
|
1169
|
+
children
|
|
1170
|
+
};
|
|
1171
|
+
}
|
|
1172
|
+
//#endregion
|
|
1173
|
+
//#region src/utils/processLine.ts
|
|
1174
|
+
function processLine(node, line, state) {
|
|
1175
|
+
const lineInfo = typeof state.lineInfo === "function" ? state.lineInfo(line) : state.lineInfo[line - 1];
|
|
1176
|
+
if (lineInfo == null) {
|
|
1177
|
+
const errorMessage = `processLine: line ${line}, contains no state.lineInfo`;
|
|
1178
|
+
console.error(errorMessage, {
|
|
1179
|
+
node,
|
|
1180
|
+
line,
|
|
1181
|
+
state
|
|
1182
|
+
});
|
|
1183
|
+
throw new Error(errorMessage);
|
|
1184
|
+
}
|
|
1185
|
+
node.tagName = "div";
|
|
1186
|
+
node.properties["data-line"] = lineInfo.lineNumber;
|
|
1187
|
+
node.properties["data-alt-line"] = lineInfo.altLineNumber;
|
|
1188
|
+
node.properties["data-line-type"] = lineInfo.type;
|
|
1189
|
+
node.properties["data-line-index"] = lineInfo.lineIndex;
|
|
1190
|
+
if (node.children.length === 0) node.children.push(createTextNodeElement("\n"));
|
|
1191
|
+
return node;
|
|
1192
|
+
}
|
|
1193
|
+
//#endregion
|
|
1194
|
+
//#region src/utils/setDeferredArrayItem.ts
|
|
1195
|
+
function setDeferredArrayItem(array, index, create) {
|
|
1196
|
+
let initialized = false;
|
|
1197
|
+
let cached;
|
|
1198
|
+
Object.defineProperty(array, index, {
|
|
1199
|
+
configurable: true,
|
|
1200
|
+
enumerable: true,
|
|
1201
|
+
get() {
|
|
1202
|
+
if (!initialized) {
|
|
1203
|
+
cached = create();
|
|
1204
|
+
initialized = true;
|
|
1205
|
+
}
|
|
1206
|
+
if (Object.getOwnPropertyDescriptor(array, index)?.configurable === true) Object.defineProperty(array, index, {
|
|
1207
|
+
configurable: true,
|
|
1208
|
+
enumerable: true,
|
|
1209
|
+
writable: true,
|
|
1210
|
+
value: cached
|
|
1211
|
+
});
|
|
1212
|
+
return cached;
|
|
1213
|
+
},
|
|
1214
|
+
set(value) {
|
|
1215
|
+
if (Object.getOwnPropertyDescriptor(array, index)?.configurable === true) Object.defineProperty(array, index, {
|
|
1216
|
+
configurable: true,
|
|
1217
|
+
enumerable: true,
|
|
1218
|
+
writable: true,
|
|
1219
|
+
value
|
|
1220
|
+
});
|
|
1221
|
+
else if (Object.isFrozen(array)) throw new TypeError(`Cannot assign to read only array index '${index}'`);
|
|
1222
|
+
else {
|
|
1223
|
+
cached = value;
|
|
1224
|
+
initialized = true;
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
});
|
|
1228
|
+
}
|
|
1229
|
+
//#endregion
|
|
1230
|
+
//#region src/utils/tokenStyle.ts
|
|
1231
|
+
const htmlStyleCache = /* @__PURE__ */ new WeakMap();
|
|
1232
|
+
function tokenStyle(token, cacheHtmlStyle) {
|
|
1233
|
+
const { htmlStyle } = token;
|
|
1234
|
+
if (htmlStyle != null) {
|
|
1235
|
+
let style = cacheHtmlStyle ? htmlStyleCache.get(htmlStyle) : void 0;
|
|
1236
|
+
if (style == null) {
|
|
1237
|
+
style = "";
|
|
1238
|
+
for (const name in htmlStyle) style += `${style === "" ? "" : ";"}${name}:${htmlStyle[name]}`;
|
|
1239
|
+
if (cacheHtmlStyle) htmlStyleCache.set(htmlStyle, style);
|
|
1240
|
+
}
|
|
1241
|
+
return style;
|
|
1242
|
+
}
|
|
1243
|
+
let style = token.color != null ? `color:${token.color}` : "";
|
|
1244
|
+
if (token.bgColor != null) style += `;background-color:${token.bgColor}`;
|
|
1245
|
+
if (token.fontStyle != null) {
|
|
1246
|
+
if ((token.fontStyle & 1) !== 0) style += ";font-style:italic";
|
|
1247
|
+
if ((token.fontStyle & 2) !== 0) style += ";font-weight:bold";
|
|
1248
|
+
const underline = (token.fontStyle & 4) !== 0;
|
|
1249
|
+
const strikethrough = (token.fontStyle & 8) !== 0;
|
|
1250
|
+
if (underline || strikethrough) style += `;text-decoration:${underline ? "underline" : ""}${underline && strikethrough ? " " : ""}${strikethrough ? "line-through" : ""}`;
|
|
1251
|
+
}
|
|
1252
|
+
return style.replace(/^;/, "");
|
|
1253
|
+
}
|
|
1254
|
+
//#endregion
|
|
1255
|
+
//#region src/utils/wrapTokenFragments.ts
|
|
1256
|
+
const NO_TOKEN = Symbol("no-token");
|
|
1257
|
+
const MULTIPLE_TOKENS = Symbol("multiple-tokens");
|
|
1258
|
+
function wrapTokenFragments(container) {
|
|
1259
|
+
const ownTokenChar = getTokenChar(container);
|
|
1260
|
+
if (ownTokenChar != null) return ownTokenChar;
|
|
1261
|
+
let containerTokenState = NO_TOKEN;
|
|
1262
|
+
const wrappedChildren = [];
|
|
1263
|
+
let currentTokenChildren = [];
|
|
1264
|
+
let currentTokenChar;
|
|
1265
|
+
const flushTokenChildren = () => {
|
|
1266
|
+
if (currentTokenChildren.length === 0 || currentTokenChar == null) {
|
|
1267
|
+
currentTokenChildren = [];
|
|
1268
|
+
currentTokenChar = void 0;
|
|
1269
|
+
return;
|
|
1270
|
+
}
|
|
1271
|
+
if (currentTokenChildren.length === 1) {
|
|
1272
|
+
const child = currentTokenChildren[0];
|
|
1273
|
+
if (child?.type === "element") {
|
|
1274
|
+
setTokenChar(child, currentTokenChar);
|
|
1275
|
+
for (const grandChild of child.children) stripTokenChar(grandChild);
|
|
1276
|
+
} else stripTokenChar(child);
|
|
1277
|
+
wrappedChildren.push(child);
|
|
1278
|
+
currentTokenChildren = [];
|
|
1279
|
+
currentTokenChar = void 0;
|
|
1280
|
+
return;
|
|
1281
|
+
}
|
|
1282
|
+
for (const child of currentTokenChildren) stripTokenChar(child);
|
|
1283
|
+
wrappedChildren.push(createHastElement({
|
|
1284
|
+
tagName: "span",
|
|
1285
|
+
properties: { "data-char": currentTokenChar },
|
|
1286
|
+
children: currentTokenChildren
|
|
1287
|
+
}));
|
|
1288
|
+
currentTokenChildren = [];
|
|
1289
|
+
currentTokenChar = void 0;
|
|
1290
|
+
};
|
|
1291
|
+
const mergeContainerTokenState = (childTokenState) => {
|
|
1292
|
+
if (childTokenState === NO_TOKEN) return;
|
|
1293
|
+
if (childTokenState === MULTIPLE_TOKENS) {
|
|
1294
|
+
containerTokenState = MULTIPLE_TOKENS;
|
|
1295
|
+
return;
|
|
1296
|
+
}
|
|
1297
|
+
if (containerTokenState === NO_TOKEN) {
|
|
1298
|
+
containerTokenState = childTokenState;
|
|
1299
|
+
return;
|
|
1300
|
+
}
|
|
1301
|
+
if (containerTokenState !== childTokenState) containerTokenState = MULTIPLE_TOKENS;
|
|
1302
|
+
};
|
|
1303
|
+
for (const child of container.children) {
|
|
1304
|
+
const childTokenState = child.type === "element" ? wrapTokenFragments(child) : NO_TOKEN;
|
|
1305
|
+
mergeContainerTokenState(childTokenState);
|
|
1306
|
+
if (typeof childTokenState !== "number") {
|
|
1307
|
+
flushTokenChildren();
|
|
1308
|
+
wrappedChildren.push(child);
|
|
1309
|
+
continue;
|
|
1310
|
+
}
|
|
1311
|
+
if (currentTokenChar != null && currentTokenChar !== childTokenState) flushTokenChildren();
|
|
1312
|
+
currentTokenChar ??= childTokenState;
|
|
1313
|
+
currentTokenChildren.push(child);
|
|
1314
|
+
}
|
|
1315
|
+
flushTokenChildren();
|
|
1316
|
+
container.children = wrappedChildren;
|
|
1317
|
+
return containerTokenState;
|
|
1318
|
+
}
|
|
1319
|
+
function getTokenChar(node) {
|
|
1320
|
+
const value = node.properties["data-char"];
|
|
1321
|
+
if (typeof value === "number") return value;
|
|
1322
|
+
}
|
|
1323
|
+
function stripTokenChar(node) {
|
|
1324
|
+
if (node.type !== "element") return;
|
|
1325
|
+
node.properties["data-char"] = void 0;
|
|
1326
|
+
for (const child of node.children) stripTokenChar(child);
|
|
1327
|
+
}
|
|
1328
|
+
function setTokenChar(node, char) {
|
|
1329
|
+
node.properties["data-char"] = char;
|
|
1330
|
+
}
|
|
1331
|
+
//#endregion
|
|
1332
|
+
//#region src/utils/renderTokenLines.ts
|
|
1333
|
+
function renderTokenLines(lines, { state, lazyLineAST = false, cacheHtmlStyles = false, useTokenTransformer = false, mergeWhitespaces = "always", decorations = [], lineOffsets = [] }) {
|
|
1334
|
+
let decorationsByLine;
|
|
1335
|
+
for (const decoration of decorations) {
|
|
1336
|
+
const byLine = decorationsByLine ??= /* @__PURE__ */ new Map();
|
|
1337
|
+
const normalized = {
|
|
1338
|
+
...decoration,
|
|
1339
|
+
start: getDecorationPosition(decoration.start, lineOffsets),
|
|
1340
|
+
end: getDecorationPosition(decoration.end, lineOffsets)
|
|
1341
|
+
};
|
|
1342
|
+
const lastLine = Math.min(normalized.end.line, lines.length - 1);
|
|
1343
|
+
for (let line = normalized.start.line; line <= lastLine; line++) {
|
|
1344
|
+
let spans = byLine.get(line);
|
|
1345
|
+
if (spans == null) byLine.set(line, spans = []);
|
|
1346
|
+
spans.push(normalized);
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
const renderLine = (tokens, lineIndex) => {
|
|
1350
|
+
let normalized;
|
|
1351
|
+
if (useTokenTransformer || mergeWhitespaces !== "never") {
|
|
1352
|
+
let pendingWhitespace = "";
|
|
1353
|
+
let whitespaceOffset = 0;
|
|
1354
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
1355
|
+
let token = tokens[i];
|
|
1356
|
+
if (useTokenTransformer) {
|
|
1357
|
+
const content = token.content.trim();
|
|
1358
|
+
if (content !== "" && content !== token.content) {
|
|
1359
|
+
normalized ??= tokens.slice(0, i);
|
|
1360
|
+
const start = token.content.indexOf(content);
|
|
1361
|
+
if (start > 0) normalized.push({
|
|
1362
|
+
content: token.content.slice(0, start),
|
|
1363
|
+
offset: token.offset
|
|
1364
|
+
});
|
|
1365
|
+
normalized.push({
|
|
1366
|
+
...token,
|
|
1367
|
+
content,
|
|
1368
|
+
offset: token.offset + start
|
|
1369
|
+
});
|
|
1370
|
+
const end = start + content.length;
|
|
1371
|
+
if (end < token.content.length) normalized.push({
|
|
1372
|
+
content: token.content.slice(end),
|
|
1373
|
+
offset: token.offset + end
|
|
1374
|
+
});
|
|
1375
|
+
continue;
|
|
1376
|
+
}
|
|
1377
|
+
} else {
|
|
1378
|
+
const decoratedWhitespace = ((token.fontStyle ?? 0) & 12) !== 0;
|
|
1379
|
+
if (!decoratedWhitespace && /^\s+$/.test(token.content) && i < tokens.length - 1) {
|
|
1380
|
+
normalized ??= tokens.slice(0, i);
|
|
1381
|
+
if (pendingWhitespace === "") whitespaceOffset = token.offset;
|
|
1382
|
+
pendingWhitespace += token.content;
|
|
1383
|
+
continue;
|
|
1384
|
+
}
|
|
1385
|
+
if (pendingWhitespace !== "" && normalized != null) {
|
|
1386
|
+
if (decoratedWhitespace) normalized.push({
|
|
1387
|
+
content: pendingWhitespace,
|
|
1388
|
+
offset: whitespaceOffset
|
|
1389
|
+
});
|
|
1390
|
+
else token = {
|
|
1391
|
+
...token,
|
|
1392
|
+
content: pendingWhitespace + token.content,
|
|
1393
|
+
offset: whitespaceOffset
|
|
1394
|
+
};
|
|
1395
|
+
pendingWhitespace = "";
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
normalized?.push(token);
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
normalized ??= tokens;
|
|
1402
|
+
const line = {
|
|
1403
|
+
type: "element",
|
|
1404
|
+
tagName: state == null ? "span" : "div",
|
|
1405
|
+
properties: state == null ? { class: "line" } : {},
|
|
1406
|
+
children: []
|
|
1407
|
+
};
|
|
1408
|
+
const spans = decorationsByLine?.get(lineIndex);
|
|
1409
|
+
const column = spans == null ? appendTokens(line, normalized, useTokenTransformer, cacheHtmlStyles) : appendDecoratedTokens(line, normalized, spans, lineIndex, useTokenTransformer, cacheHtmlStyles);
|
|
1410
|
+
if (useTokenTransformer && column === 0) line.children.push({
|
|
1411
|
+
type: "element",
|
|
1412
|
+
tagName: "br",
|
|
1413
|
+
properties: {},
|
|
1414
|
+
children: []
|
|
1415
|
+
});
|
|
1416
|
+
if (useTokenTransformer && spans != null) wrapTokenFragments(line);
|
|
1417
|
+
return state == null ? line : processLine(line, lineIndex + 1, state);
|
|
1418
|
+
};
|
|
1419
|
+
if (!lazyLineAST) return lines.map(renderLine);
|
|
1420
|
+
const rows = new Array(lines.length);
|
|
1421
|
+
for (let index = 0; index < lines.length; index++) {
|
|
1422
|
+
const tokens = lines[index];
|
|
1423
|
+
setDeferredArrayItem(rows, index, () => renderLine(tokens, index));
|
|
1424
|
+
}
|
|
1425
|
+
return rows;
|
|
1426
|
+
}
|
|
1427
|
+
function appendTokens(line, tokens, useTokenTransformer, cacheHtmlStyles) {
|
|
1428
|
+
let column = 0;
|
|
1429
|
+
for (const token of tokens) {
|
|
1430
|
+
if (token.content === "") continue;
|
|
1431
|
+
line.children.push(createTokenSpan(token, tokenStyle(token, cacheHtmlStyles), token.content, column, useTokenTransformer));
|
|
1432
|
+
column += token.content.length;
|
|
1433
|
+
}
|
|
1434
|
+
return column;
|
|
1435
|
+
}
|
|
1436
|
+
function appendDecoratedTokens(line, tokens, spans, lineIndex, useTokenTransformer, cacheHtmlStyles) {
|
|
1437
|
+
if (spans.length > 1) spans.sort((a, b) => {
|
|
1438
|
+
if (a.start.line !== b.start.line) return a.start.line - b.start.line;
|
|
1439
|
+
if (a.start.character !== b.start.character) return a.start.character - b.start.character;
|
|
1440
|
+
if (a.end.line !== b.end.line) return b.end.line - a.end.line;
|
|
1441
|
+
return b.end.character - a.end.character;
|
|
1442
|
+
});
|
|
1443
|
+
const lineLength = tokens.reduce((length, token) => length + token.content.length, 0);
|
|
1444
|
+
const boundaries = [];
|
|
1445
|
+
for (let order = 0; order < spans.length; order++) {
|
|
1446
|
+
const decoration = spans[order];
|
|
1447
|
+
const range = {
|
|
1448
|
+
decoration,
|
|
1449
|
+
order,
|
|
1450
|
+
from: decoration.start.line === lineIndex ? decoration.start.character : 0,
|
|
1451
|
+
to: decoration.end.line === lineIndex ? decoration.end.character : lineLength
|
|
1452
|
+
};
|
|
1453
|
+
boundaries.push({
|
|
1454
|
+
position: Math.max(0, Math.min(range.from, lineLength)),
|
|
1455
|
+
range,
|
|
1456
|
+
start: true
|
|
1457
|
+
}, {
|
|
1458
|
+
position: Math.max(0, Math.min(range.to, lineLength)),
|
|
1459
|
+
range,
|
|
1460
|
+
start: false
|
|
1461
|
+
});
|
|
1462
|
+
}
|
|
1463
|
+
boundaries.sort((a, b) => a.position - b.position);
|
|
1464
|
+
const active = [];
|
|
1465
|
+
const decorationStack = [];
|
|
1466
|
+
let boundaryIndex = 0;
|
|
1467
|
+
const updateActive = (range, add) => {
|
|
1468
|
+
let low = 0;
|
|
1469
|
+
let high = active.length;
|
|
1470
|
+
while (low < high) {
|
|
1471
|
+
const middle = low + high >>> 1;
|
|
1472
|
+
if (active[middle].order < range.order) low = middle + 1;
|
|
1473
|
+
else high = middle;
|
|
1474
|
+
}
|
|
1475
|
+
if (add) active.splice(low, 0, range);
|
|
1476
|
+
else if (active[low] === range) active.splice(low, 1);
|
|
1477
|
+
};
|
|
1478
|
+
const getParent = () => {
|
|
1479
|
+
let parent = line;
|
|
1480
|
+
let depth = 0;
|
|
1481
|
+
for (const range of active) {
|
|
1482
|
+
if (decorationStack[depth]?.range !== range) {
|
|
1483
|
+
decorationStack.length = depth;
|
|
1484
|
+
const wrapper = {
|
|
1485
|
+
type: "element",
|
|
1486
|
+
tagName: "span",
|
|
1487
|
+
properties: { ...range.decoration.properties },
|
|
1488
|
+
children: []
|
|
1489
|
+
};
|
|
1490
|
+
parent.children.push(wrapper);
|
|
1491
|
+
decorationStack.push({
|
|
1492
|
+
range,
|
|
1493
|
+
node: wrapper
|
|
1494
|
+
});
|
|
1495
|
+
}
|
|
1496
|
+
parent = decorationStack[depth++].node;
|
|
1497
|
+
}
|
|
1498
|
+
decorationStack.length = depth;
|
|
1499
|
+
return parent;
|
|
1500
|
+
};
|
|
1501
|
+
let parent = line;
|
|
1502
|
+
const advanceBoundary = (position) => {
|
|
1503
|
+
const first = boundaryIndex;
|
|
1504
|
+
while (boundaries[boundaryIndex]?.position === position) boundaryIndex++;
|
|
1505
|
+
let hasMarker = false;
|
|
1506
|
+
for (let i = first; i < boundaryIndex; i++) {
|
|
1507
|
+
const { range, start } = boundaries[i];
|
|
1508
|
+
if (!start && range.from !== range.to && position < lineLength) updateActive(range, false);
|
|
1509
|
+
}
|
|
1510
|
+
for (let i = first; i < boundaryIndex; i++) {
|
|
1511
|
+
const { range, start } = boundaries[i];
|
|
1512
|
+
if (start && range.from <= position && range.to >= position && (range.to > position || range.from === position || position === lineLength)) {
|
|
1513
|
+
updateActive(range, true);
|
|
1514
|
+
if (range.from === range.to) hasMarker = true;
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
if (hasMarker) {
|
|
1518
|
+
getParent();
|
|
1519
|
+
for (let i = first; i < boundaryIndex; i++) {
|
|
1520
|
+
const { range, start } = boundaries[i];
|
|
1521
|
+
if (start && range.from === range.to) updateActive(range, false);
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1524
|
+
if (position < lineLength) parent = getParent();
|
|
1525
|
+
};
|
|
1526
|
+
let column = 0;
|
|
1527
|
+
for (const token of tokens) {
|
|
1528
|
+
if (token.content === "") continue;
|
|
1529
|
+
const start = column;
|
|
1530
|
+
const end = start + token.content.length;
|
|
1531
|
+
const style = tokenStyle(token, cacheHtmlStyles);
|
|
1532
|
+
while (column < end) {
|
|
1533
|
+
if (boundaries[boundaryIndex]?.position === column) advanceBoundary(column);
|
|
1534
|
+
const to = Math.min(end, boundaries[boundaryIndex]?.position ?? end);
|
|
1535
|
+
parent.children.push(createTokenSpan(token, style, column === start && to === end ? token.content : token.content.slice(column - start, to - start), start, useTokenTransformer));
|
|
1536
|
+
column = to;
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
if (boundaries[boundaryIndex]?.position === column) advanceBoundary(column);
|
|
1540
|
+
return column;
|
|
1541
|
+
}
|
|
1542
|
+
function createTokenSpan(token, style, text, start, useTokenTransformer) {
|
|
1543
|
+
const properties = token.htmlAttrs != null ? { ...token.htmlAttrs } : {};
|
|
1544
|
+
if (style !== "") properties.style = style;
|
|
1545
|
+
if (useTokenTransformer) properties["data-char"] = start;
|
|
1546
|
+
return {
|
|
1547
|
+
type: "element",
|
|
1548
|
+
tagName: "span",
|
|
1549
|
+
properties,
|
|
1550
|
+
children: [{
|
|
1551
|
+
type: "text",
|
|
1552
|
+
value: text
|
|
1553
|
+
}]
|
|
1554
|
+
};
|
|
1555
|
+
}
|
|
1556
|
+
function getDecorationPosition(position, lineOffsets) {
|
|
1557
|
+
if (typeof position !== "number") return position;
|
|
1558
|
+
let low = 0;
|
|
1559
|
+
let high = lineOffsets.length;
|
|
1560
|
+
while (low + 1 < high) {
|
|
1561
|
+
const middle = low + high >>> 1;
|
|
1562
|
+
if (lineOffsets[middle] <= position) low = middle;
|
|
1563
|
+
else high = middle;
|
|
1564
|
+
}
|
|
1565
|
+
return {
|
|
1566
|
+
line: low,
|
|
1567
|
+
character: position - (lineOffsets[low] ?? 0)
|
|
1568
|
+
};
|
|
1569
|
+
}
|
|
1570
|
+
//#endregion
|
|
1330
1571
|
//#region src/utils/renderDiffWithHighlighter.ts
|
|
1331
1572
|
const DEFAULT_PLAIN_TEXT_OPTIONS$1 = { forcePlainText: false };
|
|
1332
|
-
function renderDiffWithHighlighter(diff, highlighter, options, { forcePlainText, startingLine, totalLines, expandedHunks, collapsedContextThreshold = 1 } = DEFAULT_PLAIN_TEXT_OPTIONS$1) {
|
|
1573
|
+
function renderDiffWithHighlighter(diff, highlighter, options, { forcePlainText, lazyLineAST = false, startingLine, totalLines, expandedHunks, collapsedContextThreshold = 1 } = DEFAULT_PLAIN_TEXT_OPTIONS$1) {
|
|
1333
1574
|
if (forcePlainText) {
|
|
1334
1575
|
startingLine ??= 0;
|
|
1335
1576
|
totalLines ??= Infinity;
|
|
@@ -1338,6 +1579,7 @@ function renderDiffWithHighlighter(diff, highlighter, options, { forcePlainText,
|
|
|
1338
1579
|
totalLines = Infinity;
|
|
1339
1580
|
}
|
|
1340
1581
|
const isWindowedHighlight = startingLine > 0 || totalLines < Infinity;
|
|
1582
|
+
lazyLineAST &&= !forcePlainText && !options.useTokenTransformer;
|
|
1341
1583
|
const baseThemeType = typeof options.theme === "string" ? highlighter.getTheme(options.theme).type : void 0;
|
|
1342
1584
|
const themeStyles = getHighlighterThemeStyles({
|
|
1343
1585
|
theme: options.theme,
|
|
@@ -1432,17 +1674,27 @@ function renderDiffWithHighlighter(diff, highlighter, options, { forcePlainText,
|
|
|
1432
1674
|
additionDecorations: bucket.additionDecorations,
|
|
1433
1675
|
highlighter,
|
|
1434
1676
|
options,
|
|
1435
|
-
languageOverride: forcePlainText ? "text" : diff.lang
|
|
1677
|
+
languageOverride: forcePlainText ? "text" : diff.lang,
|
|
1678
|
+
lazyLineAST
|
|
1436
1679
|
});
|
|
1437
1680
|
if (shouldGroupAll) {
|
|
1438
1681
|
code.deletionLines = deletionLines;
|
|
1439
1682
|
code.additionLines = additionLines;
|
|
1440
1683
|
continue;
|
|
1441
1684
|
}
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1685
|
+
for (const [target, lines, segments] of [[
|
|
1686
|
+
code.deletionLines,
|
|
1687
|
+
deletionLines,
|
|
1688
|
+
bucket.deletionSegments
|
|
1689
|
+
], [
|
|
1690
|
+
code.additionLines,
|
|
1691
|
+
additionLines,
|
|
1692
|
+
bucket.additionSegments
|
|
1693
|
+
]]) if (segments.length > 0) for (const segment of segments) for (let index = 0; index < segment.count; index++) target[segment.targetIndex + index] = lines[segment.originalOffset + index];
|
|
1694
|
+
else if (lazyLineAST) {
|
|
1695
|
+
const offset = target.length;
|
|
1696
|
+
for (let index = 0; index < lines.length; index++) setDeferredArrayItem(target, offset + index, () => lines[index]);
|
|
1697
|
+
} else appendItems(target, lines);
|
|
1446
1698
|
}
|
|
1447
1699
|
return {
|
|
1448
1700
|
code,
|
|
@@ -1574,46 +1826,20 @@ function createBucket() {
|
|
|
1574
1826
|
additionSegments: []
|
|
1575
1827
|
};
|
|
1576
1828
|
}
|
|
1577
|
-
function renderTwoFiles({ deletionFile, additionFile, deletionInfo, additionInfo, highlighter, deletionDecorations, additionDecorations, languageOverride, options: { theme
|
|
1578
|
-
const
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
defaultColor: false,
|
|
1589
|
-
cssVariablePrefix: formatCSSVariablePrefix("token"),
|
|
1590
|
-
tokenizeTimeLimit: 0
|
|
1591
|
-
} : {
|
|
1592
|
-
...options,
|
|
1593
|
-
lang: "text",
|
|
1594
|
-
themes: themeOrThemes,
|
|
1595
|
-
transformers,
|
|
1596
|
-
decorations: void 0,
|
|
1597
|
-
defaultColor: false,
|
|
1598
|
-
cssVariablePrefix: formatCSSVariablePrefix("token"),
|
|
1599
|
-
tokenizeTimeLimit: 0
|
|
1600
|
-
};
|
|
1601
|
-
})();
|
|
1829
|
+
function renderTwoFiles({ lazyLineAST, deletionFile, additionFile, deletionInfo, additionInfo, highlighter, deletionDecorations, additionDecorations, languageOverride, options: { theme, useTokenTransformer, tokenizeMaxLineLength } }) {
|
|
1830
|
+
const renderFile = (file, lineInfo, decorations) => {
|
|
1831
|
+
if (file.contents === "") return [];
|
|
1832
|
+
return renderTokenLines(highlighter.codeToTokens(cleanLastNewline(file.contents), getTokenOptions(languageOverride ?? getFiletypeFromFileName(file.name), theme, tokenizeMaxLineLength)).tokens, {
|
|
1833
|
+
state: { lineInfo },
|
|
1834
|
+
useTokenTransformer,
|
|
1835
|
+
decorations,
|
|
1836
|
+
lazyLineAST,
|
|
1837
|
+
cacheHtmlStyles: highlighter.name === "highlights"
|
|
1838
|
+
});
|
|
1839
|
+
};
|
|
1602
1840
|
return {
|
|
1603
|
-
deletionLines: (
|
|
1604
|
-
|
|
1605
|
-
hastConfig.lang = deletionLang;
|
|
1606
|
-
state.lineInfo = deletionInfo;
|
|
1607
|
-
hastConfig.decorations = deletionDecorations;
|
|
1608
|
-
return getLineNodes(highlighter.codeToHast(cleanLastNewline(deletionFile.contents), hastConfig));
|
|
1609
|
-
})(),
|
|
1610
|
-
additionLines: (() => {
|
|
1611
|
-
if (additionFile.contents === "") return [];
|
|
1612
|
-
hastConfig.lang = additionLang;
|
|
1613
|
-
hastConfig.decorations = additionDecorations;
|
|
1614
|
-
state.lineInfo = additionInfo;
|
|
1615
|
-
return getLineNodes(highlighter.codeToHast(cleanLastNewline(additionFile.contents), hastConfig));
|
|
1616
|
-
})()
|
|
1841
|
+
deletionLines: renderFile(deletionFile, deletionInfo, deletionDecorations),
|
|
1842
|
+
additionLines: renderFile(additionFile, additionInfo, additionDecorations)
|
|
1617
1843
|
};
|
|
1618
1844
|
}
|
|
1619
1845
|
//#endregion
|
|
@@ -1679,7 +1905,7 @@ function linesFromFileContents(contents) {
|
|
|
1679
1905
|
//#endregion
|
|
1680
1906
|
//#region src/utils/renderFileWithHighlighter.ts
|
|
1681
1907
|
const DEFAULT_PLAIN_TEXT_OPTIONS = { forcePlainText: false };
|
|
1682
|
-
function renderFileWithHighlighter(file, highlighter, { theme, tokenizeMaxLineLength, useTokenTransformer }, { forcePlainText, startingLine, totalLines, lines } = DEFAULT_PLAIN_TEXT_OPTIONS) {
|
|
1908
|
+
function renderFileWithHighlighter(file, highlighter, { theme, tokenizeMaxLineLength, useTokenTransformer }, { forcePlainText, lazyLineAST = false, startingLine, totalLines, lines } = DEFAULT_PLAIN_TEXT_OPTIONS) {
|
|
1683
1909
|
if (forcePlainText) {
|
|
1684
1910
|
startingLine ??= 0;
|
|
1685
1911
|
totalLines ??= Infinity;
|
|
@@ -1688,7 +1914,7 @@ function renderFileWithHighlighter(file, highlighter, { theme, tokenizeMaxLineLe
|
|
|
1688
1914
|
totalLines = Infinity;
|
|
1689
1915
|
}
|
|
1690
1916
|
const isWindowedHighlight = startingLine > 0 || totalLines < Infinity;
|
|
1691
|
-
const {
|
|
1917
|
+
const state = { lineInfo: [] };
|
|
1692
1918
|
const lang = forcePlainText ? "text" : file.lang ?? getFiletypeFromFileName(file.name);
|
|
1693
1919
|
const baseThemeType = typeof theme === "string" ? highlighter.getTheme(theme).type : void 0;
|
|
1694
1920
|
const themeStyles = getHighlighterThemeStyles({
|
|
@@ -1700,27 +1926,12 @@ function renderFileWithHighlighter(file, highlighter, { theme, tokenizeMaxLineLe
|
|
|
1700
1926
|
lineIndex: shikiLineNumber - 1 + startingLine,
|
|
1701
1927
|
lineNumber: shikiLineNumber + startingLine
|
|
1702
1928
|
});
|
|
1703
|
-
const
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
cssVariablePrefix: formatCSSVariablePrefix("token"),
|
|
1710
|
-
tokenizeMaxLineLength,
|
|
1711
|
-
tokenizeTimeLimit: 0
|
|
1712
|
-
};
|
|
1713
|
-
return {
|
|
1714
|
-
lang,
|
|
1715
|
-
themes: theme,
|
|
1716
|
-
transformers,
|
|
1717
|
-
defaultColor: false,
|
|
1718
|
-
cssVariablePrefix: formatCSSVariablePrefix("token"),
|
|
1719
|
-
tokenizeMaxLineLength,
|
|
1720
|
-
tokenizeTimeLimit: 0
|
|
1721
|
-
};
|
|
1722
|
-
})();
|
|
1723
|
-
const highlightedLines = getLineNodes(highlighter.codeToHast(normalizeHighlightLineEndings(isWindowedHighlight ? extractWindowedFileContent(lines ?? linesFromFileContents(file.contents), startingLine, totalLines) : file.contents), hastConfig));
|
|
1929
|
+
const highlightedLines = renderTokenLines(highlighter.codeToTokens(normalizeHighlightLineEndings(isWindowedHighlight ? extractWindowedFileContent(lines ?? linesFromFileContents(file.contents), startingLine, totalLines) : file.contents), getTokenOptions(lang, theme, tokenizeMaxLineLength)).tokens, {
|
|
1930
|
+
state,
|
|
1931
|
+
useTokenTransformer,
|
|
1932
|
+
lazyLineAST: lazyLineAST && !forcePlainText && !useTokenTransformer,
|
|
1933
|
+
cacheHtmlStyles: highlighter.name === "highlights"
|
|
1934
|
+
});
|
|
1724
1935
|
const code = isWindowedHighlight ? new Array(startingLine) : highlightedLines;
|
|
1725
1936
|
if (isWindowedHighlight) appendItems(code, highlightedLines);
|
|
1726
1937
|
return {
|
|
@@ -1738,8 +1949,102 @@ function extractWindowedFileContent(lines, startingLine, totalLines) {
|
|
|
1738
1949
|
return lines.slice(startingLine, endLine).join("");
|
|
1739
1950
|
}
|
|
1740
1951
|
//#endregion
|
|
1952
|
+
//#region src/worker/hastLinesTransport.ts
|
|
1953
|
+
const HOLE = 0;
|
|
1954
|
+
const TEXT = 1;
|
|
1955
|
+
const ELEMENT = 2;
|
|
1956
|
+
const OTHER = 3;
|
|
1957
|
+
const UNDEFINED = 0;
|
|
1958
|
+
const NULL = 1;
|
|
1959
|
+
const TRUE = 2;
|
|
1960
|
+
const FALSE = 3;
|
|
1961
|
+
const STRING = 4;
|
|
1962
|
+
const INTEGER = 5;
|
|
1963
|
+
const EXTRA = 6;
|
|
1964
|
+
function encodeHastLines(lines) {
|
|
1965
|
+
let ops = new Int32Array(Math.max(32, Math.min(65536, lines.length * 32)));
|
|
1966
|
+
let length = 0;
|
|
1967
|
+
const offsets = new Uint32Array(lines.length * 2);
|
|
1968
|
+
const strings = [];
|
|
1969
|
+
const stringIds = /* @__PURE__ */ new Map();
|
|
1970
|
+
const extras = [];
|
|
1971
|
+
let text = "";
|
|
1972
|
+
function push(value) {
|
|
1973
|
+
if (length === ops.length) {
|
|
1974
|
+
const next = new Int32Array(ops.length * 2);
|
|
1975
|
+
next.set(ops);
|
|
1976
|
+
ops = next;
|
|
1977
|
+
}
|
|
1978
|
+
ops[length++] = value;
|
|
1979
|
+
}
|
|
1980
|
+
function intern(value) {
|
|
1981
|
+
let id = stringIds.get(value);
|
|
1982
|
+
if (id == null) {
|
|
1983
|
+
id = strings.length;
|
|
1984
|
+
strings.push(value);
|
|
1985
|
+
stringIds.set(value, id);
|
|
1986
|
+
}
|
|
1987
|
+
return id;
|
|
1988
|
+
}
|
|
1989
|
+
function encodeNode(node) {
|
|
1990
|
+
let keys = 0;
|
|
1991
|
+
for (const _ in node) keys++;
|
|
1992
|
+
if (node?.type === "text" && keys === 2) {
|
|
1993
|
+
push(TEXT);
|
|
1994
|
+
push(node.value.length);
|
|
1995
|
+
text += node.value;
|
|
1996
|
+
} else if (node?.type === "element" && keys === 4 && node.properties != null && Object.getPrototypeOf(node.properties) === Object.prototype) {
|
|
1997
|
+
push(ELEMENT);
|
|
1998
|
+
push(intern(node.tagName));
|
|
1999
|
+
let count = 0;
|
|
2000
|
+
for (const _ in node.properties) count++;
|
|
2001
|
+
push(count);
|
|
2002
|
+
for (const key in node.properties) {
|
|
2003
|
+
const value = node.properties[key];
|
|
2004
|
+
push(intern(key));
|
|
2005
|
+
if (value === void 0) push(UNDEFINED);
|
|
2006
|
+
else if (value === null) push(NULL);
|
|
2007
|
+
else if (value === true) push(TRUE);
|
|
2008
|
+
else if (value === false) push(FALSE);
|
|
2009
|
+
else if (typeof value === "string") {
|
|
2010
|
+
push(STRING);
|
|
2011
|
+
push(intern(value));
|
|
2012
|
+
} else if (typeof value === "number" && (value | 0) === value && !Object.is(value, -0)) {
|
|
2013
|
+
push(INTEGER);
|
|
2014
|
+
push(value);
|
|
2015
|
+
} else {
|
|
2016
|
+
push(EXTRA);
|
|
2017
|
+
push(extras.length);
|
|
2018
|
+
extras.push(value);
|
|
2019
|
+
}
|
|
2020
|
+
}
|
|
2021
|
+
push(node.children.length);
|
|
2022
|
+
for (let index = 0; index < node.children.length; index++) if (index in node.children) encodeNode(node.children[index]);
|
|
2023
|
+
else push(HOLE);
|
|
2024
|
+
} else {
|
|
2025
|
+
push(OTHER);
|
|
2026
|
+
push(extras.length);
|
|
2027
|
+
extras.push(node);
|
|
2028
|
+
}
|
|
2029
|
+
}
|
|
2030
|
+
for (let index = 0; index < lines.length; index++) {
|
|
2031
|
+
offsets[index * 2] = length;
|
|
2032
|
+
offsets[index * 2 + 1] = text.length;
|
|
2033
|
+
if (index in lines) encodeNode(lines[index]);
|
|
2034
|
+
else push(HOLE);
|
|
2035
|
+
}
|
|
2036
|
+
return {
|
|
2037
|
+
ops: ops.subarray(0, length),
|
|
2038
|
+
offsets,
|
|
2039
|
+
strings,
|
|
2040
|
+
text,
|
|
2041
|
+
extras
|
|
2042
|
+
};
|
|
2043
|
+
}
|
|
2044
|
+
//#endregion
|
|
1741
2045
|
//#region src/worker/worker.ts
|
|
1742
2046
|
let highlighter;
|
|
2047
|
+
let compactResults = false;
|
|
1743
2048
|
let renderOptions = {
|
|
1744
2049
|
theme: DEFAULT_THEMES,
|
|
1745
2050
|
useTokenTransformer: false,
|
|
@@ -1749,7 +2054,7 @@ let renderOptions = {
|
|
|
1749
2054
|
};
|
|
1750
2055
|
const EMPTY_REGEXP = /(?:)/;
|
|
1751
2056
|
self.addEventListener("error", (event) => {
|
|
1752
|
-
console.error("[
|
|
2057
|
+
console.error("[Diffs Worker] Unhandled error:", event.error);
|
|
1753
2058
|
});
|
|
1754
2059
|
self.addEventListener("message", (event) => {
|
|
1755
2060
|
handleMessage(event.data);
|
|
@@ -1778,7 +2083,7 @@ async function handleMessage(request) {
|
|
|
1778
2083
|
EMPTY_REGEXP.exec("");
|
|
1779
2084
|
}
|
|
1780
2085
|
}
|
|
1781
|
-
async function handleInitialize({ id, renderOptions: options, preferredHighlighter, resolvedThemes, resolvedLanguages, customExtensionsVersion, customExtensionMap }) {
|
|
2086
|
+
async function handleInitialize({ id, renderOptions: options, preferredHighlighter, resolvedThemes, resolvedLanguages, customExtensionsVersion, customExtensionMap, resultFormat }) {
|
|
1782
2087
|
let highlighter = getHighlighter(preferredHighlighter);
|
|
1783
2088
|
if ("then" in highlighter) highlighter = await highlighter;
|
|
1784
2089
|
syncCustomExtensionsFromRequest({
|
|
@@ -1788,6 +2093,7 @@ async function handleInitialize({ id, renderOptions: options, preferredHighlight
|
|
|
1788
2093
|
attachResolvedThemes(resolvedThemes, highlighter);
|
|
1789
2094
|
if (resolvedLanguages != null) attachResolvedLanguages(resolvedLanguages, highlighter);
|
|
1790
2095
|
renderOptions = options;
|
|
2096
|
+
compactResults = resultFormat === "hast-ops-v1";
|
|
1791
2097
|
postMessage({
|
|
1792
2098
|
type: "success",
|
|
1793
2099
|
id,
|
|
@@ -1820,7 +2126,23 @@ async function handleRenderFile({ id, file, resolvedLanguages, customExtensionsV
|
|
|
1820
2126
|
useTokenTransformer: renderOptions.useTokenTransformer,
|
|
1821
2127
|
tokenizeMaxLineLength: renderOptions.tokenizeMaxLineLength
|
|
1822
2128
|
};
|
|
1823
|
-
|
|
2129
|
+
const result = renderFileWithHighlighter(file, highlighter, fileOptions);
|
|
2130
|
+
const code = compactResults ? encodeHastLines(result.code) : void 0;
|
|
2131
|
+
const response = {
|
|
2132
|
+
type: "success",
|
|
2133
|
+
requestType: "file",
|
|
2134
|
+
id,
|
|
2135
|
+
result,
|
|
2136
|
+
options: fileOptions,
|
|
2137
|
+
sentAt: Date.now()
|
|
2138
|
+
};
|
|
2139
|
+
postMessage(code == null ? response : {
|
|
2140
|
+
...response,
|
|
2141
|
+
result: {
|
|
2142
|
+
...result,
|
|
2143
|
+
code
|
|
2144
|
+
}
|
|
2145
|
+
}, code == null ? [] : [code.ops.buffer, code.offsets.buffer]);
|
|
1824
2146
|
}
|
|
1825
2147
|
async function handleRenderDiff({ id, diff, resolvedLanguages, customExtensionsVersion, customExtensionMap }) {
|
|
1826
2148
|
let highlighter = getHighlighter();
|
|
@@ -1830,14 +2152,34 @@ async function handleRenderDiff({ id, diff, resolvedLanguages, customExtensionsV
|
|
|
1830
2152
|
customExtensionMap
|
|
1831
2153
|
});
|
|
1832
2154
|
if (resolvedLanguages != null) attachResolvedLanguages(resolvedLanguages, highlighter);
|
|
1833
|
-
|
|
2155
|
+
const result = renderDiffWithHighlighter(diff, highlighter, renderOptions);
|
|
2156
|
+
const code = compactResults ? {
|
|
2157
|
+
deletionLines: encodeHastLines(result.code.deletionLines),
|
|
2158
|
+
additionLines: encodeHastLines(result.code.additionLines)
|
|
2159
|
+
} : void 0;
|
|
2160
|
+
const response = {
|
|
2161
|
+
type: "success",
|
|
2162
|
+
requestType: "diff",
|
|
2163
|
+
id,
|
|
2164
|
+
result,
|
|
2165
|
+
options: renderOptions,
|
|
2166
|
+
sentAt: Date.now()
|
|
2167
|
+
};
|
|
2168
|
+
postMessage(code == null ? response : {
|
|
2169
|
+
...response,
|
|
2170
|
+
result: {
|
|
2171
|
+
...result,
|
|
2172
|
+
code
|
|
2173
|
+
}
|
|
2174
|
+
}, code == null ? [] : [
|
|
2175
|
+
code.deletionLines.ops.buffer,
|
|
2176
|
+
code.deletionLines.offsets.buffer,
|
|
2177
|
+
code.additionLines.ops.buffer,
|
|
2178
|
+
code.additionLines.offsets.buffer
|
|
2179
|
+
]);
|
|
1834
2180
|
}
|
|
1835
|
-
function getHighlighter(preferredHighlighter
|
|
1836
|
-
highlighter ??=
|
|
1837
|
-
themes: [],
|
|
1838
|
-
langs: [],
|
|
1839
|
-
engine: preferredHighlighter === "shiki-wasm" ? createOnigurumaEngine(import("shiki/wasm")) : createJavaScriptRegexEngine()
|
|
1840
|
-
});
|
|
2181
|
+
function getHighlighter(preferredHighlighter) {
|
|
2182
|
+
highlighter ??= createHighlighter(preferredHighlighter);
|
|
1841
2183
|
return highlighter;
|
|
1842
2184
|
}
|
|
1843
2185
|
function syncCustomExtensionsFromRequest({ customExtensionsVersion, customExtensionMap }) {
|
|
@@ -1845,26 +2187,6 @@ function syncCustomExtensionsFromRequest({ customExtensionsVersion, customExtens
|
|
|
1845
2187
|
if (customExtensionsVersion == null || customExtensionMap == null) throw new Error("Worker request must include both customExtensionsVersion and customExtensionMap");
|
|
1846
2188
|
replaceCustomExtensions(customExtensionsVersion, customExtensionMap);
|
|
1847
2189
|
}
|
|
1848
|
-
function sendFileSuccess(id, result, options) {
|
|
1849
|
-
postMessage({
|
|
1850
|
-
type: "success",
|
|
1851
|
-
requestType: "file",
|
|
1852
|
-
id,
|
|
1853
|
-
result,
|
|
1854
|
-
options,
|
|
1855
|
-
sentAt: Date.now()
|
|
1856
|
-
});
|
|
1857
|
-
}
|
|
1858
|
-
function sendDiffSuccess(id, result, options) {
|
|
1859
|
-
postMessage({
|
|
1860
|
-
type: "success",
|
|
1861
|
-
requestType: "diff",
|
|
1862
|
-
id,
|
|
1863
|
-
result,
|
|
1864
|
-
options,
|
|
1865
|
-
sentAt: Date.now()
|
|
1866
|
-
});
|
|
1867
|
-
}
|
|
1868
2190
|
function sendError(id, error) {
|
|
1869
2191
|
const response = {
|
|
1870
2192
|
type: "error",
|
|
@@ -1875,5 +2197,6 @@ function sendError(id, error) {
|
|
|
1875
2197
|
postMessage(response);
|
|
1876
2198
|
}
|
|
1877
2199
|
//#endregion
|
|
2200
|
+
export { acquireHighlighterType as a, RegisteredCustomLanguages as c, HighlighterDisposedError as i, ResolvedLanguages as l, formatCSSVariablePrefix as n, releaseHighlighterType as o, appendItems as r, attachResolvedLanguages as s, renderTokenLines as t, ResolvingLanguages as u };
|
|
1878
2201
|
|
|
1879
2202
|
//# sourceMappingURL=worker.js.map
|