Merge pull request #4687 from Granite-Code/panyamkeerthana/rejected-trailing-whitespaces
rejected edits trailing whitespaces
This commit is contained in:
commit
7d95521518
4 changed files with 113 additions and 19 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");
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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