Skip to content

Commit d684d72

Browse files
committed
Refactor conflict marker parsing into a dedicated utility module with tests
1 parent 353322b commit d684d72

13 files changed

Lines changed: 251 additions & 146 deletions

File tree

‎apps/editor/src/commands/apply-chat-response-command/handlers/conflict-markers-handler.ts‎

Lines changed: 4 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import { create_safe_path, sanitize_file_name } from '@/utils/path-sanitizer'
77
import { FileItem } from '../utils/clipboard-parser'
88
import { OriginalFileState } from '../types/original-file-state'
99
import { remove_directory_if_empty } from '../utils/file-operations'
10-
11-
type Segment =
12-
| { type: 'common'; lines: string[] }
13-
| { type: 'conflict'; original_lines: string[]; updated_lines: string[] }
10+
import {
11+
apply_conflict_markers_to_content,
12+
parse_conflict_segments
13+
} from '../utils/conflict-marker-parser'
1414

1515
export const handle_conflict_markers = async (
1616
files: FileItem[]
@@ -265,145 +265,3 @@ export const handle_conflict_markers = async (
265265
return { success: false }
266266
}
267267
}
268-
269-
const parse_conflict_segments = (content: string): Segment[] => {
270-
const lines = content.split('\n')
271-
const segments: Segment[] = []
272-
let current_lines: string[] = []
273-
274-
let state: 'NORMAL' | 'ORIGINAL' | 'UPDATED' = 'NORMAL'
275-
276-
let original_buffer: string[] = []
277-
let updated_buffer: string[] = []
278-
279-
for (const line of lines) {
280-
if (line.trim().startsWith('<<<<<<<')) {
281-
if (state == 'NORMAL') {
282-
if (current_lines.length > 0) {
283-
segments.push({ type: 'common', lines: [...current_lines] })
284-
current_lines = []
285-
}
286-
state = 'ORIGINAL'
287-
original_buffer = []
288-
updated_buffer = []
289-
} else {
290-
if (state == 'ORIGINAL') original_buffer.push(line)
291-
if (state == 'UPDATED') updated_buffer.push(line)
292-
}
293-
continue
294-
}
295-
296-
if (line.trim().startsWith('=======')) {
297-
if (state == 'ORIGINAL') {
298-
state = 'UPDATED'
299-
} else if (state == 'NORMAL') {
300-
current_lines.push(line)
301-
} else {
302-
updated_buffer.push(line)
303-
}
304-
continue
305-
}
306-
307-
if (line.trim().startsWith('>>>>>>>')) {
308-
if (state == 'UPDATED') {
309-
segments.push({
310-
type: 'conflict',
311-
original_lines: original_buffer,
312-
updated_lines: updated_buffer
313-
})
314-
state = 'NORMAL'
315-
original_buffer = []
316-
updated_buffer = []
317-
} else if (state == 'ORIGINAL') {
318-
segments.push({
319-
type: 'conflict',
320-
original_lines: original_buffer,
321-
updated_lines: []
322-
})
323-
state = 'NORMAL'
324-
original_buffer = []
325-
updated_buffer = []
326-
} else {
327-
current_lines.push(line)
328-
}
329-
continue
330-
}
331-
332-
if (state == 'NORMAL') {
333-
current_lines.push(line)
334-
} else if (state == 'ORIGINAL') {
335-
original_buffer.push(line)
336-
} else if (state == 'UPDATED') {
337-
updated_buffer.push(line)
338-
}
339-
}
340-
341-
if (state == 'NORMAL' && current_lines.length > 0) {
342-
segments.push({ type: 'common', lines: current_lines })
343-
}
344-
345-
return segments
346-
}
347-
348-
const apply_conflict_markers_to_content = (
349-
original_content: string,
350-
markers_content: string
351-
): string => {
352-
let current_content = original_content
353-
const segments = parse_conflict_segments(markers_content)
354-
let cursor = 0
355-
356-
for (let i = 0; i < segments.length; i++) {
357-
const segment = segments[i]
358-
if (segment.type == 'conflict') {
359-
const prev = i > 0 ? segments[i - 1] : undefined
360-
const next = i < segments.length - 1 ? segments[i + 1] : undefined
361-
362-
const context_before = prev && prev.type == 'common' ? prev.lines : []
363-
const context_after = next && next.type == 'common' ? next.lines : []
364-
365-
const search_lines = [
366-
...context_before,
367-
...segment.original_lines,
368-
...context_after
369-
]
370-
const search_text = search_lines.join('\n')
371-
372-
const replace_lines = [
373-
...context_before,
374-
...segment.updated_lines,
375-
...context_after
376-
]
377-
const replace_text = replace_lines.join('\n')
378-
379-
const index = current_content.indexOf(search_text, cursor)
380-
381-
if (index == -1) {
382-
throw new Error(
383-
`Could not find content to replace for conflict marker. Context:\n${search_text.slice(0, 100)}...`
384-
)
385-
}
386-
387-
current_content =
388-
current_content.slice(0, index) +
389-
replace_text +
390-
current_content.slice(index + search_text.length)
391-
392-
// Advance cursor to skip the replaced part, but ensure we are positioned
393-
// to match the 'context_after' which serves as 'context_before' for the next segment.
394-
const unique_replaced_lines = [
395-
...context_before,
396-
...segment.updated_lines
397-
]
398-
const unique_replaced_text = unique_replaced_lines.join('\n')
399-
400-
let advance = unique_replaced_text.length
401-
if (context_after.length > 0 && unique_replaced_lines.length > 0) {
402-
advance += 1 // Account for newline joining unique part and context_after
403-
}
404-
405-
cursor = index + advance
406-
}
407-
}
408-
return current_content
409-
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { apply_conflict_markers_to_content } from './conflict-marker-parser'
2+
import * as fs from 'fs'
3+
import * as path from 'path'
4+
5+
describe('conflict-marker-parser', () => {
6+
const load_test_case_file = (test_case: string, filename: string): string => {
7+
return fs.readFileSync(
8+
path.join(__dirname, 'test-cases', test_case, filename),
9+
'utf-8'
10+
)
11+
}
12+
13+
describe('apply_conflict_markers_to_content', () => {
14+
it('applies a basic conflict marker correctly', () => {
15+
const test_case = 'basic-conflict'
16+
const original = load_test_case_file(test_case, 'original.txt')
17+
const markers = load_test_case_file(test_case, 'markers.txt')
18+
const expected = load_test_case_file(test_case, 'expected.txt')
19+
20+
const result = apply_conflict_markers_to_content(original, markers)
21+
expect(result).toBe(expected)
22+
})
23+
24+
it('applies multiple conflict markers in a single file', () => {
25+
const test_case = 'multiple-conflicts'
26+
const original = load_test_case_file(test_case, 'original.txt')
27+
const markers = load_test_case_file(test_case, 'markers.txt')
28+
const expected = load_test_case_file(test_case, 'expected.txt')
29+
30+
const result = apply_conflict_markers_to_content(original, markers)
31+
expect(result).toBe(expected)
32+
})
33+
34+
it('handles deletions via empty updated blocks', () => {
35+
const test_case = 'deletion'
36+
const original = load_test_case_file(test_case, 'original.txt')
37+
const markers = load_test_case_file(test_case, 'markers.txt')
38+
const expected = load_test_case_file(test_case, 'expected.txt')
39+
40+
const result = apply_conflict_markers_to_content(original, markers)
41+
expect(result).toBe(expected)
42+
})
43+
44+
it('throws an error when context cannot be found', () => {
45+
const original = 'Some random text'
46+
const markers = '<<<<<<<\nMissing\n=======\nFound\n>>>>>>>'
47+
48+
expect(() =>
49+
apply_conflict_markers_to_content(original, markers)
50+
).toThrow(/Could not find content to replace/)
51+
})
52+
})
53+
})
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
export type Segment =
2+
| { type: 'common'; lines: string[] }
3+
| { type: 'conflict'; original_lines: string[]; updated_lines: string[] }
4+
5+
export const parse_conflict_segments = (content: string): Segment[] => {
6+
const lines = content.split('\n')
7+
const segments: Segment[] = []
8+
let current_lines: string[] = []
9+
10+
let state: 'NORMAL' | 'ORIGINAL' | 'UPDATED' = 'NORMAL'
11+
12+
let original_buffer: string[] = []
13+
let updated_buffer: string[] = []
14+
15+
for (const line of lines) {
16+
if (line.trim().startsWith('<<<<<<<')) {
17+
if (state == 'NORMAL') {
18+
if (current_lines.length > 0) {
19+
segments.push({ type: 'common', lines: [...current_lines] })
20+
current_lines = []
21+
}
22+
state = 'ORIGINAL'
23+
original_buffer = []
24+
updated_buffer = []
25+
} else {
26+
if (state == 'ORIGINAL') original_buffer.push(line)
27+
if (state == 'UPDATED') updated_buffer.push(line)
28+
}
29+
continue
30+
}
31+
32+
if (line.trim().startsWith('=======')) {
33+
if (state == 'ORIGINAL') {
34+
state = 'UPDATED'
35+
} else if (state == 'NORMAL') {
36+
current_lines.push(line)
37+
} else {
38+
updated_buffer.push(line)
39+
}
40+
continue
41+
}
42+
43+
if (line.trim().startsWith('>>>>>>>')) {
44+
if (state == 'UPDATED') {
45+
segments.push({
46+
type: 'conflict',
47+
original_lines: original_buffer,
48+
updated_lines: updated_buffer
49+
})
50+
state = 'NORMAL'
51+
original_buffer = []
52+
updated_buffer = []
53+
} else if (state == 'ORIGINAL') {
54+
segments.push({
55+
type: 'conflict',
56+
original_lines: original_buffer,
57+
updated_lines: []
58+
})
59+
state = 'NORMAL'
60+
original_buffer = []
61+
updated_buffer = []
62+
} else {
63+
current_lines.push(line)
64+
}
65+
continue
66+
}
67+
68+
if (state == 'NORMAL') {
69+
current_lines.push(line)
70+
} else if (state == 'ORIGINAL') {
71+
original_buffer.push(line)
72+
} else if (state == 'UPDATED') {
73+
updated_buffer.push(line)
74+
}
75+
}
76+
77+
if (state == 'NORMAL' && current_lines.length > 0) {
78+
segments.push({ type: 'common', lines: current_lines })
79+
}
80+
81+
return segments
82+
}
83+
84+
export const apply_conflict_markers_to_content = (
85+
original_content: string,
86+
markers_content: string
87+
): string => {
88+
let current_content = original_content
89+
const segments = parse_conflict_segments(markers_content)
90+
let cursor = 0
91+
92+
for (let i = 0; i < segments.length; i++) {
93+
const segment = segments[i]
94+
if (segment.type == 'conflict') {
95+
const prev = i > 0 ? segments[i - 1] : undefined
96+
const next = i < segments.length - 1 ? segments[i + 1] : undefined
97+
98+
const context_before = prev && prev.type == 'common' ? prev.lines : []
99+
const context_after = next && next.type == 'common' ? next.lines : []
100+
101+
const search_lines = [
102+
...context_before,
103+
...segment.original_lines,
104+
...context_after
105+
]
106+
const search_text = search_lines.join('\n')
107+
108+
const replace_lines = [
109+
...context_before,
110+
...segment.updated_lines,
111+
...context_after
112+
]
113+
const replace_text = replace_lines.join('\n')
114+
115+
const index = current_content.indexOf(search_text, cursor)
116+
117+
if (index == -1) {
118+
throw new Error(
119+
`Could not find content to replace for conflict marker. Context:\n${search_text.slice(
120+
0,
121+
100
122+
)}...`
123+
)
124+
}
125+
126+
current_content =
127+
current_content.slice(0, index) +
128+
replace_text +
129+
current_content.slice(index + search_text.length)
130+
131+
const unique_replaced_lines = [
132+
...context_before,
133+
...segment.updated_lines
134+
]
135+
const unique_replaced_text = unique_replaced_lines.join('\n')
136+
137+
let advance = unique_replaced_text.length
138+
if (context_after.length > 0 && unique_replaced_lines.length > 0) {
139+
advance += 1
140+
}
141+
142+
cursor = index + advance
143+
}
144+
}
145+
return current_content
146+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './conflict-marker-parser'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
line 1
2+
line 2 updated
3+
line 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
line 1
2+
<<<<<<<
3+
line 2
4+
=======
5+
line 2 updated
6+
>>>>>>>
7+
line 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
line 1
2+
line 2
3+
line 3
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
keep
2+
keep
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
keep
2+
<<<<<<<
3+
remove
4+
=======
5+
>>>>>>>
6+
keep
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
keep
2+
remove
3+
keep

0 commit comments

Comments
 (0)