Skip to content

Commit c726dbf

Browse files
committed
Refactor standard parser header handling to correctly merge conflict blocks with bold file paths
- add missing test case - FAIL src/commands/apply-response-command/utils/response-parser/parsers/standard-parser/test/standard-parser.spec.ts (5.325 s) ● standard-parser › parse_response › merges content when merge conflicts have file path as bold text expect(received).toHaveLength(expected) Expected length: 1 Received length: 2 Received array: [{"content": "<<<<<<< ORIGINAL console.log(\"a\") ======= console.log(\"b\") >>>>>>> UPDATED", "file_path": "src/index.ts", "is_deleted": undefined, "renamed_from": undefined, "renamed_from_workspace": undefined, "type": "file", "workspace_name": undefined}, {"content": "<<<<<<< ORIGINAL console.log(\"c\") ======= console.log(\"d\") >>>>>>> UPDATED", "language": "typescript", "type": "inline-file"}] └── apps └── editor └── src └── commands └── apply-response-command └── utils └── response-parser └── parsers └── standard-parser ├── helpers │ ├── create-or-update-file-item.ts │ ├── extract-and-set-workspace-path.ts │ ├── flush-text-block.ts │ └── process-conflict-markers.ts ├── standard-parser.ts └── test ├── cases │ └── merge-conflicts-file-path-as-bold-text │ ├── 1-file.txt │ └── merge-conflicts-file-path-as-bold-text.txt └── standard-parser.spec.ts
1 parent c6dc319 commit c726dbf

4 files changed

Lines changed: 62 additions & 48 deletions

File tree

‎apps/editor/src/commands/apply-response-command/utils/response-parser/parsers/standard-parser/standard-parser.ts‎

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export const file_blocks_parser = (params: {
2121
let backtick_nesting_level = 0
2222
let last_seen_file_path_comment: string | null = null
2323
let last_seen_file_path_was_header = false
24-
let last_seen_header_was_persistent = false
2524
let header_path_already_used = false
2625
let current_block_mode: 'overwrite' | 'append' = 'overwrite'
2726
let is_markdown_container_block = false
@@ -66,47 +65,30 @@ export const file_blocks_parser = (params: {
6665
current_workspace_name = workspace_name
6766
}
6867

69-
if (last_seen_file_path_was_header) {
70-
if (header_path_already_used && !last_seen_header_was_persistent) {
68+
const file_key = `${current_workspace_name || ''}:${current_file_name}`
69+
const existing_file = file_ref_map.get(file_key)
70+
const has_conflict_markers =
71+
existing_file &&
72+
existing_file.content.includes('<<<<<<<') &&
73+
existing_file.content.includes('>>>>>>>') &&
74+
existing_file.content.includes('=======')
75+
76+
if (header_path_already_used) {
77+
if (has_conflict_markers) {
78+
current_block_mode = 'append'
79+
} else {
7180
current_file_name = ''
7281
current_workspace_name = undefined
7382
last_seen_file_path_comment = null
74-
} else if (header_path_already_used) {
75-
const file_key = `${current_workspace_name || ''}:${current_file_name}`
76-
const existing_file = file_ref_map.get(file_key)
77-
const has_conflict_markers =
78-
existing_file &&
79-
existing_file.content.includes('<<<<<<<') &&
80-
existing_file.content.includes('>>>>>>>') &&
81-
existing_file.content.includes('=======')
82-
83-
if (has_conflict_markers) {
84-
current_block_mode = 'append'
85-
} else {
86-
current_file_name = ''
87-
current_workspace_name = undefined
88-
last_seen_file_path_comment = null
89-
current_block_mode = 'overwrite'
90-
}
91-
} else {
92-
const file_key = `${current_workspace_name || ''}:${current_file_name}`
93-
const existing_file = file_ref_map.get(file_key)
94-
const has_conflict_markers =
95-
existing_file &&
96-
existing_file.content.includes('<<<<<<<') &&
97-
existing_file.content.includes('>>>>>>>') &&
98-
existing_file.content.includes('=======')
99-
100-
if (has_conflict_markers) {
101-
current_block_mode = 'append'
102-
} else {
103-
current_block_mode = 'overwrite'
104-
}
105-
header_path_already_used = true
83+
current_block_mode = 'overwrite'
10684
}
10785
} else {
108-
current_block_mode = 'overwrite'
109-
last_seen_file_path_comment = null
86+
if (has_conflict_markers) {
87+
current_block_mode = 'append'
88+
} else {
89+
current_block_mode = 'overwrite'
90+
}
91+
header_path_already_used = true
11092
}
11193
}
11294
current_content = ''
@@ -158,7 +140,6 @@ export const file_blocks_parser = (params: {
158140

159141
last_seen_file_path_comment = renamed_file_match[2]
160142
last_seen_file_path_was_header = true
161-
last_seen_header_was_persistent = true
162143
header_path_already_used = false
163144

164145
continue
@@ -283,15 +264,6 @@ export const file_blocks_parser = (params: {
283264
}
284265

285266
last_seen_file_path_was_header = is_header_line
286-
if (is_header_line) {
287-
const stripped = line
288-
.replace(/^###\s+/, '')
289-
.replace(/[`*]/g, '')
290-
.trim()
291-
last_seen_header_was_persistent =
292-
/updated|new|renamed|file/i.test(line) ||
293-
stripped == extracted_filename
294-
}
295267
header_path_already_used = false
296268
}
297269
} else {
@@ -479,7 +451,6 @@ export const file_blocks_parser = (params: {
479451
if (current_file_name && !current_content.trim()) {
480452
last_seen_file_path_comment = current_file_name
481453
last_seen_file_path_was_header = false
482-
last_seen_header_was_persistent = false
483454
header_path_already_used = false
484455
}
485456

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,17 @@
1+
**Updated file:** `src\index.ts`
2+
3+
```typescript
4+
<<<<<<< ORIGINAL
5+
console.log("a")
6+
=======
7+
console.log("b")
8+
>>>>>>> UPDATED
9+
```
10+
11+
```typescript
12+
<<<<<<< ORIGINAL
13+
console.log("c")
14+
=======
15+
console.log("d")
16+
>>>>>>> UPDATED
17+
```

‎apps/editor/src/commands/apply-response-command/utils/response-parser/parsers/standard-parser/test/standard-parser.spec.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ describe('standard-parser', () => {
7070
})
7171
})
7272

73+
it('merges content when merge conflicts have file path as bold text', () => {
74+
const test_case = 'merge-conflicts-file-path-as-bold-text'
75+
const text = load_test_case_file(test_case, `${test_case}.txt`)
76+
const result = parse_response({
77+
response: text,
78+
is_single_root_folder_workspace: true
79+
})
80+
81+
expect(result).toHaveLength(1)
82+
expect(result[0]).toMatchObject({
83+
type: 'file',
84+
file_path: 'src/index.ts',
85+
content: load_test_case_file(test_case, '1-file.txt')
86+
})
87+
})
88+
7389
it('merges content when merge conflicts use three dots notation', () => {
7490
const test_case = 'merge-conflicts-three-dots'
7591
const text = load_test_case_file(test_case, `${test_case}.txt`)

0 commit comments

Comments
 (0)