@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.
Files changed (128) hide show
  1. package/dist/components/File.d.ts +4 -2
  2. package/dist/components/File.d.ts.map +1 -1
  3. package/dist/components/File.js +80 -34
  4. package/dist/components/File.js.map +1 -1
  5. package/dist/components/FileDiff.d.ts +50 -28
  6. package/dist/components/FileDiff.d.ts.map +1 -1
  7. package/dist/components/FileDiff.js +220 -79
  8. package/dist/components/FileDiff.js.map +1 -1
  9. package/dist/components/FileStream.d.ts +1 -0
  10. package/dist/components/FileStream.d.ts.map +1 -1
  11. package/dist/components/FileStream.js +8 -4
  12. package/dist/components/FileStream.js.map +1 -1
  13. package/dist/constants.d.ts +8 -2
  14. package/dist/constants.d.ts.map +1 -1
  15. package/dist/constants.js +10 -1
  16. package/dist/constants.js.map +1 -1
  17. package/dist/index.d.ts +19 -10
  18. package/dist/index.js +14 -5
  19. package/dist/managers/LineSelectionManager.d.ts.map +1 -1
  20. package/dist/managers/LineSelectionManager.js +8 -9
  21. package/dist/managers/LineSelectionManager.js.map +1 -1
  22. package/dist/react/MultiFileDiff.js +2 -2
  23. package/dist/react/MultiFileDiff.js.map +1 -1
  24. package/dist/react/index.d.ts +2 -2
  25. package/dist/react/utils/renderDiffChildren.d.ts +4 -4
  26. package/dist/react/utils/renderDiffChildren.d.ts.map +1 -1
  27. package/dist/react/utils/renderDiffChildren.js +3 -3
  28. package/dist/react/utils/renderDiffChildren.js.map +1 -1
  29. package/dist/react/utils/useFileDiffInstance.js.map +1 -1
  30. package/dist/renderers/DiffHunksRenderer.d.ts +7 -6
  31. package/dist/renderers/DiffHunksRenderer.d.ts.map +1 -1
  32. package/dist/renderers/DiffHunksRenderer.js +263 -337
  33. package/dist/renderers/DiffHunksRenderer.js.map +1 -1
  34. package/dist/renderers/FileRenderer.d.ts +1 -0
  35. package/dist/renderers/FileRenderer.d.ts.map +1 -1
  36. package/dist/renderers/FileRenderer.js +11 -4
  37. package/dist/renderers/FileRenderer.js.map +1 -1
  38. package/dist/ssr/index.d.ts +2 -2
  39. package/dist/style.js +1 -1
  40. package/dist/style.js.map +1 -1
  41. package/dist/types.d.ts +246 -42
  42. package/dist/types.d.ts.map +1 -1
  43. package/dist/utils/areDiffLineAnnotationsEqual.d.ts +7 -0
  44. package/dist/utils/areDiffLineAnnotationsEqual.d.ts.map +1 -0
  45. package/dist/utils/areDiffLineAnnotationsEqual.js +8 -0
  46. package/dist/utils/areDiffLineAnnotationsEqual.js.map +1 -0
  47. package/dist/utils/areHunkDataEqual.d.ts +7 -0
  48. package/dist/utils/areHunkDataEqual.d.ts.map +1 -0
  49. package/dist/utils/areHunkDataEqual.js +8 -0
  50. package/dist/utils/areHunkDataEqual.js.map +1 -0
  51. package/dist/utils/areLineAnnotationsEqual.d.ts +7 -0
  52. package/dist/utils/areLineAnnotationsEqual.d.ts.map +1 -0
  53. package/dist/utils/areLineAnnotationsEqual.js +8 -0
  54. package/dist/utils/areLineAnnotationsEqual.js.map +1 -0
  55. package/dist/utils/arePrePropertiesEqual.d.ts +7 -0
  56. package/dist/utils/arePrePropertiesEqual.d.ts.map +1 -0
  57. package/dist/utils/arePrePropertiesEqual.js +9 -0
  58. package/dist/utils/arePrePropertiesEqual.js.map +1 -0
  59. package/dist/utils/areRenderRangesEqual.d.ts +7 -0
  60. package/dist/utils/areRenderRangesEqual.d.ts.map +1 -0
  61. package/dist/utils/areRenderRangesEqual.js +9 -0
  62. package/dist/utils/areRenderRangesEqual.js.map +1 -0
  63. package/dist/utils/areVirtualWindowSpecsEqual.d.ts +7 -0
  64. package/dist/utils/areVirtualWindowSpecsEqual.d.ts.map +1 -0
  65. package/dist/utils/areVirtualWindowSpecsEqual.js +9 -0
  66. package/dist/utils/areVirtualWindowSpecsEqual.js.map +1 -0
  67. package/dist/utils/areWorkerStatsEqual.d.ts +8 -0
  68. package/dist/utils/areWorkerStatsEqual.d.ts.map +1 -0
  69. package/dist/utils/areWorkerStatsEqual.js +9 -0
  70. package/dist/utils/areWorkerStatsEqual.js.map +1 -0
  71. package/dist/utils/createTransformerWithState.js +1 -1
  72. package/dist/utils/createTransformerWithState.js.map +1 -1
  73. package/dist/utils/createWindowFromScrollPosition.d.ts +22 -0
  74. package/dist/utils/createWindowFromScrollPosition.d.ts.map +1 -0
  75. package/dist/utils/createWindowFromScrollPosition.js +26 -0
  76. package/dist/utils/createWindowFromScrollPosition.js.map +1 -0
  77. package/dist/utils/diffAcceptRejectHunk.js +36 -21
  78. package/dist/utils/diffAcceptRejectHunk.js.map +1 -1
  79. package/dist/utils/getOrCreateCodeNode.d.ts +14 -0
  80. package/dist/utils/getOrCreateCodeNode.d.ts.map +1 -0
  81. package/dist/utils/getOrCreateCodeNode.js +13 -0
  82. package/dist/utils/getOrCreateCodeNode.js.map +1 -0
  83. package/dist/utils/getTotalLineCountFromHunks.js +1 -1
  84. package/dist/utils/getTotalLineCountFromHunks.js.map +1 -1
  85. package/dist/utils/hast_utils.d.ts +2 -1
  86. package/dist/utils/hast_utils.d.ts.map +1 -1
  87. package/dist/utils/hast_utils.js +10 -1
  88. package/dist/utils/hast_utils.js.map +1 -1
  89. package/dist/utils/isDefaultRenderRange.d.ts +7 -0
  90. package/dist/utils/isDefaultRenderRange.d.ts.map +1 -0
  91. package/dist/utils/isDefaultRenderRange.js +8 -0
  92. package/dist/utils/isDefaultRenderRange.js.map +1 -0
  93. package/dist/utils/iterateOverDiff.d.ts +39 -0
  94. package/dist/utils/iterateOverDiff.d.ts.map +1 -0
  95. package/dist/utils/iterateOverDiff.js +356 -0
  96. package/dist/utils/iterateOverDiff.js.map +1 -0
  97. package/dist/utils/parseDiffFromFile.d.ts.map +1 -1
  98. package/dist/utils/parseDiffFromFile.js +8 -6
  99. package/dist/utils/parseDiffFromFile.js.map +1 -1
  100. package/dist/utils/parsePatchFiles.d.ts +15 -3
  101. package/dist/utils/parsePatchFiles.d.ts.map +1 -1
  102. package/dist/utils/parsePatchFiles.js +207 -158
  103. package/dist/utils/parsePatchFiles.js.map +1 -1
  104. package/dist/utils/processLine.js +4 -3
  105. package/dist/utils/processLine.js.map +1 -1
  106. package/dist/utils/renderDiffWithHighlighter.d.ts +7 -2
  107. package/dist/utils/renderDiffWithHighlighter.d.ts.map +1 -1
  108. package/dist/utils/renderDiffWithHighlighter.js +149 -227
  109. package/dist/utils/renderDiffWithHighlighter.js.map +1 -1
  110. package/dist/utils/setWrapperNodeProps.d.ts +3 -7
  111. package/dist/utils/setWrapperNodeProps.d.ts.map +1 -1
  112. package/dist/utils/setWrapperNodeProps.js +1 -1
  113. package/dist/utils/setWrapperNodeProps.js.map +1 -1
  114. package/dist/worker/WorkerPoolManager.d.ts +9 -2
  115. package/dist/worker/WorkerPoolManager.d.ts.map +1 -1
  116. package/dist/worker/WorkerPoolManager.js +124 -45
  117. package/dist/worker/WorkerPoolManager.js.map +1 -1
  118. package/dist/worker/types.d.ts +7 -0
  119. package/dist/worker/types.d.ts.map +1 -1
  120. package/dist/worker/worker-portable.js +634 -242
  121. package/dist/worker/worker-portable.js.map +1 -1
  122. package/dist/worker/worker.js +511 -231
  123. package/dist/worker/worker.js.map +1 -1
  124. package/package.json +20 -1
  125. package/dist/utils/createCodeNode.d.ts +0 -12
  126. package/dist/utils/createCodeNode.d.ts.map +0 -1
  127. package/dist/utils/createCodeNode.js +0 -12
  128. package/dist/utils/createCodeNode.js.map +0 -1
