Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1499,7 +1499,7 @@ export interface ShowFilePayload {

export interface ApplyToFilePayload {
streamId: string;
filepath?: string;
filepath: string;
text: string;
toolCallId?: string;
isSearchAndReplace?: boolean;
Expand Down
40 changes: 16 additions & 24 deletions extensions/vscode/src/apply/ApplyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { streamDiffLines } from "core/edit/streamDiffLines";
import { pruneLinesFromBottom, pruneLinesFromTop } from "core/llm/countTokens";
import { getMarkdownLanguageTagForFile } from "core/util";
import { VerticalDiffManager } from "../diff/vertical/manager";
import { openEditorAndRevealRange } from "../util/vscode";
import { VsCodeIde } from "../VsCodeIde";
import { VsCodeWebviewProtocol } from "../webviewProtocol";

Expand All @@ -32,18 +33,14 @@ export class ApplyManager {
toolCallId,
isSearchAndReplace,
}: ApplyToFilePayload) {
if (filepath) {
await this.ensureFileOpen(filepath);
}

const { activeTextEditor } = vscode.window;
if (!activeTextEditor) {
void vscode.window.showErrorMessage("No active editor to apply edits to");
const editor = await this.getOrCreateEditor(filepath);
if (!editor) {
void vscode.window.showErrorMessage("Failed to open editor for file");
return;
}

// Capture the original file content before applying changes
const originalFileContent = activeTextEditor.document.getText();
const originalFileContent = editor.document.getText();

await this.webviewProtocol.request("updateApplyState", {
streamId,
Expand All @@ -53,42 +50,37 @@ export class ApplyManager {
toolCallId,
});

const hasExistingDocument = !!activeTextEditor.document.getText().trim();
const hasExistingDocument = !!editor.document.getText().trim();
if (hasExistingDocument) {
// Currently `isSearchAndReplace` will always provide a full file rewrite
// as the contents of `text`, so we can just instantly apply
if (isSearchAndReplace) {
await this.verticalDiffManager.instantApplyDiff(
filepath,
originalFileContent,
text,
streamId,
toolCallId,
);
} else {
await this.handleExistingDocument(
activeTextEditor,
text,
streamId,
toolCallId,
);
await this.handleExistingDocument(editor, text, streamId, toolCallId);
}
} else {
await this.handleEmptyDocument(
activeTextEditor,
text,
streamId,
toolCallId,
);
await this.handleEmptyDocument(editor, text, streamId, toolCallId);
}
}

private async ensureFileOpen(filepath: string): Promise<void> {
private async getOrCreateEditor(
filepath: string,
): Promise<vscode.TextEditor | undefined> {
const fileExists = await this.ide.fileExists(filepath);
if (!fileExists) {
await this.ide.writeFile(filepath, "");
await this.ide.openFile(filepath);
}
await this.ide.openFile(filepath);
const uri = filepath.startsWith("file://")
? vscode.Uri.parse(filepath)
: vscode.Uri.file(filepath);
return openEditorAndRevealRange(uri);
}

private modelIsTooFastForStreaming(model: string): boolean {
Expand Down
15 changes: 8 additions & 7 deletions extensions/vscode/src/diff/vertical/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { EDIT_MODE_STREAM_ID } from "core/edit/constants";
import { stripImages } from "core/util/messageContent";
import { getLastNPathParts } from "core/util/uri";
import { editOutcomeTracker } from "../../extension/EditOutcomeTracker";
import { openEditorAndRevealRange } from "../../util/vscode";
import { VerticalDiffHandler, VerticalDiffHandlerOptions } from "./handler";
import { getFirstChangedLine } from "./util";

Expand Down Expand Up @@ -296,24 +297,24 @@ export class VerticalDiffManager {
}

async instantApplyDiff(
filepath: string,
oldContent: string,
newContent: string,
streamId: string,
toolCallId?: string,
) {
vscode.commands.executeCommand("setContext", "continue.diffVisible", true);

const editor = vscode.window.activeTextEditor;
const uri = vscode.Uri.parse(filepath);
const editor = await openEditorAndRevealRange(uri);
if (!editor) {
return;
}

const fileUri = editor.document.uri.toString();

const myersDiffs = myersDiff(oldContent, newContent);

const diffHandler = this.createVerticalDiffHandler(
fileUri,
filepath,
0,
editor.document.lineCount - 1,
{
Expand All @@ -324,7 +325,7 @@ export class VerticalDiffManager {
status,
numDiffs,
fileContent,
filepath: fileUri,
filepath,
toolCallId,
}),
streamId,
Expand All @@ -347,9 +348,9 @@ export class VerticalDiffManager {
await this.webviewProtocol.request("updateApplyState", {
streamId,
status: "done",
numDiffs: this.fileUriToCodeLens.get(fileUri)?.length ?? 0,
numDiffs: this.fileUriToCodeLens.get(filepath)?.length ?? 0,
fileContent: editor.document.getText(),
filepath: fileUri,
filepath,
toolCallId,
});
}
Expand Down
Loading