diff --git a/core/edit/lazy/deterministic.test.ts b/core/edit/lazy/deterministic.test.ts index e899a250a..cf75327a1 100644 --- a/core/edit/lazy/deterministic.test.ts +++ b/core/edit/lazy/deterministic.test.ts @@ -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"); + }); }); diff --git a/core/edit/lazy/deterministic.ts b/core/edit/lazy/deterministic.ts index 979b65bf0..e4d93c17c 100644 --- a/core/edit/lazy/deterministic.ts +++ b/core/edit/lazy/deterministic.ts @@ -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 = []; diff --git a/core/edit/lazy/test-examples/no-lazy.js.diff b/core/edit/lazy/test-examples/no-lazy.js.diff new file mode 100644 index 000000000..c5195628f --- /dev/null +++ b/core/edit/lazy/test-examples/no-lazy.js.diff @@ -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"; + } +} \ No newline at end of file