From 4219a206f2407d170cf75d7abb6a86e0561bc4b8 Mon Sep 17 00:00:00 2001 From: "Owen W. Taylor" Date: Tue, 25 Feb 2025 15:27:20 -0500 Subject: [PATCH 1/4] When inserting streaming diffs, don't mess up for blank lines There was code of unclear purpose that bailed out early when totalDeletedContent == "" - but "" could either be [].join("\n") or: [""].join("\n") in the latter case, we *do* need to a deletion block. Since the deletionBuffer.length == 0 case is otherwise handled, the code is removed as unnecessary and harmful. --- extensions/vscode/src/diff/vertical/handler.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/extensions/vscode/src/diff/vertical/handler.ts b/extensions/vscode/src/diff/vertical/handler.ts index ced48e03d..5e3bcd45f 100644 --- a/extensions/vscode/src/diff/vertical/handler.ts +++ b/extensions/vscode/src/diff/vertical/handler.ts @@ -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) || []; From 43bdea273dd9c345420976c597f7af8865f46f8f Mon Sep 17 00:00:00 2001 From: "Owen W. Taylor" Date: Tue, 25 Feb 2025 13:43:16 -0500 Subject: [PATCH 2/4] Fix typo: meyersDiff => myersDiff --- extensions/vscode/src/diff/vertical/handler.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/extensions/vscode/src/diff/vertical/handler.ts b/extensions/vscode/src/diff/vertical/handler.ts index 5e3bcd45f..982df41c3 100644 --- a/extensions/vscode/src/diff/vertical/handler.ts +++ b/extensions/vscode/src/diff/vertical/handler.ts @@ -350,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", @@ -473,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)); @@ -507,11 +507,11 @@ export class VerticalDiffHandler implements vscode.Disposable { 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, From 698633815fa2d41c3bdb2358f430a89124e567f8 Mon Sep 17 00:00:00 2001 From: "Owen W. Taylor" Date: Tue, 25 Feb 2025 15:12:48 -0500 Subject: [PATCH 3/4] reapplyWithMyersDiff(): don't skip trailing blank lines myersDiff inherits the jsdiff behavior for trailing \n, so would consider "foo\n" => "foo" a changed single line, rather than a change from "foo", "" to "foo" - this would result in a trailing blank line in the highlighted region silently vanishing from the buffer. Adding \n to the inputs so that lines are \n terminated rather than separated fixes this. To make myersDiff logically consistent, pass ignoreNewlineAtEof: true to jsdiff. (This is actually not needed for this bug fix.) --- core/diff/myers.test.ts | 12 ++++++++++++ core/diff/myers.ts | 12 ++++++++++-- core/package-lock.json | 11 ++++++----- core/package.json | 2 +- extensions/vscode/src/diff/vertical/handler.ts | 9 +++++++-- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/core/diff/myers.test.ts b/core/diff/myers.test.ts index 2b47fa548..4148424a6 100644 --- a/core/diff/myers.test.ts +++ b/core/diff/myers.test.ts @@ -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" }, + ]); + }); }); diff --git a/core/diff/myers.ts b/core/diff/myers.ts index 22a2fc4e3..41f250195 100644 --- a/core/diff/myers.ts +++ b/core/diff/myers.ts @@ -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,8 +18,16 @@ 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); return ourFormat; diff --git a/core/package-lock.json b/core/package-lock.json index b6285e5e2..6f82ed7e9 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -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", @@ -5257,10 +5257,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", diff --git a/core/package.json b/core/package.json index 37a5b8a3e..82f635960 100644 --- a/core/package.json +++ b/core/package.json @@ -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", diff --git a/extensions/vscode/src/diff/vertical/handler.ts b/extensions/vscode/src/diff/vertical/handler.ts index 982df41c3..2e90de6e6 100644 --- a/extensions/vscode/src/diff/vertical/handler.ts +++ b/extensions/vscode/src/diff/vertical/handler.ts @@ -495,15 +495,20 @@ 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); From be8afa65406be881949d3e14497c0ac33c27bbe0 Mon Sep 17 00:00:00 2001 From: Nate Date: Sat, 15 Mar 2025 19:10:25 -0700 Subject: [PATCH 4/4] ignore removed trailing whitespace in myers diff --- core/diff/myers.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/core/diff/myers.ts b/core/diff/myers.ts index 41f250195..7dcb1f67c 100644 --- a/core/diff/myers.ts +++ b/core/diff/myers.ts @@ -27,8 +27,18 @@ export function convertMyersChangeToDiffLines(change: Change): DiffLine[] { // [ { 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, { ignoreNewlineAtEof: true }); + 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; }