Skip to content

Commit 256c952

Browse files
committed
Handle empty trailing lines in diff patches to improve parsing accuracy
1 parent ab4c2cd commit 256c952

6 files changed

Lines changed: 47 additions & 13 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,5 +2057,26 @@ describe('clipboard-parser', () => {
20572057
content: load_test_case_file('diffs', test_case, '1-file.txt')
20582058
})
20592059
})
2060+
2061+
it('parses diff format with unnecessary empty lines', () => {
2062+
const test_case = 'diff-markdown-unnecessary-empty-lines'
2063+
const text = load_test_case_file(
2064+
'diffs',
2065+
test_case,
2066+
'diff-markdown-path-above.txt'
2067+
)
2068+
const result = parse_response({
2069+
response: text,
2070+
is_single_root_folder_workspace: true
2071+
})
2072+
2073+
expect(result).toHaveLength(1)
2074+
expect(result[0]).toMatchObject({
2075+
type: 'diff',
2076+
file_path: 'src/index.ts',
2077+
content: load_test_case_file('diffs', test_case, '1-file.txt')
2078+
})
2079+
})
20602080
})
20612081
})
2082+

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,15 @@ const process_collected_patch_lines = (params: {
259259
patch_lines_array: string[]
260260
is_single_root: boolean
261261
}): Diff | null => {
262-
const joined_patch_text_for_checks = params.patch_lines_array.join('\n')
262+
const lines = [...params.patch_lines_array]
263+
while (lines.length > 0 && lines[lines.length - 1].trim() === '') {
264+
lines.pop()
265+
}
266+
267+
const joined_patch_text_for_checks = lines.join('\n')
263268
if (joined_patch_text_for_checks.trim() == '') return null
264269

265-
const { from_path, to_path } = extract_paths_from_lines(
266-
params.patch_lines_array
267-
)
270+
const { from_path, to_path } = extract_paths_from_lines(lines)
268271

269272
const is_new_file = from_path == '/dev/null'
270273
const is_deleted_file = to_path == '/dev/null'
@@ -283,15 +286,15 @@ const process_collected_patch_lines = (params: {
283286
message: 'Could not extract file path from collected patch lines.',
284287
data: {
285288
clipboard_start: joined_patch_text_for_checks.substring(0, 200),
286-
lines_count: params.patch_lines_array.length
289+
lines_count: lines.length
287290
}
288291
})
289292
return null
290293
}
291294

292-
const patch_start_idx = find_patch_start_index(params.patch_lines_array)
295+
const patch_start_idx = find_patch_start_index(lines)
293296
let content_str = build_patch_content({
294-
lines: params.patch_lines_array,
297+
lines,
295298
file_path,
296299
patch_start_index: patch_start_idx,
297300
is_single_root: params.is_single_root

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/diffs/diff-inner-triple-backticks/1-file.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@
1212
```
1313

1414
## Usage
15-
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,10 @@
1+
### Updated file: `src/index.ts`
2+
3+
```diff
4+
@@ -1,3 +1,3 @@
5+
console.log("hello")
6+
-console.log("old message")
7+
+console.log("new message")
8+
9+
10+
```

‎packages/vscode/src/commands/apply-chat-response-command/utils/diff-processor.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ export const apply_diff = (params: {
131131
let replace_chunks = []
132132

133133
let inside_replace_block = false
134-
let has_context_lines = false
135134

136135
for (let i = 0; i < patch_lines_normalized.length; i++) {
137136
const line = patch_lines_normalized[i]
@@ -146,7 +145,6 @@ export const apply_diff = (params: {
146145
search_chunks = []
147146
replace_chunks = []
148147
inside_replace_block = false
149-
has_context_lines = false
150148
}
151149

152150
if (line.startsWith('-') || line.startsWith('~')) {
@@ -161,7 +159,6 @@ export const apply_diff = (params: {
161159

162160
search_chunks = []
163161
replace_chunks = []
164-
has_context_lines = false
165162
}
166163

167164
if (line.startsWith('--')) {
@@ -177,10 +174,8 @@ export const apply_diff = (params: {
177174
// Also replace unchanged lines
178175
if (line.startsWith('~nnn')) {
179176
replace_chunks.push(line_original.replace(/^~nnn/, '') + '\n')
180-
has_context_lines = true
181177
} else if (line.startsWith('~')) {
182178
replace_chunks.push(line_original.replace(/^~/, '') + '\n')
183-
has_context_lines = true
184179
}
185180

186181
continue

0 commit comments

Comments
 (0)