From fe58421cc4e39f5af471280d527f10392f855a3c Mon Sep 17 00:00:00 2001 From: Nate Date: Fri, 13 Sep 2024 15:45:24 -0700 Subject: [PATCH] myers diff utility --- core/diff/myers.test.ts | 23 +++++++++++++++++++++++ core/diff/myers.ts | 19 +++++++++++++++++++ core/edit/lazy/deterministic.ts | 10 ++++++++++ core/package-lock.json | 16 ++++++++++++++++ core/package.json | 2 ++ 5 files changed, 70 insertions(+) create mode 100644 core/diff/myers.test.ts create mode 100644 core/diff/myers.ts diff --git a/core/diff/myers.test.ts b/core/diff/myers.test.ts new file mode 100644 index 000000000..5d68cfabf --- /dev/null +++ b/core/diff/myers.test.ts @@ -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); + }); +}); diff --git a/core/diff/myers.ts b/core/diff/myers.ts new file mode 100644 index 000000000..6fca96668 --- /dev/null +++ b/core/diff/myers.ts @@ -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; +} diff --git a/core/edit/lazy/deterministic.ts b/core/edit/lazy/deterministic.ts index d3da100db..ad4132524 100644 --- a/core/edit/lazy/deterministic.ts +++ b/core/edit/lazy/deterministic.ts @@ -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; diff --git a/core/package-lock.json b/core/package-lock.json index a3332dc01..f74caf540 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -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", diff --git a/core/package.json b/core/package.json index f261f7078..c63c05283 100644 --- a/core/package.json +++ b/core/package.json @@ -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",