Merge pull request #4356 from owtaylor/edit-trailing-whitespace
Don't get confused when the model removes a trailing blank line for an edit
This commit is contained in:
commit
47fd48269d
5 changed files with 51 additions and 24 deletions
|
|
@ -29,4 +29,16 @@ describe("Test myers diff function", () => {
|
|||
{ type: "same", line: "E" },
|
||||
]);
|
||||
});
|
||||
|
||||
test("should ignore newline differences at end", () => {
|
||||
const linesA = "A\nB\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" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ export function convertMyersChangeToDiffLines(change: Change): DiffLine[] {
|
|||
: "same";
|
||||
const lines = change.value.split("\n");
|
||||
|
||||
// `diff` package is always adding an extra new line at the end of the array
|
||||
// Ignore the \n at the end of the final line, if there is one
|
||||
if (lines[lines.length - 1] === "") {
|
||||
lines.pop();
|
||||
}
|
||||
|
|
@ -18,9 +18,27 @@ export function convertMyersChangeToDiffLines(change: Change): DiffLine[] {
|
|||
return lines.map((line) => ({ type, line }));
|
||||
}
|
||||
|
||||
// The interpretation of lines in oldContent and newContent is the same as jsdiff
|
||||
// Lines are separated by \n, with the exception that a trailing \n does *not*
|
||||
// represent an empty line.
|
||||
//
|
||||
// The default for jsdiff is that "foo" and "foo\n" are *different* single-line
|
||||
// contents, but we can't represent that: to avoid a diff
|
||||
// [ { type: "old", line: "foo" }, { type: "new", line: "foo" } ], we
|
||||
// pass ignoreNewlineAtEof: true.
|
||||
export function myersDiff(oldContent: string, newContent: string): DiffLine[] {
|
||||
const theirFormat = diffLines(oldContent, newContent);
|
||||
const theirFormat = diffLines(oldContent, newContent, {
|
||||
ignoreNewlineAtEof: true,
|
||||
});
|
||||
const ourFormat = theirFormat.flatMap(convertMyersChangeToDiffLines);
|
||||
|
||||
while (
|
||||
ourFormat.length > 0 &&
|
||||
ourFormat[ourFormat.length - 1].type === "old" &&
|
||||
ourFormat[ourFormat.length - 1].line === ""
|
||||
) {
|
||||
ourFormat.pop();
|
||||
}
|
||||
|
||||
return ourFormat;
|
||||
}
|
||||
|
|
|
|||
11
core/package-lock.json
generated
11
core/package-lock.json
generated
|
|
@ -78,7 +78,7 @@
|
|||
"@babel/preset-env": "^7.24.7",
|
||||
"@biomejs/biome": "1.6.4",
|
||||
"@google/generative-ai": "^0.11.4",
|
||||
"@types/diff": "^5.2.2",
|
||||
"@types/diff": "^7.0.1",
|
||||
"@types/follow-redirects": "^1.14.4",
|
||||
"@types/jest": "^29.5.12",
|
||||
"@types/jquery": "^3.5.29",
|
||||
|
|
@ -5249,10 +5249,11 @@
|
|||
"peer": true
|
||||
},
|
||||
"node_modules/@types/diff": {
|
||||
"version": "5.2.3",
|
||||
"resolved": "https://registry.npmjs.org/@types/diff/-/diff-5.2.3.tgz",
|
||||
"integrity": "sha512-K0Oqlrq3kQMaO2RhfrNQX5trmt+XLyom88zS0u84nnIcLvFnRUMRRHmrGny5GSM+kNO9IZLARsdQHDzkhAgmrQ==",
|
||||
"dev": true
|
||||
"version": "7.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/diff/-/diff-7.0.1.tgz",
|
||||
"integrity": "sha512-R/BHQFripuhW6XPXy05hIvXJQdQ4540KnTvEFHSLjXfHYM41liOLKgIJEyYYiQe796xpaMHfe4Uj/p7Uvng2vA==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/follow-redirects": {
|
||||
"version": "1.14.4",
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
"@babel/preset-env": "^7.24.7",
|
||||
"@biomejs/biome": "1.6.4",
|
||||
"@google/generative-ai": "^0.11.4",
|
||||
"@types/diff": "^5.2.2",
|
||||
"@types/diff": "^7.0.1",
|
||||
"@types/follow-redirects": "^1.14.4",
|
||||
"@types/jest": "^29.5.12",
|
||||
"@types/jquery": "^3.5.29",
|
||||
|
|
|
|||
|
|
@ -86,17 +86,8 @@ export class VerticalDiffHandler implements vscode.Disposable {
|
|||
}
|
||||
|
||||
private async insertDeletionBuffer() {
|
||||
// Don't remove trailing whitespace line
|
||||
const totalDeletedContent = this.deletionBuffer.join("\n");
|
||||
|
||||
if (
|
||||
totalDeletedContent === "" &&
|
||||
this.currentLineIndex >= this.endLine + this.newLinesAdded &&
|
||||
this.insertedInCurrentBlock === 0
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.deletionBuffer.length || this.insertedInCurrentBlock > 0) {
|
||||
const blocks = this.editorToVerticalDiffCodeLens.get(this.fileUri) || [];
|
||||
|
||||
|
|
@ -359,7 +350,7 @@ export class VerticalDiffHandler implements vscode.Disposable {
|
|||
// Clear deletion buffer
|
||||
await this.insertDeletionBuffer();
|
||||
|
||||
await this.reapplyWithMeyersDiff(diffLines);
|
||||
await this.reapplyWithMyersDiff(diffLines);
|
||||
|
||||
this.options.onStatusUpdate(
|
||||
"done",
|
||||
|
|
@ -482,10 +473,10 @@ export class VerticalDiffHandler implements vscode.Disposable {
|
|||
/**
|
||||
* This method is used to apply diff decorations after the intiial stream.
|
||||
* This is to handle scenarios where we miscalculate the original diff blocks,
|
||||
* and decide to follow up with a deterministic algorithm like Meyers Diff once
|
||||
* and decide to follow up with a deterministic algorithm like Myers Diff once
|
||||
* we have received all of the diff lines.
|
||||
*/
|
||||
async reapplyWithMeyersDiff(diffLines: DiffLine[]) {
|
||||
async reapplyWithMyersDiff(diffLines: DiffLine[]) {
|
||||
// Diff is messed up without this delay.
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
|
||||
|
|
@ -504,23 +495,28 @@ export class VerticalDiffHandler implements vscode.Disposable {
|
|||
this.clearDecorations();
|
||||
|
||||
// Then, get our old/new file content based on the original lines
|
||||
// We need the input to be "newline terminated" rather than
|
||||
// newline separated, because myersDiff() would consider
|
||||
// ["A"] => "A" and ["A", ""] => "A\n" to be the same single line.
|
||||
// "A\n" and "A\n\n" are unambiguous.
|
||||
//
|
||||
const oldFileContent = diffLines
|
||||
.filter((line) => line.type === "same" || line.type === "old")
|
||||
.map((line) => line.line)
|
||||
.join("\n");
|
||||
.join("\n") + "\n";
|
||||
|
||||
const newFileContent = diffLines
|
||||
.filter((line) => line.type === "same" || line.type === "new")
|
||||
.map((line) => line.line)
|
||||
.join("\n");
|
||||
.join("\n") + "\n";
|
||||
|
||||
const diffs = myersDiff(oldFileContent, newFileContent);
|
||||
|
||||
const meyersDiffLines = diffs.map((diff) => diff.line).join("\n");
|
||||
const myersDiffLines = diffs.map((diff) => diff.line).join("\n");
|
||||
|
||||
// Then, we insert our diff lines
|
||||
await this.editor.edit((editBuilder) => {
|
||||
editBuilder.replace(this.range, meyersDiffLines),
|
||||
editBuilder.replace(this.range, myersDiffLines),
|
||||
{
|
||||
undoStopAfter: false,
|
||||
undoStopBefore: false,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue