@pierre/diffs 1.2.0-beta.5 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/CodeView.d.ts +19 -4
- package/dist/components/CodeView.d.ts.map +1 -1
- package/dist/components/CodeView.js +148 -32
- package/dist/components/CodeView.js.map +1 -1
- package/dist/components/File.d.ts +5 -0
- package/dist/components/File.d.ts.map +1 -1
- package/dist/components/File.js +54 -10
- package/dist/components/File.js.map +1 -1
- package/dist/components/FileDiff.d.ts +5 -1
- package/dist/components/FileDiff.d.ts.map +1 -1
- package/dist/components/FileDiff.js +56 -14
- package/dist/components/FileDiff.js.map +1 -1
- package/dist/components/FileStream.js +1 -0
- package/dist/components/FileStream.js.map +1 -1
- package/dist/components/UnresolvedFile.d.ts.map +1 -1
- package/dist/components/UnresolvedFile.js +1 -1
- package/dist/components/VirtualizedFile.d.ts +1 -0
- package/dist/components/VirtualizedFile.d.ts.map +1 -1
- package/dist/components/VirtualizedFile.js +19 -6
- package/dist/components/VirtualizedFile.js.map +1 -1
- package/dist/components/VirtualizedFileDiff.d.ts +6 -0
- package/dist/components/VirtualizedFileDiff.d.ts.map +1 -1
- package/dist/components/VirtualizedFileDiff.js +75 -35
- package/dist/components/VirtualizedFileDiff.js.map +1 -1
- package/dist/components/Virtualizer.js +1 -1
- package/dist/components/Virtualizer.js.map +1 -1
- package/dist/constants.js +0 -1
- package/dist/constants.js.map +1 -1
- package/dist/highlighter/shared_highlighter.js +14 -0
- package/dist/highlighter/shared_highlighter.js.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +8 -4
- package/dist/managers/InteractionManager.d.ts.map +1 -1
- package/dist/managers/ResizeManager.d.ts +7 -2
- package/dist/managers/ResizeManager.d.ts.map +1 -1
- package/dist/managers/ResizeManager.js +52 -16
- package/dist/managers/ResizeManager.js.map +1 -1
- package/dist/react/CodeView.d.ts +1 -0
- package/dist/react/CodeView.d.ts.map +1 -1
- package/dist/react/CodeView.js +9 -0
- package/dist/react/CodeView.js.map +1 -1
- package/dist/renderers/DiffHunksRenderer.d.ts +2 -0
- package/dist/renderers/DiffHunksRenderer.d.ts.map +1 -1
- package/dist/renderers/DiffHunksRenderer.js +34 -21
- package/dist/renderers/DiffHunksRenderer.js.map +1 -1
- package/dist/renderers/FileRenderer.d.ts +3 -0
- package/dist/renderers/FileRenderer.d.ts.map +1 -1
- package/dist/renderers/FileRenderer.js +37 -23
- package/dist/renderers/FileRenderer.js.map +1 -1
- package/dist/style.js +1 -1
- package/dist/style.js.map +1 -1
- package/dist/types.d.ts +4 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/areDiffTargetsEqual.d.ts +7 -0
- package/dist/utils/areDiffTargetsEqual.d.ts.map +1 -0
- package/dist/utils/areDiffTargetsEqual.js +8 -0
- package/dist/utils/areDiffTargetsEqual.js.map +1 -0
- package/dist/utils/areFileRenderOptionsEqual.d.ts +7 -0
- package/dist/utils/areFileRenderOptionsEqual.d.ts.map +1 -0
- package/dist/utils/areFileRenderOptionsEqual.js +10 -0
- package/dist/utils/areFileRenderOptionsEqual.js.map +1 -0
- package/dist/utils/areWorkerStatsEqual.js +1 -1
- package/dist/utils/areWorkerStatsEqual.js.map +1 -1
- package/dist/utils/computeVirtualFileMetrics.d.ts +11 -0
- package/dist/utils/computeVirtualFileMetrics.d.ts.map +1 -0
- package/dist/utils/{resolveVirtualFileMetrics.js → computeVirtualFileMetrics.js} +7 -10
- package/dist/utils/computeVirtualFileMetrics.js.map +1 -0
- package/dist/utils/detachString.d.ts +6 -0
- package/dist/utils/detachString.d.ts.map +1 -0
- package/dist/utils/detachString.js +21 -0
- package/dist/utils/detachString.js.map +1 -0
- package/dist/utils/isStyleNode.d.ts +5 -0
- package/dist/utils/isStyleNode.d.ts.map +1 -0
- package/dist/utils/isStyleNode.js +10 -0
- package/dist/utils/isStyleNode.js.map +1 -0
- package/dist/utils/parsePatchFiles.d.ts +1 -7
- package/dist/utils/parsePatchFiles.d.ts.map +1 -1
- package/dist/utils/parsePatchFiles.js +180 -59
- package/dist/utils/parsePatchFiles.js.map +1 -1
- package/dist/utils/prefersReducedMotion.d.ts +5 -0
- package/dist/utils/prefersReducedMotion.d.ts.map +1 -0
- package/dist/utils/prefersReducedMotion.js +9 -0
- package/dist/utils/prefersReducedMotion.js.map +1 -0
- package/dist/worker/WorkerPoolManager.d.ts +33 -5
- package/dist/worker/WorkerPoolManager.d.ts.map +1 -1
- package/dist/worker/WorkerPoolManager.js +278 -79
- package/dist/worker/WorkerPoolManager.js.map +1 -1
- package/dist/worker/types.d.ts +7 -3
- package/dist/worker/types.d.ts.map +1 -1
- package/dist/worker/worker-portable.js +9 -10
- package/dist/worker/worker-portable.js.map +1 -1
- package/dist/worker/worker.js +3 -1
- package/dist/worker/worker.js.map +1 -1
- package/package.json +2 -6
- package/dist/utils/resolveVirtualFileMetrics.d.ts +0 -10
- package/dist/utils/resolveVirtualFileMetrics.d.ts.map +0 -1
- package/dist/utils/resolveVirtualFileMetrics.js.map +0 -1
|
@@ -1,26 +1,33 @@
|
|
|
1
|
-
import { ALTERNATE_FILE_NAMES_GIT, COMMIT_METADATA_SPLIT, FILENAME_HEADER_REGEX, FILENAME_HEADER_REGEX_GIT,
|
|
1
|
+
import { ALTERNATE_FILE_NAMES_GIT, COMMIT_METADATA_SPLIT, FILENAME_HEADER_REGEX, FILENAME_HEADER_REGEX_GIT, GIT_DIFF_FILE_BREAK_REGEX, INDEX_LINE_METADATA, UNIFIED_DIFF_FILE_BREAK_REGEX } from "../constants.js";
|
|
2
2
|
import { cleanLastNewline } from "./cleanLastNewline.js";
|
|
3
|
-
import {
|
|
3
|
+
import { detachString, releaseStringDetachBuffer } from "./detachString.js";
|
|
4
4
|
|
|
5
5
|
//#region src/utils/parsePatchFiles.ts
|
|
6
|
-
function processPatch(data, cacheKeyPrefix, throwOnError
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
function processPatch(data, cacheKeyPrefix, throwOnError) {
|
|
7
|
+
try {
|
|
8
|
+
return _processPatch(data, cacheKeyPrefix, throwOnError);
|
|
9
|
+
} finally {
|
|
10
|
+
releaseStringDetachBuffer();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function _processPatch(data, cacheKeyPrefix, throwOnError = false) {
|
|
14
|
+
const isGitDiff = isGitDiffPatch(data);
|
|
15
|
+
const rawFiles = isGitDiff ? splitAtLinePrefix(data, "diff --git") : data.split(UNIFIED_DIFF_FILE_BREAK_REGEX);
|
|
9
16
|
let patchMetadata;
|
|
10
17
|
const files = [];
|
|
11
18
|
for (const fileOrPatchMetadata of rawFiles) {
|
|
12
19
|
if (isGitDiff && !GIT_DIFF_FILE_BREAK_REGEX.test(fileOrPatchMetadata)) {
|
|
13
|
-
if (patchMetadata == null) patchMetadata = fileOrPatchMetadata;
|
|
20
|
+
if (patchMetadata == null) patchMetadata = detachString(fileOrPatchMetadata);
|
|
14
21
|
else if (throwOnError) throw Error("parsePatchContent: unknown file blob");
|
|
15
22
|
else console.error("parsePatchContent: unknown file blob:", fileOrPatchMetadata);
|
|
16
23
|
continue;
|
|
17
24
|
} else if (!isGitDiff && !UNIFIED_DIFF_FILE_BREAK_REGEX.test(fileOrPatchMetadata)) {
|
|
18
|
-
if (patchMetadata == null) patchMetadata = fileOrPatchMetadata;
|
|
25
|
+
if (patchMetadata == null) patchMetadata = detachString(fileOrPatchMetadata);
|
|
19
26
|
else if (throwOnError) throw Error("parsePatchContent: unknown file blob");
|
|
20
27
|
else console.error("parsePatchContent: unknown file blob:", fileOrPatchMetadata);
|
|
21
28
|
continue;
|
|
22
29
|
}
|
|
23
|
-
const currentFile =
|
|
30
|
+
const currentFile = _processFile(fileOrPatchMetadata, {
|
|
24
31
|
cacheKey: cacheKeyPrefix != null ? `${cacheKeyPrefix}-${files.length}` : void 0,
|
|
25
32
|
isGitDiff,
|
|
26
33
|
throwOnError
|
|
@@ -32,25 +39,32 @@ function processPatch(data, cacheKeyPrefix, throwOnError = false) {
|
|
|
32
39
|
files
|
|
33
40
|
};
|
|
34
41
|
}
|
|
35
|
-
function processFile(fileDiffString,
|
|
42
|
+
function processFile(fileDiffString, options) {
|
|
43
|
+
try {
|
|
44
|
+
return _processFile(fileDiffString, options);
|
|
45
|
+
} finally {
|
|
46
|
+
releaseStringDetachBuffer();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function _processFile(fileDiffString, { cacheKey, isGitDiff = GIT_DIFF_FILE_BREAK_REGEX.test(fileDiffString), oldFile, newFile, throwOnError = false } = {}) {
|
|
36
50
|
let lastHunkEnd = 0;
|
|
37
|
-
const hunks = fileDiffString
|
|
51
|
+
const hunks = splitAtLinePrefix(fileDiffString, "@@ ");
|
|
38
52
|
let currentFile;
|
|
39
53
|
const isPartial = oldFile == null || newFile == null;
|
|
40
54
|
let deletionLineIndex = 0;
|
|
41
55
|
let additionLineIndex = 0;
|
|
42
56
|
for (const hunk of hunks) {
|
|
43
|
-
const lines = hunk
|
|
44
|
-
const firstLine = lines
|
|
57
|
+
const lines = splitWithNewlines(hunk);
|
|
58
|
+
const firstLine = lines[0];
|
|
45
59
|
if (firstLine == null) {
|
|
46
60
|
if (throwOnError) throw Error("parsePatchContent: invalid hunk");
|
|
47
61
|
else console.error("parsePatchContent: invalid hunk", hunk);
|
|
48
62
|
continue;
|
|
49
63
|
}
|
|
50
|
-
const
|
|
64
|
+
const fileHeader = parseHunkHeader(firstLine);
|
|
51
65
|
let additionLines = 0;
|
|
52
66
|
let deletionLines = 0;
|
|
53
|
-
if (
|
|
67
|
+
if (fileHeader == null || currentFile == null) {
|
|
54
68
|
if (currentFile != null) {
|
|
55
69
|
if (throwOnError) throw Error("parsePatchContent: Invalid hunk");
|
|
56
70
|
else console.error("parsePatchContent: Invalid hunk", hunk);
|
|
@@ -63,46 +77,48 @@ function processFile(fileDiffString, { cacheKey, isGitDiff = GIT_DIFF_FILE_BREAK
|
|
|
63
77
|
splitLineCount: 0,
|
|
64
78
|
unifiedLineCount: 0,
|
|
65
79
|
isPartial,
|
|
66
|
-
additionLines: !isPartial && oldFile != null && newFile != null ? newFile.contents
|
|
67
|
-
deletionLines: !isPartial && oldFile != null && newFile != null ? oldFile.contents
|
|
68
|
-
cacheKey
|
|
80
|
+
additionLines: !isPartial && oldFile != null && newFile != null ? splitFileContents(newFile.contents) : [],
|
|
81
|
+
deletionLines: !isPartial && oldFile != null && newFile != null ? splitFileContents(oldFile.contents) : [],
|
|
82
|
+
cacheKey: maybeDetachOptionalString(cacheKey)
|
|
69
83
|
};
|
|
70
84
|
if (currentFile.additionLines.length === 1 && newFile?.contents === "") currentFile.additionLines.length = 0;
|
|
71
85
|
if (currentFile.deletionLines.length === 1 && oldFile?.contents === "") currentFile.deletionLines.length = 0;
|
|
72
|
-
lines.unshift(firstLine);
|
|
73
86
|
for (const line of lines) {
|
|
74
|
-
const filenameMatch = line.match(isGitDiff ? FILENAME_HEADER_REGEX_GIT : FILENAME_HEADER_REGEX);
|
|
75
87
|
if (line.startsWith("diff --git")) {
|
|
76
88
|
const [, , prevName, , name] = line.trim().match(ALTERNATE_FILE_NAMES_GIT) ?? [];
|
|
77
|
-
currentFile.name = name.trim();
|
|
78
|
-
if (prevName !== name) currentFile.prevName = prevName.trim();
|
|
79
|
-
|
|
89
|
+
currentFile.name = detachString(name.trim());
|
|
90
|
+
if (prevName !== name) currentFile.prevName = detachString(prevName.trim());
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const filenameMatch = line.startsWith("---") || line.startsWith("+++") ? line.match(isGitDiff ? FILENAME_HEADER_REGEX_GIT : FILENAME_HEADER_REGEX) : null;
|
|
94
|
+
if (filenameMatch != null) {
|
|
80
95
|
const [, type, fileName] = filenameMatch;
|
|
81
96
|
if (type === "---" && fileName !== "/dev/null") {
|
|
82
|
-
|
|
83
|
-
currentFile.
|
|
84
|
-
|
|
97
|
+
const detachedFileName = detachString(fileName.trim());
|
|
98
|
+
currentFile.prevName = detachedFileName;
|
|
99
|
+
currentFile.name = detachedFileName;
|
|
100
|
+
} else if (type === "+++" && fileName !== "/dev/null") currentFile.name = detachString(fileName.trim());
|
|
85
101
|
} else if (isGitDiff) {
|
|
86
|
-
if (line.startsWith("new mode ")) currentFile.mode = line.
|
|
87
|
-
if (line.startsWith("old mode ")) currentFile.prevMode = line.
|
|
102
|
+
if (line.startsWith("new mode ")) currentFile.mode = detachString(line.slice(8).trim());
|
|
103
|
+
if (line.startsWith("old mode ")) currentFile.prevMode = detachString(line.slice(8).trim());
|
|
88
104
|
if (line.startsWith("new file mode")) {
|
|
89
105
|
currentFile.type = "new";
|
|
90
|
-
currentFile.mode = line.
|
|
106
|
+
currentFile.mode = detachString(line.slice(13).trim());
|
|
91
107
|
}
|
|
92
108
|
if (line.startsWith("deleted file mode")) {
|
|
93
109
|
currentFile.type = "deleted";
|
|
94
|
-
currentFile.mode = line.
|
|
110
|
+
currentFile.mode = detachString(line.slice(17).trim());
|
|
95
111
|
}
|
|
96
112
|
if (line.startsWith("similarity index")) if (line.startsWith("similarity index 100%")) currentFile.type = "rename-pure";
|
|
97
113
|
else currentFile.type = "rename-changed";
|
|
98
114
|
if (line.startsWith("index ")) {
|
|
99
115
|
const [, prevObjectId, newObjectId, mode] = line.trim().match(INDEX_LINE_METADATA) ?? [];
|
|
100
|
-
if (prevObjectId != null) currentFile.prevObjectId = prevObjectId;
|
|
101
|
-
if (newObjectId != null) currentFile.newObjectId = newObjectId;
|
|
102
|
-
if (mode != null) currentFile.mode = mode;
|
|
116
|
+
if (prevObjectId != null) currentFile.prevObjectId = detachString(prevObjectId);
|
|
117
|
+
if (newObjectId != null) currentFile.newObjectId = detachString(newObjectId);
|
|
118
|
+
if (mode != null) currentFile.mode = detachString(mode);
|
|
103
119
|
}
|
|
104
|
-
if (line.startsWith("rename from ")) currentFile.prevName = line.
|
|
105
|
-
if (line.startsWith("rename to ")) currentFile.name = line.
|
|
120
|
+
if (line.startsWith("rename from ")) currentFile.prevName = detachString(line.slice(12).trim());
|
|
121
|
+
if (line.startsWith("rename to ")) currentFile.name = detachString(line.slice(10).trim());
|
|
106
122
|
}
|
|
107
123
|
}
|
|
108
124
|
continue;
|
|
@@ -110,8 +126,7 @@ function processFile(fileDiffString, { cacheKey, isGitDiff = GIT_DIFF_FILE_BREAK
|
|
|
110
126
|
let currentContent;
|
|
111
127
|
let lastLineType;
|
|
112
128
|
while (lines.length > 0 && (lines[lines.length - 1] === "\n" || lines[lines.length - 1] === "\r" || lines[lines.length - 1] === "\r\n" || lines[lines.length - 1] === "")) lines.pop();
|
|
113
|
-
const additionStart =
|
|
114
|
-
const deletionStart = parseInt(fileHeaderMatch[1]);
|
|
129
|
+
const { additionStart, deletionStart } = fileHeader;
|
|
115
130
|
deletionLineIndex = isPartial ? deletionLineIndex : deletionStart - 1;
|
|
116
131
|
additionLineIndex = isPartial ? additionLineIndex : additionStart - 1;
|
|
117
132
|
const hunkData = {
|
|
@@ -120,41 +135,34 @@ function processFile(fileDiffString, { cacheKey, isGitDiff = GIT_DIFF_FILE_BREAK
|
|
|
120
135
|
splitLineStart: 0,
|
|
121
136
|
unifiedLineCount: 0,
|
|
122
137
|
unifiedLineStart: 0,
|
|
123
|
-
additionCount:
|
|
138
|
+
additionCount: fileHeader.additionCount,
|
|
124
139
|
additionStart,
|
|
125
140
|
additionLines,
|
|
126
|
-
deletionCount:
|
|
141
|
+
deletionCount: fileHeader.deletionCount,
|
|
127
142
|
deletionStart,
|
|
128
143
|
deletionLines,
|
|
129
144
|
deletionLineIndex,
|
|
130
145
|
additionLineIndex,
|
|
131
146
|
hunkContent: [],
|
|
132
|
-
hunkContext:
|
|
133
|
-
hunkSpecs: firstLine,
|
|
147
|
+
hunkContext: maybeDetachOptionalString(fileHeader.hunkContext),
|
|
148
|
+
hunkSpecs: detachString(firstLine),
|
|
134
149
|
noEOFCRAdditions: false,
|
|
135
150
|
noEOFCRDeletions: false
|
|
136
151
|
};
|
|
137
|
-
if (isNaN(hunkData.additionCount) || isNaN(hunkData.deletionCount) || isNaN(hunkData.additionStart) || isNaN(hunkData.deletionStart)) {
|
|
138
|
-
if (throwOnError) throw Error("parsePatchContent: invalid hunk metadata");
|
|
139
|
-
else console.error("parsePatchContent: invalid hunk metadata", hunkData);
|
|
140
|
-
continue;
|
|
141
|
-
}
|
|
142
152
|
let parsedAdditionLines = 0;
|
|
143
153
|
let parsedDeletionLines = 0;
|
|
144
|
-
for (
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}) && !rawLine.startsWith("\\")) break;
|
|
151
|
-
const parsedLine = parseLineType(rawLine);
|
|
152
|
-
if (parsedLine == null) {
|
|
154
|
+
for (let lineIndex = 1; lineIndex < lines.length; lineIndex++) {
|
|
155
|
+
const rawLine = lines[lineIndex];
|
|
156
|
+
if (parsedAdditionLines >= hunkData.additionCount && parsedDeletionLines >= hunkData.deletionCount && !rawLine.startsWith("\\")) break;
|
|
157
|
+
const firstChar = rawLine[0];
|
|
158
|
+
if (firstChar !== "+" && firstChar !== "-" && firstChar !== " " && firstChar !== "\\") {
|
|
159
|
+
console.error(`parseLineType: Invalid firstChar: "${firstChar}", full line: "${rawLine}"`);
|
|
153
160
|
console.error("processFile: invalid rawLine:", rawLine);
|
|
154
161
|
continue;
|
|
155
162
|
}
|
|
156
|
-
const
|
|
163
|
+
const type = parseRawLineType(firstChar);
|
|
157
164
|
if (type === "addition") {
|
|
165
|
+
const line = getParsedLineContent(rawLine);
|
|
158
166
|
if (currentContent == null || currentContent.type !== "change") {
|
|
159
167
|
currentContent = createContentGroup("change", deletionLineIndex, additionLineIndex);
|
|
160
168
|
hunkData.hunkContent.push(currentContent);
|
|
@@ -166,6 +174,7 @@ function processFile(fileDiffString, { cacheKey, isGitDiff = GIT_DIFF_FILE_BREAK
|
|
|
166
174
|
additionLines++;
|
|
167
175
|
lastLineType = "addition";
|
|
168
176
|
} else if (type === "deletion") {
|
|
177
|
+
const line = getParsedLineContent(rawLine);
|
|
169
178
|
if (currentContent == null || currentContent.type !== "change") {
|
|
170
179
|
currentContent = createContentGroup("change", deletionLineIndex, additionLineIndex);
|
|
171
180
|
hunkData.hunkContent.push(currentContent);
|
|
@@ -177,6 +186,7 @@ function processFile(fileDiffString, { cacheKey, isGitDiff = GIT_DIFF_FILE_BREAK
|
|
|
177
186
|
deletionLines++;
|
|
178
187
|
lastLineType = "deletion";
|
|
179
188
|
} else if (type === "context") {
|
|
189
|
+
const line = getParsedLineContent(rawLine);
|
|
180
190
|
if (currentContent == null || currentContent.type !== "context") {
|
|
181
191
|
currentContent = createContentGroup("context", deletionLineIndex, additionLineIndex);
|
|
182
192
|
hunkData.hunkContent.push(currentContent);
|
|
@@ -242,9 +252,6 @@ function processFile(fileDiffString, { cacheKey, isGitDiff = GIT_DIFF_FILE_BREAK
|
|
|
242
252
|
if (currentFile.type !== "rename-pure" && currentFile.type !== "rename-changed") currentFile.prevName = void 0;
|
|
243
253
|
return currentFile;
|
|
244
254
|
}
|
|
245
|
-
function isHunkBodyComplete({ additionCount, parsedAdditionLines, deletionCount, parsedDeletionLines }) {
|
|
246
|
-
return parsedAdditionLines >= additionCount && parsedDeletionLines >= deletionCount;
|
|
247
|
-
}
|
|
248
255
|
/**
|
|
249
256
|
* Parses a patch file string into an array of parsed patches.
|
|
250
257
|
*
|
|
@@ -255,7 +262,8 @@ function isHunkBodyComplete({ additionCount, parsedAdditionLines, deletionCount,
|
|
|
255
262
|
*/
|
|
256
263
|
function parsePatchFiles(data, cacheKeyPrefix, throwOnError = false) {
|
|
257
264
|
const patches = [];
|
|
258
|
-
|
|
265
|
+
const rawPatches = hasCommitMetadataBoundary(data) ? data.split(COMMIT_METADATA_SPLIT) : [data];
|
|
266
|
+
for (const patch of rawPatches) try {
|
|
259
267
|
patches.push(processPatch(patch, cacheKeyPrefix != null ? `${cacheKeyPrefix}-${patches.length}` : void 0, throwOnError));
|
|
260
268
|
} catch (error) {
|
|
261
269
|
if (throwOnError) throw error;
|
|
@@ -263,6 +271,119 @@ function parsePatchFiles(data, cacheKeyPrefix, throwOnError = false) {
|
|
|
263
271
|
}
|
|
264
272
|
return patches;
|
|
265
273
|
}
|
|
274
|
+
function hasCommitMetadataBoundary(data) {
|
|
275
|
+
return data.startsWith("From ") || data.includes("\nFrom ");
|
|
276
|
+
}
|
|
277
|
+
function splitFileContents(contents) {
|
|
278
|
+
const lines = splitWithNewlines(contents);
|
|
279
|
+
for (let index = 0; index < lines.length; index++) lines[index] = detachString(lines[index]);
|
|
280
|
+
return lines;
|
|
281
|
+
}
|
|
282
|
+
function splitWithNewlines(contents) {
|
|
283
|
+
if (contents.length === 0) return [""];
|
|
284
|
+
const lines = [];
|
|
285
|
+
let startIndex = 0;
|
|
286
|
+
for (;;) {
|
|
287
|
+
const newlineIndex = contents.indexOf("\n", startIndex);
|
|
288
|
+
if (newlineIndex === -1) break;
|
|
289
|
+
lines.push(contents.slice(startIndex, newlineIndex + 1));
|
|
290
|
+
startIndex = newlineIndex + 1;
|
|
291
|
+
}
|
|
292
|
+
if (startIndex < contents.length) lines.push(contents.slice(startIndex));
|
|
293
|
+
return lines;
|
|
294
|
+
}
|
|
295
|
+
function parseHunkHeader(line) {
|
|
296
|
+
if (!line.startsWith("@@ -")) return;
|
|
297
|
+
let index = 4;
|
|
298
|
+
const deletionStartResult = readPositiveInteger(line, index);
|
|
299
|
+
if (deletionStartResult == null) return;
|
|
300
|
+
const deletionStart = deletionStartResult.value;
|
|
301
|
+
index = deletionStartResult.endIndex;
|
|
302
|
+
let deletionCount = 1;
|
|
303
|
+
if (line[index] === ",") {
|
|
304
|
+
const deletionCountResult = readPositiveInteger(line, index + 1);
|
|
305
|
+
if (deletionCountResult == null) return;
|
|
306
|
+
deletionCount = deletionCountResult.value;
|
|
307
|
+
index = deletionCountResult.endIndex;
|
|
308
|
+
}
|
|
309
|
+
if (line[index] !== " " || line[index + 1] !== "+") return;
|
|
310
|
+
index += 2;
|
|
311
|
+
const additionStartResult = readPositiveInteger(line, index);
|
|
312
|
+
if (additionStartResult == null) return;
|
|
313
|
+
const additionStart = additionStartResult.value;
|
|
314
|
+
index = additionStartResult.endIndex;
|
|
315
|
+
let additionCount = 1;
|
|
316
|
+
if (line[index] === ",") {
|
|
317
|
+
const additionCountResult = readPositiveInteger(line, index + 1);
|
|
318
|
+
if (additionCountResult == null) return;
|
|
319
|
+
additionCount = additionCountResult.value;
|
|
320
|
+
index = additionCountResult.endIndex;
|
|
321
|
+
}
|
|
322
|
+
if (line[index] !== " " || line[index + 1] !== "@" || line[index + 2] !== "@") return;
|
|
323
|
+
let hunkContext;
|
|
324
|
+
const contextStartIndex = index + 3;
|
|
325
|
+
if (line[contextStartIndex] === " ") hunkContext = trimLineEnd(line.slice(contextStartIndex + 1));
|
|
326
|
+
return {
|
|
327
|
+
additionCount,
|
|
328
|
+
additionStart,
|
|
329
|
+
deletionCount,
|
|
330
|
+
deletionStart,
|
|
331
|
+
hunkContext
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
function readPositiveInteger(value, startIndex) {
|
|
335
|
+
let index = startIndex;
|
|
336
|
+
let parsedValue = 0;
|
|
337
|
+
for (; index < value.length; index++) {
|
|
338
|
+
const digit = value.charCodeAt(index) - 48;
|
|
339
|
+
if (digit < 0 || digit > 9) break;
|
|
340
|
+
parsedValue = parsedValue * 10 + digit;
|
|
341
|
+
}
|
|
342
|
+
if (index === startIndex) return;
|
|
343
|
+
return {
|
|
344
|
+
value: parsedValue,
|
|
345
|
+
endIndex: index
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
function trimLineEnd(value) {
|
|
349
|
+
if (value.endsWith("\r\n")) return value.slice(0, -2);
|
|
350
|
+
if (value.endsWith("\n")) return value.slice(0, -1);
|
|
351
|
+
return value;
|
|
352
|
+
}
|
|
353
|
+
function isGitDiffPatch(data) {
|
|
354
|
+
return data.startsWith("diff --git") || data.includes("\ndiff --git");
|
|
355
|
+
}
|
|
356
|
+
function splitAtLinePrefix(contents, prefix) {
|
|
357
|
+
if (contents.length === 0) return [""];
|
|
358
|
+
const newlinePrefix = `\n${prefix}`;
|
|
359
|
+
const firstBoundaryIndex = contents.startsWith(prefix) ? 0 : findLinePrefixIndex(contents, newlinePrefix, 0);
|
|
360
|
+
if (firstBoundaryIndex === -1) return [contents];
|
|
361
|
+
const parts = [];
|
|
362
|
+
if (firstBoundaryIndex > 0) parts.push(contents.slice(0, firstBoundaryIndex));
|
|
363
|
+
let startIndex = firstBoundaryIndex;
|
|
364
|
+
for (;;) {
|
|
365
|
+
const nextBoundaryIndex = findLinePrefixIndex(contents, newlinePrefix, startIndex + 1);
|
|
366
|
+
if (nextBoundaryIndex === -1) break;
|
|
367
|
+
parts.push(contents.slice(startIndex, nextBoundaryIndex));
|
|
368
|
+
startIndex = nextBoundaryIndex;
|
|
369
|
+
}
|
|
370
|
+
parts.push(contents.slice(startIndex));
|
|
371
|
+
return parts;
|
|
372
|
+
}
|
|
373
|
+
function findLinePrefixIndex(contents, newlinePrefix, fromIndex) {
|
|
374
|
+
const index = contents.indexOf(newlinePrefix, fromIndex);
|
|
375
|
+
return index === -1 ? -1 : index + 1;
|
|
376
|
+
}
|
|
377
|
+
function maybeDetachOptionalString(value) {
|
|
378
|
+
return value == null ? value : detachString(value);
|
|
379
|
+
}
|
|
380
|
+
function parseRawLineType(firstChar) {
|
|
381
|
+
return firstChar === " " ? "context" : firstChar === "\\" ? "metadata" : firstChar === "+" ? "addition" : "deletion";
|
|
382
|
+
}
|
|
383
|
+
function getParsedLineContent(rawLine) {
|
|
384
|
+
const processedLine = rawLine.slice(1);
|
|
385
|
+
return detachString(processedLine === "" ? "\n" : processedLine);
|
|
386
|
+
}
|
|
266
387
|
function createContentGroup(type, deletionLineIndex, additionLineIndex) {
|
|
267
388
|
if (type === "change") return {
|
|
268
389
|
type: "change",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parsePatchFiles.js","names":["patchMetadata: string | undefined","files: FileDiffMetadata[]","currentFile: FileDiffMetadata | undefined","currentContent: ContextContent | ChangeContent | undefined","lastLineType: 'context' | 'addition' | 'deletion' | undefined","hunkData: Hunk","lastHunkEnd","patches: ParsedPatch[]"],"sources":["../../src/utils/parsePatchFiles.ts"],"sourcesContent":["import {\n ALTERNATE_FILE_NAMES_GIT,\n COMMIT_METADATA_SPLIT,\n FILE_CONTEXT_BLOB,\n FILENAME_HEADER_REGEX,\n FILENAME_HEADER_REGEX_GIT,\n GIT_DIFF_FILE_BREAK_REGEX,\n HUNK_HEADER,\n INDEX_LINE_METADATA,\n SPLIT_WITH_NEWLINES,\n UNIFIED_DIFF_FILE_BREAK_REGEX,\n} from '../constants';\nimport type {\n ChangeContent,\n ContextContent,\n FileContents,\n FileDiffMetadata,\n Hunk,\n ParsedPatch,\n} from '../types';\nimport { cleanLastNewline } from './cleanLastNewline';\nimport { parseLineType } from './parseLineType';\n\nexport function processPatch(\n data: string,\n cacheKeyPrefix?: string,\n throwOnError = false\n): ParsedPatch {\n const isGitDiff = GIT_DIFF_FILE_BREAK_REGEX.test(data);\n const rawFiles = data.split(\n isGitDiff ? GIT_DIFF_FILE_BREAK_REGEX : UNIFIED_DIFF_FILE_BREAK_REGEX\n );\n let patchMetadata: string | undefined;\n const files: FileDiffMetadata[] = [];\n for (const fileOrPatchMetadata of rawFiles) {\n if (isGitDiff && !GIT_DIFF_FILE_BREAK_REGEX.test(fileOrPatchMetadata)) {\n if (patchMetadata == null) {\n patchMetadata = fileOrPatchMetadata;\n } else {\n if (throwOnError) {\n throw Error('parsePatchContent: unknown file blob');\n } else {\n console.error(\n 'parsePatchContent: unknown file blob:',\n fileOrPatchMetadata\n );\n }\n }\n // If we get in here, it's most likely the introductory metadata from the\n // patch, or something is fucked with the diff format\n continue;\n } else if (\n !isGitDiff &&\n !UNIFIED_DIFF_FILE_BREAK_REGEX.test(fileOrPatchMetadata)\n ) {\n if (patchMetadata == null) {\n patchMetadata = fileOrPatchMetadata;\n } else {\n if (throwOnError) {\n throw Error('parsePatchContent: unknown file blob');\n } else {\n console.error(\n 'parsePatchContent: unknown file blob:',\n fileOrPatchMetadata\n );\n }\n }\n continue;\n }\n const currentFile = processFile(fileOrPatchMetadata, {\n cacheKey:\n cacheKeyPrefix != null\n ? `${cacheKeyPrefix}-${files.length}`\n : undefined,\n isGitDiff,\n throwOnError,\n });\n if (currentFile != null) {\n files.push(currentFile);\n }\n }\n return { patchMetadata, files };\n}\n\ninterface ProcessFileOptions {\n cacheKey?: string;\n isGitDiff?: boolean;\n oldFile?: FileContents;\n newFile?: FileContents;\n throwOnError?: boolean;\n}\n\nexport function processFile(\n fileDiffString: string,\n {\n cacheKey,\n isGitDiff = GIT_DIFF_FILE_BREAK_REGEX.test(fileDiffString),\n oldFile,\n newFile,\n throwOnError = false,\n }: ProcessFileOptions = {}\n): FileDiffMetadata | undefined {\n let lastHunkEnd = 0;\n const hunks = fileDiffString.split(FILE_CONTEXT_BLOB);\n let currentFile: FileDiffMetadata | undefined;\n const isPartial = oldFile == null || newFile == null;\n let deletionLineIndex = 0;\n let additionLineIndex = 0;\n for (const hunk of hunks) {\n const lines = hunk.split(SPLIT_WITH_NEWLINES);\n const firstLine = lines.shift();\n if (firstLine == null) {\n if (throwOnError) {\n throw Error('parsePatchContent: invalid hunk');\n } else {\n console.error('parsePatchContent: invalid hunk', hunk);\n }\n continue;\n }\n const fileHeaderMatch = firstLine.match(HUNK_HEADER);\n let additionLines = 0;\n let deletionLines = 0;\n // Setup currentFile, this should be the first iteration of our hunks, and\n // technically not a hunk\n if (fileHeaderMatch == null || currentFile == null) {\n if (currentFile != null) {\n if (throwOnError) {\n throw Error('parsePatchContent: Invalid hunk');\n } else {\n console.error('parsePatchContent: Invalid hunk', hunk);\n }\n continue;\n }\n currentFile = {\n name: '',\n type: 'change',\n hunks: [],\n splitLineCount: 0,\n unifiedLineCount: 0,\n isPartial,\n additionLines:\n !isPartial && oldFile != null && newFile != null\n ? newFile.contents.split(SPLIT_WITH_NEWLINES)\n : [],\n deletionLines:\n !isPartial && oldFile != null && newFile != null\n ? oldFile.contents.split(SPLIT_WITH_NEWLINES)\n : [],\n cacheKey,\n };\n // If either file is technically empty, then we should empty the\n // arrays respectively\n if (currentFile.additionLines.length === 1 && newFile?.contents === '') {\n currentFile.additionLines.length = 0;\n }\n if (currentFile.deletionLines.length === 1 && oldFile?.contents === '') {\n currentFile.deletionLines.length = 0;\n }\n\n // Push that first line back into the group of lines so we can properly\n // parse it out\n lines.unshift(firstLine);\n for (const line of lines) {\n const filenameMatch = line.match(\n isGitDiff ? FILENAME_HEADER_REGEX_GIT : FILENAME_HEADER_REGEX\n );\n if (line.startsWith('diff --git')) {\n const [, , prevName, , name] =\n line.trim().match(ALTERNATE_FILE_NAMES_GIT) ?? [];\n currentFile.name = name.trim();\n if (prevName !== name) {\n currentFile.prevName = prevName.trim();\n }\n } else if (filenameMatch != null) {\n const [, type, fileName] = filenameMatch;\n if (type === '---' && fileName !== '/dev/null') {\n currentFile.prevName = fileName.trim();\n currentFile.name = fileName.trim();\n } else if (type === '+++' && fileName !== '/dev/null') {\n currentFile.name = fileName.trim();\n }\n }\n // Git diffs have a bunch of additional metadata we can pull from\n else if (isGitDiff) {\n if (line.startsWith('new mode ')) {\n currentFile.mode = line.replace('new mode', '').trim();\n }\n if (line.startsWith('old mode ')) {\n currentFile.prevMode = line.replace('old mode', '').trim();\n }\n if (line.startsWith('new file mode')) {\n currentFile.type = 'new';\n currentFile.mode = line.replace('new file mode', '').trim();\n }\n if (line.startsWith('deleted file mode')) {\n currentFile.type = 'deleted';\n currentFile.mode = line.replace('deleted file mode', '').trim();\n }\n if (line.startsWith('similarity index')) {\n if (line.startsWith('similarity index 100%')) {\n currentFile.type = 'rename-pure';\n } else {\n currentFile.type = 'rename-changed';\n }\n }\n if (line.startsWith('index ')) {\n const [, prevObjectId, newObjectId, mode] =\n line.trim().match(INDEX_LINE_METADATA) ?? [];\n if (prevObjectId != null) {\n currentFile.prevObjectId = prevObjectId;\n }\n if (newObjectId != null) {\n currentFile.newObjectId = newObjectId;\n }\n if (mode != null) {\n currentFile.mode = mode;\n }\n }\n // We have to handle these for pure renames because there won't be\n // --- and +++ lines\n if (line.startsWith('rename from ')) {\n currentFile.prevName = line.replace('rename from ', '').trim();\n }\n if (line.startsWith('rename to ')) {\n currentFile.name = line.replace('rename to ', '').trim();\n }\n }\n }\n continue;\n }\n\n // Otherwise, time to start parsing out the hunk\n let currentContent: ContextContent | ChangeContent | undefined;\n let lastLineType: 'context' | 'addition' | 'deletion' | undefined;\n\n // Strip trailing bare newlines (format-patch separators between commits)\n // if needed\n while (\n lines.length > 0 &&\n (lines[lines.length - 1] === '\\n' ||\n lines[lines.length - 1] === '\\r' ||\n lines[lines.length - 1] === '\\r\\n' ||\n lines[lines.length - 1] === '')\n ) {\n lines.pop();\n }\n\n const additionStart = parseInt(fileHeaderMatch[3]);\n const deletionStart = parseInt(fileHeaderMatch[1]);\n deletionLineIndex = isPartial ? deletionLineIndex : deletionStart - 1;\n additionLineIndex = isPartial ? additionLineIndex : additionStart - 1;\n\n const hunkData: Hunk = {\n collapsedBefore: 0,\n\n splitLineCount: 0,\n splitLineStart: 0,\n\n unifiedLineCount: 0,\n unifiedLineStart: 0,\n\n additionCount: parseInt(fileHeaderMatch[4] ?? '1'),\n additionStart,\n additionLines,\n\n deletionCount: parseInt(fileHeaderMatch[2] ?? '1'),\n deletionStart,\n deletionLines,\n\n deletionLineIndex,\n additionLineIndex,\n\n hunkContent: [],\n hunkContext: fileHeaderMatch[5],\n hunkSpecs: firstLine,\n\n noEOFCRAdditions: false,\n noEOFCRDeletions: false,\n };\n\n // Lets validate out hunkData to ensure there's no broken data from the\n // regex\n if (\n isNaN(hunkData.additionCount) ||\n isNaN(hunkData.deletionCount) ||\n isNaN(hunkData.additionStart) ||\n isNaN(hunkData.deletionStart)\n ) {\n if (throwOnError) {\n throw Error('parsePatchContent: invalid hunk metadata');\n } else {\n console.error('parsePatchContent: invalid hunk metadata', hunkData);\n }\n continue;\n }\n\n // Now we process each line of the hunk\n let parsedAdditionLines = 0;\n let parsedDeletionLines = 0;\n for (const rawLine of lines) {\n if (\n isHunkBodyComplete({\n additionCount: hunkData.additionCount,\n parsedAdditionLines,\n deletionCount: hunkData.deletionCount,\n parsedDeletionLines,\n }) &&\n !rawLine.startsWith('\\\\')\n ) {\n break;\n }\n\n const parsedLine = parseLineType(rawLine);\n // If we can't properly process the line, well, lets just try to salvage\n // things and continue... It's possible an AI generated diff might have\n // some stray blank lines or something in there\n if (parsedLine == null) {\n console.error('processFile: invalid rawLine:', rawLine);\n continue;\n }\n\n const { type, line } = parsedLine;\n if (type === 'addition') {\n if (currentContent == null || currentContent.type !== 'change') {\n currentContent = createContentGroup(\n 'change',\n deletionLineIndex,\n additionLineIndex\n );\n hunkData.hunkContent.push(currentContent);\n }\n additionLineIndex++;\n parsedAdditionLines++;\n if (isPartial) {\n currentFile.additionLines.push(line);\n }\n currentContent.additions++;\n additionLines++;\n lastLineType = 'addition';\n } else if (type === 'deletion') {\n if (currentContent == null || currentContent.type !== 'change') {\n currentContent = createContentGroup(\n 'change',\n deletionLineIndex,\n additionLineIndex\n );\n hunkData.hunkContent.push(currentContent);\n }\n deletionLineIndex++;\n parsedDeletionLines++;\n if (isPartial) {\n currentFile.deletionLines.push(line);\n }\n currentContent.deletions++;\n deletionLines++;\n lastLineType = 'deletion';\n } else if (type === 'context') {\n if (currentContent == null || currentContent.type !== 'context') {\n currentContent = createContentGroup(\n 'context',\n deletionLineIndex,\n additionLineIndex\n );\n hunkData.hunkContent.push(currentContent);\n }\n additionLineIndex++;\n deletionLineIndex++;\n parsedAdditionLines++;\n parsedDeletionLines++;\n if (isPartial) {\n currentFile.deletionLines.push(line);\n currentFile.additionLines.push(line);\n }\n currentContent.lines++;\n lastLineType = 'context';\n } else if (type === 'metadata' && currentContent != null) {\n if (currentContent.type === 'context') {\n hunkData.noEOFCRAdditions = true;\n hunkData.noEOFCRDeletions = true;\n } else if (lastLineType === 'deletion') {\n hunkData.noEOFCRDeletions = true;\n } else if (lastLineType === 'addition') {\n hunkData.noEOFCRAdditions = true;\n }\n // If we're dealing with partial content from a diff, we need to strip\n // newlines manually from the content\n if (\n isPartial &&\n (lastLineType === 'addition' || lastLineType === 'context')\n ) {\n const lastIndex = currentFile.additionLines.length - 1;\n if (lastIndex >= 0) {\n currentFile.additionLines[lastIndex] = cleanLastNewline(\n currentFile.additionLines[lastIndex]\n );\n }\n }\n if (\n isPartial &&\n (lastLineType === 'deletion' || lastLineType === 'context')\n ) {\n const lastIndex = currentFile.deletionLines.length - 1;\n if (lastIndex >= 0) {\n currentFile.deletionLines[lastIndex] = cleanLastNewline(\n currentFile.deletionLines[lastIndex]\n );\n }\n }\n }\n }\n\n hunkData.additionLines = additionLines;\n hunkData.deletionLines = deletionLines;\n\n hunkData.collapsedBefore = Math.max(\n hunkData.additionStart - 1 - lastHunkEnd,\n 0\n );\n currentFile.hunks.push(hunkData);\n lastHunkEnd = hunkData.additionStart + hunkData.additionCount - 1;\n for (const content of hunkData.hunkContent) {\n if (content.type === 'context') {\n hunkData.splitLineCount += content.lines;\n hunkData.unifiedLineCount += content.lines;\n } else {\n hunkData.splitLineCount += Math.max(\n content.additions,\n content.deletions\n );\n hunkData.unifiedLineCount += content.deletions + content.additions;\n }\n }\n hunkData.splitLineStart =\n currentFile.splitLineCount + hunkData.collapsedBefore;\n hunkData.unifiedLineStart =\n currentFile.unifiedLineCount + hunkData.collapsedBefore;\n\n currentFile.splitLineCount +=\n hunkData.collapsedBefore + hunkData.splitLineCount;\n currentFile.unifiedLineCount +=\n hunkData.collapsedBefore + hunkData.unifiedLineCount;\n }\n if (currentFile == null) {\n return undefined;\n }\n\n // Account for collapsed lines after the final hunk and increment the\n // split/unified counts properly\n if (\n currentFile.hunks.length > 0 &&\n !isPartial &&\n currentFile.additionLines.length > 0 &&\n currentFile.deletionLines.length > 0\n ) {\n const lastHunk = currentFile.hunks[currentFile.hunks.length - 1];\n const lastHunkEnd = lastHunk.additionStart + lastHunk.additionCount - 1;\n const totalFileLines = currentFile.additionLines.length;\n const collapsedAfter = Math.max(totalFileLines - lastHunkEnd, 0);\n currentFile.splitLineCount += collapsedAfter;\n currentFile.unifiedLineCount += collapsedAfter;\n }\n\n // If this isn't a git diff style patch, then we'll need to sus out some\n // additional metadata manually\n if (!isGitDiff) {\n if (\n currentFile.prevName != null &&\n currentFile.name !== currentFile.prevName\n ) {\n if (currentFile.hunks.length > 0) {\n currentFile.type = 'rename-changed';\n } else {\n currentFile.type = 'rename-pure';\n }\n }\n // Sort of a hack for detecting deleted/added files...\n else if (\n (oldFile == null || oldFile.contents === '') &&\n newFile != null &&\n newFile.contents !== ''\n ) {\n currentFile.type = 'new';\n } else if (\n oldFile != null &&\n oldFile.contents !== '' &&\n (newFile == null || newFile.contents === '')\n ) {\n currentFile.type = 'deleted';\n }\n }\n if (\n currentFile.type !== 'rename-pure' &&\n currentFile.type !== 'rename-changed'\n ) {\n currentFile.prevName = undefined;\n }\n return currentFile;\n}\n\ninterface isHunkBodyCompleteProps {\n additionCount: number;\n parsedAdditionLines: number;\n deletionCount: number;\n parsedDeletionLines: number;\n}\n\n// Git format-patch trailers follow the final hunk without a new diff marker.\n// Once both header-declared line counts are satisfied, the next non-metadata\n// line belongs to the surrounding patch file rather than the hunk body.\nfunction isHunkBodyComplete({\n additionCount,\n parsedAdditionLines,\n deletionCount,\n parsedDeletionLines,\n}: isHunkBodyCompleteProps): boolean {\n return (\n parsedAdditionLines >= additionCount && parsedDeletionLines >= deletionCount\n );\n}\n\n/**\n * Parses a patch file string into an array of parsed patches.\n *\n * @param data - The raw patch file content (supports multi-commit patches)\n * @param cacheKeyPrefix - Optional prefix for generating cache keys. When provided,\n * each file in the patch will get a cache key in the format `prefix-patchIndex-fileIndex`.\n * This enables caching of rendered diff results in the worker pool.\n */\nexport function parsePatchFiles(\n data: string,\n cacheKeyPrefix?: string,\n throwOnError = false\n): ParsedPatch[] {\n // NOTE(amadeus): This function is pretty forgiving in that it can accept a\n // patch file that includes commit metdata, multiple commits, or not\n const patches: ParsedPatch[] = [];\n for (const patch of data.split(COMMIT_METADATA_SPLIT)) {\n try {\n patches.push(\n processPatch(\n patch,\n cacheKeyPrefix != null\n ? `${cacheKeyPrefix}-${patches.length}`\n : undefined,\n throwOnError\n )\n );\n } catch (error) {\n if (throwOnError) {\n throw error;\n } else {\n console.error(error);\n }\n }\n }\n return patches;\n}\n\nfunction createContentGroup(\n type: 'change',\n deletionLineIndex: number,\n additionLineIndex: number\n): ChangeContent;\nfunction createContentGroup(\n type: 'context',\n deletionLineIndex: number,\n additionLineIndex: number\n): ContextContent;\nfunction createContentGroup(\n type: 'change' | 'context',\n deletionLineIndex: number,\n additionLineIndex: number\n): ChangeContent | ContextContent {\n if (type === 'change') {\n return {\n type: 'change',\n additions: 0,\n deletions: 0,\n additionLineIndex,\n deletionLineIndex,\n };\n }\n return {\n type: 'context',\n lines: 0,\n additionLineIndex,\n deletionLineIndex,\n };\n}\n"],"mappings":";;;;;AAuBA,SAAgB,aACd,MACA,gBACA,eAAe,OACF;CACb,MAAM,YAAY,0BAA0B,KAAK,KAAK;CACtD,MAAM,WAAW,KAAK,MACpB,YAAY,4BAA4B,8BACzC;CACD,IAAIA;CACJ,MAAMC,QAA4B,EAAE;AACpC,MAAK,MAAM,uBAAuB,UAAU;AAC1C,MAAI,aAAa,CAAC,0BAA0B,KAAK,oBAAoB,EAAE;AACrE,OAAI,iBAAiB,KACnB,iBAAgB;YAEZ,aACF,OAAM,MAAM,uCAAuC;OAEnD,SAAQ,MACN,yCACA,oBACD;AAKL;aAEA,CAAC,aACD,CAAC,8BAA8B,KAAK,oBAAoB,EACxD;AACA,OAAI,iBAAiB,KACnB,iBAAgB;YAEZ,aACF,OAAM,MAAM,uCAAuC;OAEnD,SAAQ,MACN,yCACA,oBACD;AAGL;;EAEF,MAAM,cAAc,YAAY,qBAAqB;GACnD,UACE,kBAAkB,OACd,GAAG,eAAe,GAAG,MAAM,WAC3B;GACN;GACA;GACD,CAAC;AACF,MAAI,eA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Y,SAAS,iBACrB,YAAY,SAAS,iBAErB,aAAY,WAAW;AAEzB,QAAO;;AAaT,SAAS,mBAAmB,EAC1B,eACA,qBACA,eACA,uBACmC;AACnC,QACE,uBAAuB,iBAAiB,uBAAuB;;;;;;;;;;AAYnE,SAAgB,gBACd,MACA,gBACA,eAAe,OACA;CAGf,MAAMC,UAAyB,EAAE;AACjC,MAAK,MAAM,SAAS,KAAK,MAAM,sBAAsB,CACnD,KAAI;AACF,UAAQ,KACN,aACE,OACA,kBAAkB,OACd,GAAG,eAAe,GAAG,QAAQ,WAC7B,QACJ,aACD,CACF;UACM,OAAO;AACd,MAAI,aACF,OAAM;MAEN,SAAQ,MAAM,MAAM;;AAI1B,QAAO;;AAaT,SAAS,mBACP,MACA,mBACA,mBACgC;AAChC,KAAI,SAAS,SACX,QAAO;EACL,MAAM;EACN,WAAW;EACX,WAAW;EACX;EACA;EACD;AAEH,QAAO;EACL,MAAM;EACN,OAAO;EACP;EACA;EACD"}
|
|
1
|
+
{"version":3,"file":"parsePatchFiles.js","names":["patchMetadata: string | undefined","files: FileDiffMetadata[]","currentFile: FileDiffMetadata | undefined","currentContent: ContextContent | ChangeContent | undefined","lastLineType: 'context' | 'addition' | 'deletion' | undefined","hunkData: Hunk","lastHunkEnd","patches: ParsedPatch[]","lines: string[]","hunkContext: string | undefined","parts: string[]"],"sources":["../../src/utils/parsePatchFiles.ts"],"sourcesContent":["import {\n ALTERNATE_FILE_NAMES_GIT,\n COMMIT_METADATA_SPLIT,\n FILENAME_HEADER_REGEX,\n FILENAME_HEADER_REGEX_GIT,\n GIT_DIFF_FILE_BREAK_REGEX,\n INDEX_LINE_METADATA,\n UNIFIED_DIFF_FILE_BREAK_REGEX,\n} from '../constants';\nimport type {\n ChangeContent,\n ContextContent,\n FileContents,\n FileDiffMetadata,\n Hunk,\n HunkLineType,\n ParsedPatch,\n} from '../types';\nimport { cleanLastNewline } from './cleanLastNewline';\nimport { detachString, releaseStringDetachBuffer } from './detachString';\n\ninterface ParsedHunkHeader {\n additionCount: number;\n additionStart: number;\n deletionCount: number;\n deletionStart: number;\n hunkContext?: string;\n}\n\nexport function processPatch(\n data: string,\n cacheKeyPrefix?: string,\n throwOnError?: boolean\n): ParsedPatch {\n try {\n return _processPatch(data, cacheKeyPrefix, throwOnError);\n } finally {\n releaseStringDetachBuffer();\n }\n}\n\nfunction _processPatch(\n data: string,\n cacheKeyPrefix?: string,\n throwOnError = false\n): ParsedPatch {\n const isGitDiff = isGitDiffPatch(data);\n const rawFiles = isGitDiff\n ? splitAtLinePrefix(data, 'diff --git')\n : data.split(UNIFIED_DIFF_FILE_BREAK_REGEX);\n let patchMetadata: string | undefined;\n const files: FileDiffMetadata[] = [];\n for (const fileOrPatchMetadata of rawFiles) {\n if (isGitDiff && !GIT_DIFF_FILE_BREAK_REGEX.test(fileOrPatchMetadata)) {\n if (patchMetadata == null) {\n patchMetadata = detachString(fileOrPatchMetadata);\n } else {\n if (throwOnError) {\n throw Error('parsePatchContent: unknown file blob');\n } else {\n console.error(\n 'parsePatchContent: unknown file blob:',\n fileOrPatchMetadata\n );\n }\n }\n // If we get in here, it's most likely the introductory metadata from the\n // patch, or something is fucked with the diff format\n continue;\n } else if (\n !isGitDiff &&\n !UNIFIED_DIFF_FILE_BREAK_REGEX.test(fileOrPatchMetadata)\n ) {\n if (patchMetadata == null) {\n patchMetadata = detachString(fileOrPatchMetadata);\n } else {\n if (throwOnError) {\n throw Error('parsePatchContent: unknown file blob');\n } else {\n console.error(\n 'parsePatchContent: unknown file blob:',\n fileOrPatchMetadata\n );\n }\n }\n continue;\n }\n const currentFile = _processFile(fileOrPatchMetadata, {\n cacheKey:\n cacheKeyPrefix != null\n ? `${cacheKeyPrefix}-${files.length}`\n : undefined,\n isGitDiff,\n throwOnError,\n });\n if (currentFile != null) {\n files.push(currentFile);\n }\n }\n return { patchMetadata, files };\n}\n\ninterface ProcessFileOptions {\n cacheKey?: string;\n isGitDiff?: boolean;\n oldFile?: FileContents;\n newFile?: FileContents;\n throwOnError?: boolean;\n}\n\nexport function processFile(\n fileDiffString: string,\n options?: ProcessFileOptions\n): FileDiffMetadata | undefined {\n try {\n return _processFile(fileDiffString, options);\n } finally {\n releaseStringDetachBuffer();\n }\n}\n\nfunction _processFile(\n fileDiffString: string,\n {\n cacheKey,\n isGitDiff = GIT_DIFF_FILE_BREAK_REGEX.test(fileDiffString),\n oldFile,\n newFile,\n throwOnError = false,\n }: ProcessFileOptions = {}\n): FileDiffMetadata | undefined {\n let lastHunkEnd = 0;\n const hunks = splitAtLinePrefix(fileDiffString, '@@ ');\n let currentFile: FileDiffMetadata | undefined;\n const isPartial = oldFile == null || newFile == null;\n let deletionLineIndex = 0;\n let additionLineIndex = 0;\n for (const hunk of hunks) {\n const lines = splitWithNewlines(hunk);\n const firstLine = lines[0];\n if (firstLine == null) {\n if (throwOnError) {\n throw Error('parsePatchContent: invalid hunk');\n } else {\n console.error('parsePatchContent: invalid hunk', hunk);\n }\n continue;\n }\n const fileHeader = parseHunkHeader(firstLine);\n let additionLines = 0;\n let deletionLines = 0;\n // Setup currentFile, this should be the first iteration of our hunks, and\n // technically not a hunk\n if (fileHeader == null || currentFile == null) {\n if (currentFile != null) {\n if (throwOnError) {\n throw Error('parsePatchContent: Invalid hunk');\n } else {\n console.error('parsePatchContent: Invalid hunk', hunk);\n }\n continue;\n }\n currentFile = {\n name: '',\n type: 'change',\n hunks: [],\n splitLineCount: 0,\n unifiedLineCount: 0,\n isPartial,\n additionLines:\n !isPartial && oldFile != null && newFile != null\n ? splitFileContents(newFile.contents)\n : [],\n deletionLines:\n !isPartial && oldFile != null && newFile != null\n ? splitFileContents(oldFile.contents)\n : [],\n cacheKey: maybeDetachOptionalString(cacheKey),\n };\n // If either file is technically empty, then we should empty the\n // arrays respectively\n if (currentFile.additionLines.length === 1 && newFile?.contents === '') {\n currentFile.additionLines.length = 0;\n }\n if (currentFile.deletionLines.length === 1 && oldFile?.contents === '') {\n currentFile.deletionLines.length = 0;\n }\n\n for (const line of lines) {\n if (line.startsWith('diff --git')) {\n const [, , prevName, , name] =\n line.trim().match(ALTERNATE_FILE_NAMES_GIT) ?? [];\n currentFile.name = detachString(name.trim());\n if (prevName !== name) {\n currentFile.prevName = detachString(prevName.trim());\n }\n continue;\n }\n\n const filenameMatch =\n line.startsWith('---') || line.startsWith('+++')\n ? line.match(\n isGitDiff ? FILENAME_HEADER_REGEX_GIT : FILENAME_HEADER_REGEX\n )\n : null;\n if (filenameMatch != null) {\n const [, type, fileName] = filenameMatch;\n if (type === '---' && fileName !== '/dev/null') {\n const detachedFileName = detachString(fileName.trim());\n currentFile.prevName = detachedFileName;\n currentFile.name = detachedFileName;\n } else if (type === '+++' && fileName !== '/dev/null') {\n currentFile.name = detachString(fileName.trim());\n }\n }\n // Git diffs have a bunch of additional metadata we can pull from\n else if (isGitDiff) {\n if (line.startsWith('new mode ')) {\n currentFile.mode = detachString(\n line.slice('new mode'.length).trim()\n );\n }\n if (line.startsWith('old mode ')) {\n currentFile.prevMode = detachString(\n line.slice('old mode'.length).trim()\n );\n }\n if (line.startsWith('new file mode')) {\n currentFile.type = 'new';\n currentFile.mode = detachString(\n line.slice('new file mode'.length).trim()\n );\n }\n if (line.startsWith('deleted file mode')) {\n currentFile.type = 'deleted';\n currentFile.mode = detachString(\n line.slice('deleted file mode'.length).trim()\n );\n }\n if (line.startsWith('similarity index')) {\n if (line.startsWith('similarity index 100%')) {\n currentFile.type = 'rename-pure';\n } else {\n currentFile.type = 'rename-changed';\n }\n }\n if (line.startsWith('index ')) {\n const [, prevObjectId, newObjectId, mode] =\n line.trim().match(INDEX_LINE_METADATA) ?? [];\n if (prevObjectId != null) {\n currentFile.prevObjectId = detachString(prevObjectId);\n }\n if (newObjectId != null) {\n currentFile.newObjectId = detachString(newObjectId);\n }\n if (mode != null) {\n currentFile.mode = detachString(mode);\n }\n }\n // We have to handle these for pure renames because there won't be\n // --- and +++ lines\n if (line.startsWith('rename from ')) {\n currentFile.prevName = detachString(\n line.slice('rename from '.length).trim()\n );\n }\n if (line.startsWith('rename to ')) {\n currentFile.name = detachString(\n line.slice('rename to '.length).trim()\n );\n }\n }\n }\n continue;\n }\n\n // Otherwise, time to start parsing out the hunk\n let currentContent: ContextContent | ChangeContent | undefined;\n let lastLineType: 'context' | 'addition' | 'deletion' | undefined;\n\n // Strip trailing bare newlines (format-patch separators between commits)\n // if needed\n while (\n lines.length > 0 &&\n (lines[lines.length - 1] === '\\n' ||\n lines[lines.length - 1] === '\\r' ||\n lines[lines.length - 1] === '\\r\\n' ||\n lines[lines.length - 1] === '')\n ) {\n lines.pop();\n }\n\n const { additionStart, deletionStart } = fileHeader;\n deletionLineIndex = isPartial ? deletionLineIndex : deletionStart - 1;\n additionLineIndex = isPartial ? additionLineIndex : additionStart - 1;\n\n const hunkData: Hunk = {\n collapsedBefore: 0,\n\n splitLineCount: 0,\n splitLineStart: 0,\n\n unifiedLineCount: 0,\n unifiedLineStart: 0,\n\n additionCount: fileHeader.additionCount,\n additionStart,\n additionLines,\n\n deletionCount: fileHeader.deletionCount,\n deletionStart,\n deletionLines,\n\n deletionLineIndex,\n additionLineIndex,\n\n hunkContent: [],\n hunkContext: maybeDetachOptionalString(fileHeader.hunkContext),\n hunkSpecs: detachString(firstLine),\n\n noEOFCRAdditions: false,\n noEOFCRDeletions: false,\n };\n\n // Now we process each line of the hunk\n let parsedAdditionLines = 0;\n let parsedDeletionLines = 0;\n for (let lineIndex = 1; lineIndex < lines.length; lineIndex++) {\n const rawLine = lines[lineIndex];\n if (\n parsedAdditionLines >= hunkData.additionCount &&\n parsedDeletionLines >= hunkData.deletionCount &&\n !rawLine.startsWith('\\\\')\n ) {\n break;\n }\n\n const firstChar = rawLine[0];\n // If we can't properly process the line, well, lets just try to salvage\n // things and continue... It's possible an AI generated diff might have\n // some stray blank lines or something in there\n if (\n firstChar !== '+' &&\n firstChar !== '-' &&\n firstChar !== ' ' &&\n firstChar !== '\\\\'\n ) {\n console.error(\n `parseLineType: Invalid firstChar: \"${firstChar}\", full line: \"${rawLine}\"`\n );\n console.error('processFile: invalid rawLine:', rawLine);\n continue;\n }\n\n const type = parseRawLineType(firstChar);\n if (type === 'addition') {\n const line = getParsedLineContent(rawLine);\n if (currentContent == null || currentContent.type !== 'change') {\n currentContent = createContentGroup(\n 'change',\n deletionLineIndex,\n additionLineIndex\n );\n hunkData.hunkContent.push(currentContent);\n }\n additionLineIndex++;\n parsedAdditionLines++;\n if (isPartial) {\n currentFile.additionLines.push(line);\n }\n currentContent.additions++;\n additionLines++;\n lastLineType = 'addition';\n } else if (type === 'deletion') {\n const line = getParsedLineContent(rawLine);\n if (currentContent == null || currentContent.type !== 'change') {\n currentContent = createContentGroup(\n 'change',\n deletionLineIndex,\n additionLineIndex\n );\n hunkData.hunkContent.push(currentContent);\n }\n deletionLineIndex++;\n parsedDeletionLines++;\n if (isPartial) {\n currentFile.deletionLines.push(line);\n }\n currentContent.deletions++;\n deletionLines++;\n lastLineType = 'deletion';\n } else if (type === 'context') {\n const line = getParsedLineContent(rawLine);\n if (currentContent == null || currentContent.type !== 'context') {\n currentContent = createContentGroup(\n 'context',\n deletionLineIndex,\n additionLineIndex\n );\n hunkData.hunkContent.push(currentContent);\n }\n additionLineIndex++;\n deletionLineIndex++;\n parsedAdditionLines++;\n parsedDeletionLines++;\n if (isPartial) {\n currentFile.deletionLines.push(line);\n currentFile.additionLines.push(line);\n }\n currentContent.lines++;\n lastLineType = 'context';\n } else if (type === 'metadata' && currentContent != null) {\n if (currentContent.type === 'context') {\n hunkData.noEOFCRAdditions = true;\n hunkData.noEOFCRDeletions = true;\n } else if (lastLineType === 'deletion') {\n hunkData.noEOFCRDeletions = true;\n } else if (lastLineType === 'addition') {\n hunkData.noEOFCRAdditions = true;\n }\n // If we're dealing with partial content from a diff, we need to strip\n // newlines manually from the content\n if (\n isPartial &&\n (lastLineType === 'addition' || lastLineType === 'context')\n ) {\n const lastIndex = currentFile.additionLines.length - 1;\n if (lastIndex >= 0) {\n currentFile.additionLines[lastIndex] = cleanLastNewline(\n currentFile.additionLines[lastIndex]\n );\n }\n }\n if (\n isPartial &&\n (lastLineType === 'deletion' || lastLineType === 'context')\n ) {\n const lastIndex = currentFile.deletionLines.length - 1;\n if (lastIndex >= 0) {\n currentFile.deletionLines[lastIndex] = cleanLastNewline(\n currentFile.deletionLines[lastIndex]\n );\n }\n }\n }\n }\n\n hunkData.additionLines = additionLines;\n hunkData.deletionLines = deletionLines;\n\n hunkData.collapsedBefore = Math.max(\n hunkData.additionStart - 1 - lastHunkEnd,\n 0\n );\n currentFile.hunks.push(hunkData);\n lastHunkEnd = hunkData.additionStart + hunkData.additionCount - 1;\n for (const content of hunkData.hunkContent) {\n if (content.type === 'context') {\n hunkData.splitLineCount += content.lines;\n hunkData.unifiedLineCount += content.lines;\n } else {\n hunkData.splitLineCount += Math.max(\n content.additions,\n content.deletions\n );\n hunkData.unifiedLineCount += content.deletions + content.additions;\n }\n }\n hunkData.splitLineStart =\n currentFile.splitLineCount + hunkData.collapsedBefore;\n hunkData.unifiedLineStart =\n currentFile.unifiedLineCount + hunkData.collapsedBefore;\n\n currentFile.splitLineCount +=\n hunkData.collapsedBefore + hunkData.splitLineCount;\n currentFile.unifiedLineCount +=\n hunkData.collapsedBefore + hunkData.unifiedLineCount;\n }\n if (currentFile == null) {\n return undefined;\n }\n\n // Account for collapsed lines after the final hunk and increment the\n // split/unified counts properly\n if (\n currentFile.hunks.length > 0 &&\n !isPartial &&\n currentFile.additionLines.length > 0 &&\n currentFile.deletionLines.length > 0\n ) {\n const lastHunk = currentFile.hunks[currentFile.hunks.length - 1];\n const lastHunkEnd = lastHunk.additionStart + lastHunk.additionCount - 1;\n const totalFileLines = currentFile.additionLines.length;\n const collapsedAfter = Math.max(totalFileLines - lastHunkEnd, 0);\n currentFile.splitLineCount += collapsedAfter;\n currentFile.unifiedLineCount += collapsedAfter;\n }\n\n // If this isn't a git diff style patch, then we'll need to sus out some\n // additional metadata manually\n if (!isGitDiff) {\n if (\n currentFile.prevName != null &&\n currentFile.name !== currentFile.prevName\n ) {\n if (currentFile.hunks.length > 0) {\n currentFile.type = 'rename-changed';\n } else {\n currentFile.type = 'rename-pure';\n }\n }\n // Sort of a hack for detecting deleted/added files...\n else if (\n (oldFile == null || oldFile.contents === '') &&\n newFile != null &&\n newFile.contents !== ''\n ) {\n currentFile.type = 'new';\n } else if (\n oldFile != null &&\n oldFile.contents !== '' &&\n (newFile == null || newFile.contents === '')\n ) {\n currentFile.type = 'deleted';\n }\n }\n if (\n currentFile.type !== 'rename-pure' &&\n currentFile.type !== 'rename-changed'\n ) {\n currentFile.prevName = undefined;\n }\n return currentFile;\n}\n\n/**\n * Parses a patch file string into an array of parsed patches.\n *\n * @param data - The raw patch file content (supports multi-commit patches)\n * @param cacheKeyPrefix - Optional prefix for generating cache keys. When provided,\n * each file in the patch will get a cache key in the format `prefix-patchIndex-fileIndex`.\n * This enables caching of rendered diff results in the worker pool.\n */\nexport function parsePatchFiles(\n data: string,\n cacheKeyPrefix?: string,\n throwOnError = false\n): ParsedPatch[] {\n // NOTE(amadeus): This function is pretty forgiving in that it can accept a\n // patch file that includes commit metdata, multiple commits, or not\n const patches: ParsedPatch[] = [];\n const rawPatches = hasCommitMetadataBoundary(data)\n ? data.split(COMMIT_METADATA_SPLIT)\n : [data];\n for (const patch of rawPatches) {\n try {\n patches.push(\n processPatch(\n patch,\n cacheKeyPrefix != null\n ? `${cacheKeyPrefix}-${patches.length}`\n : undefined,\n throwOnError\n )\n );\n } catch (error) {\n if (throwOnError) {\n throw error;\n } else {\n console.error(error);\n }\n }\n }\n return patches;\n}\n\nfunction hasCommitMetadataBoundary(data: string): boolean {\n return data.startsWith('From ') || data.includes('\\nFrom ');\n}\n\nfunction splitFileContents(contents: string): string[] {\n const lines = splitWithNewlines(contents);\n for (let index = 0; index < lines.length; index++) {\n lines[index] = detachString(lines[index]);\n }\n return lines;\n}\n\nfunction splitWithNewlines(contents: string): string[] {\n if (contents.length === 0) {\n return [''];\n }\n\n const lines: string[] = [];\n let startIndex = 0;\n for (;;) {\n const newlineIndex = contents.indexOf('\\n', startIndex);\n if (newlineIndex === -1) {\n break;\n }\n\n lines.push(contents.slice(startIndex, newlineIndex + 1));\n startIndex = newlineIndex + 1;\n }\n\n if (startIndex < contents.length) {\n lines.push(contents.slice(startIndex));\n }\n return lines;\n}\n\nfunction parseHunkHeader(line: string): ParsedHunkHeader | undefined {\n if (!line.startsWith('@@ -')) {\n return undefined;\n }\n\n let index = 4;\n const deletionStartResult = readPositiveInteger(line, index);\n if (deletionStartResult == null) {\n return undefined;\n }\n const deletionStart = deletionStartResult.value;\n index = deletionStartResult.endIndex;\n\n let deletionCount = 1;\n if (line[index] === ',') {\n const deletionCountResult = readPositiveInteger(line, index + 1);\n if (deletionCountResult == null) {\n return undefined;\n }\n deletionCount = deletionCountResult.value;\n index = deletionCountResult.endIndex;\n }\n\n if (line[index] !== ' ' || line[index + 1] !== '+') {\n return undefined;\n }\n index += 2;\n\n const additionStartResult = readPositiveInteger(line, index);\n if (additionStartResult == null) {\n return undefined;\n }\n const additionStart = additionStartResult.value;\n index = additionStartResult.endIndex;\n\n let additionCount = 1;\n if (line[index] === ',') {\n const additionCountResult = readPositiveInteger(line, index + 1);\n if (additionCountResult == null) {\n return undefined;\n }\n additionCount = additionCountResult.value;\n index = additionCountResult.endIndex;\n }\n\n if (\n line[index] !== ' ' ||\n line[index + 1] !== '@' ||\n line[index + 2] !== '@'\n ) {\n return undefined;\n }\n\n let hunkContext: string | undefined;\n const contextStartIndex = index + 3;\n if (line[contextStartIndex] === ' ') {\n hunkContext = trimLineEnd(line.slice(contextStartIndex + 1));\n }\n\n return {\n additionCount,\n additionStart,\n deletionCount,\n deletionStart,\n hunkContext,\n };\n}\n\nfunction readPositiveInteger(\n value: string,\n startIndex: number\n): { value: number; endIndex: number } | undefined {\n let index = startIndex;\n let parsedValue = 0;\n for (; index < value.length; index++) {\n const digit = value.charCodeAt(index) - 48;\n if (digit < 0 || digit > 9) {\n break;\n }\n parsedValue = parsedValue * 10 + digit;\n }\n\n if (index === startIndex) {\n return undefined;\n }\n return { value: parsedValue, endIndex: index };\n}\n\nfunction trimLineEnd(value: string): string {\n if (value.endsWith('\\r\\n')) {\n return value.slice(0, -2);\n }\n if (value.endsWith('\\n')) {\n return value.slice(0, -1);\n }\n return value;\n}\n\nfunction isGitDiffPatch(data: string): boolean {\n return data.startsWith('diff --git') || data.includes('\\ndiff --git');\n}\n\nfunction splitAtLinePrefix(contents: string, prefix: string): string[] {\n if (contents.length === 0) {\n return [''];\n }\n\n const newlinePrefix = `\\n${prefix}`;\n const firstBoundaryIndex = contents.startsWith(prefix)\n ? 0\n : findLinePrefixIndex(contents, newlinePrefix, 0);\n if (firstBoundaryIndex === -1) {\n return [contents];\n }\n\n const parts: string[] = [];\n if (firstBoundaryIndex > 0) {\n parts.push(contents.slice(0, firstBoundaryIndex));\n }\n\n let startIndex = firstBoundaryIndex;\n for (;;) {\n const nextBoundaryIndex = findLinePrefixIndex(\n contents,\n newlinePrefix,\n startIndex + 1\n );\n if (nextBoundaryIndex === -1) {\n break;\n }\n\n parts.push(contents.slice(startIndex, nextBoundaryIndex));\n startIndex = nextBoundaryIndex;\n }\n parts.push(contents.slice(startIndex));\n return parts;\n}\n\nfunction findLinePrefixIndex(\n contents: string,\n newlinePrefix: string,\n fromIndex: number\n): number {\n const index = contents.indexOf(newlinePrefix, fromIndex);\n return index === -1 ? -1 : index + 1;\n}\n\nfunction maybeDetachOptionalString<T extends string | undefined>(value: T): T {\n return (value == null ? value : detachString(value)) as T;\n}\n\nfunction parseRawLineType(\n firstChar: string | undefined\n): Exclude<HunkLineType, 'expanded'> {\n return firstChar === ' '\n ? 'context'\n : firstChar === '\\\\'\n ? 'metadata'\n : firstChar === '+'\n ? 'addition'\n : 'deletion';\n}\n\nfunction getParsedLineContent(rawLine: string): string {\n const processedLine = rawLine.slice(1);\n return detachString(processedLine === '' ? '\\n' : processedLine);\n}\n\nfunction createContentGroup(\n type: 'change',\n deletionLineIndex: number,\n additionLineIndex: number\n): ChangeContent;\nfunction createContentGroup(\n type: 'context',\n deletionLineIndex: number,\n additionLineIndex: number\n): ContextContent;\nfunction createContentGroup(\n type: 'change' | 'context',\n deletionLineIndex: number,\n additionLineIndex: number\n): ChangeContent | ContextContent {\n if (type === 'change') {\n return {\n type: 'change',\n additions: 0,\n deletions: 0,\n additionLineIndex,\n deletionLineIndex,\n };\n }\n return {\n type: 'context',\n lines: 0,\n additionLineIndex,\n deletionLineIndex,\n };\n}\n"],"mappings":";;;;;AA6BA,SAAgB,aACd,MACA,gBACA,cACa;AACb,KAAI;AACF,SAAO,cAAc,MAAM,gBAAgB,aAAa;WAChD;AACR,6BAA2B;;;AAI/B,SAAS,cACP,MACA,gBACA,eAAe,OACF;CACb,MAAM,YAAY,eAAe,KAAK;CACtC,MAAM,WAAW,YACb,kBAAkB,MAAM,aAAa,GACrC,KAAK,MAAM,8BAA8B;CAC7C,IAAIA;CACJ,MAAMC,QAA4B,EAAE;AACpC,MAAK,MAAM,uBAAuB,UAAU;AAC1C,MAAI,aAAa,CAAC,0BAA0B,KAAK,oBAAoB,EAAE;AACrE,OAAI,iBAAiB,KACnB,iBAAgB,aAAa,oBAAoB;YAE7C,aACF,OAAM,MAAM,uCAAuC;OAEnD,SAAQ,MACN,yCACA,oBACD;AAKL;aAEA,CAAC,aACD,CAAC,8BAA8B,KAAK,oBAAoB,EACxD;AACA,OAAI,iBAAiB,KACnB,iBAAgB,aAAa,oBAAoB;YAE7C,aACF,OAAM,MAAM,uCAAuC;OAEnD,SAAQ,MACN,yCACA,oBACD;AAGL;;EAEF,MAAM,cAAc,aAAa,qBAAqB;GACpD,UACE,kBAAkB,OACd,GAAG,eAAe,GAAG,MAAM,WAC3B;GACN;GACA;GACD,CAAC;AACF,MAAI,eAAe,KACjB,OAAM,KAAK,YAAY;;AAG3B,QAAO;EAAE;EAAe;EAAO;;AAWjC,SAAgB,YACd,gBACA,SAC8B;AAC9B,KAAI;AACF,SAAO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EACL,MAAM;EACN,OAAO;EACP;EACA;EACD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefersReducedMotion.d.ts","names":["prefersReducedMotion"],"sources":["../../src/utils/prefersReducedMotion.d.ts"],"sourcesContent":["export declare function prefersReducedMotion(): boolean;\n//# sourceMappingURL=prefersReducedMotion.d.ts.map"],"mappings":";iBAAwBA,oBAAAA,CAAAA"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/utils/prefersReducedMotion.ts
|
|
2
|
+
function prefersReducedMotion() {
|
|
3
|
+
if (typeof window === "undefined" || typeof window.matchMedia !== "function") return false;
|
|
4
|
+
return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
//#endregion
|
|
8
|
+
export { prefersReducedMotion };
|
|
9
|
+
//# sourceMappingURL=prefersReducedMotion.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefersReducedMotion.js","names":[],"sources":["../../src/utils/prefersReducedMotion.ts"],"sourcesContent":["export function prefersReducedMotion(): boolean {\n if (\n typeof window === 'undefined' ||\n typeof window.matchMedia !== 'function'\n ) {\n return false;\n }\n return window.matchMedia('(prefers-reduced-motion: reduce)').matches;\n}\n"],"mappings":";AAAA,SAAgB,uBAAgC;AAC9C,KACE,OAAO,WAAW,eAClB,OAAO,OAAO,eAAe,WAE7B,QAAO;AAET,QAAO,OAAO,WAAW,mCAAmC,CAAC"}
|
|
@@ -10,19 +10,23 @@ interface GetCachesResult {
|
|
|
10
10
|
interface ThemeSubscriber {
|
|
11
11
|
rerender(): void;
|
|
12
12
|
}
|
|
13
|
+
type RenderTaskInstance = FileRendererInstance | DiffRendererInstance;
|
|
13
14
|
declare class WorkerPoolManager {
|
|
14
15
|
private options;
|
|
15
16
|
private highlighter;
|
|
16
17
|
private readonly preferredHighlighter;
|
|
17
18
|
private renderOptions;
|
|
19
|
+
private renderOptionsVersion;
|
|
18
20
|
private initialized;
|
|
19
21
|
private workers;
|
|
20
|
-
private
|
|
21
|
-
private
|
|
22
|
+
private queuedTasks;
|
|
23
|
+
private queuedTaskByInstance;
|
|
24
|
+
private taskByHighlightKey;
|
|
25
|
+
private activeTaskById;
|
|
26
|
+
private activeRequestByInstance;
|
|
22
27
|
private nextRequestId;
|
|
23
28
|
private themeSubscribers;
|
|
24
29
|
private workersFailed;
|
|
25
|
-
private instanceRequestMap;
|
|
26
30
|
private statSubscribers;
|
|
27
31
|
private fileCache;
|
|
28
32
|
private diffCache;
|
|
@@ -58,22 +62,26 @@ declare class WorkerPoolManager {
|
|
|
58
62
|
subscribeToStatChanges(callback: (stats: WorkerStats) => unknown): () => void;
|
|
59
63
|
private queueBroadcastStateChanges;
|
|
60
64
|
private _broadcastStateChanges;
|
|
61
|
-
|
|
65
|
+
cleanUpTasks(instance: RenderTaskInstance): void;
|
|
62
66
|
isInitialized(): boolean;
|
|
63
67
|
initialize(languages?: SupportedLanguages[]): Promise<void>;
|
|
64
68
|
private initializeWorkers;
|
|
65
69
|
private drainQueue;
|
|
66
70
|
highlightFileAST(instance: FileRendererInstance, file: FileContents): void;
|
|
71
|
+
primeFileHighlightCache(file: FileContents): void;
|
|
67
72
|
getPlainFileAST(file: FileContents, startingLine: number, totalLines: number, lines?: string[]): ThemedFileResult | undefined;
|
|
68
73
|
highlightDiffAST(instance: DiffRendererInstance, diff: FileDiffMetadata): void;
|
|
74
|
+
primeDiffHighlightCache(diff: FileDiffMetadata): void;
|
|
69
75
|
getPlainDiffAST(diff: FileDiffMetadata, startingLine: number, totalLines: number, expandedHunks?: Map<number, HunkExpansionRegion> | true, collapsedContextThreshold?: number): ThemedDiffResult | undefined;
|
|
70
76
|
terminate(): void;
|
|
71
77
|
private isCurrentLifecycle;
|
|
72
78
|
private queueInitialization;
|
|
73
|
-
private
|
|
79
|
+
private cancelActiveWorkerTasks;
|
|
74
80
|
private terminateWorkers;
|
|
75
81
|
getStats(): WorkerStats;
|
|
76
82
|
private submitTask;
|
|
83
|
+
private submitCacheTask;
|
|
84
|
+
private enqueueRenderTask;
|
|
77
85
|
private resolveLanguagesAndExecuteTask;
|
|
78
86
|
private handleWorkerMessage;
|
|
79
87
|
private _queuedDrain;
|
|
@@ -84,6 +92,26 @@ declare class WorkerPoolManager {
|
|
|
84
92
|
private maybeAttachCustomExtensions;
|
|
85
93
|
private syncCustomExtensionVersion;
|
|
86
94
|
private getAvailableWorker;
|
|
95
|
+
private getFileHighlightKey;
|
|
96
|
+
private getDiffHighlightKey;
|
|
97
|
+
private getHighlightKeyForRequest;
|
|
98
|
+
private hasActiveRequest;
|
|
99
|
+
private addInstanceToTask;
|
|
100
|
+
private detachInstanceFromQueuedTasks;
|
|
101
|
+
private detachInstanceFromRenderTask;
|
|
102
|
+
private removeQueuedTask;
|
|
103
|
+
private removeActiveTask;
|
|
104
|
+
private clearQueuedInstanceRequests;
|
|
105
|
+
private clearHighlightKey;
|
|
106
|
+
private trackInstanceRequests;
|
|
107
|
+
private clearInstanceRequests;
|
|
108
|
+
private notifyFileInstances;
|
|
109
|
+
private notifyDiffInstances;
|
|
110
|
+
private notifyHighlightError;
|
|
111
|
+
private hasMatchingFileInstanceTask;
|
|
112
|
+
private hasMatchingDiffInstanceTask;
|
|
113
|
+
private getTaskByHighlightKey;
|
|
114
|
+
private iterateRenderTasks;
|
|
87
115
|
private generateRequestId;
|
|
88
116
|
}
|
|
89
117
|
//#endregion
|