Skip to content

Commit 2844e52

Browse files
committed
Handle merge conflicts for the same file across multiple code blocks by combining content and adjacent text
1 parent 533ad1f commit 2844e52

7 files changed

Lines changed: 112 additions & 1 deletion

File tree

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,30 @@ describe('clipboard-parser', () => {
427427
})
428428
})
429429

430+
it('merges content and text when multiple merge conflicts for the same file are provided in separate code blocks with text between them', () => {
431+
const test_case = 'merge-conflicts-merge-the-same-file'
432+
const text = load_test_case_file(
433+
'standard',
434+
test_case,
435+
`${test_case}.txt`
436+
)
437+
const result = parse_response({
438+
response: text,
439+
is_single_root_folder_workspace: true
440+
})
441+
442+
expect(result).toHaveLength(2)
443+
expect(result[0]).toMatchObject({
444+
type: 'text',
445+
content: load_test_case_file('standard', test_case, '1-text.txt')
446+
})
447+
expect(result[1]).toMatchObject({
448+
type: 'file',
449+
file_path: 'src/index.ts',
450+
content: load_test_case_file('standard', test_case, '2-file.txt')
451+
})
452+
})
453+
430454
it('merges content when merge conflicts use three dots notation', () => {
431455
const test_case = 'merge-conflicts-three-dots'
432456
const text = load_test_case_file(

‎apps/editor/src/commands/apply-chat-response-command/utils/clipboard-parser/parsers/multiple-files-parser/helpers/item-management.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,43 @@ export const create_or_update_file_item = (params: {
3535

3636
if (mode == 'append') {
3737
existing_file.content = existing_file.content + '\n' + processed_content
38+
39+
const file_index = results.indexOf(existing_file)
40+
if (file_index >= 0) {
41+
const text_indices: number[] = []
42+
for (let i = file_index + 1; i < results.length; i++) {
43+
if (results[i].type == 'text') {
44+
text_indices.push(i)
45+
}
46+
}
47+
48+
if (text_indices.length > 0) {
49+
const text_contents = text_indices
50+
.map((idx) => (results[idx] as TextItem).content.trim())
51+
.filter((c) => c)
52+
53+
for (let i = text_indices.length - 1; i >= 0; i--) {
54+
results.splice(text_indices[i], 1)
55+
}
56+
57+
if (text_contents.length > 0) {
58+
const combined = text_contents.join('\n')
59+
const updated_file_index = results.indexOf(existing_file)
60+
if (
61+
updated_file_index > 0 &&
62+
results[updated_file_index - 1].type == 'text'
63+
) {
64+
const prev_text = results[updated_file_index - 1] as TextItem
65+
prev_text.content = prev_text.content.trim() + '\n' + combined
66+
} else {
67+
results.splice(updated_file_index, 0, {
68+
type: 'text' as const,
69+
content: combined
70+
})
71+
}
72+
}
73+
}
74+
}
3875
} else {
3976
existing_file.content = processed_content
4077
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,19 @@ export const parse_multiple_files = (params: {
168168
current_block_mode = 'overwrite'
169169
}
170170
} else {
171-
current_block_mode = 'overwrite'
171+
const file_key = `${current_workspace_name || ''}:${current_file_name}`
172+
const existing_file = file_ref_map.get(file_key)
173+
const has_conflict_markers =
174+
existing_file &&
175+
existing_file.content.includes('<<<<<<<') &&
176+
existing_file.content.includes('>>>>>>>') &&
177+
existing_file.content.includes('=======')
178+
179+
if (has_conflict_markers) {
180+
current_block_mode = 'append'
181+
} else {
182+
current_block_mode = 'overwrite'
183+
}
172184
header_path_already_used = true
173185
}
174186
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Lorem.
2+
Ipsum.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<<<<<<< ORIGINAL
2+
console.log("a")
3+
=======
4+
console.log("b")
5+
>>>>>>> UPDATED
6+
<<<<<<< ORIGINAL
7+
console.log("c")
8+
=======
9+
console.log("d")
10+
>>>>>>> UPDATED
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Lorem.
2+
3+
### Updated file: `src\index.ts`
4+
5+
```typescript
6+
<<<<<<< ORIGINAL
7+
console.log("a")
8+
=======
9+
console.log("b")
10+
>>>>>>> UPDATED
11+
```
12+
13+
Ipsum.
14+
15+
### Updated file: `src\index.ts`
16+
17+
```typescript
18+
<<<<<<< ORIGINAL
19+
console.log("c")
20+
=======
21+
console.log("d")
22+
>>>>>>> UPDATED
23+
```

‎apps/editor/src/commands/search-files-for-context.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ export const search_files_for_context_command = (
111111
}
112112
}
113113

114+
console.log(search_term)
115+
console.log(all_files)
116+
114117
const matched_files = await search_files_by_keywords({
115118
files: all_files,
116119
search_term

0 commit comments

Comments
 (0)