Skip to content

Commit 4ffa495

Browse files
committed
Enhance search-replace content application logic to robustly handle edge cases and add comprehensive tests
- are the changes valid? [Changes] - replace generic 1, generic 2, with actual problems they address (use generic examples) - assess correctness of the processor, fix as needed └── apps └── editor └── src └── utils └── changes-integration └── search-replace-processor ├── apply-search-replace-to-content │ ├── apply-search-replace-to-content.spec.ts │ ├── apply-search-replace-to-content.ts │ ├── index.ts │ └── test-cases │ ├── basic-conflict │ │ ├── expected.txt │ │ ├── markers.txt │ │ └── original.txt │ ├── delete-first-line │ │ ├── expected.txt │ │ ├── markers.txt │ │ └── original.txt │ ├── deletion │ │ ├── expected.txt │ │ ├── markers.txt │ │ └── original.txt │ ├── extra-blank-lines-in-search │ │ ├── expected.txt │ │ ├── markers.txt │ │ └── original.txt │ ├── insert-after-context │ │ ├── expected.txt │ │ ├── markers.txt │ │ └── original.txt │ ├── insert-before-context │ │ ├── expected.txt │ │ ├── markers.txt │ │ └── original.txt │ ├── multiple-conflicts │ │ ├── expected.txt │ │ ├── markers.txt │ │ └── original.txt │ ├── reorder-without-context │ │ ├── expected.txt │ │ ├── markers.txt │ │ └── original.txt │ ├── rst-syntax │ │ ├── expected.txt │ │ ├── markers.txt │ │ └── original.txt │ ├── similar-code-multiple-blocks │ │ ├── expected.txt │ │ ├── markers.txt │ │ └── original.txt │ ├── trailing-whitespace │ │ ├── expected.txt │ │ ├── markers.txt │ │ └── original.txt │ └── whitespace-issues │ ├── expected.txt │ ├── markers.txt │ └── original.txt ├── index.ts ├── parse-search-replace-segments.ts └── types.ts
1 parent 73778c7 commit 4ffa495

24 files changed

Lines changed: 287 additions & 96 deletions

File tree

‎apps/editor/src/utils/changes-integration/search-replace-processor/apply-search-replace-to-content/apply-search-replace-to-content.spec.ts‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,28 @@ describe('search-replace-parser', () => {
6363
run_test_case('rst-syntax')
6464
})
6565

66-
it('handles generic-1 correctly', () => {
67-
run_test_case('generic-1')
66+
it('keeps the original blank lines when the search block has extra or missing ones', () => {
67+
run_test_case('extra-blank-lines-in-search')
68+
})
69+
70+
it('applies several blocks to similar code with repeated lines', () => {
71+
run_test_case('similar-code-multiple-blocks')
72+
})
73+
74+
it('inserts a line between preceding context and a matched line', () => {
75+
run_test_case('insert-after-context')
76+
})
77+
78+
it('inserts lines before following context when the search block is empty', () => {
79+
run_test_case('insert-before-context')
80+
})
81+
82+
it('deletes the first line without leaving a stray newline', () => {
83+
run_test_case('delete-first-line')
84+
})
85+
86+
it('does not prepend a newline when the first kept line moves without context', () => {
87+
run_test_case('reorder-without-context')
6888
})
6989
})
7090
})

‎apps/editor/src/utils/changes-integration/search-replace-processor/apply-search-replace-to-content/apply-search-replace-to-content.ts‎

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ type BlockMatch = {
124124
original_start: number
125125
original_end: number
126126
original_indexed_lines: IndexedLine[]
127+
leading_newline_needed: boolean
128+
trailing_newline_needed: boolean
127129
}
128130

