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.)
This commit is contained in:
parent
43bdea273d
commit
698633815f
5 changed files with 36 additions and 10 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,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;
|
||||
|
|
|
|||
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",
|
||||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue