Negative indentation

This commit is contained in:
Matt Rubens
2024-12-15 16:42:54 -05:00
parent 0bdb325ea1
commit 25da0b1907
2 changed files with 98 additions and 4 deletions

View File

@@ -416,6 +416,93 @@ class Example {
console.error(e); 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();
}
}
}`); }`);
}); });
}) })

View File

@@ -211,11 +211,18 @@ Your search/replace content here
const currentIndent = currentIndentMatch ? currentIndentMatch[0] : ''; const currentIndent = currentIndentMatch ? currentIndentMatch[0] : '';
const searchBaseIndent = searchIndents[0] || ''; const searchBaseIndent = searchIndents[0] || '';
// Calculate the relative indentation from the search content // Calculate the relative indentation level
const relativeIndent = currentIndent.slice(searchBaseIndent.length); const searchBaseLevel = searchBaseIndent.length;
const currentLevel = currentIndent.length;
const relativeLevel = currentLevel - searchBaseLevel;
// Apply the matched indentation plus any relative indentation // If relative level is negative, remove indentation from matched indent
return matchedIndent + relativeIndent + line.trim(); // 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 // Construct the final content