129131
const find_block = (params: {
@@ -148,7 +150,9 @@ const find_block = (params: {
148150
match_end: params.from,
149151
original_start: params.from,
150152
original_end: params.from,
151-
original_indexed_lines: []
153+
original_indexed_lines: [],
154+
leading_newline_needed: false,
155+
trailing_newline_needed: false
152156
}
153157
}
154158

@@ -193,7 +197,10 @@ const find_block = (params: {
193197
match_end,
194198
original_start,
195199
original_end,
196-
original_indexed_lines
200+
original_indexed_lines,
201+
leading_newline_needed: before.length > 0,
202+
trailing_newline_needed:
203+
original.length == 0 && before.length == 0 && after.length > 0
197204
}
198205
}
199206

@@ -277,7 +284,25 @@ export const apply_search_replace_to_content = (params: {
277284
}
278285
const gap_end = match.original_indexed_lines[orig_idx].start
279286

280-
replacement_text += current_content.slice(gap_start, gap_end)
287+
let gap_text = current_content.slice(gap_start, gap_end)
288+
289+
if (replacement_text.length === 0 && !match.leading_newline_needed) {
290+
gap_text = ''
291+
}
292+
293+
if (replacement_text.length > 0 && !gap_text.includes('\n')) {
294+
if (!replacement_text.endsWith('\n')) {
295+
replacement_text += line_ending
296+
}
297+
const upd_gap = upd_structure.gaps[j]
298+
if (upd_gap.length > 0) {
299+
replacement_text +=
300+
upd_gap.map(strip_invisible).join(line_ending) + line_ending
301+
}
302+
gap_text = ''
303+
}
304+
305+
replacement_text += gap_text
281306
replacement_text += upd_structure.non_blanks[j].text
282307

283308
last_matched_orig_idx = orig_idx
@@ -308,7 +333,9 @@ export const apply_search_replace_to_content = (params: {
308333
if (gap_to_use !== null) {
309334
replacement_text += gap_to_use
310335
} else {
311-
if (replacement_text.length > 0 && !replacement_text.endsWith('\n')) {
336+
const needs_line_break =
337+
replacement_text.length > 0 || match.leading_newline_needed
338+
if (needs_line_break && !replacement_text.endsWith('\n')) {
312339
replacement_text += line_ending
313340
}
314341
const upd_gap = upd_structure.gaps[j]
@@ -343,10 +370,25 @@ export const apply_search_replace_to_content = (params: {
343370
replacement_text += last_upd_gap.map(strip_invisible).join(line_ending)
344371
}
345372

373+
if (match.trailing_newline_needed && replacement_text.length > 0) {
374+
replacement_text += line_ending
375+
}
376+
377+
// Deleting whole lines with no preceding context: the region starts at a
378+
// line start, so the line break after the last deleted line has to go too.
379+
let original_end = match.original_end
380+
if (
381+
replacement_text.length === 0 &&
382+
!match.leading_newline_needed &&
383+
match.original_indexed_lines.length > 0
384+
) {
385+
const next_line_break = current_content.indexOf('\n', original_end)
386+
if (next_line_break !== -1) original_end = next_line_break + 1
387+
}
346388
current_content =
347389
current_content.slice(0, match.original_start) +
348390
replacement_text +
349-
current_content.slice(match.original_end)
391+
current_content.slice(original_end)
350392

351393
cursor = match.original_start + replacement_text.length
352394
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
B
2+
C
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<<<<<<<
2+
A
3+
=======
4+
>>>>>>>
5+
B
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
A
2+
B
3+
C
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Guide
2+
3+
Build the project
4+
pnpm install
5+
pnpm run build
6+
7+
Start the project
8+
9+
pnpm run serve
10+
pnpm run watch
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<<<<<<< SEARCH
2+
Build the project
3+
4+
npm install
5+
6+
npm run build
7+
=======
8+
Build the project
9+
10+
pnpm install
11+
12+
pnpm run build
13+
>>>>>>> REPLACE
14+
<<<<<<< SEARCH
15+
Start the project
16+
npm run serve
17+
npm run watch
18+
=======
19+
Start the project
20+
pnpm run serve
21+
pnpm run watch
22+
>>>>>>> REPLACE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Guide
2+
3+
Build the project
4+
npm install
5+
npm run build
6+
7+
Start the project
8+
9+
npm run serve
10+
npm run watch

‎apps/editor/src/utils/changes-integration/search-replace-processor/apply-search-replace-to-content/test-cases/generic-1/expected.txt‎

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎apps/editor/src/utils/changes-integration/search-replace-processor/apply-search-replace-to-content/test-cases/generic-1/markers.txt‎

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)