Skip to content

Commit 28fc85f

Browse files
committed
Add support for parsing diffs with redundant deletion headers and prevent duplicate diff patches
1 parent 43e8013 commit 28fc85f

5 files changed

Lines changed: 47 additions & 7 deletions

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
@@ -1406,6 +1406,22 @@ describe('clipboard-parser', () => {
14061406
})
14071407
})
14081408

1409+
it('parses diff and deletion via redundant header', () => {
1410+
const test_case = 'diff-deletion-via-redundant-header'
1411+
const text = load_test_case_file(test_case, `${test_case}.txt`)
1412+
const result = parse_response({
1413+
response: text,
1414+
is_single_root_folder_workspace: true
1415+
})
1416+
1417+
expect(result).toHaveLength(1)
1418+
expect(result[0]).toMatchObject({
1419+
type: 'diff',
1420+
file_path: 'src/index.ts',
1421+
content: load_test_case_file(test_case, '1-file.txt')
1422+
})
1423+
})
1424+
14091425
it('parses a mix of a new file from heading and code block, and a separate diff block', () => {
14101426
const test_case = 'diff-mix-new-file-from-heading-and-diff'
14111427
const text = load_test_case_file(test_case, `${test_case}.txt`)

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -711,14 +711,26 @@ const extract_all_code_block_patches = (params: {
711711

712712
if (block.type == 'diff' || block.type == 'patch') {
713713
const text_before_lines = lines.slice(last_block_end + 1, block.start)
714-
items.push(
715-
...process_text_for_deleted_files({
716-
lines: text_before_lines,
717-
is_single_root: params.is_single_root
714+
const preceding_items = process_text_for_deleted_files({
715+
lines: text_before_lines,
716+
is_single_root: params.is_single_root
717+
})
718+
items.push(...preceding_items)
719+
720+
let patches = diff_block_patches.get(block.start) || []
721+
722+
if (patches.length > 0) {
723+
patches = patches.filter((p) => {
724+
const is_redundant = preceding_items.some(
725+
(item) =>
726+
item.type == 'diff' &&
727+
item.file_path == p.file_path &&
728+
item.workspace_name == p.workspace_name
729+
)
730+
return !is_redundant
718731
})
719-
)
732+
}
720733

721-
const patches = diff_block_patches.get(block.start) || []
722734
if (patches.length > 0) {
723735
const last_item = items.length > 0 ? items[items.length - 1] : undefined
724736
if (last_item && last_item.type == 'text') {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--- a/src/index.ts
2+
+++ /dev/null
3+
@@ +0,0 +0,0 @@
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
### Deleted file: `src/index.ts`
2+
3+
```diff
4+
--- a/src/index.ts
5+
+++ /dev/null
6+
@@ -1,2 +0,0 @@
7+
-console.log("hello")
8+
-console.log("old message")
9+
```

packages/vscode/src/commands/apply-chat-response-command/utils/preview/preview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export const preview = async (params: {
120120
})
121121
}
122122

123-
if (prepared_files.length === 0) {
123+
if (prepared_files.length == 0) {
124124
return null
125125
}
126126

0 commit comments

Comments
 (0)