Skip to content

Commit 031ce8b

Browse files
committed
Merge multiple diffs for the same file into a single diff item during clipboard parsing
1 parent 10d6c9d commit 031ce8b

6 files changed

Lines changed: 107 additions & 37 deletions

File tree

‎apps/editor/src/commands/apply-chat-response-command/utils/clipboard-parser/clipboard-parser.spec.ts‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,26 @@ describe('clipboard-parser', () => {
15091509
})
15101510
})
15111511

1512+
it('merges multiple diffs for the same file into a single diff item', () => {
1513+
const test_case = 'diff-multiple-edits-for-the-same-file'
1514+
const text = load_test_case_file('diffs', test_case, `${test_case}.txt`)
1515+
const result = parse_response({
1516+
response: text,
1517+
is_single_root_folder_workspace: true
1518+
})
1519+
1520+
expect(result).toHaveLength(2)
1521+
expect(result[0]).toMatchObject({
1522+
type: 'text',
1523+
content: load_test_case_file('diffs', test_case, '1-text.txt')
1524+
})
1525+
expect(result[1]).toMatchObject({
1526+
type: 'diff',
1527+
file_path: 'src/ipsum.ts',
1528+
content: load_test_case_file('diffs', test_case, '2-file.txt')
1529+
})
1530+
})
1531+
15121532
it('parses multiple diffs with quoted file paths in separate markdown code blocks', () => {
15131533
const test_case = 'diff-multiple-files-with-quoted-paths'
15141534
const text = load_test_case_file('diffs', test_case, `${test_case}.txt`)
@@ -1915,22 +1935,6 @@ describe('clipboard-parser', () => {
19151935
})
19161936
})
19171937

