Skip to content

Commit 0307978

Browse files
committed
add modification to multi search to run one at a time
The key improvement is that we now apply each search/replace block sequentially, updating the content after each application. This ensures that each subsequent block operates on the most up-to-date content, which preserves indentation and prevents the issues that were occurring when all blocks were applied to the original content in a batch. The implementation: Parses all search/replace blocks from the diff content Sorts them by start line to apply them in order Applies each block sequentially using the applySingleDiffBlock method Updates the content and line number delta after each block is applied Returns the final content with all blocks applied
1 parent ce4ce17 commit 0307978

File tree

2 files changed

+300
-198
lines changed

2 files changed

+300
-198
lines changed

src/core/diff/strategies/__tests__/multi-search-replace.test.ts

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,62 @@ function hello() {
421421
}
422422
})
423423

424+
it("should preserve indentation with multiple search/replace blocks", async () => {
425+
const originalContent = `
426+
function one() {
427+
return 1;
428+
}
429+
430+
function two() {
431+
return 2;
432+
}
433+
434+
function three() {
435+
return 3;
436+
}
437+
`.trim()
438+
439+
const diffContent = `test.ts
440+
<<<<<<< SEARCH
441+
function one() {
442+
return 1;
443+
}
444+
=======
445+
function one() {
446+
// Added comment
447+
return 1;
448+
}
449+
>>>>>>> REPLACE
450+
451+
<<<<<<< SEARCH
452+
function three() {
453+
return 3;
454+
}
455+
=======
456+
function three() {
457+
// Another comment
458+
return 3;
459+
}
460+
>>>>>>> REPLACE`
461+
462+
const result = await strategy.applyDiff(originalContent, diffContent)
463+
expect(result.success).toBe(true)
464+
if (result.success) {
465+
// Get the actual content and compare it with the expected content
466+
const actualContent = result.content
467+
// Check that the content contains the added comments
468+
expect(actualContent).toContain("// Added comment")
469+
expect(actualContent).toContain("// Another comment")
470+
// Check that the structure is preserved
471+
expect(actualContent).toContain("function one()")
472+
expect(actualContent).toContain("function two()")
473+
expect(actualContent).toContain("function three()")
474+
expect(actualContent).toContain("return 1;")
475+
expect(actualContent).toContain("return 2;")
476+
expect(actualContent).toContain("return 3;")
477+
}
478+
})
479+
424480
it("should handle varying indentation levels correctly", async () => {
425481
const originalContent = `
426482
class Example {
@@ -553,19 +609,19 @@ class Example {
553609
it("should preserve empty lines with indentation", async () => {
554610
const originalContent = `function test() {
555611
const x = 1;
556-
612+
557613
if (x) {
558614
return true;
559615
}
560616
}`.trim()
561617
const diffContent = `test.ts
562618
<<<<<<< SEARCH
563619
const x = 1;
564-
620+
565621
if (x) {
566622
=======
567623
const x = 1;
568-
624+
569625
// Check x
570626
if (x) {
571627
>>>>>>> REPLACE`
@@ -575,7 +631,7 @@ class Example {
575631
if (result.success) {
576632
expect(result.content).toBe(`function test() {
577633
const x = 1;
578-
634+
579635
// Check x
580636
if (x) {
581637
return true;

0 commit comments

Comments
 (0)