Skip to content

Commit 0db2712

Browse files
committed
Refactor conflict marker handling to pre-parse segments and improve syntax support
- add missing test case - [Pasted text] - feel like we have circular dependencies - structure in such way `apps/editor/src/commands/apply-response-command/utils/edit-formats/conflict-markers/apply-conflict-markers-to-content/apply-conflict-markers-to-content.ts` is not dependent on `apps/editor/src/commands/apply-response-command/utils/edit-formats/conflict-markers/parse-conflict-segments.ts` - [Pasted text]
1 parent f0f7588 commit 0db2712

9 files changed

Lines changed: 73 additions & 12 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
get_rename_source_info
1313
} from '../utils/file-operations'
1414
import { apply_conflict_markers_to_content } from '../utils/edit-formats/conflict-markers/apply-conflict-markers-to-content'
15-
import { parse_conflict_segments } from '../utils/edit-formats/conflict-markers'
15+
import { parse_conflict_segments } from '../utils/edit-formats/conflict-markers/parse-conflict-segments'
1616

1717
export const handle_conflict_markers = async (params: {
1818
files: FileItem[]
@@ -111,7 +111,7 @@ export const handle_conflict_markers = async (params: {
111111
if (has_markers) {
112112
new_content = apply_conflict_markers_to_content({
113113
original_content: rename_source_content,
114-
markers_content: file.content
114+
segments: parse_conflict_segments(file.content)
115115
})
116116
} else if (file.content != '') {
117117
new_content = file.content
@@ -149,7 +149,7 @@ export const handle_conflict_markers = async (params: {
149149
const original_content = document.getText()
150150
const current_content = apply_conflict_markers_to_content({
151151
original_content,
152-
markers_content: file.content
152+
segments: parse_conflict_segments(file.content)
153153
})
154154

155155
if (current_content !== original_content) {

‎apps/editor/src/commands/apply-response-command/utils/edit-formats/conflict-markers/apply-conflict-markers-to-content/apply-conflict-markers-to-content.spec.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as fs from 'fs'
22
import * as path from 'path'
33
import { apply_conflict_markers_to_content } from './apply-conflict-markers-to-content'
4+
import { parse_conflict_segments } from '../parse-conflict-segments'
45

56
describe('conflict-marker-parser', () => {
67
const load_test_case_file = (test_case: string, filename: string): string => {
@@ -14,10 +15,11 @@ describe('conflict-marker-parser', () => {
1415
const original_content = load_test_case_file(test_case, 'original.txt')
1516
const markers_content = load_test_case_file(test_case, 'markers.txt')
1617
const expected_content = load_test_case_file(test_case, 'expected.txt')
18+
const segments = parse_conflict_segments(markers_content)
1719

1820
const result = apply_conflict_markers_to_content({
1921
original_content,
20-
markers_content
22+
segments
2123
})
2224

2325
expect(result).toBe(expected_content)
@@ -39,17 +41,22 @@ describe('conflict-marker-parser', () => {
3941
it('throws an error when context cannot be found', () => {
4042
const original_content = 'Some random text'
4143
const markers_content = '<<<<<<<\nMissing\n=======\nFound\n>>>>>>>'
44+
const segments = parse_conflict_segments(markers_content)
4245

4346
expect(() =>
4447
apply_conflict_markers_to_content({
4548
original_content,
46-
markers_content
49+
segments
4750
})
4851
).toThrow(/Could not find content to replace/)
4952
})
5053

5154
it('handles whitespace issues in conflict markers', () => {
5255
run_test_case('whitespace-issues')
5356
})
57+
58+
it('handles rst syntax correctly', () => {
59+
run_test_case('rst-syntax')
60+
})
5461
})
5562
})

‎apps/editor/src/commands/apply-response-command/utils/edit-formats/conflict-markers/apply-conflict-markers-to-content/apply-conflict-markers-to-content.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { parse_conflict_segments } from '../parse-conflict-segments'
1+
import { Segment } from '../types'
22

33
const escape_reg_exp = (string: string) => {
44
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
55
}
66

77
export const apply_conflict_markers_to_content = (params: {
88
original_content: string
9-
markers_content: string
9+
segments: Segment[]
1010
}): string => {
1111
const line_ending = params.original_content.includes('\r\n') ? '\r\n' : '\n'
1212
let current_content = params.original_content
13-
const segments = parse_conflict_segments(params.markers_content)
13+
const segments = params.segments
1414
let cursor = 0
1515

1616
const MAX_CONTEXT_LINES = 5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=====================
2+
First Top-Level Title
3+
=====================
4+
5+
Content under the first title.
6+
7+
=======================
8+
Another Different Title
9+
=======================
10+
11+
Content under the new title section.
12+
13+
---------------------
14+
A Sub-Section Title
15+
---------------------
16+
17+
Content under the sub-section.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<<<<<<< SEARCH
2+
Content under the first title.
3+
4+
======================
5+
Second Top-Level Title
6+
======================
7+
8+
Content under the second title.
9+
=======
10+
Content under the first title.
11+
12+
=======================
13+
Another Different Title
14+
=======================
15+
16+
Content under the new title section.
17+
>>>>>>> REPLACE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=====================
2+
First Top-Level Title
3+
=====================
4+
5+
Content under the first title.
6+
7+
======================
8+
Second Top-Level Title
9+
======================
10+
11+
Content under the second title.
12+
13+
---------------------
14+
A Sub-Section Title
15+
---------------------
16+
17+
Content under the sub-section.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './parse-conflict-segments'
1+
export * from './types'

‎apps/editor/src/commands/apply-response-command/utils/edit-formats/conflict-markers/parse-conflict-segments.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const parse_conflict_segments = (content: string): Segment[] => {
1414

1515
for (const line of lines) {
1616
const trimmed = line.trim()
17-
if (trimmed.startsWith('<<<<<<<')) {
17+
if (trimmed == '<<<<<<<' || trimmed.startsWith('<<<<<<< ')) {
1818
if (state == 'NORMAL') {
1919
if (current_lines.length > 0) {
2020
segments.push({ type: 'common', lines: [...current_lines] })
@@ -30,7 +30,7 @@ export const parse_conflict_segments = (content: string): Segment[] => {
3030
continue
3131
}
3232

33-
if (trimmed.startsWith('=======')) {
33+
if (trimmed == '=======' || trimmed.startsWith('======= ')) {
3434
if (state == 'ORIGINAL') {
3535
state = 'UPDATED'
3636
} else if (state == 'NORMAL') {
@@ -41,7 +41,7 @@ export const parse_conflict_segments = (content: string): Segment[] => {
4141
continue
4242
}
4343

44-
if (trimmed.startsWith('>>>>>>>')) {
44+
if (trimmed == '>>>>>>>' || trimmed.startsWith('>>>>>>> ')) {
4545
if (state == 'UPDATED' || state == 'ORIGINAL') {
4646
segments.push({
4747
type: 'conflict',
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type Segment =
2+
| { type: 'common'; lines: string[] }
3+
| { type: 'conflict'; original_lines: string[]; updated_lines: string[] }

0 commit comments

Comments
 (0)