From a4cc558cf593d7b2044747cd1984ac2fcc34dd0e Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 21 Apr 2025 12:09:14 -0700 Subject: [PATCH] diff cleaning logic --- core/diff/myers.test.ts | 12 ++++++++++++ core/diff/myers.ts | 15 ++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/core/diff/myers.test.ts b/core/diff/myers.test.ts index 4148424a6..0683e5a96 100644 --- a/core/diff/myers.test.ts +++ b/core/diff/myers.test.ts @@ -41,4 +41,16 @@ describe("Test myers diff function", () => { { type: "same", line: "C" }, ]); }); + + test("should ignore single-line whitespace-only differences", () => { + const linesA = "A\n B\nC\n"; + const linesB = "A\nB\nC"; + + const diffLines = myersDiff(linesA, linesB); + expect(diffLines).toEqual([ + { type: "same", line: "A" }, + { type: "same", line: " B" }, + { type: "same", line: "C" }, + ]); + }); }); diff --git a/core/diff/myers.ts b/core/diff/myers.ts index 7dcb1f67c..0b5d23730 100644 --- a/core/diff/myers.ts +++ b/core/diff/myers.ts @@ -30,8 +30,21 @@ export function myersDiff(oldContent: string, newContent: string): DiffLine[] { const theirFormat = diffLines(oldContent, newContent, { ignoreNewlineAtEof: true, }); - const ourFormat = theirFormat.flatMap(convertMyersChangeToDiffLines); + let ourFormat = theirFormat.flatMap(convertMyersChangeToDiffLines); + // Combine consecutive old/new pairs that are identical after trimming + for (let i = 0; i < ourFormat.length - 1; i++) { + if ( + ourFormat[i]?.type === "old" && + ourFormat[i + 1]?.type === "new" && + ourFormat[i].line.trim() === ourFormat[i + 1].line.trim() + ) { + ourFormat[i] = { type: "same", line: ourFormat[i].line }; + ourFormat.splice(i + 1, 1); + } + } + + // Remove trailing empty old lines while ( ourFormat.length > 0 && ourFormat[ourFormat.length - 1].type === "old" &&