Merge pull request #5156 from continuedev/pe/instant-apply-reintroduction

feat: reintroduce lazy apply for full files
This commit is contained in:
Patrick Erichsen 2025-04-14 12:42:12 -07:00 • committed by GitHub
commit 6f0eebffae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 70 additions and 41 deletions

View file

@ -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<DiffLine>]> {
// This was buggy, removed for now, maybe forever
// if (canUseInstantApply(filename)) {
// const diffLines = await deterministicApplyLazyEdit(
// oldFile,
// newFile,
// filename,
// );
): Promise<{
isInstantApply: boolean;
diffLinesGenerator: AsyncGenerator<DiffLine>;
}> {
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),
};
}

View file

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

View file

@ -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<DiffLine[] | undefined> {
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<DiffLine[] | undefined> {
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(

View file

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

View file

@ -116,7 +116,7 @@ export function BlockSettingsTopToolbar() {
);
return (
<div className="flex w-full items-center justify-between">
<div className="flex w-full items-center justify-between gap-4">
<div className="flex flex-row">
<div className="xs:flex hidden items-center justify-center text-gray-400">
<BlockSettingsToolbarIcon
@ -150,7 +150,7 @@ export function BlockSettingsTopToolbar() {
</div>
</div>
</div>
<div className="flex gap-0.5">
<div className="flex">
<AssistantSelect />
</div>
</div>