@@ -8,171 +8,219 @@ function processPatch(data, cacheKeyPrefix) {
8
8
  const rawFiles = data.split(isGitDiff ? GIT_DIFF_FILE_BREAK_REGEX : UNIFIED_DIFF_FILE_BREAK_REGEX);
9
9
  let patchMetadata;
10
10
  const files = [];
11
- let currentFile;
12
- for (const file of rawFiles) {
13
- if (isGitDiff && !GIT_DIFF_FILE_BREAK_REGEX.test(file)) {
14
- if (patchMetadata == null) patchMetadata = file;
15
- else console.error("parsePatchContent: unknown file blob:", file);
11
+ for (const fileOrPatchMetadata of rawFiles) {
12
+ if (isGitDiff && !GIT_DIFF_FILE_BREAK_REGEX.test(fileOrPatchMetadata)) {
13
+ if (patchMetadata == null) patchMetadata = fileOrPatchMetadata;
14
+ else console.error("parsePatchContent: unknown file blob:", fileOrPatchMetadata);
15
+ continue;
16
+ } else if (!isGitDiff && !UNIFIED_DIFF_FILE_BREAK_REGEX.test(fileOrPatchMetadata)) {
17
+ if (patchMetadata == null) patchMetadata = fileOrPatchMetadata;
18
+ else console.error("parsePatchContent: unknown file blob:", fileOrPatchMetadata);
16
19
  continue;
17
- } else if (!isGitDiff && !UNIFIED_DIFF_FILE_BREAK_REGEX.test(file)) {
18
- if (patchMetadata == null) patchMetadata = file;
19
- else console.error("parsePatchContent: unknown file blob:", file);
20
+ }
21
+ const currentFile = processFile(fileOrPatchMetadata, {
22
+ cacheKey: cacheKeyPrefix != null ? `${cacheKeyPrefix}-${files.length}` : void 0,
23
+ isGitDiff
24
+ });
25
+ if (currentFile != null) files.push(currentFile);
26
+ }
27
+ return {
28
+ patchMetadata,
29
+ files
30
+ };
31
+ }
32
+ function processFile(fileDiffString, { cacheKey, isGitDiff = GIT_DIFF_FILE_BREAK_REGEX.test(fileDiffString), oldFile, newFile } = {}) {
33
+ let lastHunkEnd = 0;
34
+ const hunks = fileDiffString.split(FILE_CONTEXT_BLOB);
35
+ let currentFile;
36
+ const isPartial = oldFile == null || newFile == null;
37
+ let deletionLineIndex = 0;
38
+ let additionLineIndex = 0;
39
+ for (const hunk of hunks) {
40
+ const lines = hunk.split(SPLIT_WITH_NEWLINES);
41
+ const firstLine = lines.shift();
42
+ if (firstLine == null) {
43
+ console.error("parsePatchContent: invalid hunk", hunk);
20
44
  continue;
21
45
  }
22
- let lastHunkEnd = 0;
23
- const hunks = file.split(FILE_CONTEXT_BLOB);
24
- currentFile = void 0;
25
- for (const hunk of hunks) {
26
- const lines = hunk.split(SPLIT_WITH_NEWLINES);
27
- const firstLine = lines.shift();
28
- if (firstLine == null) {
29
- console.error("parsePatchContent: invalid hunk", hunk);
46
+ const fileHeaderMatch = firstLine.match(HUNK_HEADER);
47
+ let additionLines = 0;
48
+ let deletionLines = 0;
49
+ if (fileHeaderMatch == null || currentFile == null) {
50
+ if (currentFile != null) {
51
+ console.error("parsePatchContent: Invalid hunk", hunk);
30
52
  continue;
31
53
  }
32
- const match = firstLine.match(HUNK_HEADER);
33
- const hunkContent = [];
34
- let additionLines = 0;
35
- let deletionLines = 0;
36
- if (match == null || currentFile == null) {
37
- if (currentFile != null) {
38
- console.error("parsePatchContent: Invalid hunk", hunk);
39
- continue;
40
- }
41
- currentFile = {
42
- name: "",
43
- prevName: void 0,
44
- type: "change",
45
- hunks: [],
46
- splitLineCount: 0,
47
- unifiedLineCount: 0,
48
- cacheKey: cacheKeyPrefix != null ? `${cacheKeyPrefix}-${files.length}` : void 0
49
- };
50
- lines.unshift(firstLine);
51
- for (const line of lines) {
52
- const filenameMatch = line.match(isGitDiff ? FILENAME_HEADER_REGEX_GIT : FILENAME_HEADER_REGEX);
53
- if (line.startsWith("diff --git")) {
54
- const [, , prevName, , name] = line.trim().match(ALTERNATE_FILE_NAMES_GIT) ?? [];
55
- currentFile.name = name.trim();
56
- if (prevName !== name) currentFile.prevName = prevName.trim();
57
- } else if (filenameMatch != null) {
58
- const [, type, fileName] = filenameMatch;
59
- if (type === "---" && fileName !== "/dev/null") {
60
- currentFile.prevName = fileName.trim();
61
- currentFile.name = fileName.trim();
62
- } else if (type === "+++" && fileName !== "/dev/null") currentFile.name = fileName.trim();
63
- } else if (isGitDiff) {
64
- if (line.startsWith("new mode ")) currentFile.mode = line.replace("new mode", "").trim();
65
- if (line.startsWith("old mode ")) currentFile.oldMode = line.replace("old mode", "").trim();
66
- if (line.startsWith("new file mode")) {
67
- currentFile.type = "new";
68
- currentFile.mode = line.replace("new file mode", "").trim();
69
- }
70
- if (line.startsWith("deleted file mode")) {
71
- currentFile.type = "deleted";
72
- currentFile.mode = line.replace("deleted file mode", "").trim();
73
- }
74
- if (line.startsWith("similarity index")) if (line.startsWith("similarity index 100%")) currentFile.type = "rename-pure";
75
- else currentFile.type = "rename-changed";
76
- if (line.startsWith("index ")) {
77
- const [, mode] = line.trim().match(FILE_MODE_FROM_INDEX) ?? [];
78
- if (mode != null) currentFile.mode = mode;
79
- }
80
- if (line.startsWith("rename from ")) currentFile.prevName = line.replace("rename from ", "");
81
- if (line.startsWith("rename to ")) currentFile.name = line.replace("rename to ", "").trim();
54
+ currentFile = {
55
+ name: "",
56
+ type: "change",
57
+ hunks: [],
58
+ splitLineCount: 0,
59
+ unifiedLineCount: 0,
60
+ isPartial,
61
+ additionLines: !isPartial && oldFile != null && newFile != null ? newFile.contents.split(SPLIT_WITH_NEWLINES) : [],
62
+ deletionLines: !isPartial && oldFile != null && newFile != null ? oldFile.contents.split(SPLIT_WITH_NEWLINES) : [],
63
+ cacheKey
64
+ };
65
+ if (currentFile.additionLines.length === 1 && newFile?.contents === "") currentFile.additionLines.length = 0;
66
+ if (currentFile.deletionLines.length === 1 && oldFile?.contents === "") currentFile.deletionLines.length = 0;
67
+ lines.unshift(firstLine);
68
+ for (const line of lines) {
69
+ const filenameMatch = line.match(isGitDiff ? FILENAME_HEADER_REGEX_GIT : FILENAME_HEADER_REGEX);
70
+ if (line.startsWith("diff --git")) {
71
+ const [, , prevName, , name] = line.trim().match(ALTERNATE_FILE_NAMES_GIT) ?? [];
72
+ currentFile.name = name.trim();
73
+ if (prevName !== name) currentFile.prevName = prevName.trim();
74
+ } else if (filenameMatch != null) {
75
+ const [, type, fileName] = filenameMatch;
76
+ if (type === "---" && fileName !== "/dev/null") {
77
+ currentFile.prevName = fileName.trim();
78
+ currentFile.name = fileName.trim();
79
+ } else if (type === "+++" && fileName !== "/dev/null") currentFile.name = fileName.trim();
80
+ } else if (isGitDiff) {
81
+ if (line.startsWith("new mode ")) currentFile.mode = line.replace("new mode", "").trim();
82
+ if (line.startsWith("old mode ")) currentFile.prevMode = line.replace("old mode", "").trim();
83
+ if (line.startsWith("new file mode")) {
84
+ currentFile.type = "new";
85
+ currentFile.mode = line.replace("new file mode", "").trim();
82
86
  }
83
- }
84
- continue;
85
- } else {
86
- let currentContent;
87
- let lastLineType;
88
- while (lines.length > 0 && (lines[lines.length - 1] === "\n" || lines[lines.length - 1] === "")) lines.pop();
89
- for (const rawLine of lines) {
90
- const parsedLine = parseLineType(rawLine);
91
- if (parsedLine == null) continue;
92
- const { type, line } = parsedLine;
93
- if (type === "addition") {
94
- if (currentContent == null || currentContent.type !== "change") {
95
- currentContent = createContentGroup("change");
96
- hunkContent.push(currentContent);
97
- }
98
- currentContent.additions.push(line);
99
- additionLines++;
100
- lastLineType = "addition";
101
- } else if (type === "deletion") {
102
- if (currentContent == null || currentContent.type !== "change") {
103
- currentContent = createContentGroup("change");
104
- hunkContent.push(currentContent);
105
- }
106
- currentContent.deletions.push(line);
107
- deletionLines++;
108
- lastLineType = "deletion";
109
- } else if (type === "context") {
110
- if (currentContent == null || currentContent.type !== "context") {
111
- currentContent = createContentGroup("context");
112
- hunkContent.push(currentContent);
113
- }
114
- currentContent.lines.push(line);
115
- lastLineType = "context";
116
- } else if (type === "metadata" && currentContent != null) {
117
- if (currentContent.type === "context") currentContent.noEOFCR = true;
118
- else if (lastLineType === "deletion") {
119
- currentContent.noEOFCRDeletions = true;
120
- const lastIndex = currentContent.deletions.length - 1;
121
- if (lastIndex >= 0) currentContent.deletions[lastIndex] = cleanLastNewline(currentContent.deletions[lastIndex]);
122
- } else if (lastLineType === "addition") {
123
- currentContent.noEOFCRAdditions = true;
124
- const lastIndex = currentContent.additions.length - 1;
125
- if (lastIndex >= 0) currentContent.additions[lastIndex] = cleanLastNewline(currentContent.additions[lastIndex]);
126
- }
87
+ if (line.startsWith("deleted file mode")) {
88
+ currentFile.type = "deleted";
89
+ currentFile.mode = line.replace("deleted file mode", "").trim();
127
90
  }
91
+ if (line.startsWith("similarity index")) if (line.startsWith("similarity index 100%")) currentFile.type = "rename-pure";
92
+ else currentFile.type = "rename-changed";
93
+ if (line.startsWith("index ")) {
94
+ const [, mode] = line.trim().match(FILE_MODE_FROM_INDEX) ?? [];
95
+ if (mode != null) currentFile.mode = mode;
96
+ }
97
+ if (line.startsWith("rename from ")) currentFile.prevName = line.replace("rename from ", "");
98
+ if (line.startsWith("rename to ")) currentFile.name = line.replace("rename to ", "").trim();
128
99
  }
129
100
  }
130
- const hunkData = {
131
- collapsedBefore: 0,
132
- splitLineCount: 0,
133
- splitLineStart: 0,
134
- unifiedLineCount: 0,
135
- unifiedLineStart: 0,
136
- additionCount: parseInt(match[4] ?? "1"),
137
- additionStart: parseInt(match[3]),
138
- additionLines,
139
- deletionCount: parseInt(match[2] ?? "1"),
140
- deletionStart: parseInt(match[1]),
141
- deletionLines,
142
- hunkContent,
143
- hunkContext: match[5],
144
- hunkSpecs: firstLine
145
- };
146
- if (isNaN(hunkData.additionCount) || isNaN(hunkData.deletionCount) || isNaN(hunkData.additionStart) || isNaN(hunkData.deletionStart)) {
147
- console.error("parsePatchContent: invalid hunk metadata", hunkData);
101
+ continue;
102
+ }
103
+ let currentContent;
104
+ let lastLineType;
105
+ 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();
106
+ const additionStart = parseInt(fileHeaderMatch[3]);
107
+ const deletionStart = parseInt(fileHeaderMatch[1]);
108
+ deletionLineIndex = isPartial ? deletionLineIndex : deletionStart - 1;
109
+ additionLineIndex = isPartial ? additionLineIndex : additionStart - 1;
110
+ const hunkData = {
111
+ collapsedBefore: 0,
112
+ splitLineCount: 0,
113
+ splitLineStart: 0,
114
+ unifiedLineCount: 0,
115
+ unifiedLineStart: 0,
116
+ additionCount: parseInt(fileHeaderMatch[4] ?? "1"),
117
+ additionStart,
118
+ additionLines,
119
+ deletionCount: parseInt(fileHeaderMatch[2] ?? "1"),
120
+ deletionStart,
121
+ deletionLines,
122
+ deletionLineIndex,
123
+ additionLineIndex,
124
+ hunkContent: [],
125
+ hunkContext: fileHeaderMatch[5],
126
+ hunkSpecs: firstLine,
127
+ noEOFCRAdditions: false,
128
+ noEOFCRDeletions: false
129
+ };
130
+ if (isNaN(hunkData.additionCount) || isNaN(hunkData.deletionCount) || isNaN(hunkData.additionStart) || isNaN(hunkData.deletionStart)) {
131
+ console.error("parsePatchContent: invalid hunk metadata", hunkData);
132
+ continue;
133
+ }
134
+ for (const rawLine of lines) {
135
+ const parsedLine = parseLineType(rawLine);
136
+ if (parsedLine == null) {
137
+ console.error("processFile: invalid rawLine:", rawLine);
148
138
  continue;
149
139
  }
150
- hunkData.collapsedBefore = Math.max(hunkData.additionStart - 1 - lastHunkEnd, 0);
151
- currentFile.hunks.push(hunkData);
152
- lastHunkEnd = hunkData.additionStart + hunkData.additionCount - 1;
153
- for (const content of hunkContent) if (content.type === "context") {
154
- hunkData.splitLineCount += content.lines.length;
155
- hunkData.unifiedLineCount += content.lines.length;
156
- } else {
157
- hunkData.splitLineCount += Math.max(content.additions.length, content.deletions.length);
158
- hunkData.unifiedLineCount += content.deletions.length + content.additions.length;
140
+ const { type, line } = parsedLine;
141
+ if (type === "addition") {
142
+ if (currentContent == null || currentContent.type !== "change") {
143
+ currentContent = createContentGroup("change", deletionLineIndex, additionLineIndex);
144
+ hunkData.hunkContent.push(currentContent);
145
+ }
146
+ additionLineIndex++;
147
+ if (isPartial) currentFile.additionLines.push(line);
148
+ currentContent.additions++;
149
+ additionLines++;
150
+ lastLineType = "addition";
151
+ } else if (type === "deletion") {
152
+ if (currentContent == null || currentContent.type !== "change") {
153
+ currentContent = createContentGroup("change", deletionLineIndex, additionLineIndex);
154
+ hunkData.hunkContent.push(currentContent);
155
+ }
156
+ deletionLineIndex++;
157
+ if (isPartial) currentFile.deletionLines.push(line);
158
+ currentContent.deletions++;
159
+ deletionLines++;
160
+ lastLineType = "deletion";
161
+ } else if (type === "context") {
162
+ if (currentContent == null || currentContent.type !== "context") {
163
+ currentContent = createContentGroup("context", deletionLineIndex, additionLineIndex);
164
+ hunkData.hunkContent.push(currentContent);
165
+ }
166
+ additionLineIndex++;
167
+ deletionLineIndex++;
168
+ if (isPartial) {
169
+ currentFile.deletionLines.push(line);
170
+ currentFile.additionLines.push(line);
171
+ }
172
+ currentContent.lines++;
173
+ lastLineType = "context";
174
+ } else if (type === "metadata" && currentContent != null) {
175
+ if (currentContent.type === "context") {
176
+ hunkData.noEOFCRAdditions = true;
177
+ hunkData.noEOFCRDeletions = true;
178
+ } else if (lastLineType === "deletion") hunkData.noEOFCRDeletions = true;
179
+ else if (lastLineType === "addition") hunkData.noEOFCRAdditions = true;
180
+ if (isPartial && (lastLineType === "addition" || lastLineType === "context")) {
181
+ const lastIndex = currentFile.additionLines.length - 1;
182
+ if (lastIndex >= 0) currentFile.additionLines[lastIndex] = cleanLastNewline(currentFile.additionLines[lastIndex]);
183
+ }
184
+ if (isPartial && (lastLineType === "deletion" || lastLineType === "context")) {
185
+ const lastIndex = currentFile.deletionLines.length - 1;
186
+ if (lastIndex >= 0) currentFile.deletionLines[lastIndex] = cleanLastNewline(currentFile.deletionLines[lastIndex]);
187
+ }
159
188
  }
160
- hunkData.splitLineStart = currentFile.splitLineCount;
161
- hunkData.unifiedLineStart = currentFile.unifiedLineCount;
162
- currentFile.splitLineCount += hunkData.splitLineCount;
163
- currentFile.unifiedLineCount += hunkData.unifiedLineCount;
164
189
  }
165
- if (currentFile != null) {
166
- if (!isGitDiff && currentFile.prevName != null && currentFile.name !== currentFile.prevName) if (currentFile.hunks.length > 0) currentFile.type = "rename-changed";
167
- else currentFile.type = "rename-pure";
168
- if (currentFile.type !== "rename-pure" && currentFile.type !== "rename-changed") currentFile.prevName = void 0;
169
- files.push(currentFile);
190
+ hunkData.additionLines = additionLines;
191
+ hunkData.deletionLines = deletionLines;
192
+ hunkData.collapsedBefore = Math.max(hunkData.additionStart - 1 - lastHunkEnd, 0);
193
+ currentFile.hunks.push(hunkData);
194
+ lastHunkEnd = hunkData.additionStart + hunkData.additionCount - 1;
195
+ for (const content of hunkData.hunkContent) if (content.type === "context") {
196
+ hunkData.splitLineCount += content.lines;
197
+ hunkData.unifiedLineCount += content.lines;
198
+ } else {
199
+ hunkData.splitLineCount += Math.max(content.additions, content.deletions);
200
+ hunkData.unifiedLineCount += content.deletions + content.additions;
170
201
  }
202
+ hunkData.splitLineStart = currentFile.splitLineCount + hunkData.collapsedBefore;
203
+ hunkData.unifiedLineStart = currentFile.unifiedLineCount + hunkData.collapsedBefore;
204
+ currentFile.splitLineCount += hunkData.collapsedBefore + hunkData.splitLineCount;
205
+ currentFile.unifiedLineCount += hunkData.collapsedBefore + hunkData.unifiedLineCount;
171
206
  }
172
- return {
173
- patchMetadata,
174
- files
175
- };
207
+ if (currentFile == null) return;
208
+ if (currentFile.hunks.length > 0 && !isPartial && currentFile.additionLines.length > 0 && currentFile.deletionLines.length > 0) {
209
+ const lastHunk = currentFile.hunks[currentFile.hunks.length - 1];
210
+ const lastHunkEnd$1 = lastHunk.additionStart + lastHunk.additionCount - 1;
211
+ const totalFileLines = currentFile.additionLines.length;
212
+ const collapsedAfter = Math.max(totalFileLines - lastHunkEnd$1, 0);
213
+ currentFile.splitLineCount += collapsedAfter;
214
+ currentFile.unifiedLineCount += collapsedAfter;
215
+ }
216
+ if (!isGitDiff) {
217
+ if (currentFile.prevName != null && currentFile.name !== currentFile.prevName) if (currentFile.hunks.length > 0) currentFile.type = "rename-changed";
218
+ else currentFile.type = "rename-pure";
219
+ else if (newFile != null && newFile.contents === "") currentFile.type = "deleted";
220
+ else if (oldFile != null && oldFile.contents === "") currentFile.type = "new";
221
+ }
222
+ if (currentFile.type !== "rename-pure" && currentFile.type !== "rename-changed") currentFile.prevName = void 0;
223
+ return currentFile;
176
224
  }
177
225
  /**
178
226
  * Parses a patch file string into an array of parsed patches.
@@ -191,21 +239,22 @@ function parsePatchFiles(data, cacheKeyPrefix) {
191
239
  }
192
240
  return patches;
193
241
  }
194
- function createContentGroup(type) {
242
+ function createContentGroup(type, deletionLineIndex, additionLineIndex) {
195
243
  if (type === "change") return {
196
244
  type: "change",
197
- additions: [],
198
- deletions: [],
199
- noEOFCRAdditions: false,
200
- noEOFCRDeletions: false
245
+ additions: 0,
246
+ deletions: 0,
247
+ additionLineIndex,
248
+ deletionLineIndex
201
249
  };
202
250
  return {
203
251
  type: "context",
204
- lines: [],
205
- noEOFCR: false
252
+ lines: 0,
253
+ additionLineIndex,
254
+ deletionLineIndex
206
255
  };
207
256
  }
208
257
 
209
258
  //#endregion
210
- export { parsePatchFiles };
259
+ export { parsePatchFiles, processFile, processPatch };
211
260
  //# sourceMappingURL=parsePatchFiles.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"parsePatchFiles.js","names":["patchMetadata: string | undefined","files: FileDiffMetadata[]","currentFile: FileDiffMetadata | undefined","hunkContent: (ContextContent | ChangeContent)[]","currentContent: ContextContent | ChangeContent | undefined","lastLineType: 'context' | 'addition' | 'deletion' | undefined","hunkData: Hunk","patches: ParsedPatch[]"],"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 FILE_CONTEXT_BLOB,\n FILE_MODE_FROM_INDEX,\n GIT_DIFF_FILE_BREAK_REGEX,\n HUNK_HEADER,\n SPLIT_WITH_NEWLINES,\n UNIFIED_DIFF_FILE_BREAK_REGEX,\n} from '../constants';\nimport type {\n ChangeContent,\n ContextContent,\n FileDiffMetadata,\n Hunk,\n ParsedPatch,\n} from '../types';\nimport { cleanLastNewline } from './cleanLastNewline';\nimport { parseLineType } from './parseLineType';\n\nfunction processPatch(data: string, cacheKeyPrefix?: string): 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 let currentFile: FileDiffMetadata | undefined;\n for (const file of rawFiles) {\n if (isGitDiff && !GIT_DIFF_FILE_BREAK_REGEX.test(file)) {\n if (patchMetadata == null) {\n patchMetadata = file;\n } else {\n console.error('parsePatchContent: unknown file blob:', file);\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 (!isGitDiff && !UNIFIED_DIFF_FILE_BREAK_REGEX.test(file)) {\n if (patchMetadata == null) {\n patchMetadata = file;\n } else {\n console.error('parsePatchContent: unknown file blob:', file);\n }\n continue;\n }\n let lastHunkEnd = 0;\n const hunks = file.split(FILE_CONTEXT_BLOB);\n currentFile = undefined;\n for (const hunk of hunks) {\n const lines = hunk.split(SPLIT_WITH_NEWLINES);\n const firstLine = lines.shift();\n if (firstLine == null) {\n console.error('parsePatchContent: invalid hunk', hunk);\n continue;\n }\n const match = firstLine.match(HUNK_HEADER);\n const hunkContent: (ContextContent | ChangeContent)[] = [];\n let additionLines = 0;\n let deletionLines = 0;\n if (match == null || currentFile == null) {\n if (currentFile != null) {\n console.error('parsePatchContent: Invalid hunk', hunk);\n continue;\n }\n currentFile = {\n name: '',\n prevName: undefined,\n type: 'change',\n hunks: [],\n splitLineCount: 0,\n unifiedLineCount: 0,\n cacheKey:\n cacheKeyPrefix != null\n ? `${cacheKeyPrefix}-${files.length}`\n : undefined,\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.oldMode = 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 [, mode] = line.trim().match(FILE_MODE_FROM_INDEX) ?? [];\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 ', '');\n }\n if (line.startsWith('rename to ')) {\n currentFile.name = line.replace('rename to ', '').trim();\n }\n }\n }\n continue;\n } else {\n let currentContent: ContextContent | ChangeContent | undefined;\n let lastLineType: 'context' | 'addition' | 'deletion' | undefined;\n // Strip trailing bare newlines (format-patch separators between commits)\n while (\n lines.length > 0 &&\n (lines[lines.length - 1] === '\\n' || lines[lines.length - 1] === '')\n ) {\n lines.pop();\n }\n for (const rawLine of lines) {\n const parsedLine = parseLineType(rawLine);\n if (parsedLine == null) {\n continue;\n }\n const { type, line } = parsedLine;\n if (type === 'addition') {\n if (currentContent == null || currentContent.type !== 'change') {\n currentContent = createContentGroup('change');\n hunkContent.push(currentContent);\n }\n currentContent.additions.push(line);\n additionLines++;\n lastLineType = 'addition';\n } else if (type === 'deletion') {\n if (currentContent == null || currentContent.type !== 'change') {\n currentContent = createContentGroup('change');\n hunkContent.push(currentContent);\n }\n currentContent.deletions.push(line);\n deletionLines++;\n lastLineType = 'deletion';\n } else if (type === 'context') {\n if (currentContent == null || currentContent.type !== 'context') {\n currentContent = createContentGroup('context');\n hunkContent.push(currentContent);\n }\n currentContent.lines.push(line);\n lastLineType = 'context';\n } else if (type === 'metadata' && currentContent != null) {\n if (currentContent.type === 'context') {\n currentContent.noEOFCR = true;\n } else if (lastLineType === 'deletion') {\n currentContent.noEOFCRDeletions = true;\n const lastIndex = currentContent.deletions.length - 1;\n if (lastIndex >= 0) {\n currentContent.deletions[lastIndex] = cleanLastNewline(\n currentContent.deletions[lastIndex]\n );\n }\n } else if (lastLineType === 'addition') {\n currentContent.noEOFCRAdditions = true;\n const lastIndex = currentContent.additions.length - 1;\n if (lastIndex >= 0) {\n currentContent.additions[lastIndex] = cleanLastNewline(\n currentContent.additions[lastIndex]\n );\n }\n }\n }\n }\n }\n const hunkData: Hunk = {\n collapsedBefore: 0,\n splitLineCount: 0,\n splitLineStart: 0,\n unifiedLineCount: 0,\n unifiedLineStart: 0,\n additionCount: parseInt(match[4] ?? '1'),\n additionStart: parseInt(match[3]),\n additionLines,\n deletionCount: parseInt(match[2] ?? '1'),\n deletionStart: parseInt(match[1]),\n deletionLines,\n hunkContent,\n hunkContext: match[5],\n hunkSpecs: firstLine,\n };\n if (\n isNaN(hunkData.additionCount) ||\n isNaN(hunkData.deletionCount) ||\n isNaN(hunkData.additionStart) ||\n isNaN(hunkData.deletionStart)\n ) {\n console.error('parsePatchContent: invalid hunk metadata', hunkData);\n continue;\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 hunkContent) {\n if (content.type === 'context') {\n hunkData.splitLineCount += content.lines.length;\n hunkData.unifiedLineCount += content.lines.length;\n } else {\n hunkData.splitLineCount += Math.max(\n content.additions.length,\n content.deletions.length\n );\n hunkData.unifiedLineCount +=\n content.deletions.length + content.additions.length;\n }\n }\n hunkData.splitLineStart = currentFile.splitLineCount;\n hunkData.unifiedLineStart = currentFile.unifiedLineCount;\n\n currentFile.splitLineCount += hunkData.splitLineCount;\n currentFile.unifiedLineCount += hunkData.unifiedLineCount;\n }\n if (currentFile != null) {\n if (\n !isGitDiff &&\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 if (\n currentFile.type !== 'rename-pure' &&\n currentFile.type !== 'rename-changed'\n ) {\n currentFile.prevName = undefined;\n }\n files.push(currentFile);\n }\n }\n return { patchMetadata, files };\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): 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 )\n );\n } catch (error) {\n console.error(error);\n }\n }\n return patches;\n}\n\nfunction createContentGroup(type: 'change'): ChangeContent;\nfunction createContentGroup(type: 'context'): ContextContent;\nfunction createContentGroup(\n type: 'change' | 'context'\n): ChangeContent | ContextContent {\n if (type === 'change') {\n return {\n type: 'change',\n additions: [],\n deletions: [],\n noEOFCRAdditions: false,\n noEOFCRDeletions: false,\n };\n }\n return { type: 'context', lines: [], noEOFCR: false };\n}\n"],"mappings":";;;;;AAsBA,SAAS,aAAa,MAAc,gBAAsC;CACxE,MAAM,YAAY,0BAA0B,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,MAAM,MAAM;;AAGxB,QAAO;;AAKT,SAAS,mBACP,MACgC;AAChC,KAAI,SAAS,SACX,QAAO;EACL,MAAM;EACN,WAAW,EAAE;EACb,WAAW,EAAE;EACb,kBAAkB;EAClB,kBAAkB;EACnB;AAEH,QAAO;EAAE,MAAM;EAAW,OAAO,EAAE;EAAE,SAAS;EAAO"}
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 FILENAME_HEADER_REGEX,\n FILENAME_HEADER_REGEX_GIT,\n FILE_CONTEXT_BLOB,\n FILE_MODE_FROM_INDEX,\n GIT_DIFF_FILE_BREAK_REGEX,\n HUNK_HEADER,\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): 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 console.error(\n 'parsePatchContent: unknown file blob:',\n fileOrPatchMetadata\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 console.error(\n 'parsePatchContent: unknown file blob:',\n fileOrPatchMetadata\n );\n }\n continue;\n }\n const currentFile = processFile(fileOrPatchMetadata, {\n cacheKey:\n cacheKeyPrefix != null\n ? `${cacheKeyPrefix}-${files.length}`\n : undefined,\n isGitDiff,\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}\n\nexport function processFile(\n fileDiffString: string,\n {\n cacheKey,\n isGitDiff = GIT_DIFF_FILE_BREAK_REGEX.test(fileDiffString),\n oldFile,\n newFile,\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 console.error('parsePatchContent: invalid hunk', hunk);\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 console.error('parsePatchContent: Invalid hunk', hunk);\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 [, mode] = line.trim().match(FILE_MODE_FROM_INDEX) ?? [];\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 ', '');\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 console.error('parsePatchContent: invalid hunk metadata', hunkData);\n continue;\n }\n\n // Now we process each line of the hunk\n for (const rawLine of lines) {\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 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 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 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 (newFile != null && newFile.contents === '') {\n currentFile.type = 'deleted';\n } else if (oldFile != null && oldFile.contents === '') {\n currentFile.type = 'new';\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): 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 )\n );\n } catch (error) {\n console.error(error);\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;CACb,MAAM,YAAY,0BAA0B,KAAK,KAAK;CACtD,MAAM,WAAW,KAAK,MACpB,YAAY,4BAA4B,8BACzC;CACD,IAAIA;CACJ,MAAMC,QAA4B,EAAE;AACpC,MAAK,MAAM,uBAAuB,UAA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ACe;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,OACL,CACF;UACM,OAAO;AACd,UAAQ,MAAM,MAAM;;AAGxB,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"}
@@ -2,14 +2,15 @@ import { createHastElement, createTextNodeElement } from "./hast_utils.js";
2
2
 
3
3
  //#region src/utils/processLine.ts
4
4
  function processLine(node, line, state) {
5
- const lineInfo = typeof state.lineInfo === "function" ? state.lineInfo(line) : state.lineInfo[line];
5
+ const lineInfo = typeof state.lineInfo === "function" ? state.lineInfo(line) : state.lineInfo[line - 1];
6
6
  if (lineInfo == null) {
7
- console.error({
7
+ const errorMessage = `processLine: line ${line}, contains no state.lineInfo`;
8
+ console.error(errorMessage, {
8
9
  node,
9
10
  line,
10
11
  state
11
12
  });
12
- throw new Error(`processLine: line ${line}, contains no state.lineInfo`);
13
+ throw new Error(errorMessage);
13
14
  }
14
15
  node.tagName = "span";
15
16
  node.properties["data-column-content"] = "";
@@ -1 +1 @@
1
- {"version":3,"file":"processLine.js","names":[],"sources":["../../src/utils/processLine.ts"],"sourcesContent":["import type { ElementContent, Element as HASTElement } from 'hast';\n\nimport type { SharedRenderState } from '../types';\nimport { createHastElement, createTextNodeElement } from './hast_utils';\n\nexport function processLine(\n node: HASTElement,\n line: number,\n state: SharedRenderState\n): ElementContent {\n const lineInfo =\n typeof state.lineInfo === 'function'\n ? state.lineInfo(line)\n : state.lineInfo[line];\n if (lineInfo == null) {\n console.error({ node, line, state });\n throw new Error(`processLine: line ${line}, contains no state.lineInfo`);\n }\n // We need to convert the current line to a div but keep all the decorations\n // that may be applied\n node.tagName = 'span';\n node.properties['data-column-content'] = '';\n\n // NOTE(amadeus): We need to push newline characters into empty rows or else\n // copy/pasta will have issues\n if (node.children.length === 0) {\n node.children.push(createTextNodeElement('\\n'));\n }\n const children = [\n createHastElement({\n tagName: 'span',\n children: [\n createHastElement({\n tagName: 'span',\n children: [{ type: 'text', value: `${lineInfo.lineNumber}` }],\n properties: { 'data-line-number-content': '' },\n }),\n ],\n properties: { 'data-column-number': '' },\n }),\n node,\n ];\n return createHastElement({\n tagName: 'div',\n children,\n properties: {\n 'data-line': lineInfo.lineNumber,\n 'data-alt-line': lineInfo.altLineNumber,\n 'data-line-type': lineInfo.type,\n 'data-line-index': lineInfo.lineIndex,\n },\n });\n}\n"],"mappings":";;;AAKA,SAAgB,YACd,MACA,MACA,OACgB;CAChB,MAAM,WACJ,OAAO,MAAM,aAAa,aACtB,MAAM,SAAS,KAAK,GACpB,MAAM,SAAS;AACrB,KAAI,YAAY,MAAM;AACpB,UAAQ,MAAM;GAAE;GAAM;GAAM;GAAO,CAAC;AACpC,QAAM,IAAI,MAAM,qBAAqB,KAAK,8BAA8B;;AAI1E,MAAK,UAAU;AACf,MAAK,WAAW,yBAAyB;AAIzC,KAAI,KAAK,SAAS,WAAW,EAC3B,MAAK,SAAS,KAAK,sBAAsB,KAAK,CAAC;AAgBjD,QAAO,kBAAkB;EACvB,SAAS;EACT,UAhBe,CACf,kBAAkB;GAChB,SAAS;GACT,UAAU,CACR,kBAAkB;IAChB,SAAS;IACT,UAAU,CAAC;KAAE,MAAM;KAAQ,OAAO,GAAG,SAAS;KAAc,CAAC;IAC7D,YAAY,EAAE,4BAA4B,IAAI;IAC/C,CAAC,CACH;GACD,YAAY,EAAE,sBAAsB,IAAI;GACzC,CAAC,EACF,KACD;EAIC,YAAY;GACV,aAAa,SAAS;GACtB,iBAAiB,SAAS;GAC1B,kBAAkB,SAAS;GAC3B,mBAAmB,SAAS;GAC7B;EACF,CAAC"}
1
+ {"version":3,"file":"processLine.js","names":[],"sources":["../../src/utils/processLine.ts"],"sourcesContent":["import type { ElementContent, Element as HASTElement } from 'hast';\n\nimport type { SharedRenderState } from '../types';\nimport { createHastElement, createTextNodeElement } from './hast_utils';\n\nexport function processLine(\n node: HASTElement,\n line: number,\n state: SharedRenderState\n): ElementContent {\n const lineInfo =\n typeof state.lineInfo === 'function'\n ? state.lineInfo(line)\n : state.lineInfo[line - 1];\n if (lineInfo == null) {\n const errorMessage = `processLine: line ${line}, contains no state.lineInfo`;\n console.error(errorMessage, { node, line, state });\n throw new Error(errorMessage);\n }\n // We need to convert the current line to a div but keep all the decorations\n // that may be applied\n node.tagName = 'span';\n node.properties['data-column-content'] = '';\n\n // NOTE(amadeus): We need to push newline characters into empty rows or else\n // copy/pasta will have issues\n if (node.children.length === 0) {\n node.children.push(createTextNodeElement('\\n'));\n }\n const children = [\n createHastElement({\n tagName: 'span',\n children: [\n createHastElement({\n tagName: 'span',\n children: [{ type: 'text', value: `${lineInfo.lineNumber}` }],\n properties: { 'data-line-number-content': '' },\n }),\n ],\n properties: { 'data-column-number': '' },\n }),\n node,\n ];\n return createHastElement({\n tagName: 'div',\n children,\n properties: {\n 'data-line': lineInfo.lineNumber,\n 'data-alt-line': lineInfo.altLineNumber,\n 'data-line-type': lineInfo.type,\n 'data-line-index': lineInfo.lineIndex,\n },\n });\n}\n"],"mappings":";;;AAKA,SAAgB,YACd,MACA,MACA,OACgB;CAChB,MAAM,WACJ,OAAO,MAAM,aAAa,aACtB,MAAM,SAAS,KAAK,GACpB,MAAM,SAAS,OAAO;AAC5B,KAAI,YAAY,MAAM;EACpB,MAAM,eAAe,qBAAqB,KAAK;AAC/C,UAAQ,MAAM,cAAc;GAAE;GAAM;GAAM;GAAO,CAAC;AAClD,QAAM,IAAI,MAAM,aAAa;;AAI/B,MAAK,UAAU;AACf,MAAK,WAAW,yBAAyB;AAIzC,KAAI,KAAK,SAAS,WAAW,EAC3B,MAAK,SAAS,KAAK,sBAAsB,KAAK,CAAC;AAgBjD,QAAO,kBAAkB;EACvB,SAAS;EACT,UAhBe,CACf,kBAAkB;GAChB,SAAS;GACT,UAAU,CACR,kBAAkB;IAChB,SAAS;IACT,UAAU,CAAC;KAAE,MAAM;KAAQ,OAAO,GAAG,SAAS;KAAc,CAAC;IAC7D,YAAY,EAAE,4BAA4B,IAAI;IAC/C,CAAC,CACH;GACD,YAAY,EAAE,sBAAsB,IAAI;GACzC,CAAC,EACF,KACD;EAIC,YAAY;GACV,aAAa,SAAS;GACtB,iBAAiB,SAAS;GAC1B,kBAAkB,SAAS;GAC3B,mBAAmB,SAAS;GAC7B;EACF,CAAC"}
@@ -1,7 +1,12 @@
1
- import { DiffsHighlighter, FileDiffMetadata, RenderDiffOptions, ThemedDiffResult } from "../types.js";
1
+ import { DiffsHighlighter, FileDiffMetadata, ForcePlainTextOptions, RenderDiffOptions, ThemedDiffResult } from "../types.js";
2
2
 
3
3
  //#region src/utils/renderDiffWithHighlighter.d.ts
4
- declare function renderDiffWithHighlighter(diff: FileDiffMetadata, highlighter: DiffsHighlighter, options: RenderDiffOptions, forcePlainText?: boolean): ThemedDiffResult;
4
+ declare function renderDiffWithHighlighter(diff: FileDiffMetadata, highlighter: DiffsHighlighter, options: RenderDiffOptions, {
5
+ forcePlainText,
6
+ startingLine,
7
+ totalLines,
8
+ expandedHunks
9
+ }?: ForcePlainTextOptions): ThemedDiffResult;
5
10
  //#endregion
6
11
  export { renderDiffWithHighlighter };
7
12
  //# sourceMappingURL=renderDiffWithHighlighter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"renderDiffWithHighlighter.d.ts","names":[],"sources":["../../src/utils/renderDiffWithHighlighter.ts"],"sourcesContent":[],"mappings":";;;iBA6BgB,yBAAA,OACR,+BACO,2BACJ,8CAER"}
1
+ {"version":3,"file":"renderDiffWithHighlighter.d.ts","names":[],"sources":["../../src/utils/renderDiffWithHighlighter.ts"],"sourcesContent":[],"mappings":";;;iBAkCgB,yBAAA,OACR,+BACO,2BACJ;;;;;IAMN,wBACF"}