Skip to content

Commit 23d0cf7

Browse files
committed
Refine code block parsing to remove preceding file path hints from text blocks when converting code blocks into new file diffs
1 parent 20d99ff commit 23d0cf7

5 files changed

Lines changed: 71 additions & 11 deletions

File tree

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

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -643,15 +643,71 @@ const extract_all_code_block_patches = (params: {
643643
})
644644

645645
if (patch) {
646-
const last_item = items.length > 0 ? items[items.length - 1] : undefined
647-
if (
648-
file_path_hint &&
649-
last_item &&
650-
last_item.type == 'text' &&
651-
(last_item.content.trim() == file_path_hint ||
652-
last_item.content.trim() == `\`${file_path_hint}\``)
653-
) {
654-
items.pop()
646+
const last_item =
647+
items.length > 0 ? items[items.length - 1] : undefined
648+
if (last_item && last_item.type == 'text') {
649+
const content_lines = last_item.content.split('\n')
650+
let last_non_empty_line_index = -1
651+
let last_non_empty_line = ''
652+
for (let i = content_lines.length - 1; i >= 0; i--) {
653+
if (content_lines[i].trim() !== '') {
654+
last_non_empty_line_index = i
655+
last_non_empty_line = content_lines[i].trim()
656+
break
657+
}
658+
}
659+
660+
if (last_non_empty_line) {
661+
let extracted = extract_path_from_line_of_code(last_non_empty_line)
662+
if (!extracted) {
663+
const xml_match =
664+
last_non_empty_line.match(/^<[^>]+>([^<]+)<\/[^>]+>$/)
665+
if (xml_match && xml_match[1]) {
666+
const potential_path = xml_match[1].trim()
667+
if (
668+
potential_path &&
669+
(potential_path.includes('/') ||
670+
potential_path.includes('\\') ||
671+
potential_path.includes('.')) &&
672+
!potential_path.includes(' ')
673+
) {
674+
extracted = potential_path
675+
}
676+
}
677+
}
678+
if (!extracted) {
679+
const match = last_non_empty_line.match(/`([^`]+)`/)
680+
if (match && match[1]) {
681+
const potential_path = match[1]
682+
if (
683+
potential_path.includes('/') ||
684+
potential_path.includes('\\') ||
685+
potential_path.includes('.')
686+
) {
687+
extracted = potential_path
688+
}
689+
}
690+
}
691+
692+
if (extracted) {
693+
const { relative_path } = extract_workspace_and_path({
694+
raw_file_path: extracted,
695+
is_single_root_folder_workspace: params.is_single_root
696+
})
697+
if (relative_path === patch.file_path) {
698+
const new_content = content_lines
699+
.slice(0, last_non_empty_line_index)
700+
.join('\n')
701+
.trim()
702+
703+
if (new_content) {
704+
last_item.content = new_content
705+
} else {
706+
items.pop()
707+
}
708+
}
709+
}
710+
}
655711
}
656712

657713
const file_key = `${patch.workspace_name || ''}:${patch.file_path}`
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
### New file: `src/lorem.html`
1+
Lorem ipsum.

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/diff-mix-new-file-from-heading-and-diff/diff-mix-new-file-from-heading-and-diff.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
Lorem ipsum.
2+
13
### New file: `src/lorem.html`
24

35
```html
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<summary>src/lorem.html</summary>
1+
Lorem ipsum.

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/diff-mix-new-file-from-xml-tag-heading-and-diff/diff-mix-new-file-from-xml-tag-heading-and-diff.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
Lorem ipsum.
2+
13
<summary>src/lorem.html</summary>
24

35
```html

0 commit comments

Comments
 (0)