Skip to content

Commit 7fd18da

Browse files
committed
Refactor diff patch extraction to prevent duplicate file path mentions by removing path hints from preceding text blocks when a corresponding diff block is found
1 parent 3d8ed57 commit 7fd18da

4 files changed

Lines changed: 44 additions & 22 deletions

File tree

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

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,11 @@ const extract_all_code_block_patches = (params: {
421421
if (potential_path.endsWith(':')) {
422422
potential_path = potential_path.slice(0, -1).trim()
423423
}
424+
const backtick_match = potential_path.match(/`([^`]+)`/)
425+
if (backtick_match && backtick_match[1]) {
426+
potential_path = backtick_match[1]
427+
}
428+
424429
if (
425430
potential_path &&
426431
(potential_path.includes('/') ||
@@ -507,21 +512,16 @@ const extract_all_code_block_patches = (params: {
507512
const last_item = items.length > 0 ? items[items.length - 1] : undefined
508513
if (last_item && last_item.type == 'text') {
509514
const content_lines = last_item.content.split('\n')
510-
let last_non_empty_line_index = -1
511-
let last_non_empty_line = ''
515+
let path_line_index = -1
516+
512517
for (let i = content_lines.length - 1; i >= 0; i--) {
513-
if (content_lines[i].trim() !== '') {
514-
last_non_empty_line_index = i
515-
last_non_empty_line = content_lines[i].trim()
516-
break
517-
}
518-
}
518+
const line = content_lines[i].trim()
519+
if (line == '') continue
519520

520-
if (last_non_empty_line) {
521-
let extracted = extract_path_from_line_of_code(last_non_empty_line)
521+
let extracted = extract_path_from_line_of_code(line)
522522

523523
if (!extracted) {
524-
let potential_path = last_non_empty_line
524+
let potential_path = line
525525
if (potential_path.endsWith(':')) {
526526
potential_path = potential_path.slice(0, -1).trim()
527527
}
@@ -548,20 +548,38 @@ const extract_all_code_block_patches = (params: {
548548
is_single_root_folder_workspace: params.is_single_root
549549
})
550550
if (relative_path === patches[0].file_path) {
551-
const new_content = content_lines
552-
.slice(0, last_non_empty_line_index)
553-
.join('\n')
554-
.trim()
551+
path_line_index = i
552+
break
553+
}
554+
}
555+
}
555556

556-
if (new_content) {
557-
last_item.content = new_content
558-
} else {
559-
items.pop()
560-
}
557+
if (path_line_index > -1) {
558+
const new_content_lines_without_path = content_lines.filter(
559+
(_, index) => index !== path_line_index
560+
)
561+
562+
const collapsed_lines: string[] = []
563+
for (const line of new_content_lines_without_path) {
564+
if (
565+
line.trim() == '' &&
566+
collapsed_lines.length > 0 &&
567+
collapsed_lines[collapsed_lines.length - 1].trim() === ''
568+
) {
569+
continue
561570
}
571+
collapsed_lines.push(line)
572+
}
573+
const new_content = collapsed_lines.join('\n').trim()
574+
575+
if (new_content) {
576+
last_item.content = new_content
577+
} else {
578+
items.pop()
562579
}
563580
}
564581
}
582+
565583
}
566584
items.push(...patches)
567585
} else {

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/diff-markdown-path-above/diff-markdown-path-above.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
src/index.ts:
1+
# `src/index.ts`:
22

33
```diff
44
@@ -1,3 +1,3 @@
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
Lorem ipsum.
2+
13
Lorem ipsum.

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/diff-markdown-repeated-path-above/diff-markdown-repeated-path-above.txt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Lorem ipsum.
22

3-
# `src/index.ts`:
3+
### `src/index.ts`
4+
5+
Lorem ipsum.
46

57
```diff
68
--- a/src/index.ts

0 commit comments

Comments
 (0)