fix(hashline-edit): preserve intentional whitespace removal in autocorrect
restoreIndentForPairedReplacement() and restoreLeadingIndent() unconditionally restored original indentation when replacement had none, preventing intentional indentation changes (e.g. removing a tab from '\t1절' to '1절'). Skip indent restoration when trimmed content is identical, indicating a whitespace-only edit.
This commit is contained in:
@@ -159,6 +159,7 @@ export function restoreIndentForPairedReplacement(
|
||||
if (leadingWhitespace(line).length > 0) return line
|
||||
const indent = leadingWhitespace(originalLines[idx])
|
||||
if (indent.length === 0) return line
|
||||
if (originalLines[idx].trim() === line.trim()) return line
|
||||
return `${indent}${line}`
|
||||
})
|
||||
}
|
||||
|
||||
@@ -177,6 +177,28 @@ describe("hashline edit operations", () => {
|
||||
expect(result).toEqual(["if (x) {", " return 2", "}"])
|
||||
})
|
||||
|
||||
it("preserves intentional indentation removal (tab to no-tab)", () => {
|
||||
//#given
|
||||
const lines = ["# Title", "\t1절", "content"]
|
||||
|
||||
//#when
|
||||
const result = applySetLine(lines, anchorFor(lines, 2), "1절")
|
||||
|
||||
//#then
|
||||
expect(result).toEqual(["# Title", "1절", "content"])
|
||||
})
|
||||
|
||||
it("preserves intentional indentation removal (spaces to no-spaces)", () => {
|
||||
//#given
|
||||
const lines = ["function foo() {", " indented", "}"]
|
||||
|
||||
//#when
|
||||
const result = applySetLine(lines, anchorFor(lines, 2), "indented")
|
||||
|
||||
//#then
|
||||
expect(result).toEqual(["function foo() {", "indented", "}"])
|
||||
})
|
||||
|
||||
it("strips boundary echo around replace_lines content", () => {
|
||||
//#given
|
||||
const lines = ["before", "old 1", "old 2", "after"]
|
||||
|
||||
@@ -53,6 +53,7 @@ export function restoreLeadingIndent(templateLine: string, line: string): string
|
||||
const templateIndent = leadingWhitespace(templateLine)
|
||||
if (templateIndent.length === 0) return line
|
||||
if (leadingWhitespace(line).length > 0) return line
|
||||
if (templateLine.trim() === line.trim()) return line
|
||||
return `${templateIndent}${line}`
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user