From c7efe8f002f8845bbdb4645c6a94e4b16e5f3d4e Mon Sep 17 00:00:00 2001 From: minpeter Date: Tue, 24 Feb 2026 14:07:21 +0900 Subject: [PATCH] fix(hashline-edit): preserve intentional whitespace removal in autocorrect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../autocorrect-replacement-lines.ts | 1 + .../hashline-edit/edit-operations.test.ts | 22 +++++++++++++++++++ .../hashline-edit/edit-text-normalization.ts | 1 + 3 files changed, 24 insertions(+) diff --git a/src/tools/hashline-edit/autocorrect-replacement-lines.ts b/src/tools/hashline-edit/autocorrect-replacement-lines.ts index 6b9d9a77..719f9d6c 100644 --- a/src/tools/hashline-edit/autocorrect-replacement-lines.ts +++ b/src/tools/hashline-edit/autocorrect-replacement-lines.ts @@ -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}` }) } diff --git a/src/tools/hashline-edit/edit-operations.test.ts b/src/tools/hashline-edit/edit-operations.test.ts index 53910660..d66c2d94 100644 --- a/src/tools/hashline-edit/edit-operations.test.ts +++ b/src/tools/hashline-edit/edit-operations.test.ts @@ -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"] diff --git a/src/tools/hashline-edit/edit-text-normalization.ts b/src/tools/hashline-edit/edit-text-normalization.ts index 8e38c50c..9bae8cc4 100644 --- a/src/tools/hashline-edit/edit-text-normalization.ts +++ b/src/tools/hashline-edit/edit-text-normalization.ts @@ -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}` }