diff --git a/src/core/diff/strategies/__tests__/search-replace.test.ts b/src/core/diff/strategies/__tests__/search-replace.test.ts index cdfa137..9afb9e4 100644 --- a/src/core/diff/strategies/__tests__/search-replace.test.ts +++ b/src/core/diff/strategies/__tests__/search-replace.test.ts @@ -416,6 +416,93 @@ class Example { console.error(e); } } +}`); + }); + + it('should handle negative indentation relative to search content', () => { + const originalContent = `class Example { + constructor() { + if (true) { + this.init(); + this.setup(); + } + } +}`.trim(); + const diffContent = `test.ts +<<<<<<< SEARCH + this.init(); + this.setup(); +======= + this.init(); + this.setup(); +>>>>>>> REPLACE`; + + const result = strategy.applyDiff(originalContent, diffContent); + expect(result).toBe(`class Example { + constructor() { + if (true) { + this.init(); + this.setup(); + } + } +}`); + }); + + it('should handle extreme negative indentation (no indent)', () => { + const originalContent = `class Example { + constructor() { + if (true) { + this.init(); + } + } +}`.trim(); + const diffContent = `test.ts +<<<<<<< SEARCH + this.init(); +======= +this.init(); +>>>>>>> REPLACE`; + + const result = strategy.applyDiff(originalContent, diffContent); + expect(result).toBe(`class Example { + constructor() { + if (true) { +this.init(); + } + } +}`); + }); + + it('should handle mixed indentation changes in replace block', () => { + const originalContent = `class Example { + constructor() { + if (true) { + this.init(); + this.setup(); + this.validate(); + } + } +}`.trim(); + const diffContent = `test.ts +<<<<<<< SEARCH + this.init(); + this.setup(); + this.validate(); +======= + this.init(); + this.setup(); + this.validate(); +>>>>>>> REPLACE`; + + const result = strategy.applyDiff(originalContent, diffContent); + expect(result).toBe(`class Example { + constructor() { + if (true) { + this.init(); + this.setup(); + this.validate(); + } + } }`); }); }) diff --git a/src/core/diff/strategies/search-replace.ts b/src/core/diff/strategies/search-replace.ts index 3d524b1..021aa12 100644 --- a/src/core/diff/strategies/search-replace.ts +++ b/src/core/diff/strategies/search-replace.ts @@ -211,11 +211,18 @@ Your search/replace content here const currentIndent = currentIndentMatch ? currentIndentMatch[0] : ''; const searchBaseIndent = searchIndents[0] || ''; - // Calculate the relative indentation from the search content - const relativeIndent = currentIndent.slice(searchBaseIndent.length); + // Calculate the relative indentation level + const searchBaseLevel = searchBaseIndent.length; + const currentLevel = currentIndent.length; + const relativeLevel = currentLevel - searchBaseLevel; - // Apply the matched indentation plus any relative indentation - return matchedIndent + relativeIndent + line.trim(); + // If relative level is negative, remove indentation from matched indent + // If positive, add to matched indent + const finalIndent = relativeLevel < 0 + ? matchedIndent.slice(0, Math.max(0, matchedIndent.length + relativeLevel)) + : matchedIndent + currentIndent.slice(searchBaseLevel); + + return finalIndent + line.trim(); }); // Construct the final content