add lazy blocks if there are none

This commit is contained in:
Nate 2024-09-18 15:38:10 -07:00
parent b9a327eac7
commit 4258d5587f
3 changed files with 49 additions and 3 deletions

View file

@ -143,7 +143,11 @@ describe("deterministicApplyLazyEdit(", () => {
await expectDiff("gui.js");
});
test.only("rust calculator", async () => {
test("rust calculator", async () => {
await expectDiff("rust-calc-exp.rs");
});
test("no lazy blocks", async () => {
await expectDiff("no-lazy.js");
});
});

View file

@ -1,6 +1,8 @@
import { distance } from "fastest-levenshtein";
import path from "path";
import Parser from "web-tree-sitter";
import { DiffLine } from "../..";
import { LANGUAGES } from "../../autocomplete/languages";
import { myersDiff } from "../../diff/myers";
import { getParserForFile } from "../../util/treeSitter";
import { findInAst } from "./findInAst";
@ -57,7 +59,7 @@ function reconstructNewFile(
let newLinesFound = 0;
while (
startIndex > 0 &&
newFileChars[startIndex - 1].trim() === "" &&
newFileChars[startIndex - 1]?.trim() === "" &&
newLinesFound < 2
) {
startIndex--;
@ -97,11 +99,17 @@ export async function deterministicApplyLazyEdit(
}
const oldTree = parser.parse(oldFile);
const newTree = parser.parse(newLazyFile);
let newTree = parser.parse(newLazyFile);
// If there is no lazy block anywhere, we add our own to the outsides
// so that large chunks of the file don't get removed
if (!findInAst(newTree.rootNode, isLazyBlock)) {
const ext = path.extname(filename).slice(1);
const language = LANGUAGES[ext];
if (language) {
newLazyFile = `${language.singleLineComment} ... existing code ...\n\n${newLazyFile}\n\n${language.singleLineComment} ... existing code...`;
newTree = parser.parse(newLazyFile);
}
}
const acc: LazyBlockReplacements = [];

View file

@ -0,0 +1,34 @@
class Hello {
hello() {
return "Hello World";
}
}
class Goodbye {
goodbye() {
return "Goodbye World";
}
}
---
class Goodbye {
goodbye() {
return "Au revoir le monde";
}
}
---
class Hello {
hello() {
return "Hello World";
}
}
class Goodbye {
goodbye() {
- return "Goodbye World";
+ return "Au revoir le monde";
}
}