myers diff utility

This commit is contained in:
Nate 2024-09-13 15:45:24 -07:00
parent a0e503fde0
commit fe58421cc4
5 changed files with 70 additions and 0 deletions

23
core/diff/myers.test.ts Normal file
View file

@ -0,0 +1,23 @@
import { dedent } from "../util";
import { myersDiff } from "./myers";
describe("Test myers diff function", () => {
test("should ...", () => {
const linesA = dedent`
A
B
C
D
E
`;
const linesB = dedent`
A
B
C'
D'
E
`;
const diffLines = myersDiff(linesA, linesB);
console.log(diffLines);
});
});

19
core/diff/myers.ts Normal file
View file

@ -0,0 +1,19 @@
import { diffLines } from "diff";
import { DiffLine } from "..";
const headerLineRegex = /^\s*@@ -(\d+),\d+ \+(\d+),\d+ @@/i;
const noNewLineRegex = /\\ No newline at end of file/;
export function myersDiff(oldContent: string, newContent: string): DiffLine[] {
const theirFormat = diffLines(oldContent, newContent);
const ourFormat = theirFormat.flatMap((change) => {
const type: DiffLine["type"] = change.added
? "new"
: change.removed
? "old"
: "same";
const lines = change.value.split("\n");
return lines.map((line) => ({ type, line }));
});
return ourFormat;
}

View file

@ -1,5 +1,6 @@
import Parser from "web-tree-sitter";
import { DiffLine } from "../..";
import { myersDiff } from "../../diff/myers";
import { getParserForFile } from "../../util/treeSitter";
// TODO: If we don't have high confidence, return undefined to fall back to slower methods
@ -72,6 +73,15 @@ function diffNodes(
return diffLinesForNode(newNode, "same");
}
// Other base case: no lazy blocks => use line-by-line diff
const firstLazyBlockIndex = newNode.children.find((child) =>
isLazyBlock(child),
);
if (!firstLazyBlockIndex) {
const diffLines = myersDiff(oldNode.text, newNode.text);
return diffLines;
}
const diffLines: DiffLine[] = [];
const leftChildren = oldNode.children;
const rightChildren = newNode.children;

16
core/package-lock.json generated
View file

@ -26,6 +26,7 @@
"commander": "^12.0.0",
"comment-json": "^4.2.3",
"dbinfoz": "^0.1.4",
"diff": "^7.0.0",
"dotenv": "^16.4.5",
"fastest-levenshtein": "^1.0.16",
"follow-redirects": "^1.15.5",
@ -69,6 +70,7 @@
"@babel/preset-env": "^7.24.7",
"@biomejs/biome": "1.6.4",
"@google/generative-ai": "^0.11.4",
"@types/diff": "^5.2.2",
"@types/follow-redirects": "^1.14.4",
"@types/jest": "^29.5.12",
"@types/jquery": "^3.5.29",
@ -4756,6 +4758,12 @@
"integrity": "sha512-n7RlEEJ+4x4TS7ZQddTmNSxP+zziEG0TNsMfiRIxcIVXt71ENJ9ojeXmGO3wPoTdn7pJcU2xc3CJYMktNT6DPg==",
"peer": true
},
"node_modules/@types/diff": {
"version": "5.2.2",
"resolved": "https://registry.npmjs.org/@types/diff/-/diff-5.2.2.tgz",
"integrity": "sha512-qVqLpd49rmJA2nZzLVsmfS/aiiBpfVE95dHhPVwG0NmSBAt+riPxnj53wq2oBq5m4Q2RF1IWFEUpnZTgrQZfEQ==",
"dev": true
},
"node_modules/@types/follow-redirects": {
"version": "1.14.4",
"resolved": "https://registry.npmjs.org/@types/follow-redirects/-/follow-redirects-1.14.4.tgz",
@ -6922,6 +6930,14 @@
"resolved": "https://registry.npmjs.org/devtools-protocol/-/devtools-protocol-0.0.1312386.tgz",
"integrity": "sha512-DPnhUXvmvKT2dFA/j7B+riVLUt9Q6RKJlcppojL5CoRywJJKLDYnRlw0gTFKfgDPHP5E04UoB71SxoJlVZy8FA=="
},
"node_modules/diff": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/diff/-/diff-7.0.0.tgz",
"integrity": "sha512-PJWHUb1RFevKCwaFA9RlG5tCd+FO5iRh9A8HEtkmBH2Li03iJriB6m6JIN4rGz3K3JLawI7/veA1xzRKP6ISBw==",
"engines": {
"node": ">=0.3.1"
}
},
"node_modules/diff-sequences": {
"version": "29.6.3",
"resolved": "https://registry.npmjs.org/diff-sequences/-/diff-sequences-29.6.3.tgz",

View file

@ -15,6 +15,7 @@
"@babel/preset-env": "^7.24.7",
"@biomejs/biome": "1.6.4",
"@google/generative-ai": "^0.11.4",
"@types/diff": "^5.2.2",
"@types/follow-redirects": "^1.14.4",
"@types/jest": "^29.5.12",
"@types/jquery": "^3.5.29",
@ -55,6 +56,7 @@
"commander": "^12.0.0",
"comment-json": "^4.2.3",
"dbinfoz": "^0.1.4",
"diff": "^7.0.0",
"dotenv": "^16.4.5",
"fastest-levenshtein": "^1.0.16",
"follow-redirects": "^1.15.5",