diff --git a/core/edit/lazy/applyCodeBlock.ts b/core/edit/lazy/applyCodeBlock.ts index d2323aca3..8ca9a8154 100644 --- a/core/edit/lazy/applyCodeBlock.ts +++ b/core/edit/lazy/applyCodeBlock.ts @@ -2,7 +2,7 @@ import { DiffLine, ILLM } from "../.."; import { generateLines } from "../../diff/util"; import { supportedLanguages } from "../../util/treeSitter"; import { getUriFileExtension } from "../../util/uri"; - +import { deterministicApplyLazyEdit } from "./deterministic"; import { streamLazyApply } from "./streamLazyApply"; import { applyUnifiedDiff, isUnifiedDiffFormat } from "./unifiedDiffApply"; @@ -13,35 +13,44 @@ function canUseInstantApply(filename: string) { export async function applyCodeBlock( oldFile: string, - newFile: string, + newLazyFile: string, filename: string, llm: ILLM, -): Promise<[boolean, AsyncGenerator]> { - // This was buggy, removed for now, maybe forever - // if (canUseInstantApply(filename)) { - // const diffLines = await deterministicApplyLazyEdit( - // oldFile, - // newFile, - // filename, - // ); +): Promise<{ + isInstantApply: boolean; + diffLinesGenerator: AsyncGenerator; +}> { + if (canUseInstantApply(filename)) { + const diffLines = await deterministicApplyLazyEdit({ + oldFile, + newLazyFile, + filename, + onlyFullFileRewrite: true, + }); - // // Fall back to LLM method if we couldn't apply deterministically - // if (diffLines !== undefined) { - // const diffGenerator = generateLines(diffLines!); - // return [true, diffGenerator]; - // } - // } + if (diffLines !== undefined) { + return { + isInstantApply: true, + diffLinesGenerator: generateLines(diffLines!), + }; + } + } // If the code block is a diff - if (isUnifiedDiffFormat(newFile)) { + if (isUnifiedDiffFormat(newLazyFile)) { try { - const diffLines = applyUnifiedDiff(oldFile, newFile); - const diffGenerator = generateLines(diffLines!); - return [true, diffGenerator]; + const diffLines = applyUnifiedDiff(oldFile, newLazyFile); + return { + isInstantApply: true, + diffLinesGenerator: generateLines(diffLines!), + }; } catch (e) { console.error("Failed to apply unified diff", e); } } - return [false, streamLazyApply(oldFile, filename, newFile, llm)]; + return { + isInstantApply: false, + diffLinesGenerator: streamLazyApply(oldFile, filename, newLazyFile, llm), + }; } diff --git a/core/edit/lazy/deterministic.test.ts b/core/edit/lazy/deterministic.test.ts index 7590e5025..adfaafa5e 100644 --- a/core/edit/lazy/deterministic.test.ts +++ b/core/edit/lazy/deterministic.test.ts @@ -21,11 +21,11 @@ async function collectDiffs( ): Promise<{ ourDiffs: DiffLine[]; myersDiffs: any }> { const ourDiffs: DiffLine[] = []; - for (const diffLine of (await deterministicApplyLazyEdit( + for (const diffLine of (await deterministicApplyLazyEdit({ oldFile, - newFile, + newLazyFile: newFile, filename, - )) ?? []) { + })) ?? []) { ourDiffs.push(diffLine); } diff --git a/core/edit/lazy/deterministic.ts b/core/edit/lazy/deterministic.ts index 1e3a0ec4e..947f628e4 100644 --- a/core/edit/lazy/deterministic.ts +++ b/core/edit/lazy/deterministic.ts @@ -15,6 +15,11 @@ type AstReplacements = Array<{ replacementNodes: Parser.SyntaxNode[]; }>; +const LAZY_COMMENT_REGEX = /\.{3}\s*(.+?)\s*\.{3}/; +export function isLazyText(text: string): boolean { + return LAZY_COMMENT_REGEX.test(text); +} + function reconstructNewFile( oldFile: string, newFile: string, @@ -121,11 +126,23 @@ function nodeSurroundedInLazyBlocks( } // TODO: If we don't have high confidence, return undefined to fall back to slower methods -export async function deterministicApplyLazyEdit( - oldFile: string, - newLazyFile: string, - filename: string, -): Promise { +export async function deterministicApplyLazyEdit({ + oldFile, + newLazyFile, + filename, + /** + * Using this as a flag to slowly reintroduce lazy applies. + * With this set, we will only attempt to deterministically apply + * when there are no lazy blocks and then just replace the whole file, + * and otherwise never use instant apply + */ + onlyFullFileRewrite = false, +}: { + oldFile: string; + newLazyFile: string; + filename: string; + onlyFullFileRewrite?: boolean; +}): Promise { const parser = await getParserForFile(filename); if (!parser) { return undefined; @@ -135,6 +152,14 @@ export async function deterministicApplyLazyEdit( let newTree = parser.parse(newLazyFile); let reconstructedNewFile: string | undefined = undefined; + if (onlyFullFileRewrite) { + if (!isLazyText(newTree.rootNode.text)) { + return myersDiff(oldFile, newLazyFile); + } else { + return undefined; + } + } + // 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)) { @@ -198,11 +223,6 @@ export async function deterministicApplyLazyEdit( return diff; } -const LAZY_COMMENT_REGEX = /\.{3}\s*(.+?)\s*\.{3}/; -export function isLazyLine(text: string): boolean { - return LAZY_COMMENT_REGEX.test(text); -} - function isLazyBlock(node: Parser.SyntaxNode): boolean { // Special case for "{/* ... existing code ... */}" if ( @@ -213,7 +233,7 @@ function isLazyBlock(node: Parser.SyntaxNode): boolean { return true; } - return node.type.includes("comment") && isLazyLine(node.text); + return node.type.includes("comment") && isLazyText(node.text); } function stringsWithinLevDistThreshold( diff --git a/extensions/vscode/src/apply/ApplyManager.ts b/extensions/vscode/src/apply/ApplyManager.ts index 4942cb0c1..2741fea0b 100644 --- a/extensions/vscode/src/apply/ApplyManager.ts +++ b/extensions/vscode/src/apply/ApplyManager.ts @@ -116,17 +116,17 @@ export class ApplyManager { return; } - const [instant, diffLines] = await applyCodeBlock( + const { isInstantApply, diffLinesGenerator } = await applyCodeBlock( editor.document.getText(), text, getUriPathBasename(editor.document.uri.toString()), llm, ); - if (instant) { + if (isInstantApply) { await this.verticalDiffManager.streamDiffLines( - diffLines, - instant, + diffLinesGenerator, + isInstantApply, streamId, toolCallId, ); diff --git a/gui/src/components/mainInput/Lump/BlockSettingsTopToolbar.tsx b/gui/src/components/mainInput/Lump/BlockSettingsTopToolbar.tsx index 4ecf299b7..f7464e793 100644 --- a/gui/src/components/mainInput/Lump/BlockSettingsTopToolbar.tsx +++ b/gui/src/components/mainInput/Lump/BlockSettingsTopToolbar.tsx @@ -116,7 +116,7 @@ export function BlockSettingsTopToolbar() { ); return ( -
+
-
+