Skip to content

Commit 59c38af

Browse files
committed
Improve diff parsing by correctly handling timestamps in file headers
1 parent 0f860cb commit 59c38af

2 files changed

Lines changed: 43 additions & 38 deletions

File tree

packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/clipboard-parser.spec.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -390,30 +390,30 @@ describe('clipboard-parser', () => {
390390
`)
391391
})
392392

393-
// it('should parse multiple diff files format in variant f', () => {
394-
// const text = load_clipboard_text('diff-multiple-files-variant-f.txt')
395-
// const result = parse_clipboard_content(text, true)
396-
397-
// expect(result.type).toBe('patches')
398-
// expect(result.patches).toHaveLength(2)
399-
400-
// expect(result.patches![0].file_path).toBe('src/lorem.ts')
401-
// expect(result.patches![1].file_path).toBe('src/ipsum.ts')
402-
403-
// expect(result.patches![0].content).toBe(`--- a/src/lorem.ts
404-
// +++ b/src/lorem.ts
405-
// @@ -1,3 +1,3 @@
406-
// console.log("hello")
407-
// -console.log("old lorem")
408-
// +console.log("new lorem")
409-
// `)
410-
// expect(result.patches![1].content).toBe(`--- a/src/ipsum.ts
411-
// +++ b/src/ipsum.ts
412-
// @@ -1,3 +1,3 @@
413-
// console.log("hello")
414-
// -console.log("old ipsum")
415-
// +console.log("new ipsum")
416-
// `)
417-
// })
393+
it('should parse multiple diff files format in variant f', () => {
394+
const text = load_clipboard_text('diff-multiple-files-variant-f.txt')
395+
const result = parse_clipboard_content(text, true)
396+
397+
expect(result.type).toBe('patches')
398+
expect(result.patches).toHaveLength(2)
399+
400+
expect(result.patches![0].file_path).toBe('src/lorem.ts')
401+
expect(result.patches![1].file_path).toBe('src/ipsum.ts')
402+
403+
expect(result.patches![0].content).toBe(`--- a/src/lorem.ts
404+
+++ b/src/lorem.ts
405+
@@ -1,3 +1,3 @@
406+
console.log("hello")
407+
-console.log("old lorem")
408+
+console.log("new lorem")
409+
`)
410+
expect(result.patches![1].content).toBe(`--- a/src/ipsum.ts
411+
+++ b/src/ipsum.ts
412+
@@ -1,3 +1,3 @@
413+
console.log("hello")
414+
-console.log("old ipsum")
415+
+console.log("new ipsum")
416+
`)
417+
})
418418
})
419419
})

packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/extract-diff-patches/extract-diff-patches.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ export const extract_diff_patches = (clipboard_text: string): DiffPatch[] => {
6363
// Extract patch content starting from the --- line
6464
const patch_lines = lines.slice(patch_start_index).map((line) => {
6565
if (line.startsWith('--- a/') || line.startsWith('+++ b/')) {
66-
return line.replace(/\t.*$/, '') // Remove everything after tab
66+
// Remove timestamps and everything after tab
67+
return line.replace(/\s+\d{4}-\d{2}-\d{2}.*$/, '').replace(/\t.*$/, '')
6768
}
6869
return line
6970
})
@@ -180,16 +181,20 @@ export const extract_diff_patches = (clipboard_text: string): DiffPatch[] => {
180181
const final_patch_lines = patch_content.split('\n')
181182
const formatted_patch_lines: string[] = []
182183
for (const patch_line of final_patch_lines) {
183-
// Check if line starts with @@ and has content after it without newline
184-
const hunk_match = patch_line.match(
185-
/^(@@ -\d+,\d+ \+\d+,\d+ @@)(.*)$/
186-
)
187-
if (hunk_match && hunk_match[2].trim() != '') {
188-
// Split hunk header and content onto separate lines
189-
formatted_patch_lines.push(hunk_match[1])
190-
formatted_patch_lines.push(hunk_match[2])
191-
} else {
192-
formatted_patch_lines.push(patch_line)
184+
// Remove timestamps from ---/+++ lines
185+
if (patch_line.startsWith('--- a/') || patch_line.startsWith('+++ b/')) {
186+
formatted_patch_lines.push(patch_line.replace(/\s+\d{4}-\d{2}-\d{2}.*$/, ''))
187+
}
188+
// Handle hunk headers
189+
else {
190+
const hunk_match = patch_line.match(/^(@@ -\d+,\d+ \+\d+,\d+ @@)(.*)$/)
191+
if (hunk_match && hunk_match[2].trim() != '') {
192+
// Split hunk header and content onto separate lines
193+
formatted_patch_lines.push(hunk_match[1])
194+
formatted_patch_lines.push(hunk_match[2])
195+
} else {
196+
formatted_patch_lines.push(patch_line)
197+
}
193198
}
194199
}
195200
patch_content = formatted_patch_lines.join('\n')
@@ -212,7 +217,7 @@ export const extract_diff_patches = (clipboard_text: string): DiffPatch[] => {
212217

213218
// Extract file path from the +++ line or diff --git line
214219
if (!current_file_path) {
215-
const file_path_match = line.match(/^\+\+\+ b\/(.+)$/)
220+
const file_path_match = line.match(/^\+\+\+ b\/(.+?)(?:\s+\d{4}-\d{2}-\d{2}.*)?$/)
216221
if (file_path_match) {
217222
current_file_path = file_path_match[1]
218223
} else {
@@ -230,4 +235,4 @@ export const extract_diff_patches = (clipboard_text: string): DiffPatch[] => {
230235

231236
return patches
232237
}
233-
}
238+
}

0 commit comments

Comments
 (0)