@pierre/diffs 1.0.7 → 1.1.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/dist/components/File.d.ts +4 -2
- package/dist/components/File.d.ts.map +1 -1
- package/dist/components/File.js +80 -34
- package/dist/components/File.js.map +1 -1
- package/dist/components/FileDiff.d.ts +50 -28
- package/dist/components/FileDiff.d.ts.map +1 -1
- package/dist/components/FileDiff.js +220 -79
- package/dist/components/FileDiff.js.map +1 -1
- package/dist/components/FileStream.d.ts +1 -0
- package/dist/components/FileStream.d.ts.map +1 -1
- package/dist/components/FileStream.js +8 -4
- package/dist/components/FileStream.js.map +1 -1
- package/dist/constants.d.ts +8 -2
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +10 -1
- package/dist/constants.js.map +1 -1
- package/dist/index.d.ts +19 -10
- package/dist/index.js +14 -5
- package/dist/managers/LineSelectionManager.d.ts.map +1 -1
- package/dist/managers/LineSelectionManager.js +8 -9
- package/dist/managers/LineSelectionManager.js.map +1 -1
- package/dist/react/MultiFileDiff.js +2 -2
- package/dist/react/MultiFileDiff.js.map +1 -1
- package/dist/react/index.d.ts +2 -2
- package/dist/react/utils/renderDiffChildren.d.ts +4 -4
- package/dist/react/utils/renderDiffChildren.d.ts.map +1 -1
- package/dist/react/utils/renderDiffChildren.js +3 -3
- package/dist/react/utils/renderDiffChildren.js.map +1 -1
- package/dist/react/utils/useFileDiffInstance.js.map +1 -1
- package/dist/renderers/DiffHunksRenderer.d.ts +7 -6
- package/dist/renderers/DiffHunksRenderer.d.ts.map +1 -1
- package/dist/renderers/DiffHunksRenderer.js +263 -337
- package/dist/renderers/DiffHunksRenderer.js.map +1 -1
- package/dist/renderers/FileRenderer.d.ts +1 -0
- package/dist/renderers/FileRenderer.d.ts.map +1 -1
- package/dist/renderers/FileRenderer.js +11 -4
- package/dist/renderers/FileRenderer.js.map +1 -1
- package/dist/ssr/index.d.ts +2 -2
- package/dist/style.js +1 -1
- package/dist/style.js.map +1 -1
- package/dist/types.d.ts +246 -42
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/areDiffLineAnnotationsEqual.d.ts +7 -0
- package/dist/utils/areDiffLineAnnotationsEqual.d.ts.map +1 -0
- package/dist/utils/areDiffLineAnnotationsEqual.js +8 -0
- package/dist/utils/areDiffLineAnnotationsEqual.js.map +1 -0
- package/dist/utils/areHunkDataEqual.d.ts +7 -0
- package/dist/utils/areHunkDataEqual.d.ts.map +1 -0
- package/dist/utils/areHunkDataEqual.js +8 -0
- package/dist/utils/areHunkDataEqual.js.map +1 -0
- package/dist/utils/areLineAnnotationsEqual.d.ts +7 -0
- package/dist/utils/areLineAnnotationsEqual.d.ts.map +1 -0
- package/dist/utils/areLineAnnotationsEqual.js +8 -0
- package/dist/utils/areLineAnnotationsEqual.js.map +1 -0
- package/dist/utils/arePrePropertiesEqual.d.ts +7 -0
- package/dist/utils/arePrePropertiesEqual.d.ts.map +1 -0
- package/dist/utils/arePrePropertiesEqual.js +9 -0
- package/dist/utils/arePrePropertiesEqual.js.map +1 -0
- package/dist/utils/areRenderRangesEqual.d.ts +7 -0
- package/dist/utils/areRenderRangesEqual.d.ts.map +1 -0
- package/dist/utils/areRenderRangesEqual.js +9 -0
- package/dist/utils/areRenderRangesEqual.js.map +1 -0
- package/dist/utils/areVirtualWindowSpecsEqual.d.ts +7 -0
- package/dist/utils/areVirtualWindowSpecsEqual.d.ts.map +1 -0
- package/dist/utils/areVirtualWindowSpecsEqual.js +9 -0
- package/dist/utils/areVirtualWindowSpecsEqual.js.map +1 -0
- package/dist/utils/areWorkerStatsEqual.d.ts +8 -0
- package/dist/utils/areWorkerStatsEqual.d.ts.map +1 -0
- package/dist/utils/areWorkerStatsEqual.js +9 -0
- package/dist/utils/areWorkerStatsEqual.js.map +1 -0
- package/dist/utils/createTransformerWithState.js +1 -1
- package/dist/utils/createTransformerWithState.js.map +1 -1
- package/dist/utils/createWindowFromScrollPosition.d.ts +22 -0
- package/dist/utils/createWindowFromScrollPosition.d.ts.map +1 -0
- package/dist/utils/createWindowFromScrollPosition.js +26 -0
- package/dist/utils/createWindowFromScrollPosition.js.map +1 -0
- package/dist/utils/diffAcceptRejectHunk.js +36 -21
- package/dist/utils/diffAcceptRejectHunk.js.map +1 -1
- package/dist/utils/getOrCreateCodeNode.d.ts +14 -0
- package/dist/utils/getOrCreateCodeNode.d.ts.map +1 -0
- package/dist/utils/getOrCreateCodeNode.js +13 -0
- package/dist/utils/getOrCreateCodeNode.js.map +1 -0
- package/dist/utils/getTotalLineCountFromHunks.js +1 -1
- package/dist/utils/getTotalLineCountFromHunks.js.map +1 -1
- package/dist/utils/hast_utils.d.ts +2 -1
- package/dist/utils/hast_utils.d.ts.map +1 -1
- package/dist/utils/hast_utils.js +10 -1
- package/dist/utils/hast_utils.js.map +1 -1
- package/dist/utils/isDefaultRenderRange.d.ts +7 -0
- package/dist/utils/isDefaultRenderRange.d.ts.map +1 -0
- package/dist/utils/isDefaultRenderRange.js +8 -0
- package/dist/utils/isDefaultRenderRange.js.map +1 -0
- package/dist/utils/iterateOverDiff.d.ts +39 -0
- package/dist/utils/iterateOverDiff.d.ts.map +1 -0
- package/dist/utils/iterateOverDiff.js +356 -0
- package/dist/utils/iterateOverDiff.js.map +1 -0
- package/dist/utils/parseDiffFromFile.d.ts.map +1 -1
- package/dist/utils/parseDiffFromFile.js +8 -6
- package/dist/utils/parseDiffFromFile.js.map +1 -1
- package/dist/utils/parsePatchFiles.d.ts +15 -3
- package/dist/utils/parsePatchFiles.d.ts.map +1 -1
- package/dist/utils/parsePatchFiles.js +207 -158
- package/dist/utils/parsePatchFiles.js.map +1 -1
- package/dist/utils/processLine.js +4 -3
- package/dist/utils/processLine.js.map +1 -1
- package/dist/utils/renderDiffWithHighlighter.d.ts +7 -2
- package/dist/utils/renderDiffWithHighlighter.d.ts.map +1 -1
- package/dist/utils/renderDiffWithHighlighter.js +149 -227
- package/dist/utils/renderDiffWithHighlighter.js.map +1 -1
- package/dist/utils/setWrapperNodeProps.d.ts +3 -7
- package/dist/utils/setWrapperNodeProps.d.ts.map +1 -1
- package/dist/utils/setWrapperNodeProps.js +1 -1
- package/dist/utils/setWrapperNodeProps.js.map +1 -1
- package/dist/worker/WorkerPoolManager.d.ts +9 -2
- package/dist/worker/WorkerPoolManager.d.ts.map +1 -1
- package/dist/worker/WorkerPoolManager.js +124 -45
- package/dist/worker/WorkerPoolManager.js.map +1 -1
- package/dist/worker/types.d.ts +7 -0
- package/dist/worker/types.d.ts.map +1 -1
- package/dist/worker/worker-portable.js +634 -242
- package/dist/worker/worker-portable.js.map +1 -1
- package/dist/worker/worker.js +511 -231
- package/dist/worker/worker.js.map +1 -1
- package/package.json +20 -1
- package/dist/utils/createCodeNode.d.ts +0 -12
- package/dist/utils/createCodeNode.d.ts.map +0 -1
- package/dist/utils/createCodeNode.js +0 -12
- package/dist/utils/createCodeNode.js.map +0 -1
|
@@ -5,11 +5,21 @@ import { createTransformerWithState } from "./createTransformerWithState.js";
|
|
|
5
5
|
import { formatCSSVariablePrefix } from "./formatCSSVariablePrefix.js";
|
|
6
6
|
import { getHighlighterThemeStyles } from "./getHighlighterThemeStyles.js";
|
|
7
7
|
import { getLineNodes } from "./getLineNodes.js";
|
|
8
|
+
import { iterateOverDiff } from "./iterateOverDiff.js";
|
|
8
9
|
import { createDiffSpanDecoration, pushOrJoinSpan } from "./parseDiffDecorations.js";
|
|
9
10
|
import { diffChars, diffWordsWithSpace } from "diff";
|
|
10
11
|
|
|
11
12
|
//#region src/utils/renderDiffWithHighlighter.ts
|
|
12
|
-
|
|
13
|
+
const DEFAULT_PLAIN_TEXT_OPTIONS = { forcePlainText: false };
|
|
14
|
+
function renderDiffWithHighlighter(diff, highlighter, options, { forcePlainText, startingLine, totalLines, expandedHunks } = DEFAULT_PLAIN_TEXT_OPTIONS) {
|
|
15
|
+
if (forcePlainText) {
|
|
16
|
+
startingLine ??= 0;
|
|
17
|
+
totalLines ??= Infinity;
|
|
18
|
+
} else {
|
|
19
|
+
startingLine = 0;
|
|
20
|
+
totalLines = Infinity;
|
|
21
|
+
}
|
|
22
|
+
const isWindowedHighlight = startingLine > 0 || totalLines < Infinity;
|
|
13
23
|
const baseThemeType = (() => {
|
|
14
24
|
const theme = options.theme ?? DEFAULT_THEMES;
|
|
15
25
|
if (typeof theme === "string") return highlighter.getTheme(theme).type;
|
|
@@ -18,92 +28,120 @@ function renderDiffWithHighlighter(diff, highlighter, options, forcePlainText =
|
|
|
18
28
|
theme: options.theme,
|
|
19
29
|
highlighter
|
|
20
30
|
});
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
oldInfo,
|
|
35
|
-
oldDecorations,
|
|
36
|
-
newFile: {
|
|
37
|
-
name: diff.name,
|
|
38
|
-
contents: newContent
|
|
39
|
-
},
|
|
40
|
-
newInfo,
|
|
41
|
-
newDecorations,
|
|
42
|
-
highlighter,
|
|
43
|
-
options,
|
|
44
|
-
languageOverride: forcePlainText ? "text" : diff.lang
|
|
45
|
-
}),
|
|
46
|
-
themeStyles,
|
|
47
|
-
baseThemeType
|
|
48
|
-
};
|
|
31
|
+
const lineDiffType = forcePlainText && !isWindowedHighlight && (diff.unifiedLineCount > 1e3 || diff.splitLineCount > 1e3) ? "none" : options.lineDiffType;
|
|
32
|
+
const code = {
|
|
33
|
+
deletionLines: [],
|
|
34
|
+
additionLines: []
|
|
35
|
+
};
|
|
36
|
+
const shouldGroupAll = !forcePlainText && !diff.isPartial;
|
|
37
|
+
const expandedHunksForIteration = forcePlainText ? expandedHunks : void 0;
|
|
38
|
+
const buckets = /* @__PURE__ */ new Map();
|
|
39
|
+
function getBucketForHunk(hunkIndex) {
|
|
40
|
+
const index = shouldGroupAll ? 0 : hunkIndex;
|
|
41
|
+
const bucket = buckets.get(index) ?? createBucket();
|
|
42
|
+
buckets.set(index, bucket);
|
|
43
|
+
return bucket;
|
|
49
44
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
45
|
+
function appendContent(lineContent, lineIndex, segments, contentWrapper) {
|
|
46
|
+
if (isWindowedHighlight) {
|
|
47
|
+
let segment = segments.at(-1);
|
|
48
|
+
if (segment == null || segment.targetIndex + segment.count !== lineIndex) {
|
|
49
|
+
segment = {
|
|
50
|
+
targetIndex: lineIndex,
|
|
51
|
+
originalOffset: contentWrapper.length,
|
|
52
|
+
count: 0
|
|
53
|
+
};
|
|
54
|
+
segments.push(segment);
|
|
55
|
+
}
|
|
56
|
+
segment.count++;
|
|
57
|
+
}
|
|
58
|
+
contentWrapper.push(lineContent);
|
|
59
|
+
}
|
|
60
|
+
iterateOverDiff({
|
|
61
|
+
diff,
|
|
62
|
+
diffStyle: "both",
|
|
63
|
+
startingLine,
|
|
64
|
+
totalLines,
|
|
65
|
+
expandedHunks: isWindowedHighlight ? expandedHunksForIteration : true,
|
|
66
|
+
callback: ({ hunkIndex, additionLineIndex, deletionLineIndex, additionLineNumber, deletionLineNumber, unifiedAdditionLineIndex, unifiedDeletionLineIndex, splitLineIndex, type }) => {
|
|
67
|
+
const bucket = getBucketForHunk(hunkIndex);
|
|
68
|
+
if (type === "change" && lineDiffType !== "none" && additionLineIndex != null && deletionLineIndex != null) computeLineDiffDecorations({
|
|
69
|
+
additionLine: diff.additionLines[additionLineIndex],
|
|
70
|
+
deletionLine: diff.deletionLines[deletionLineIndex],
|
|
71
|
+
deletionLineIndex: bucket.deletionContent.length,
|
|
72
|
+
additionLineIndex: bucket.additionContent.length,
|
|
73
|
+
deletionDecorations: bucket.deletionDecorations,
|
|
74
|
+
additionDecorations: bucket.additionDecorations,
|
|
75
|
+
lineDiffType
|
|
76
|
+
});
|
|
77
|
+
if (deletionLineIndex != null && deletionLineNumber != null && unifiedDeletionLineIndex != null) {
|
|
78
|
+
appendContent(diff.deletionLines[deletionLineIndex], deletionLineIndex, bucket.deletionSegments, bucket.deletionContent);
|
|
79
|
+
bucket.deletionInfo.push({
|
|
80
|
+
type: type === "change" ? "change-deletion" : type,
|
|
81
|
+
lineNumber: deletionLineNumber,
|
|
82
|
+
altLineNumber: type === "change" ? void 0 : additionLineNumber ?? void 0,
|
|
83
|
+
lineIndex: `${unifiedDeletionLineIndex},${splitLineIndex}`
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
if (additionLineIndex != null && additionLineNumber != null && unifiedAdditionLineIndex != null) {
|
|
87
|
+
appendContent(diff.additionLines[additionLineIndex], additionLineIndex, bucket.additionSegments, bucket.additionContent);
|
|
88
|
+
bucket.additionInfo.push({
|
|
89
|
+
type: type === "change" ? "change-addition" : type,
|
|
90
|
+
lineNumber: additionLineNumber,
|
|
91
|
+
altLineNumber: type === "change" ? void 0 : deletionLineNumber ?? void 0,
|
|
92
|
+
lineIndex: `${unifiedAdditionLineIndex},${splitLineIndex}`
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
for (const bucket of buckets.values()) {
|
|
98
|
+
if (bucket.deletionContent.length === 0 && bucket.additionContent.length === 0) continue;
|
|
99
|
+
const deletionFile = {
|
|
61
100
|
name: diff.prevName ?? diff.name,
|
|
62
|
-
contents:
|
|
101
|
+
contents: bucket.deletionContent.value
|
|
63
102
|
};
|
|
64
|
-
const
|
|
103
|
+
const additionFile = {
|
|
65
104
|
name: diff.name,
|
|
66
|
-
contents:
|
|
105
|
+
contents: bucket.additionContent.value
|
|
67
106
|
};
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
107
|
+
const { deletionLines, additionLines } = renderTwoFiles({
|
|
108
|
+
deletionFile,
|
|
109
|
+
deletionInfo: bucket.deletionInfo,
|
|
110
|
+
deletionDecorations: bucket.deletionDecorations,
|
|
111
|
+
additionFile,
|
|
112
|
+
additionInfo: bucket.additionInfo,
|
|
113
|
+
additionDecorations: bucket.additionDecorations,
|
|
75
114
|
highlighter,
|
|
76
115
|
options,
|
|
77
116
|
languageOverride: forcePlainText ? "text" : diff.lang
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
|
|
117
|
+
});
|
|
118
|
+
if (shouldGroupAll) {
|
|
119
|
+
code.deletionLines = deletionLines;
|
|
120
|
+
code.additionLines = additionLines;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (bucket.deletionSegments.length > 0) for (const seg of bucket.deletionSegments) for (let i = 0; i < seg.count; i++) code.deletionLines[seg.targetIndex + i] = deletionLines[seg.originalOffset + i];
|
|
124
|
+
else code.deletionLines.push(...deletionLines);
|
|
125
|
+
if (bucket.additionSegments.length > 0) for (const seg of bucket.additionSegments) for (let i = 0; i < seg.count; i++) code.additionLines[seg.targetIndex + i] = additionLines[seg.originalOffset + i];
|
|
126
|
+
else code.additionLines.push(...additionLines);
|
|
81
127
|
}
|
|
82
128
|
return {
|
|
83
|
-
code
|
|
84
|
-
if (hunks.length <= 1) {
|
|
85
|
-
const hunk = hunks[0] ?? {
|
|
86
|
-
oldLines: [],
|
|
87
|
-
newLines: []
|
|
88
|
-
};
|
|
89
|
-
if (hunk.newLines.length === 0 || hunk.oldLines.length === 0) return hunk;
|
|
90
|
-
}
|
|
91
|
-
return { hunks };
|
|
92
|
-
})(),
|
|
129
|
+
code,
|
|
93
130
|
themeStyles,
|
|
94
131
|
baseThemeType
|
|
95
132
|
};
|
|
96
133
|
}
|
|
97
|
-
function computeLineDiffDecorations({
|
|
98
|
-
if (
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const lineDiff = lineDiffType === "char" ? diffChars(
|
|
134
|
+
function computeLineDiffDecorations({ deletionLine, additionLine, deletionLineIndex, additionLineIndex, deletionDecorations, additionDecorations, lineDiffType }) {
|
|
135
|
+
if (deletionLine == null || additionLine == null || lineDiffType === "none") return;
|
|
136
|
+
deletionLine = cleanLastNewline(deletionLine);
|
|
137
|
+
additionLine = cleanLastNewline(additionLine);
|
|
138
|
+
const lineDiff = lineDiffType === "char" ? diffChars(deletionLine, additionLine) : diffWordsWithSpace(deletionLine, additionLine);
|
|
102
139
|
const deletionSpans = [];
|
|
103
140
|
const additionSpans = [];
|
|
104
141
|
const enableJoin = lineDiffType === "word-alt";
|
|
142
|
+
const lastItem = lineDiff.at(-1);
|
|
105
143
|
for (const item of lineDiff) {
|
|
106
|
-
const isLastItem = item ===
|
|
144
|
+
const isLastItem = item === lastItem;
|
|
107
145
|
if (!item.added && !item.removed) {
|
|
108
146
|
pushOrJoinSpan({
|
|
109
147
|
item,
|
|
@@ -134,8 +172,8 @@ function computeLineDiffDecorations({ oldLine, newLine, oldLineIndex, newLineInd
|
|
|
134
172
|
}
|
|
135
173
|
let spanIndex = 0;
|
|
136
174
|
for (const span of deletionSpans) {
|
|
137
|
-
if (span[0] === 1)
|
|
138
|
-
line:
|
|
175
|
+
if (span[0] === 1) deletionDecorations.push(createDiffSpanDecoration({
|
|
176
|
+
line: deletionLineIndex,
|
|
139
177
|
spanStart: spanIndex,
|
|
140
178
|
spanLength: span[1].length
|
|
141
179
|
}));
|
|
@@ -143,159 +181,43 @@ function computeLineDiffDecorations({ oldLine, newLine, oldLineIndex, newLineInd
|
|
|
143
181
|
}
|
|
144
182
|
spanIndex = 0;
|
|
145
183
|
for (const span of additionSpans) {
|
|
146
|
-
if (span[0] === 1)
|
|
147
|
-
line:
|
|
184
|
+
if (span[0] === 1) additionDecorations.push(createDiffSpanDecoration({
|
|
185
|
+
line: additionLineIndex,
|
|
148
186
|
spanStart: spanIndex,
|
|
149
187
|
spanLength: span[1].length
|
|
150
188
|
}));
|
|
151
189
|
spanIndex += span[1].length;
|
|
152
190
|
}
|
|
153
191
|
}
|
|
154
|
-
function
|
|
155
|
-
const oldInfo = {};
|
|
156
|
-
const newInfo = {};
|
|
157
|
-
const oldDecorations = [];
|
|
158
|
-
const newDecorations = [];
|
|
159
|
-
let newLineIndex = 1;
|
|
160
|
-
let oldLineIndex = 1;
|
|
161
|
-
let newLineNumber = 1;
|
|
162
|
-
let oldLineNumber = 1;
|
|
163
|
-
let oldContent = "";
|
|
164
|
-
let newContent = "";
|
|
165
|
-
for (const hunk of hunks) {
|
|
166
|
-
while (oldLines != null && newLines != null && newLineIndex < hunk.additionStart && oldLineIndex < hunk.deletionStart) {
|
|
167
|
-
oldInfo[oldLineIndex] = {
|
|
168
|
-
type: "context-expanded",
|
|
169
|
-
lineNumber: oldLineNumber,
|
|
170
|
-
altLineNumber: newLineNumber,
|
|
171
|
-
lineIndex: `${unifiedLineIndex},${splitLineIndex}`
|
|
172
|
-
};
|
|
173
|
-
newInfo[newLineIndex] = {
|
|
174
|
-
type: "context-expanded",
|
|
175
|
-
lineNumber: newLineNumber,
|
|
176
|
-
altLineNumber: oldLineNumber,
|
|
177
|
-
lineIndex: `${unifiedLineIndex},${splitLineIndex}`
|
|
178
|
-
};
|
|
179
|
-
oldContent += oldLines[oldLineIndex - 1];
|
|
180
|
-
newContent += newLines[newLineIndex - 1];
|
|
181
|
-
oldLineIndex++;
|
|
182
|
-
newLineIndex++;
|
|
183
|
-
oldLineNumber++;
|
|
184
|
-
newLineNumber++;
|
|
185
|
-
splitLineIndex++;
|
|
186
|
-
unifiedLineIndex++;
|
|
187
|
-
}
|
|
188
|
-
oldLineNumber = hunk.deletionStart;
|
|
189
|
-
newLineNumber = hunk.additionStart;
|
|
190
|
-
for (const hunkContent of hunk.hunkContent) if (hunkContent.type === "context") for (const line of hunkContent.lines) {
|
|
191
|
-
oldInfo[oldLineIndex] = {
|
|
192
|
-
type: "context",
|
|
193
|
-
lineNumber: oldLineNumber,
|
|
194
|
-
altLineNumber: newLineNumber,
|
|
195
|
-
lineIndex: `${unifiedLineIndex},${splitLineIndex}`
|
|
196
|
-
};
|
|
197
|
-
newInfo[newLineIndex] = {
|
|
198
|
-
type: "context",
|
|
199
|
-
lineNumber: newLineNumber,
|
|
200
|
-
altLineNumber: oldLineNumber,
|
|
201
|
-
lineIndex: `${unifiedLineIndex},${splitLineIndex}`
|
|
202
|
-
};
|
|
203
|
-
oldContent += line;
|
|
204
|
-
newContent += line;
|
|
205
|
-
oldLineIndex++;
|
|
206
|
-
newLineIndex++;
|
|
207
|
-
newLineNumber++;
|
|
208
|
-
oldLineNumber++;
|
|
209
|
-
splitLineIndex++;
|
|
210
|
-
unifiedLineIndex++;
|
|
211
|
-
}
|
|
212
|
-
else {
|
|
213
|
-
const len = Math.max(hunkContent.additions.length, hunkContent.deletions.length);
|
|
214
|
-
let i = 0;
|
|
215
|
-
let _unifiedLineIndex = unifiedLineIndex;
|
|
216
|
-
while (i < len) {
|
|
217
|
-
const oldLine = hunkContent.deletions[i];
|
|
218
|
-
const newLine = hunkContent.additions[i];
|
|
219
|
-
computeLineDiffDecorations({
|
|
220
|
-
newLine,
|
|
221
|
-
oldLine,
|
|
222
|
-
oldLineIndex,
|
|
223
|
-
newLineIndex,
|
|
224
|
-
oldDecorations,
|
|
225
|
-
newDecorations,
|
|
226
|
-
lineDiffType
|
|
227
|
-
});
|
|
228
|
-
if (oldLine != null) {
|
|
229
|
-
oldInfo[oldLineIndex] = {
|
|
230
|
-
type: "change-deletion",
|
|
231
|
-
lineNumber: oldLineNumber,
|
|
232
|
-
lineIndex: `${_unifiedLineIndex},${splitLineIndex}`
|
|
233
|
-
};
|
|
234
|
-
oldContent += oldLine;
|
|
235
|
-
oldLineIndex++;
|
|
236
|
-
oldLineNumber++;
|
|
237
|
-
}
|
|
238
|
-
if (newLine != null) {
|
|
239
|
-
newInfo[newLineIndex] = {
|
|
240
|
-
type: "change-addition",
|
|
241
|
-
lineNumber: newLineNumber,
|
|
242
|
-
lineIndex: `${_unifiedLineIndex + hunkContent.deletions.length},${splitLineIndex}`
|
|
243
|
-
};
|
|
244
|
-
newContent += newLine;
|
|
245
|
-
newLineIndex++;
|
|
246
|
-
newLineNumber++;
|
|
247
|
-
}
|
|
248
|
-
splitLineIndex++;
|
|
249
|
-
_unifiedLineIndex++;
|
|
250
|
-
i++;
|
|
251
|
-
}
|
|
252
|
-
unifiedLineIndex += hunkContent.additions.length + hunkContent.deletions.length;
|
|
253
|
-
}
|
|
254
|
-
if (oldLines == null || newLines == null || hunk !== hunks[hunks.length - 1]) continue;
|
|
255
|
-
while (oldLineIndex <= oldLines.length || newLineIndex <= oldLines.length) {
|
|
256
|
-
const oldLine = oldLines[oldLineIndex - 1];
|
|
257
|
-
const newLine = newLines[newLineIndex - 1];
|
|
258
|
-
if (oldLine == null && newLine == null) break;
|
|
259
|
-
if (oldLine != null) {
|
|
260
|
-
oldInfo[oldLineIndex] = {
|
|
261
|
-
type: "context-expanded",
|
|
262
|
-
lineNumber: oldLineNumber,
|
|
263
|
-
altLineNumber: newLineNumber,
|
|
264
|
-
lineIndex: `${unifiedLineIndex},${splitLineIndex}`
|
|
265
|
-
};
|
|
266
|
-
oldContent += oldLine;
|
|
267
|
-
oldLineIndex++;
|
|
268
|
-
oldLineNumber++;
|
|
269
|
-
}
|
|
270
|
-
if (newLine != null) {
|
|
271
|
-
newInfo[newLineIndex] = {
|
|
272
|
-
type: "context-expanded",
|
|
273
|
-
lineNumber: newLineNumber,
|
|
274
|
-
altLineNumber: oldLineNumber,
|
|
275
|
-
lineIndex: `${unifiedLineIndex},${splitLineIndex}`
|
|
276
|
-
};
|
|
277
|
-
newContent += newLine;
|
|
278
|
-
newLineIndex++;
|
|
279
|
-
newLineNumber++;
|
|
280
|
-
}
|
|
281
|
-
splitLineIndex++;
|
|
282
|
-
unifiedLineIndex++;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
192
|
+
function createBucket() {
|
|
285
193
|
return {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
194
|
+
deletionContent: {
|
|
195
|
+
push(value) {
|
|
196
|
+
this.value += value;
|
|
197
|
+
this.length++;
|
|
198
|
+
},
|
|
199
|
+
value: "",
|
|
200
|
+
length: 0
|
|
201
|
+
},
|
|
202
|
+
additionContent: {
|
|
203
|
+
push(value) {
|
|
204
|
+
this.value += value;
|
|
205
|
+
this.length++;
|
|
206
|
+
},
|
|
207
|
+
value: "",
|
|
208
|
+
length: 0
|
|
209
|
+
},
|
|
210
|
+
deletionInfo: [],
|
|
211
|
+
additionInfo: [],
|
|
212
|
+
deletionDecorations: [],
|
|
213
|
+
additionDecorations: [],
|
|
214
|
+
deletionSegments: [],
|
|
215
|
+
additionSegments: []
|
|
294
216
|
};
|
|
295
217
|
}
|
|
296
|
-
function renderTwoFiles({
|
|
297
|
-
const
|
|
298
|
-
const
|
|
218
|
+
function renderTwoFiles({ deletionFile, additionFile, deletionInfo, additionInfo, highlighter, deletionDecorations, additionDecorations, languageOverride, options: { theme: themeOrThemes = DEFAULT_THEMES,...options } }) {
|
|
219
|
+
const deletionLang = languageOverride ?? getFiletypeFromFileName(deletionFile.name);
|
|
220
|
+
const additionLang = languageOverride ?? getFiletypeFromFileName(additionFile.name);
|
|
299
221
|
const { state, transformers } = createTransformerWithState();
|
|
300
222
|
const hastConfig = (() => {
|
|
301
223
|
return typeof themeOrThemes === "string" ? {
|
|
@@ -317,19 +239,19 @@ function renderTwoFiles({ oldFile, newFile, oldInfo, newInfo, highlighter, oldDe
|
|
|
317
239
|
};
|
|
318
240
|
})();
|
|
319
241
|
return {
|
|
320
|
-
|
|
321
|
-
if (
|
|
322
|
-
hastConfig.lang =
|
|
323
|
-
state.lineInfo =
|
|
324
|
-
hastConfig.decorations =
|
|
325
|
-
return getLineNodes(highlighter.codeToHast(cleanLastNewline(
|
|
242
|
+
deletionLines: (() => {
|
|
243
|
+
if (deletionFile.contents === "") return [];
|
|
244
|
+
hastConfig.lang = deletionLang;
|
|
245
|
+
state.lineInfo = deletionInfo;
|
|
246
|
+
hastConfig.decorations = deletionDecorations;
|
|
247
|
+
return getLineNodes(highlighter.codeToHast(cleanLastNewline(deletionFile.contents), hastConfig));
|
|
326
248
|
})(),
|
|
327
|
-
|
|
328
|
-
if (
|
|
329
|
-
hastConfig.lang =
|
|
330
|
-
hastConfig.decorations =
|
|
331
|
-
state.lineInfo =
|
|
332
|
-
return getLineNodes(highlighter.codeToHast(cleanLastNewline(
|
|
249
|
+
additionLines: (() => {
|
|
250
|
+
if (additionFile.contents === "") return [];
|
|
251
|
+
hastConfig.lang = additionLang;
|
|
252
|
+
hastConfig.decorations = additionDecorations;
|
|
253
|
+
state.lineInfo = additionInfo;
|
|
254
|
+
return getLineNodes(highlighter.codeToHast(cleanLastNewline(additionFile.contents), hastConfig));
|
|
333
255
|
})()
|
|
334
256
|
};
|
|
335
257
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"renderDiffWithHighlighter.js","names":["hunks: RenderDiffFilesResult[]","deletionSpans: [0 | 1, string][]","additionSpans: [0 | 1, string][]","oldInfo: Record<number, LineInfo | undefined>","newInfo: Record<number, LineInfo | undefined>","oldDecorations: DecorationItem[]","newDecorations: DecorationItem[]","hastConfig: CodeToHastOptions<DiffsThemeNames>"],"sources":["../../src/utils/renderDiffWithHighlighter.ts"],"sourcesContent":["import { diffChars, diffWordsWithSpace } from 'diff';\n\nimport { DEFAULT_THEMES } from '../constants';\nimport type {\n CodeToHastOptions,\n DecorationItem,\n DiffsHighlighter,\n DiffsThemeNames,\n FileContents,\n FileDiffMetadata,\n Hunk,\n LineDiffTypes,\n LineInfo,\n RenderDiffFilesResult,\n RenderDiffOptions,\n SupportedLanguages,\n ThemedDiffResult,\n} from '../types';\nimport { cleanLastNewline } from './cleanLastNewline';\nimport { createTransformerWithState } from './createTransformerWithState';\nimport { formatCSSVariablePrefix } from './formatCSSVariablePrefix';\nimport { getFiletypeFromFileName } from './getFiletypeFromFileName';\nimport { getHighlighterThemeStyles } from './getHighlighterThemeStyles';\nimport { getLineNodes } from './getLineNodes';\nimport {\n createDiffSpanDecoration,\n pushOrJoinSpan,\n} from './parseDiffDecorations';\n\nexport function renderDiffWithHighlighter(\n diff: FileDiffMetadata,\n highlighter: DiffsHighlighter,\n options: RenderDiffOptions,\n forcePlainText = false\n): ThemedDiffResult {\n const baseThemeType = (() => {\n const theme = options.theme ?? DEFAULT_THEMES;\n if (typeof theme === 'string') {\n return highlighter.getTheme(theme).type;\n }\n return undefined;\n })();\n const themeStyles = getHighlighterThemeStyles({\n theme: options.theme,\n highlighter,\n });\n // If we've received a diff with both files\n if (diff.newLines != null && diff.oldLines != null) {\n const {\n oldContent,\n newContent,\n oldInfo,\n newInfo,\n oldDecorations,\n newDecorations,\n } = processLines({\n hunks: diff.hunks,\n oldLines: diff.oldLines,\n newLines: diff.newLines,\n lineDiffType: options.lineDiffType,\n });\n const oldFile = {\n name: diff.prevName ?? diff.name,\n contents: oldContent,\n };\n const newFile = {\n name: diff.name,\n contents: newContent,\n };\n const code = renderTwoFiles({\n oldFile,\n oldInfo,\n oldDecorations,\n\n newFile,\n newInfo,\n newDecorations,\n\n highlighter,\n options,\n languageOverride: forcePlainText ? 'text' : diff.lang,\n });\n return { code, themeStyles, baseThemeType };\n }\n const hunks: RenderDiffFilesResult[] = [];\n let splitLineIndex = 0;\n let unifiedLineIndex = 0;\n for (const hunk of diff.hunks) {\n const {\n oldContent,\n newContent,\n oldInfo,\n newInfo,\n oldDecorations,\n newDecorations,\n splitLineIndex: newSplitLineIndex,\n unifiedLineIndex: newUnifiedLineIndex,\n } = processLines({\n hunks: [hunk],\n splitLineIndex,\n unifiedLineIndex,\n lineDiffType: options.lineDiffType,\n });\n const oldFile = {\n name: diff.prevName ?? diff.name,\n contents: oldContent,\n };\n const newFile = {\n name: diff.name,\n contents: newContent,\n };\n hunks.push(\n renderTwoFiles({\n oldFile,\n oldInfo,\n oldDecorations,\n\n newFile,\n newInfo,\n newDecorations,\n\n highlighter,\n options,\n languageOverride: forcePlainText ? 'text' : diff.lang,\n })\n );\n splitLineIndex = newSplitLineIndex;\n unifiedLineIndex = newUnifiedLineIndex;\n }\n\n const code = (() => {\n if (hunks.length <= 1) {\n const hunk = hunks[0] ?? { oldLines: [], newLines: [] };\n if (hunk.newLines.length === 0 || hunk.oldLines.length === 0) {\n return hunk;\n }\n }\n return { hunks };\n })();\n\n return { code, themeStyles, baseThemeType };\n}\n\ninterface ProcessLineDiffProps {\n oldLine: string | undefined;\n newLine: string | undefined;\n oldLineIndex: number;\n newLineIndex: number;\n oldDecorations: DecorationItem[];\n newDecorations: DecorationItem[];\n lineDiffType: LineDiffTypes;\n}\n\nfunction computeLineDiffDecorations({\n oldLine,\n newLine,\n oldLineIndex,\n newLineIndex,\n oldDecorations,\n newDecorations,\n lineDiffType,\n}: ProcessLineDiffProps) {\n if (oldLine == null || newLine == null || lineDiffType === 'none') {\n return;\n }\n oldLine = cleanLastNewline(oldLine);\n newLine = cleanLastNewline(newLine);\n // NOTE(amadeus): Because we visually trim trailing newlines when rendering,\n // we also gotta make sure the diff parsing doesn't include the newline\n // character that could be there...\n const lineDiff =\n lineDiffType === 'char'\n ? diffChars(oldLine, newLine)\n : diffWordsWithSpace(oldLine, newLine);\n const deletionSpans: [0 | 1, string][] = [];\n const additionSpans: [0 | 1, string][] = [];\n const enableJoin = lineDiffType === 'word-alt';\n for (const item of lineDiff) {\n const isLastItem = item === lineDiff[lineDiff.length - 1];\n if (!item.added && !item.removed) {\n pushOrJoinSpan({\n item,\n arr: deletionSpans,\n enableJoin,\n isNeutral: true,\n isLastItem,\n });\n pushOrJoinSpan({\n item,\n arr: additionSpans,\n enableJoin,\n isNeutral: true,\n isLastItem,\n });\n } else if (item.removed) {\n pushOrJoinSpan({ item, arr: deletionSpans, enableJoin, isLastItem });\n } else {\n pushOrJoinSpan({ item, arr: additionSpans, enableJoin, isLastItem });\n }\n }\n let spanIndex = 0;\n for (const span of deletionSpans) {\n if (span[0] === 1) {\n oldDecorations.push(\n createDiffSpanDecoration({\n // Decoration indexes start at 0\n line: oldLineIndex - 1,\n spanStart: spanIndex,\n spanLength: span[1].length,\n })\n );\n }\n spanIndex += span[1].length;\n }\n spanIndex = 0;\n for (const span of additionSpans) {\n if (span[0] === 1) {\n newDecorations.push(\n createDiffSpanDecoration({\n // Decoration indexes start at 0\n line: newLineIndex - 1,\n spanStart: spanIndex,\n spanLength: span[1].length,\n })\n );\n }\n spanIndex += span[1].length;\n }\n}\n\ninterface ProcessLinesProps {\n hunks: Hunk[];\n oldLines?: string[];\n newLines?: string[];\n splitLineIndex?: number;\n unifiedLineIndex?: number;\n newLineIndex?: number;\n oldLineIndex?: number;\n lineDiffType: LineDiffTypes;\n}\n\nfunction processLines({\n hunks,\n oldLines,\n newLines,\n splitLineIndex = 0,\n unifiedLineIndex = 0,\n lineDiffType,\n}: ProcessLinesProps) {\n const oldInfo: Record<number, LineInfo | undefined> = {};\n const newInfo: Record<number, LineInfo | undefined> = {};\n const oldDecorations: DecorationItem[] = [];\n const newDecorations: DecorationItem[] = [];\n let newLineIndex = 1;\n let oldLineIndex = 1;\n let newLineNumber = 1;\n let oldLineNumber = 1;\n let oldContent = '';\n let newContent = '';\n for (const hunk of hunks) {\n // If there's content prior to the hunk, lets fill it up\n while (\n oldLines != null &&\n newLines != null &&\n newLineIndex < hunk.additionStart &&\n oldLineIndex < hunk.deletionStart\n ) {\n oldInfo[oldLineIndex] = {\n type: 'context-expanded',\n lineNumber: oldLineNumber,\n altLineNumber: newLineNumber,\n lineIndex: `${unifiedLineIndex},${splitLineIndex}`,\n };\n newInfo[newLineIndex] = {\n type: 'context-expanded',\n lineNumber: newLineNumber,\n altLineNumber: oldLineNumber,\n lineIndex: `${unifiedLineIndex},${splitLineIndex}`,\n };\n oldContent += oldLines[oldLineIndex - 1];\n newContent += newLines[newLineIndex - 1];\n oldLineIndex++;\n newLineIndex++;\n oldLineNumber++;\n newLineNumber++;\n splitLineIndex++;\n unifiedLineIndex++;\n }\n oldLineNumber = hunk.deletionStart;\n newLineNumber = hunk.additionStart;\n\n // Lets process the actual hunk content\n for (const hunkContent of hunk.hunkContent) {\n if (hunkContent.type === 'context') {\n for (const line of hunkContent.lines) {\n oldInfo[oldLineIndex] = {\n type: 'context',\n lineNumber: oldLineNumber,\n altLineNumber: newLineNumber,\n lineIndex: `${unifiedLineIndex},${splitLineIndex}`,\n };\n newInfo[newLineIndex] = {\n type: 'context',\n lineNumber: newLineNumber,\n altLineNumber: oldLineNumber,\n lineIndex: `${unifiedLineIndex},${splitLineIndex}`,\n };\n oldContent += line;\n newContent += line;\n oldLineIndex++;\n newLineIndex++;\n newLineNumber++;\n oldLineNumber++;\n splitLineIndex++;\n unifiedLineIndex++;\n }\n } else {\n const len = Math.max(\n hunkContent.additions.length,\n hunkContent.deletions.length\n );\n let i = 0;\n // NOTE(amadeus): Since we iterate through deletions and additions\n // simultaneously, we have to create a secondary iterator for\n // unifiedLineIndex, and then when we're done, add the combined lengths\n // of additions/deletions to the main variable\n let _unifiedLineIndex = unifiedLineIndex;\n while (i < len) {\n const oldLine = hunkContent.deletions[i];\n const newLine = hunkContent.additions[i];\n computeLineDiffDecorations({\n newLine,\n oldLine,\n oldLineIndex,\n newLineIndex,\n oldDecorations,\n newDecorations,\n lineDiffType,\n });\n if (oldLine != null) {\n oldInfo[oldLineIndex] = {\n type: 'change-deletion',\n lineNumber: oldLineNumber,\n lineIndex: `${_unifiedLineIndex},${splitLineIndex}`,\n };\n oldContent += oldLine;\n oldLineIndex++;\n oldLineNumber++;\n }\n if (newLine != null) {\n newInfo[newLineIndex] = {\n type: 'change-addition',\n lineNumber: newLineNumber,\n lineIndex: `${_unifiedLineIndex + hunkContent.deletions.length},${splitLineIndex}`,\n };\n newContent += newLine;\n newLineIndex++;\n newLineNumber++;\n }\n splitLineIndex++;\n _unifiedLineIndex++;\n i++;\n }\n unifiedLineIndex +=\n hunkContent.additions.length + hunkContent.deletions.length;\n }\n }\n\n if (\n oldLines == null ||\n newLines == null ||\n hunk !== hunks[hunks.length - 1]\n )\n continue;\n // If we are on the last hunk, we should fully iterate through the rest\n // of the lines\n while (oldLineIndex <= oldLines.length || newLineIndex <= oldLines.length) {\n const oldLine = oldLines[oldLineIndex - 1];\n const newLine = newLines[newLineIndex - 1];\n if (oldLine == null && newLine == null) {\n break;\n }\n if (oldLine != null) {\n oldInfo[oldLineIndex] = {\n type: 'context-expanded',\n lineNumber: oldLineNumber,\n altLineNumber: newLineNumber,\n lineIndex: `${unifiedLineIndex},${splitLineIndex}`,\n };\n oldContent += oldLine;\n oldLineIndex++;\n oldLineNumber++;\n }\n if (newLine != null) {\n newInfo[newLineIndex] = {\n type: 'context-expanded',\n lineNumber: newLineNumber,\n altLineNumber: oldLineNumber,\n lineIndex: `${unifiedLineIndex},${splitLineIndex}`,\n };\n newContent += newLine;\n newLineIndex++;\n newLineNumber++;\n }\n splitLineIndex++;\n unifiedLineIndex++;\n }\n }\n return {\n oldContent,\n newContent,\n oldInfo,\n newInfo,\n oldDecorations,\n newDecorations,\n splitLineIndex,\n unifiedLineIndex,\n };\n}\n\ninterface RenderTwoFilesProps {\n oldFile: FileContents;\n newFile: FileContents;\n oldInfo: Record<number, LineInfo | undefined>;\n newInfo: Record<number, LineInfo | undefined>;\n oldDecorations: DecorationItem[];\n newDecorations: DecorationItem[];\n options: RenderDiffOptions;\n highlighter: DiffsHighlighter;\n languageOverride: SupportedLanguages | undefined;\n}\n\nfunction renderTwoFiles({\n oldFile,\n newFile,\n oldInfo,\n newInfo,\n highlighter,\n oldDecorations,\n newDecorations,\n languageOverride,\n options: { theme: themeOrThemes = DEFAULT_THEMES, ...options },\n}: RenderTwoFilesProps) {\n const oldLang = languageOverride ?? getFiletypeFromFileName(oldFile.name);\n const newLang = languageOverride ?? getFiletypeFromFileName(newFile.name);\n const { state, transformers } = createTransformerWithState();\n const hastConfig: CodeToHastOptions<DiffsThemeNames> = (() => {\n return typeof themeOrThemes === 'string'\n ? {\n ...options,\n // language will be overwritten for each highlight\n lang: 'text',\n theme: themeOrThemes,\n transformers,\n decorations: undefined,\n defaultColor: false,\n cssVariablePrefix: formatCSSVariablePrefix('token'),\n }\n : {\n ...options,\n // language will be overwritten for each highlight\n lang: 'text',\n themes: themeOrThemes,\n transformers,\n decorations: undefined,\n defaultColor: false,\n cssVariablePrefix: formatCSSVariablePrefix('token'),\n };\n })();\n\n const oldLines = (() => {\n if (oldFile.contents === '') {\n return [];\n }\n hastConfig.lang = oldLang;\n state.lineInfo = oldInfo;\n hastConfig.decorations = oldDecorations;\n return getLineNodes(\n highlighter.codeToHast(cleanLastNewline(oldFile.contents), hastConfig)\n );\n })();\n const newLines = (() => {\n if (newFile.contents === '') {\n return [];\n }\n hastConfig.lang = newLang;\n hastConfig.decorations = newDecorations;\n state.lineInfo = newInfo;\n return getLineNodes(\n highlighter.codeToHast(cleanLastNewline(newFile.contents), hastConfig)\n );\n })();\n\n return { oldLines, newLines };\n}\n"],"mappings":";;;;;;;;;;;AA6BA,SAAgB,0BACd,MACA,aACA,SACA,iBAAiB,OACC;CAClB,MAAM,uBAAuB;EAC3B,MAAM,QAAQ,QAAQ,SAAS;AAC/B,MAAI,OAAO,UAAU,SACnB,QAAO,YAAY,SAAS,MAAM,CAAC;KAGnC;CACJ,MAAM,cAAc,0BAA0B;EAC5C,OAAO,QAAQ;EACf;EACD,CAAC;AAEF,KAAI,KAAK,YAAY,QAAQ,KAAK,YAAY,MAAM;EAClD,MAAM,EACJ,YACA,YACA,SACA,SACA,gBACA,mBACE,aAAa;GACf,OAAO,KAAK;GACZ,UAAU,KAAK;GACf,UAAU,KAAK;GACf,cAAc,QAAQ;GACvB,CAAC;AAsBF,SAAO;GAAE,MAbI,eAAe;IAC1B,SATc;KACd,MAAM,KAAK,YAAY,KAAK;KAC5B,UAAU;KACX;IAOC;IACA;IAEA,SATc;KACd,MAAM,KAAK;KACX,UAAU;KACX;IAOC;IACA;IAEA;IACA;IACA,kBAAkB,iBAAiB,SAAS,KAAK;IAClD,CAAC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,SAAS,EAAE,WAAW,CACvE;MACC;EAEyB"}
|
|
1
|
+
{"version":3,"file":"renderDiffWithHighlighter.js","names":["DEFAULT_PLAIN_TEXT_OPTIONS: ForcePlainTextOptions","code: RenderDiffFilesResult","deletionSpans: [0 | 1, string][]","additionSpans: [0 | 1, string][]","hastConfig: CodeToHastOptions<DiffsThemeNames>"],"sources":["../../src/utils/renderDiffWithHighlighter.ts"],"sourcesContent":["import { diffChars, diffWordsWithSpace } from 'diff';\n\nimport { DEFAULT_THEMES } from '../constants';\nimport type {\n CodeToHastOptions,\n DecorationItem,\n DiffsHighlighter,\n DiffsThemeNames,\n FileContents,\n FileDiffMetadata,\n ForcePlainTextOptions,\n LineDiffTypes,\n LineInfo,\n RenderDiffFilesResult,\n RenderDiffOptions,\n SupportedLanguages,\n ThemedDiffResult,\n} from '../types';\nimport { cleanLastNewline } from './cleanLastNewline';\nimport { createTransformerWithState } from './createTransformerWithState';\nimport { formatCSSVariablePrefix } from './formatCSSVariablePrefix';\nimport { getFiletypeFromFileName } from './getFiletypeFromFileName';\nimport { getHighlighterThemeStyles } from './getHighlighterThemeStyles';\nimport { getLineNodes } from './getLineNodes';\nimport { iterateOverDiff } from './iterateOverDiff';\nimport {\n createDiffSpanDecoration,\n pushOrJoinSpan,\n} from './parseDiffDecorations';\n\nconst DEFAULT_PLAIN_TEXT_OPTIONS: ForcePlainTextOptions = {\n forcePlainText: false,\n};\n\nexport function renderDiffWithHighlighter(\n diff: FileDiffMetadata,\n highlighter: DiffsHighlighter,\n options: RenderDiffOptions,\n {\n forcePlainText,\n startingLine,\n totalLines,\n expandedHunks,\n }: ForcePlainTextOptions = DEFAULT_PLAIN_TEXT_OPTIONS\n): ThemedDiffResult {\n if (forcePlainText) {\n startingLine ??= 0;\n totalLines ??= Infinity;\n } else {\n // If we aren't forcing plain text, then we intentionally do not support\n // ranges for highlighting as that could break the syntax highlighting, we\n // we override any values that may have been passed in. Maybe one day we\n // warn about this?\n startingLine = 0;\n totalLines = Infinity;\n }\n const isWindowedHighlight = startingLine > 0 || totalLines < Infinity;\n const baseThemeType = (() => {\n const theme = options.theme ?? DEFAULT_THEMES;\n if (typeof theme === 'string') {\n return highlighter.getTheme(theme).type;\n }\n return undefined;\n })();\n const themeStyles = getHighlighterThemeStyles({\n theme: options.theme,\n highlighter,\n });\n\n // If we have a large file and we are rendering the WHOLE plain diff ast,\n // then we should remove the lineDiffType to make sure things render quickly.\n // For highlighted ASTs or windowed highlights, we should just inherit the\n // setting\n const lineDiffType =\n forcePlainText &&\n !isWindowedHighlight &&\n (diff.unifiedLineCount > 1000 || diff.splitLineCount > 1000)\n ? 'none'\n : options.lineDiffType;\n\n const code: RenderDiffFilesResult = {\n deletionLines: [],\n additionLines: [],\n };\n\n const shouldGroupAll = !forcePlainText && !diff.isPartial;\n const expandedHunksForIteration = forcePlainText ? expandedHunks : undefined;\n const buckets = new Map<number, RenderBucket>();\n function getBucketForHunk(hunkIndex: number) {\n const index = shouldGroupAll ? 0 : hunkIndex;\n const bucket = buckets.get(index) ?? createBucket();\n buckets.set(index, bucket);\n return bucket;\n }\n\n function appendContent(\n lineContent: string,\n lineIndex: number,\n segments: HighlightSegment[],\n contentWrapper: FakeArrayType\n ) {\n if (isWindowedHighlight) {\n let segment = segments.at(-1);\n if (\n segment == null ||\n segment.targetIndex + segment.count !== lineIndex\n ) {\n segment = {\n targetIndex: lineIndex,\n originalOffset: contentWrapper.length,\n count: 0,\n };\n segments.push(segment);\n }\n segment.count++;\n }\n contentWrapper.push(lineContent);\n }\n\n iterateOverDiff({\n diff,\n diffStyle: 'both',\n startingLine,\n totalLines,\n expandedHunks: isWindowedHighlight ? expandedHunksForIteration : true,\n callback: ({\n hunkIndex,\n additionLineIndex,\n deletionLineIndex,\n additionLineNumber,\n deletionLineNumber,\n unifiedAdditionLineIndex,\n unifiedDeletionLineIndex,\n splitLineIndex,\n type,\n }) => {\n const bucket = getBucketForHunk(hunkIndex);\n\n if (\n type === 'change' &&\n lineDiffType !== 'none' &&\n additionLineIndex != null &&\n deletionLineIndex != null\n ) {\n computeLineDiffDecorations({\n additionLine: diff.additionLines[additionLineIndex],\n deletionLine: diff.deletionLines[deletionLineIndex],\n deletionLineIndex: bucket.deletionContent.length,\n additionLineIndex: bucket.additionContent.length,\n deletionDecorations: bucket.deletionDecorations,\n additionDecorations: bucket.additionDecorations,\n lineDiffType,\n });\n }\n\n if (\n deletionLineIndex != null &&\n deletionLineNumber != null &&\n unifiedDeletionLineIndex != null\n ) {\n appendContent(\n diff.deletionLines[deletionLineIndex],\n deletionLineIndex,\n bucket.deletionSegments,\n bucket.deletionContent\n );\n bucket.deletionInfo.push({\n type: type === 'change' ? 'change-deletion' : type,\n lineNumber: deletionLineNumber,\n altLineNumber:\n type === 'change' ? undefined : (additionLineNumber ?? undefined),\n lineIndex: `${unifiedDeletionLineIndex},${splitLineIndex}`,\n });\n }\n\n if (\n additionLineIndex != null &&\n additionLineNumber != null &&\n unifiedAdditionLineIndex != null\n ) {\n appendContent(\n diff.additionLines[additionLineIndex],\n additionLineIndex,\n bucket.additionSegments,\n bucket.additionContent\n );\n bucket.additionInfo.push({\n type: type === 'change' ? 'change-addition' : type,\n lineNumber: additionLineNumber,\n altLineNumber:\n type === 'change' ? undefined : (deletionLineNumber ?? undefined),\n lineIndex: `${unifiedAdditionLineIndex},${splitLineIndex}`,\n });\n }\n },\n });\n\n for (const bucket of buckets.values()) {\n if (\n bucket.deletionContent.length === 0 &&\n bucket.additionContent.length === 0\n ) {\n continue;\n }\n\n const deletionFile = {\n name: diff.prevName ?? diff.name,\n contents: bucket.deletionContent.value,\n };\n const additionFile = {\n name: diff.name,\n contents: bucket.additionContent.value,\n };\n const { deletionLines, additionLines } = renderTwoFiles({\n deletionFile,\n deletionInfo: bucket.deletionInfo,\n deletionDecorations: bucket.deletionDecorations,\n\n additionFile,\n additionInfo: bucket.additionInfo,\n additionDecorations: bucket.additionDecorations,\n\n highlighter,\n options,\n languageOverride: forcePlainText ? 'text' : diff.lang,\n });\n\n if (shouldGroupAll) {\n code.deletionLines = deletionLines;\n code.additionLines = additionLines;\n continue;\n }\n\n if (bucket.deletionSegments.length > 0) {\n for (const seg of bucket.deletionSegments) {\n for (let i = 0; i < seg.count; i++) {\n code.deletionLines[seg.targetIndex + i] =\n deletionLines[seg.originalOffset + i];\n }\n }\n } else {\n code.deletionLines.push(...deletionLines);\n }\n if (bucket.additionSegments.length > 0) {\n for (const seg of bucket.additionSegments) {\n for (let i = 0; i < seg.count; i++) {\n code.additionLines[seg.targetIndex + i] =\n additionLines[seg.originalOffset + i];\n }\n }\n } else {\n code.additionLines.push(...additionLines);\n }\n }\n\n return { code, themeStyles, baseThemeType };\n}\n\ninterface ProcessLineDiffProps {\n deletionLine: string | undefined;\n additionLine: string | undefined;\n deletionLineIndex: number;\n additionLineIndex: number;\n deletionDecorations: DecorationItem[];\n additionDecorations: DecorationItem[];\n lineDiffType: LineDiffTypes;\n}\n\nfunction computeLineDiffDecorations({\n deletionLine,\n additionLine,\n deletionLineIndex,\n additionLineIndex,\n deletionDecorations,\n additionDecorations,\n lineDiffType,\n}: ProcessLineDiffProps) {\n if (deletionLine == null || additionLine == null || lineDiffType === 'none') {\n return;\n }\n deletionLine = cleanLastNewline(deletionLine);\n additionLine = cleanLastNewline(additionLine);\n // NOTE(amadeus): Because we visually trim trailing newlines when rendering,\n // we also gotta make sure the diff parsing doesn't include the newline\n // character that could be there...\n const lineDiff =\n lineDiffType === 'char'\n ? diffChars(deletionLine, additionLine)\n : diffWordsWithSpace(deletionLine, additionLine);\n const deletionSpans: [0 | 1, string][] = [];\n const additionSpans: [0 | 1, string][] = [];\n const enableJoin = lineDiffType === 'word-alt';\n const lastItem = lineDiff.at(-1);\n for (const item of lineDiff) {\n const isLastItem = item === lastItem;\n if (!item.added && !item.removed) {\n pushOrJoinSpan({\n item,\n arr: deletionSpans,\n enableJoin,\n isNeutral: true,\n isLastItem,\n });\n pushOrJoinSpan({\n item,\n arr: additionSpans,\n enableJoin,\n isNeutral: true,\n isLastItem,\n });\n } else if (item.removed) {\n pushOrJoinSpan({ item, arr: deletionSpans, enableJoin, isLastItem });\n } else {\n pushOrJoinSpan({ item, arr: additionSpans, enableJoin, isLastItem });\n }\n }\n let spanIndex = 0;\n for (const span of deletionSpans) {\n if (span[0] === 1) {\n deletionDecorations.push(\n createDiffSpanDecoration({\n line: deletionLineIndex,\n spanStart: spanIndex,\n spanLength: span[1].length,\n })\n );\n }\n spanIndex += span[1].length;\n }\n spanIndex = 0;\n for (const span of additionSpans) {\n if (span[0] === 1) {\n additionDecorations.push(\n createDiffSpanDecoration({\n line: additionLineIndex,\n spanStart: spanIndex,\n spanLength: span[1].length,\n })\n );\n }\n spanIndex += span[1].length;\n }\n}\n\ninterface HighlightSegment {\n // The where the highlighted region starts\n originalOffset: number;\n // Where to place the highlighted line in RenderDiffFilesResult\n targetIndex: number;\n // Number of highlighted lines\n count: number;\n}\n\ninterface FakeArrayType {\n push(value: string): void;\n value: string;\n length: number;\n}\n\ninterface RenderBucket {\n deletionContent: FakeArrayType;\n additionContent: FakeArrayType;\n deletionInfo: (LineInfo | undefined)[];\n additionInfo: (LineInfo | undefined)[];\n deletionDecorations: DecorationItem[];\n additionDecorations: DecorationItem[];\n deletionSegments: HighlightSegment[];\n additionSegments: HighlightSegment[];\n}\n\nfunction createBucket(): RenderBucket {\n return {\n deletionContent: {\n push(value: string) {\n this.value += value;\n this.length++;\n },\n value: '',\n length: 0,\n },\n additionContent: {\n push(value: string) {\n this.value += value;\n this.length++;\n },\n value: '',\n length: 0,\n },\n deletionInfo: [],\n additionInfo: [],\n deletionDecorations: [],\n additionDecorations: [],\n deletionSegments: [],\n additionSegments: [],\n };\n}\n\ninterface RenderTwoFilesProps {\n deletionFile: FileContents;\n additionFile: FileContents;\n deletionInfo: (LineInfo | undefined)[];\n additionInfo: (LineInfo | undefined)[];\n deletionDecorations: DecorationItem[];\n additionDecorations: DecorationItem[];\n options: RenderDiffOptions;\n highlighter: DiffsHighlighter;\n languageOverride: SupportedLanguages | undefined;\n}\n\nfunction renderTwoFiles({\n deletionFile,\n additionFile,\n deletionInfo,\n additionInfo,\n highlighter,\n deletionDecorations,\n additionDecorations,\n languageOverride,\n options: { theme: themeOrThemes = DEFAULT_THEMES, ...options },\n}: RenderTwoFilesProps): RenderDiffFilesResult {\n const deletionLang =\n languageOverride ?? getFiletypeFromFileName(deletionFile.name);\n const additionLang =\n languageOverride ?? getFiletypeFromFileName(additionFile.name);\n const { state, transformers } = createTransformerWithState();\n const hastConfig: CodeToHastOptions<DiffsThemeNames> = (() => {\n return typeof themeOrThemes === 'string'\n ? {\n ...options,\n // language will be overwritten for each highlight\n lang: 'text',\n theme: themeOrThemes,\n transformers,\n decorations: undefined,\n defaultColor: false,\n cssVariablePrefix: formatCSSVariablePrefix('token'),\n }\n : {\n ...options,\n // language will be overwritten for each highlight\n lang: 'text',\n themes: themeOrThemes,\n transformers,\n decorations: undefined,\n defaultColor: false,\n cssVariablePrefix: formatCSSVariablePrefix('token'),\n };\n })();\n\n const deletionLines = (() => {\n if (deletionFile.contents === '') {\n return [];\n }\n hastConfig.lang = deletionLang;\n state.lineInfo = deletionInfo;\n hastConfig.decorations = deletionDecorations;\n return getLineNodes(\n highlighter.codeToHast(\n cleanLastNewline(deletionFile.contents),\n hastConfig\n )\n );\n })();\n const additionLines = (() => {\n if (additionFile.contents === '') {\n return [];\n }\n hastConfig.lang = additionLang;\n hastConfig.decorations = additionDecorations;\n state.lineInfo = additionInfo;\n return getLineNodes(\n highlighter.codeToHast(\n cleanLastNewline(additionFile.contents),\n hastConfig\n )\n );\n })();\n\n return { deletionLines, additionLines };\n}\n"],"mappings":";;;;;;;;;;;;AA8BA,MAAMA,6BAAoD,EACxD,gBAAgB,OACjB;AAED,SAAgB,0BACd,MACA,aACA,SACA,EACE,gBACA,cACA,YACA,kBACyB,4BACT;AAClB,KAAI,gBAAgB;AAClB,mBAAiB;AACjB,iBAAe;QACV;AAKL,iBAAe;AACf,eAAa;;CAEf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AAO,aACL,YAAY,WACV,iBAAiB,aAAa,SAAS,EACvC,WACD,CACF;MACC;EAgBoB,sBAfK;AAC3B,OAAI,aAAa,aAAa,GAC5B,QAAO,EAAE;AAEX,cAAW,OAAO;AAClB,cAAW,cAAc;AACzB,SAAM,WAAW;AACjB,UAAO,aACL,YAAY,WACV,iBAAiB,aAAa,SAAS,EACvC,WACD,CACF;MACC;EAEmC"}
|
|
@@ -1,20 +1,16 @@
|
|
|
1
1
|
import { PrePropertiesConfig } from "../types.js";
|
|
2
2
|
|
|
3
3
|
//#region src/utils/setWrapperNodeProps.d.ts
|
|
4
|
-
|
|
5
|
-
pre: HTMLPreElement;
|
|
6
|
-
}
|
|
7
|
-
declare function setPreNodeProperties({
|
|
4
|
+
declare function setPreNodeProperties(pre: HTMLPreElement, {
|
|
8
5
|
diffIndicators,
|
|
9
6
|
disableBackground,
|
|
10
7
|
disableLineNumbers,
|
|
11
8
|
overflow,
|
|
12
|
-
pre,
|
|
13
9
|
split,
|
|
14
10
|
themeStyles,
|
|
15
11
|
themeType,
|
|
16
12
|
totalLines
|
|
17
|
-
}:
|
|
13
|
+
}: PrePropertiesConfig): HTMLPreElement;
|
|
18
14
|
//#endregion
|
|
19
|
-
export {
|
|
15
|
+
export { setPreNodeProperties };
|
|
20
16
|
//# sourceMappingURL=setWrapperNodeProps.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setWrapperNodeProps.d.ts","names":[],"sources":["../../src/utils/setWrapperNodeProps.ts"],"sourcesContent":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"setWrapperNodeProps.d.ts","names":[],"sources":["../../src/utils/setWrapperNodeProps.ts"],"sourcesContent":[],"mappings":";;;iBAEgB,oBAAA,MACT;;;;;;;;;GAUF,sBACF"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
//#region src/utils/setWrapperNodeProps.ts
|
|
2
|
-
function setPreNodeProperties({ diffIndicators, disableBackground, disableLineNumbers, overflow,
|
|
2
|
+
function setPreNodeProperties(pre, { diffIndicators, disableBackground, disableLineNumbers, overflow, split, themeStyles, themeType, totalLines }) {
|
|
3
3
|
if (themeType === "system") delete pre.dataset.themeType;
|
|
4
4
|
else pre.dataset.themeType = themeType;
|
|
5
5
|
switch (diffIndicators) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setWrapperNodeProps.js","names":[],"sources":["../../src/utils/setWrapperNodeProps.ts"],"sourcesContent":["import type { PrePropertiesConfig } from '../types';\n\nexport
|
|
1
|
+
{"version":3,"file":"setWrapperNodeProps.js","names":[],"sources":["../../src/utils/setWrapperNodeProps.ts"],"sourcesContent":["import type { PrePropertiesConfig } from '../types';\n\nexport function setPreNodeProperties(\n pre: HTMLPreElement,\n {\n diffIndicators,\n disableBackground,\n disableLineNumbers,\n overflow,\n split,\n themeStyles,\n themeType,\n totalLines,\n }: PrePropertiesConfig\n): HTMLPreElement {\n if (themeType === 'system') {\n delete pre.dataset.themeType;\n } else {\n pre.dataset.themeType = themeType;\n }\n switch (diffIndicators) {\n case 'bars':\n case 'classic':\n pre.dataset.indicators = diffIndicators;\n break;\n case 'none':\n delete pre.dataset.indicators;\n break;\n }\n if (disableLineNumbers) {\n pre.dataset.disableLineNumbers = '';\n } else {\n delete pre.dataset.disableLineNumbers;\n }\n if (disableBackground) {\n delete pre.dataset.background;\n } else {\n pre.dataset.background = '';\n }\n pre.dataset.type = split ? 'split' : 'file';\n pre.dataset.overflow = overflow;\n pre.dataset.diffs = '';\n pre.tabIndex = 0;\n // Set theme color custom properties as inline styles on pre element\n pre.style = themeStyles;\n // Set CSS custom property for line number column width\n pre.style.setProperty(\n '--diffs-min-number-column-width-default',\n `${`${totalLines}`.length}ch`\n );\n return pre;\n}\n"],"mappings":";AAEA,SAAgB,qBACd,KACA,EACE,gBACA,mBACA,oBACA,UACA,OACA,aACA,WACA,cAEc;AAChB,KAAI,cAAc,SAChB,QAAO,IAAI,QAAQ;KAEnB,KAAI,QAAQ,YAAY;AAE1B,SAAQ,gBAAR;EACE,KAAK;EACL,KAAK;AACH,OAAI,QAAQ,aAAa;AACzB;EACF,KAAK;AACH,UAAO,IAAI,QAAQ;AACnB;;AAEJ,KAAI,mBACF,KAAI,QAAQ,qBAAqB;KAEjC,QAAO,IAAI,QAAQ;AAErB,KAAI,kBACF,QAAO,IAAI,QAAQ;KAEnB,KAAI,QAAQ,aAAa;AAE3B,KAAI,QAAQ,OAAO,QAAQ,UAAU;AACrC,KAAI,QAAQ,WAAW;AACvB,KAAI,QAAQ,QAAQ;AACpB,KAAI,WAAW;AAEf,KAAI,QAAQ;AAEZ,KAAI,MAAM,YACR,2CACA,GAAG,GAAG,aAAa,OAAO,IAC3B;AACD,QAAO"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FileContents, FileDiffMetadata, RenderDiffOptions, RenderDiffResult, RenderFileOptions, RenderFileResult, SupportedLanguages, ThemedDiffResult, ThemedFileResult } from "../types.js";
|
|
1
|
+
import { FileContents, FileDiffMetadata, HunkExpansionRegion, RenderDiffOptions, RenderDiffResult, RenderFileOptions, RenderFileResult, SupportedLanguages, ThemedDiffResult, ThemedFileResult } from "../types.js";
|
|
2
2
|
import { DiffRendererInstance, FileRendererInstance, WorkerInitializationRenderOptions, WorkerPoolOptions, WorkerRenderingOptions, WorkerStats } from "./types.js";
|
|
3
3
|
import LRUMapPkg from "lru_map";
|
|
4
4
|
|
|
@@ -22,8 +22,10 @@ declare class WorkerPoolManager {
|
|
|
22
22
|
private themeSubscribers;
|
|
23
23
|
private workersFailed;
|
|
24
24
|
private instanceRequestMap;
|
|
25
|
+
private statSubscribers;
|
|
25
26
|
private fileCache;
|
|
26
27
|
private diffCache;
|
|
28
|
+
private _queuedBroadcast;
|
|
27
29
|
constructor(options: WorkerPoolOptions, {
|
|
28
30
|
langs,
|
|
29
31
|
theme,
|
|
@@ -46,6 +48,10 @@ declare class WorkerPoolManager {
|
|
|
46
48
|
private setRenderOptionsOnWorkers;
|
|
47
49
|
subscribeToThemeChanges(instance: ThemeSubscriber): () => void;
|
|
48
50
|
unsubscribeToThemeChanges(instance: ThemeSubscriber): void;
|
|
51
|
+
subscribeToStatChanges(callback: (stats: WorkerStats) => unknown): () => void;
|
|
52
|
+
private queueBroadcastStateChanges;
|
|
53
|
+
private _broadcastStateChanges;
|
|
54
|
+
cleanUpPendingTasks(instance: FileRendererInstance | DiffRendererInstance): void;
|
|
49
55
|
isInitialized(): boolean;
|
|
50
56
|
initialize(languages?: SupportedLanguages[]): Promise<void>;
|
|
51
57
|
private initializeWorkers;
|
|
@@ -53,7 +59,7 @@ declare class WorkerPoolManager {
|
|
|
53
59
|
highlightFileAST(instance: FileRendererInstance, file: FileContents): void;
|
|
54
60
|
getPlainFileAST(file: FileContents): ThemedFileResult | undefined;
|
|
55
61
|
highlightDiffAST(instance: DiffRendererInstance, diff: FileDiffMetadata): void;
|
|
56
|
-
getPlainDiffAST(diff: FileDiffMetadata): ThemedDiffResult | undefined;
|
|
62
|
+
getPlainDiffAST(diff: FileDiffMetadata, startingLine: number, totalLines: number, expandedHunks?: Map<number, HunkExpansionRegion> | true): ThemedDiffResult | undefined;
|
|
57
63
|
terminate(): void;
|
|
58
64
|
private terminateWorkers;
|
|
59
65
|
getStats(): WorkerStats;
|
|
@@ -63,6 +69,7 @@ declare class WorkerPoolManager {
|
|
|
63
69
|
private handleWorkerMessage;
|
|
64
70
|
private _queuedDrain;
|
|
65
71
|
private queueDrain;
|
|
72
|
+
private assignWorkerToTask;
|
|
66
73
|
private executeTask;
|
|
67
74
|
private getAvailableWorker;
|
|
68
75
|
private generateRequestId;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WorkerPoolManager.d.ts","names":["options: WorkerPoolOptions"],"sources":["../../src/worker/WorkerPoolManager.ts"],"sourcesContent":[],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"WorkerPoolManager.d.ts","names":["options: WorkerPoolOptions"],"sources":["../../src/worker/WorkerPoolManager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