Skip to content

Commit f22165e

Browse files
committed
Add support for parsing renamed file operations in diff patches
1 parent 6141100 commit f22165e

5 files changed

Lines changed: 89 additions & 9 deletions

File tree

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,28 @@ describe('clipboard-parser', () => {
17261726
})
17271727
})
17281728

1729+
it('parses a mix of a rename one-liner and a diff', () => {
1730+
const test_case = 'diff-mix-rename-one-liner-and-diff'
1731+
const text = load_test_case_file('diffs', test_case, `${test_case}.txt`)
1732+
const result = parse_response({
1733+
response: text,
1734+
is_single_root_folder_workspace: true
1735+
})
1736+
1737+
expect(result).toHaveLength(2)
1738+
expect(result[0]).toMatchObject({
1739+
type: 'diff',
1740+
file_path: 'src/old.ts',
1741+
new_file_path: 'src/new.ts',
1742+
content: load_test_case_file('diffs', test_case, '1-file.txt')
1743+
})
1744+
expect(result[1]).toMatchObject({
1745+
type: 'diff',
1746+
file_path: 'src/index.ts',
1747+
content: load_test_case_file('diffs', test_case, '2-file.txt')
1748+
})
1749+
})
1750+
17291751
it('parses multiple files with text between the markdown code blocks', () => {
17301752
const test_case = 'text-between'
17311753
const text = load_test_case_file(
@@ -2079,4 +2101,3 @@ describe('clipboard-parser', () => {
20792101
})
20802102
})
20812103
})
2082-

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

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -497,30 +497,33 @@ const convert_code_block_to_new_file_diff = (params: {
497497
}
498498
}
499499

500-
const process_text_for_deleted_files = (params: {
500+
const process_text_for_file_operations = (params: {
501501
lines: string[]
502502
is_single_root: boolean
503503
}): DiffOrTextBlock[] => {
504504
const items: DiffOrTextBlock[] = []
505505
let current_text_lines: string[] = []
506506

507507
const deleted_file_header_regex = /^###\s+Deleted file:\s*`([^`]+)`/
508+
const renamed_file_header_regex =
509+
/^###\s+Renamed file:\s*`([^`]+)`\s*\(old\)\s*`([^`]+)`\s*\(new\)/
508510

509511
for (const line of params.lines) {
510512
const trimmed_line = line.trim()
511513
if (trimmed_line === '<files>' || trimmed_line === '</files>') {
512514
continue
513515
}
514-
const match = trimmed_line.match(deleted_file_header_regex)
516+
const deleted_match = trimmed_line.match(deleted_file_header_regex)
517+
const renamed_match = trimmed_line.match(renamed_file_header_regex)
515518

516-
if (match && match[1]) {
519+
if (deleted_match && deleted_match[1]) {
517520
const text_content = current_text_lines.join('\n').trim()
518521
if (text_content) {
519522
items.push({ type: 'text', content: text_content })
520523
}
521524
current_text_lines = []
522525

523-
const raw_file_path = normalize_path(match[1])
526+
const raw_file_path = normalize_path(deleted_match[1])
524527
const { workspace_name, relative_path } = extract_workspace_and_path({
525528
raw_file_path,
526529
is_single_root_folder_workspace: params.is_single_root
@@ -532,6 +535,41 @@ const process_text_for_deleted_files = (params: {
532535
workspace_name,
533536
content
534537
})
538+
} else if (renamed_match && renamed_match[1] && renamed_match[2]) {
539+
const text_content = current_text_lines.join('\n').trim()
540+
if (text_content) {
541+
items.push({ type: 'text', content: text_content })
542+
}
543+
current_text_lines = []
544+
545+
const raw_old_path = normalize_path(renamed_match[1])
546+
const raw_new_path = normalize_path(renamed_match[2])
547+
548+
const old_info = extract_workspace_and_path({
549+
raw_file_path: raw_old_path,
550+
is_single_root_folder_workspace: params.is_single_root
551+
})
552+
const new_info = extract_workspace_and_path({
553+
raw_file_path: raw_new_path,
554+
is_single_root_folder_workspace: params.is_single_root
555+
})
556+
557+
const content = `--- a/${old_info.relative_path}\n+++ b/${old_info.relative_path}\n`
558+
559+
const patch: Diff = {
560+
type: 'diff',
561+
file_path: old_info.relative_path,
562+
workspace_name: old_info.workspace_name,
563+
new_file_path: new_info.relative_path,
564+
content
565+
}
566+
if (
567+
new_info.workspace_name &&
568+
new_info.workspace_name !== old_info.workspace_name
569+
) {
570+
patch.workspace_name = new_info.workspace_name
571+
}
572+
items.push(patch)
535573
} else {
536574
current_text_lines.push(line)
537575
}
@@ -709,7 +747,7 @@ const extract_all_code_block_patches = (params: {
709747

710748
if (block.type == 'diff' || block.type == 'patch') {
711749
const text_before_lines = lines.slice(last_block_end + 1, block.start)
712-
const preceding_items = process_text_for_deleted_files({
750+
const preceding_items = process_text_for_file_operations({
713751
lines: text_before_lines,
714752
is_single_root: params.is_single_root
715753
})
@@ -747,7 +785,7 @@ const extract_all_code_block_patches = (params: {
747785
block.start
748786
)
749787
items.push(
750-
...process_text_for_deleted_files({
788+
...process_text_for_file_operations({
751789
lines: text_before_lines,
752790
is_single_root: params.is_single_root
753791
})
@@ -774,7 +812,7 @@ const extract_all_code_block_patches = (params: {
774812
if (patch) {
775813
const text_before_lines = lines.slice(last_block_end + 1, block.start)
776814
items.push(
777-
...process_text_for_deleted_files({
815+
...process_text_for_file_operations({
778816
lines: text_before_lines,
779817
is_single_root: params.is_single_root
780818
})
@@ -805,7 +843,7 @@ const extract_all_code_block_patches = (params: {
805843
if (last_block_end < lines.length - 1) {
806844
const remaining_lines = lines.slice(last_block_end + 1)
807845
items.push(
808-
...process_text_for_deleted_files({
846+
...process_text_for_file_operations({
809847
lines: remaining_lines,
810848
is_single_root: params.is_single_root
811849
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--- a/src/old.ts
2+
+++ b/src/old.ts
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,13 @@
1+
### Renamed file: `src/old.ts` (old) `src/new.ts` (new)
2+
3+
### Updated file: `src/index.ts`
4+
5+
```diff
6+
--- a/src/index.ts
7+
+++ b/src/index.ts
8+
@@ -1,3 +1,3 @@
9+
console.log("hello")
10+
-console.log("old message")
11+
+console.log("new message")
12+
13+
```

0 commit comments

Comments
 (0)