1918-
it('merges a new file and a diff for the same file path into one patch', () => {
1919-
const test_case = 'diff-merge-new-file-and-diff-same-path'
1920-
const text = load_test_case_file('diffs', test_case, `${test_case}.txt`)
1921-
const result = parse_response({
1922-
response: text,
1923-
is_single_root_folder_workspace: true
1924-
})
1925-
1926-
expect(result).toHaveLength(1)
1927-
expect(result[0]).toMatchObject({
1928-
type: 'diff',
1929-
file_path: 'src/ipsum.ts',
1930-
content: load_test_case_file('diffs', test_case, '1-file.txt')
1931-
})
1932-
})
1933-
19341938
it('parses a diff patch where the file path is specified using an XML tag preceding the diff block', () => {
19351939
const test_case = 'diff-with-xml-file-path'
19361940
const text = load_test_case_file('diffs', test_case, `${test_case}.txt`)

‎apps/editor/src/commands/apply-chat-response-command/utils/clipboard-parser/parsers/diff-parser/diff-parser.ts‎

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Logger } from '@shared/utils/logger'
22
import {
3+
TextItem,
34
DiffItem,
45
InlineFileItem,
5-
TextItem,
66
extract_workspace_and_path
77
} from '../../clipboard-parser'
88
import {
@@ -729,22 +729,65 @@ const parse_multiple_raw_patches = (params: {
729729
return patches
730730
}
731731

732-
const filter_last_diff_per_file = (
733-
items: DiffParserItem[]
734-
): DiffParserItem[] => {
735-
const seen_files = new Set<string>()
732+
const merge_diffs_per_file = (items: DiffParserItem[]): DiffParserItem[] => {
736733
const result: DiffParserItem[] = []
734+
const file_diff_map = new Map<string, DiffItem>()
737735

738-
for (let i = items.length - 1; i >= 0; i--) {
739-
const item = items[i]
736+
for (const item of items) {
740737
if (item.type == 'diff') {
741738
const file_key = `${item.workspace_name || ''}:${item.file_path}`
742-
if (!seen_files.has(file_key)) {
743-
seen_files.add(file_key)
744-
result.unshift(item)
739+
if (file_diff_map.has(file_key)) {
740+
const existing_diff = file_diff_map.get(file_key)!
741+
const existing_lines = existing_diff.content
742+
.replace(/\n$/, '')
743+
.split('\n')
744+
const existing_has_real_hunks = existing_lines.some(
745+
(l) =>
746+
(l.startsWith('+') && !l.startsWith('+++ ')) ||
747+
(l.startsWith('-') && !l.startsWith('--- ')) ||
748+
(l.startsWith(' ') && !l.startsWith(' '))
749+
)
750+
751+
if (!existing_has_real_hunks) {
752+
existing_diff.content = item.content
753+
} else {
754+
const new_lines = item.content.replace(/\n$/, '').split('\n')
755+
756+
let start_idx = new_lines.findIndex((l) => l.startsWith('@@'))
757+
if (start_idx == -1) {
758+
start_idx = new_lines.findIndex(
759+
(l) =>
760+
!l.startsWith('--- ') &&
761+
!l.startsWith('+++ ') &&
762+
!l.startsWith('diff --git') &&
763+
l.trim() != ''
764+
)
765+
}
766+
767+
if (start_idx != -1) {
768+
const content_to_add = new_lines.slice(start_idx).join('\n') + '\n'
769+
existing_diff.content = existing_diff.content.endsWith('\n')
770+
? existing_diff.content + content_to_add
771+
: existing_diff.content + '\n' + content_to_add
772+
}
773+
}
774+
775+
if (result.length > 0 && result[result.length - 1].type == 'text') {
776+
const orphaned_text = result.pop() as TextItem
777+
const diff_index = result.indexOf(existing_diff)
778+
if (diff_index > 0 && result[diff_index - 1].type == 'text') {
779+
;(result[diff_index - 1] as TextItem).content +=
780+
'\n' + orphaned_text.content
781+
} else {
782+
result.splice(diff_index, 0, orphaned_text)
783+
}
784+
}
785+
} else {
786+
file_diff_map.set(file_key, item)
787+
result.push(item)
745788
}
746789
} else {
747-
result.unshift(item)
790+
result.push(item)
748791
}
749792
}
750793
return result
@@ -780,7 +823,7 @@ export const extract_diffs = (params: {
780823
is_single_root: params.is_single_root
781824
})
782825
}
783-
return filter_last_diff_per_file(items)
826+
return merge_diffs_per_file(items)
784827
}
785828

786829
export { extract_paths_from_lines }

‎apps/editor/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/diffs/diff-merge-new-file-and-diff-same-path/diff-merge-new-file-and-diff-same-path.txt‎

Lines changed: 0 additions & 8 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Lorem.
2+
Ipsum.

apps/editor/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/diffs/diff-merge-new-file-and-diff-same-path/1-file.txt renamed to apps/editor/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/diffs/diff-multiple-edits-for-the-same-file/2-file.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
--- a/src/ipsum.ts
22
+++ b/src/ipsum.ts
3-
@@ -1,3 +1,3 @@
3+
@@ -0,0 +0,0 @@
44
console.log("hello")
5+
-console.log("old lorem")
6+
+console.log("new lorem")
7+
@@ -0,0 +0,0 @@
8+
console.log("welcome")
59
-console.log("old ipsum")
610
+console.log("new ipsum")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
### Updated file: `src/ipsum.ts`
2+
3+
Lorem.
4+
5+
```diff
6+
--- a/src/ipsum.ts
7+
+++ b/src/ipsum.ts
8+
@@ -0,0 +0,0 @@
9+
console.log("hello")
10+
-console.log("old lorem")
11+
+console.log("new lorem")
12+
```
13+
14+
### Updated file: `src/ipsum.ts`
15+
16+
Ipsum.
17+
18+
```diff
19+
--- a/src/ipsum.ts
20+
+++ b/src/ipsum.ts
21+
@@ -0,0 +0,0 @@
22+
console.log("welcome")
23+
-console.log("old ipsum")
24+
+console.log("new ipsum")
25+
```

0 commit comments

Comments
 (0)