From 7c0d10e744e2cb5dd79b0ed40c87429520b0719c Mon Sep 17 00:00:00 2001 From: Nate Date: Fri, 13 Sep 2024 19:31:58 -0700 Subject: [PATCH] progress --- core/diff/myers.ts | 8 +- core/edit/lazy/deterministic.test.ts | 23 +++- core/edit/lazy/deterministic2.ts | 29 +++- .../test-examples/calculator-comments.js.diff | 129 ++++++++++++++++++ 4 files changed, 182 insertions(+), 7 deletions(-) create mode 100644 core/edit/lazy/test-examples/calculator-comments.js.diff diff --git a/core/diff/myers.ts b/core/diff/myers.ts index 16600e5d4..06b0ce7ec 100644 --- a/core/diff/myers.ts +++ b/core/diff/myers.ts @@ -7,7 +7,13 @@ export function convertMyersChangeToDiffLines(change: Change): DiffLine[] { : change.removed ? "old" : "same"; - const lines = change.value.trimEnd().split("\n"); + const lines = change.value.split("\n"); + + // `diff` package is always adding an extra new line at the end of the array + if (lines[lines.length - 1] === "") { + lines.pop(); + } + return lines.map((line) => ({ type, line })); } diff --git a/core/edit/lazy/deterministic.test.ts b/core/edit/lazy/deterministic.test.ts index c061bf681..c146df0cf 100644 --- a/core/edit/lazy/deterministic.test.ts +++ b/core/edit/lazy/deterministic.test.ts @@ -5,7 +5,7 @@ import { changed, diff as myersDiff } from "myers-diff"; import path from "node:path"; import { DiffLine, DiffLineType } from "../.."; import { dedent } from "../../util"; -import { deterministicApplyLazyEdit } from "./deterministic"; +import { deterministicApplyLazyEdit2 as deterministicApplyLazyEdit } from "./deterministic2"; // "modification" is an extra type used to represent an "old" + "new" diff line type MyersDiffTypes = Extract | "modification"; @@ -58,6 +58,13 @@ function displayDiff(diff: DiffLine[]) { .join("\n"); } +function normalizeDisplayedDiff(d: string): string { + return d + .split("\n") + .map((line) => (line.trim() === "" ? "" : line)) + .join("\n"); +} + async function expectDiff(file: string) { const testFilePath = path.join( __dirname, @@ -89,7 +96,9 @@ async function expectDiff(file: string) { throw new Error("Expected diff is empty"); } - expect(displayedDiff).toEqual(expectedDiff); + expect(normalizeDisplayedDiff(displayedDiff)).toEqual( + normalizeDisplayedDiff(expectedDiff), + ); } describe("deterministicApplyLazyEdit(", () => { @@ -120,11 +129,15 @@ describe("deterministicApplyLazyEdit(", () => { expect(myersDiffs).toEqual([]); }); - test.only("fastapi", async () => { + test("fastapi", async () => { await expectDiff("fastapi.py"); }); - test.only("calculator exp", async () => { - await expectDiff("fastapi.py"); + test("calculator exp", async () => { + await expectDiff("calculator-exp.js"); + }); + + test.only("calculator comments", async () => { + await expectDiff("calculator-comments.js"); }); }); diff --git a/core/edit/lazy/deterministic2.ts b/core/edit/lazy/deterministic2.ts index 868c4c370..ae1a1bbbf 100644 --- a/core/edit/lazy/deterministic2.ts +++ b/core/edit/lazy/deterministic2.ts @@ -8,7 +8,26 @@ export async function deterministicApplyLazyEdit2( newLazyFile: string, filename: string, ): Promise { - const theirFormat = diffLines(oldFile, newLazyFile); + // // Strip surrounding new lines away from lazy blocks + // const lazyFileLines = newLazyFile.split("\n"); + // const processedLazyFileLines = []; + // for (let i = 0; i < lazyFileLines.length - 1; ++i) { + // const line = lazyFileLines[i]; + // const nextLine = lazyFileLines[i + 1]; + // if (line.trim() === "" && isLazyLine(nextLine)) { + // continue; + // } else if (isLazyLine(line) && nextLine.trim() === "") { + // i++; + // processedLazyFileLines.push(line); + // continue; + // } + // processedLazyFileLines.push(line); + // } + // newLazyFile = processedLazyFileLines.join("\n"); + + const theirFormat = diffLines(oldFile, newLazyFile, { + newlineIsToken: false, + }); const lines: DiffLine[] = []; const removedLinesBuffer: DiffLine[] = []; @@ -26,6 +45,14 @@ export async function deterministicApplyLazyEdit2( })), ); removedLinesBuffer.length = 0; + if (!isLazyLine(change.value.split("\n")[0])) { + lines.push( + ...convertMyersChangeToDiffLines({ + ...change, + value: change.value.split("\n").slice(0, -1).join("\n"), + }), + ); + } } else if (change.added) { lines.push(...removedLinesBuffer); removedLinesBuffer.length = 0; diff --git a/core/edit/lazy/test-examples/calculator-comments.js.diff b/core/edit/lazy/test-examples/calculator-comments.js.diff new file mode 100644 index 000000000..da4c6f5bc --- /dev/null +++ b/core/edit/lazy/test-examples/calculator-comments.js.diff @@ -0,0 +1,129 @@ +class Calculator { + constructor() { + this.result = 0; + } + + add(number) { + this.result += number; + return this; + } + + subtract(number) { + this.result -= number; + return this; + } + + multiply(number) { + this.result *= number; + return this; + } + + divide(number) { + if (number === 0) { + throw new Error("Cannot divide by zero"); + } + this.result /= number; + return this; + } + + getResult() { + return this.result; + } + + reset() { + this.result = 0; + return this; + } +} + +--- + +class Calculator { + // ... existing code ... + + // Adds the given number to the result + add(number) { + this.result += number; + return this; + } + + // Subtracts the given number from the result + subtract(number) { + this.result -= number; + return this; + } + + // Multiplies the result by the given number + multiply(number) { + this.result *= number; + return this; + } + + // Divides the result by the given number + // Throws an error if attempting to divide by zero + divide(number) { + if (number === 0) { + throw new Error("Cannot divide by zero"); + } + this.result /= number; + return this; + } + + // Returns the current result + getResult() { + return this.result; + } + + // Resets the result to zero + reset() { + this.result = 0; + return this; + } +} + +--- + + class Calculator { + constructor() { + this.result = 0; + } + ++ // Adds the given number to the result + add(number) { + this.result += number; + return this; + } + ++ // Subtracts the given number from the result + subtract(number) { + this.result -= number; + return this; + } + ++ // Multiplies the result by the given number + multiply(number) { + this.result *= number; + return this; + } + ++ // Divides the result by the given number ++ // Throws an error if attempting to divide by zero + divide(number) { + if (number === 0) { + throw new Error("Cannot divide by zero"); + } + this.result /= number; + return this; + } + ++ // Returns the current result + getResult() { + return this.result; + } + ++ // Resets the result to zero + reset() { + this.result = 0; + return this; + } + } \ No newline at end of file