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.
This commit is contained in:
parent
9889b469dd
commit
f6fa09bafd
3 changed files with 111 additions and 6 deletions
|
|
@ -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");
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue