Skip to content

Commit 25da0b1

Browse files
committed
Negative indentation
1 parent 0bdb325 commit 25da0b1

File tree

2 files changed

+98
-4
lines changed

2 files changed

+98
-4
lines changed

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,93 @@ class Example {
416416
console.error(e);
417417
}
418418
}
419+
}`);
420+
});
421+
422+
it('should handle negative indentation relative to search content', () => {
423+
const originalContent = `class Example {
424+
constructor() {
425+
if (true) {
426+
this.init();
427+
this.setup();
428+
}
429+
}
430+
}`.trim();
431+
const diffContent = `test.ts
432+
<<<<<<< SEARCH
433+
this.init();
434+
this.setup();
435+
=======
436+
this.init();
437+
this.setup();
438+
>>>>>>> REPLACE`;
439+
440+
const result = strategy.applyDiff(originalContent, diffContent);
441+
expect(result).toBe(`class Example {
442+
constructor() {
443+
if (true) {
444+
this.init();
445+
this.setup();
446+
}
447+
}
448+
}`);
449+
});
450+
451+
it('should handle extreme negative indentation (no indent)', () => {
452+
const originalContent = `class Example {
453+
constructor() {
454+
if (true) {
455+
this.init();
456+
}
457+
}
458+
}`.trim();
459+
const diffContent = `test.ts
460+
<<<<<<< SEARCH
461+
this.init();
462+
=======
463+
this.init();
464+
>>>>>>> REPLACE`;
465+
466+
const result = strategy.applyDiff(originalContent, diffContent);
467+
expect(result).toBe(`class Example {
468+
constructor() {
469+
if (true) {
470+
this.init();
471+
}
472+
}
473+
}`);
474+
});
475+
476+
it('should handle mixed indentation changes in replace block', () => {
477+
const originalContent = `class Example {
478+
constructor() {
479+
if (true) {
480+
this.init();
481+
this.setup();
482+
this.validate();
483+
}
484+
}
485+
}`.trim();
486+
const diffContent = `test.ts
487+
<<<<<<< SEARCH
488+
this.init();
489+
this.setup();
490+
this.validate();
491+
=======
492+
this.init();
493+
this.setup();
494+
this.validate();
495+
>>>>>>> REPLACE`;
496+
497+
const result = strategy.applyDiff(originalContent, diffContent);
498+
expect(result).toBe(`class Example {
499+
constructor() {
500+
if (true) {
501+
this.init();
502+
this.setup();
503+
this.validate();
504+
}
505+
}
419506
}`);
420507
});
421508
})

src/core/diff/strategies/search-replace.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,18 @@ Your search/replace content here
211211
const currentIndent = currentIndentMatch ? currentIndentMatch[0] : '';
212212
const searchBaseIndent = searchIndents[0] || '';
213213

214-
// Calculate the relative indentation from the search content
215-
const relativeIndent = currentIndent.slice(searchBaseIndent.length);
214+
// Calculate the relative indentation level
215+
const searchBaseLevel = searchBaseIndent.length;
216+
const currentLevel = currentIndent.length;
217+
const relativeLevel = currentLevel - searchBaseLevel;
216218

217-
// Apply the matched indentation plus any relative indentation
218-
return matchedIndent + relativeIndent + line.trim();
219+
// If relative level is negative, remove indentation from matched indent
220+
// If positive, add to matched indent
221+
const finalIndent = relativeLevel < 0
222+
? matchedIndent.slice(0, Math.max(0, matchedIndent.length + relativeLevel))
223+
: matchedIndent + currentIndent.slice(searchBaseLevel);
224+
225+
return finalIndent + line.trim();
219226
});
220227

221228
// Construct the final content

0 commit comments

Comments
 (0)