Skip to content

Commit 78ba3cc

Browse files
committed
Filter out empty lines in diff patches to correctly parse responses with interspersed blank lines
1 parent 930da94 commit 78ba3cc

4 files changed

Lines changed: 37 additions & 1 deletion

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,6 +2131,22 @@ describe('clipboard-parser', () => {
21312131
})
21322132
})
21332133

2134+
it('parses diff with empty lines interspersed between headers and content', () => {
2135+
const test_case = 'diff-with-empty-lines-between'
2136+
const text = load_test_case_file('diffs', test_case, `${test_case}.txt`)
2137+
const result = parse_response({
2138+
response: text,
2139+
is_single_root_folder_workspace: true
2140+
})
2141+
2142+
expect(result).toHaveLength(1)
2143+
expect(result[0]).toMatchObject({
2144+
type: 'diff',
2145+
file_path: 'src/index.ts',
2146+
content: load_test_case_file('diffs', test_case, '1-file.txt')
2147+
})
2148+
})
2149+
21342150
it('parses diff format with redundant closing backticks', () => {
21352151
const test_case = 'diff-markdown-redundant-closing-backticks'
21362152
const text = load_test_case_file(

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ const process_collected_patch_lines = (params: {
2222
patch_lines_array: string[]
2323
is_single_root: boolean
2424
}): DiffItem | null => {
25-
const lines = [...params.patch_lines_array]
25+
let lines = [...params.patch_lines_array]
26+
27+
lines = lines.filter((l) => l.length > 0)
28+
2629
while (lines.length > 0 && lines[lines.length - 1].trim() == '') {
2730
lines.pop()
2831
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
--- a/src/index.ts
2+
+++ b/src/index.ts
3+
@@ -1,3 +1,3 @@
4+
console.log("hello")
5+
-console.log("old message")
6+
+console.log("new message")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--- a/src/index.ts
2+
3+
+++ b/src/index.ts
4+
5+
@@ -1,3 +1,3 @@
6+
7+
console.log("hello")
8+
9+
-console.log("old message")
10+
11+
+console.log("new message")

0 commit comments

Comments
 (0)