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);