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..7dcb1f67c 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,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; } diff --git a/core/package-lock.json b/core/package-lock.json index c99cf8b73..609dc31b8 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", @@ -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", diff --git a/core/package.json b/core/package.json index 9f06706a9..927f8e583 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 ced48e03d..2e90de6e6 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) || []; @@ -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,