From f6fa09bafddbc3a2225adb3b17663fb7cc34d108 Mon Sep 17 00:00:00 2001 From: Keerthana Panyam Date: Sun, 16 Mar 2025 21:49:27 -0400 Subject: [PATCH 1/2] rejected edit trims the trailing whitespaces trailing whitespace was stripped before generating diffs, causing mismatches when rejecting a diff in the editor. this fix ensures that trailing whitespace differences do not interfere with accurate diff rejection. --- core/diff/streamDiff.test.ts | 103 +++++++++++++++++++++++++++++++++++ core/diff/util.ts | 11 +++- core/edit/streamDiffLines.ts | 3 - 3 files changed, 111 insertions(+), 6 deletions(-) diff --git a/core/diff/streamDiff.test.ts b/core/diff/streamDiff.test.ts index fa7db52ac..560d2e13d 100644 --- a/core/diff/streamDiff.test.ts +++ b/core/diff/streamDiff.test.ts @@ -223,6 +223,109 @@ describe("streamDiff(", () => { await expectDiff("fastapi-tabs-vs-spaces.py"); }); + test("trailing whitespaces should match ", async () => { + const oldLines = ["first item ", "second arg ", "third param "]; + + const newLines = ["first item", "second arg", "third param "]; + + const { streamDiffs } = await collectDiffs(oldLines, newLines); + + expect(streamDiffs).toEqual([ + { type: "same", line: "first item " }, + { type: "same", line: "second arg " }, + { type: "same", line: "third param " }, + ]); + }); + + //indentation and whitespace handling + test.each([false, true])( + "ignores indentation changes for sufficiently long lines (trailingWhitespace: %s)", + async (trailingWhitespace) => { + let oldLines = [ + " short", + " middle", + " a long enough line", + " short2", + "indented line", + "final line", + ]; + + let newLines = [ + "short", + "middle", + "a long enough line", + "short2", + " indented line", + "final line", + ]; + + if (trailingWhitespace) { + oldLines = oldLines.map((line) => line + " "); + } + + const { streamDiffs } = await collectDiffs(oldLines, newLines); + const expected = trailingWhitespace + ? [ + { type: "old", line: " short " }, + { type: "new", line: "short" }, + { type: "old", line: " middle " }, + { type: "new", line: "middle" }, + { type: "same", line: " a long enough line " }, + { type: "same", line: " short2 " }, + { type: "same", line: "indented line " }, + { type: "same", line: "final line " }, + ] + : [ + { type: "old", line: " short" }, + { type: "new", line: "short" }, + { type: "old", line: " middle" }, + { type: "new", line: "middle" }, + { type: "same", line: " a long enough line" }, + { type: "same", line: " short2" }, + { type: "same", line: "indented line" }, + { type: "same", line: "final line" }, + ]; + + expect(streamDiffs).toEqual(expected); + }, + ); + + test("preserves original lines for minor reindentation in simple block", async () => { + const oldLines = ["if (checkValueOf(x)) {", " doSomethingWith(x);", "}"]; + const newLines = ["if (checkValueOf(x)) {", " doSomethingWith(x);", "}"]; + + const { streamDiffs } = await collectDiffs(oldLines, newLines); + + expect(streamDiffs).toEqual([ + { type: "same", line: "if (checkValueOf(x)) {" }, + { type: "same", line: " doSomethingWith(x);" }, + { type: "same", line: "}" }, + ]); + }); + + test("uses new lines for nested reindentation changes", async () => { + const oldLines = ["if (checkValueOf(x)) {", " doSomethingWith(x);", "}"]; + const newLines = [ + "if (checkValueOf(x)) {", + " if (reallyCheckValueOf(x)) {", + " doSomethingElseWith(x);", + " }", + "}", + ]; + + const { streamDiffs } = await collectDiffs(oldLines, newLines); + + expect(streamDiffs).toEqual([ + { type: "same", line: "if (checkValueOf(x)) {" }, + { type: "new", line: " if (reallyCheckValueOf(x)) {" }, + { type: "old", line: " doSomethingWith(x);" }, + { type: "new", line: " doSomethingElseWith(x);" }, + { type: "old", line: "}" }, + { type: "new", line: " }" }, + { type: "new", line: "}" }, + ]); + }); + test("FastAPI example", async () => { await expectDiff("fastapi.py"); }); diff --git a/core/diff/util.ts b/core/diff/util.ts index c56e88504..571ede7b9 100644 --- a/core/diff/util.ts +++ b/core/diff/util.ts @@ -62,18 +62,23 @@ export function matchLine( const isEndBracket = END_BRACKETS.includes(newLine.trim()); for (let i = 0; i < oldLines.length; i++) { + // trims trailing whitespaces from the lines before comparison + //this ensures trailing spaces don't affect matching. + const oldLineTrimmed = oldLines[i].trimEnd(); + const newLineTrimmed = newLine.trimEnd(); + // Don't match end bracket lines if too far away if (i > 4 && isEndBracket) { return { matchIndex: -1, isPerfectMatch: false, newLine }; } - if (linesMatchPerfectly(newLine, oldLines[i])) { + if (linesMatchPerfectly(newLineTrimmed, oldLineTrimmed)) { return { matchIndex: i, isPerfectMatch: true, newLine }; } - if (linesMatch(newLine, oldLines[i], i)) { + if (linesMatch(newLineTrimmed, oldLineTrimmed, i)) { // This is a way to fix indentation, but only for sufficiently long lines to avoid matching whitespace or short lines if ( - newLine.trimStart() === oldLines[i].trimStart() && + newLineTrimmed.trimStart() === oldLineTrimmed.trimStart() && (permissiveAboutIndentation || newLine.trim().length > 8) ) { return { diff --git a/core/edit/streamDiffLines.ts b/core/edit/streamDiffLines.ts index f7b54dff6..4f530cd60 100644 --- a/core/edit/streamDiffLines.ts +++ b/core/edit/streamDiffLines.ts @@ -78,9 +78,6 @@ export async function* streamDiffLines( oldLines = []; } - // Trim end of oldLines, otherwise we have trailing \r on every line for CRLF files - oldLines = oldLines.map((line) => line.trimEnd()); - const prompt = overridePrompt ?? constructPrompt(prefix, highlighted, suffix, llm, input, language); From 3ba54526fb3efafb9676c6e59156dbb03d82f54f Mon Sep 17 00:00:00 2001 From: Keerthana Panyam Date: Tue, 18 Mar 2025 15:35:51 -0400 Subject: [PATCH 2/2] remove redundant code since `oldLinesCopy[0] === newLine` should always be classified as `"same"` or `"old"` in the earlier block, the `else` statement (which sets `isLineRemoval = true`)feels redundant and never actually executed. --- core/diff/streamDiff.ts | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/core/diff/streamDiff.ts b/core/diff/streamDiff.ts index 4daa30804..436da1c09 100644 --- a/core/diff/streamDiff.ts +++ b/core/diff/streamDiff.ts @@ -35,7 +35,6 @@ export async function* streamDiff( let type: DiffLineType; - let isLineRemoval = false; const isNewLine = matchIndex === -1; if (isNewLine) { @@ -45,7 +44,6 @@ export async function* streamDiff( for (let i = 0; i < matchIndex; i++) { yield { type: "old", line: oldLinesCopy.shift()! }; } - type = isPerfectMatch ? "same" : "old"; } @@ -60,22 +58,13 @@ export async function* streamDiff( case "old": yield { type, line: oldLinesCopy.shift()! }; - - if (oldLinesCopy[0] !== newLine) { - yield { type: "new", line: newLine }; - } else { - isLineRemoval = true; - } - + yield { type: "new", line: newLine }; break; default: console.error(`Error streaming diff, unrecognized diff type: ${type}`); } - - if (!isLineRemoval) { - newLineResult = await newLines.next(); - } + newLineResult = await newLines.next(); } // Once at the edge, only one choice