This commit is contained in:
Nate 2024-09-13 19:31:58 -07:00
parent c4f2881bf5
commit 7c0d10e744
4 changed files with 182 additions and 7 deletions

View file

@ -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 }));
}

View file

@ -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<DiffLineType, "new" | "old"> | "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");
});
});

View file

@ -8,7 +8,26 @@ export async function deterministicApplyLazyEdit2(
newLazyFile: string,
filename: string,
): Promise<DiffLine[] | undefined> {
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;

View file

@ -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;
}
}