Skip to content

Commit 1f4e4cf

Browse files
committed
Refactor patch extraction logic to correctly handle text blocks preceding diff blocks, ensuring path lines marked with ### are removed and non-diff code blocks are treated as surrounding text unless they are file hints
1 parent ceac0f2 commit 1f4e4cf

5 files changed

Lines changed: 40 additions & 26 deletions

File tree

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

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ const remove_path_line_from_text_block = (params: {
172172
is_single_root_folder_workspace: params.is_single_root
173173
})
174174
if (relative_path === params.target_file_path) {
175-
path_line_index = i
175+
if (line.startsWith('###')) {
176+
path_line_index = i
177+
}
176178
break
177179
}
178180
}
@@ -638,11 +640,11 @@ const extract_all_code_block_patches = (params: {
638640
}
639641

640642
const text_before = lines.slice(last_block_end + 1, block.start).join('\n')
641-
if (text_before.trim()) {
642-
items.push({ type: 'text', content: text_before.trim() })
643-
}
644643

645644
if (block.type == 'diff' || block.type == 'patch') {
645+
if (text_before.trim()) {
646+
items.push({ type: 'text', content: text_before.trim() })
647+
}
646648
const patches = diff_block_patches.get(block.start) || []
647649
if (patches.length > 0) {
648650
const last_item = items.length > 0 ? items[items.length - 1] : undefined
@@ -659,6 +661,7 @@ const extract_all_code_block_patches = (params: {
659661
}
660662
}
661663
items.push(...patches)
664+
last_block_end = block.end
662665
} else {
663666
// Check if this block was a hint for the next block.
664667
if (i < code_blocks.length - 1) {
@@ -670,6 +673,9 @@ const extract_all_code_block_patches = (params: {
670673
const match = non_empty_lines[0].match(xml_path_regex)
671674
if (match && match[1]) {
672675
// This was a hint for the next block, so we should skip processing it as a file content block.
676+
if (text_before.trim()) {
677+
items.push({ type: 'text', content: text_before.trim() })
678+
}
673679
last_block_end = block.end
674680
continue
675681
}
@@ -691,35 +697,35 @@ const extract_all_code_block_patches = (params: {
691697
})
692698

693699
if (patch) {
694-
const last_item = items.length > 0 ? items[items.length - 1] : undefined
695-
if (last_item && last_item.type == 'text') {
696-
remove_path_line_from_text_block({
697-
text_item: last_item,
698-
target_file_path: patch.file_path,
699-
is_single_root: params.is_single_root
700-
})
701-
702-
if (!last_item.content) {
703-
items.pop()
704-
}
705-
}
706-
707700
const file_key = `${patch.workspace_name || ''}:${patch.file_path}`
708-
709701
if (!files_with_diffs.has(file_key)) {
702+
if (text_before.trim()) {
703+
items.push({ type: 'text', content: text_before.trim() })
704+
}
705+
const last_item =
706+
items.length > 0 ? items[items.length - 1] : undefined
707+
if (last_item && last_item.type == 'text') {
708+
remove_path_line_from_text_block({
709+
text_item: last_item,
710+
target_file_path: patch.file_path,
711+
is_single_root: params.is_single_root
712+
})
713+
714+
if (!last_item.content) {
715+
items.pop()
716+
}
717+
}
718+
710719
if (hint_result.workspace_name && !patch.workspace_name) {
711720
patch.workspace_name = hint_result.workspace_name
712721
}
713722
items.push(patch)
714-
}
715-
} else {
716-
const block_content = lines.slice(block.start, block.end + 1).join('\n')
717-
if (block_lines.join('').trim()) {
718-
items.push({ type: 'text', content: block_content })
723+
last_block_end = block.end
719724
}
720725
}
726+
// If it's not a diff, patch, or hint, it's just a regular code block.
727+
// We don't do anything here, so it gets included in the next text block.
721728
}
722-
last_block_end = block.end
723729
}
724730

725731
if (last_block_end < lines.length - 1) {

‎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,3 +1,6 @@
11
Lorem ipsum.
2+
```bash
3+
pnpm add -D prettier
4+
```
25

36
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
Lorem ipsum.
2+
```bash
3+
pnpm add -D prettier
4+
```
25

36
### `src/index.ts`
47

Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
Lorem ipsum.
1+
Lorem ipsum.
2+
3+
<summary>src/lorem.html</summary>

0 commit comments

Comments
 (0)