refactored quickedit

This commit is contained in:
Nate Sesti 2024-07-04 11:39:41 -07:00
parent 51ac34a3df
commit b226eb8d25
3 changed files with 199 additions and 131 deletions

View file

@ -0,0 +1,186 @@
import { IDE } from "core";
import { ConfigHandler } from "core/config/handler";
import { fetchwithRequestOptions } from "core/util/fetchWithOptions";
import * as vscode from "vscode";
import { VerticalPerLineDiffManager } from "./diff/verticalPerLine/manager";
import { getPlatform } from "./util/util";
import { VsCodeWebviewProtocol } from "./webviewProtocol";
interface QuickEditFlowStuff {
defaultModelTitle: string;
quickPickItems: vscode.QuickPickItem[];
previousInput: string | undefined;
injectedPrompt?: string | undefined;
}
export class QuickEdit {
constructor(
private readonly verticalDiffManager: VerticalPerLineDiffManager,
private readonly configHandler: ConfigHandler,
private readonly webviewProtocol: VsCodeWebviewProtocol,
private readonly ide: IDE,
) {}
private async _getDefaultModelTitle(): Promise<string> {
const config = await this.configHandler.loadConfig();
let defaultModelTitle =
config.experimental?.modelRoles?.inlineEdit ??
(await this.webviewProtocol.request("getDefaultModelTitle", undefined));
if (!defaultModelTitle) {
defaultModelTitle = config.models[0]?.title!;
}
return defaultModelTitle;
}
private async _getQuickPickItems(): Promise<vscode.QuickPickItem[]> {
const contextProviders = (await this.configHandler.loadConfig())
.contextProviders;
const quickPickItems =
contextProviders
?.filter((provider) => provider.description.type === "normal")
.map((provider) => {
return {
label: provider.description.displayTitle,
description: provider.description.title,
detail: provider.description.description,
};
}) || [];
return quickPickItems;
}
private async _getStuff(
injectedPrompt: string | undefined,
): Promise<QuickEditFlowStuff> {
const editor = vscode.window.activeTextEditor;
const existingHandler = this.verticalDiffManager.getHandlerForFile(
editor?.document.uri.fsPath ?? "",
);
const previousInput = existingHandler?.input;
const stuff: QuickEditFlowStuff = {
defaultModelTitle: await this._getDefaultModelTitle(),
quickPickItems: await this._getQuickPickItems(),
previousInput,
injectedPrompt,
};
return stuff;
}
async _getTextInputOptions(stuff: QuickEditFlowStuff) {
const selectionEmpty = vscode.window.activeTextEditor?.selection.isEmpty;
const addContextMsg = stuff.quickPickItems.length
? " (or press enter to add context first)"
: "";
const textInputOptions: vscode.InputBoxOptions = {
placeHolder: selectionEmpty
? `Type instructions to generate code${addContextMsg}`
: `Describe how to edit the highlighted code${addContextMsg}`,
title: `${getPlatform() === "mac" ? "Cmd" : "Ctrl"}+I`,
prompt: `[${stuff.defaultModelTitle}]`,
value: stuff.injectedPrompt,
ignoreFocusOut: true,
};
if (stuff.previousInput) {
textInputOptions.value = stuff.previousInput + ", ";
textInputOptions.valueSelection = [
textInputOptions.value.length,
textInputOptions.value.length,
];
}
return textInputOptions;
}
async _collectTextInput(
stuff: QuickEditFlowStuff,
): Promise<string | undefined> {
return await vscode.window.showInputBox(
await this._getTextInputOptions(stuff),
);
}
private async _runWithContextItems(stuff: QuickEditFlowStuff) {
const selectedProviders = await vscode.window.showQuickPick(
stuff.quickPickItems,
{
title: "Add Context",
canPickMany: true,
},
);
const input = await vscode.window.showInputBox(
await this._getTextInputOptions(stuff),
);
if (input) {
const inputWithContext = await this._addSelectedProvidersToInput(
input,
selectedProviders,
);
await this._sendRequest(inputWithContext, stuff);
}
}
private async _addSelectedProvidersToInput(
input: string,
selectedProviders: vscode.QuickPickItem[] | undefined,
): Promise<string> {
const llm = await this.configHandler.llmFromTitle();
const config = await this.configHandler.loadConfig();
const context = (
await Promise.all(
selectedProviders?.map((providerTitle) => {
const provider = config.contextProviders?.find(
(provider) =>
provider.description.title === providerTitle.description,
);
if (!provider) {
return [];
}
return provider.getContextItems("", {
embeddingsProvider: config.embeddingsProvider,
reranker: config.reranker,
ide: this.ide,
llm,
fullInput: input || "",
selectedCode: [],
fetch: (url, init) =>
fetchwithRequestOptions(url, init, config.requestOptions),
});
}) || [],
)
).flat();
return (
context.map((item) => item.content).join("\n\n") + "\n\n---\n\n" + input
);
}
private async _handleInitialInput(
initialInput: string | undefined,
stuff: QuickEditFlowStuff,
) {
// If the user presses enter, we take them to context items selector
if (initialInput?.length === 0 && stuff.quickPickItems.length > 0) {
await this._runWithContextItems(stuff);
} else if (initialInput) {
// If there's input, then start generating the diff
await this._sendRequest(initialInput, stuff);
}
}
private async _sendRequest(input: string, stuff: QuickEditFlowStuff) {
this.webviewProtocol.request("incrementFtc", undefined);
await this.verticalDiffManager.streamEdit(
input,
stuff.defaultModelTitle,
undefined,
stuff.previousInput,
);
}
async run(injectedPrompt: string | undefined) {
const stuff = await this._getStuff(injectedPrompt);
const initialInput = await this._collectTextInput(stuff);
await this._handleInitialInput(initialInput, stuff);
}
}

View file

@ -7,7 +7,6 @@ import { ContextMenuConfig, IDE } from "core";
import { CompletionProvider } from "core/autocomplete/completionProvider";
import { ConfigHandler } from "core/config/handler";
import { ContinueServerClient } from "core/continueServer/stubs/client";
import { fetchwithRequestOptions } from "core/util/fetchWithOptions";
import { GlobalContext } from "core/util/GlobalContext";
import { getConfigJsonPath, getDevDataFilePath } from "core/util/paths";
import { Telemetry } from "core/util/posthog";
@ -22,8 +21,8 @@ import {
import { ContinueGUIWebviewViewProvider } from "./debugPanel";
import { DiffManager } from "./diff/horizontal";
import { VerticalPerLineDiffManager } from "./diff/verticalPerLine/manager";
import { QuickEdit } from "./QuickEdit";
import { Battery } from "./util/battery";
import { getPlatform } from "./util/util";
import type { VsCodeWebviewProtocol } from "./webviewProtocol";
let fullScreenPanel: vscode.WebviewPanel | undefined;
@ -188,6 +187,14 @@ const commandsMap: (
onlyOneInsertion,
);
}
const quickEdit = new QuickEdit(
verticalDiffManager,
configHandler,
sidebar.webviewProtocol,
ide,
);
return {
"continue.acceptDiff": async (newFilepath?: string | vscode.Uri) => {
if (newFilepath instanceof vscode.Uri) {
@ -249,123 +256,8 @@ const commandsMap: (
"continue.toggleAuxiliaryBar": () => {
vscode.commands.executeCommand("workbench.action.toggleAuxiliaryBar");
},
"continue.quickEdit": async (prompt?: string) => {
const selectionEmpty = vscode.window.activeTextEditor?.selection.isEmpty;
const editor = vscode.window.activeTextEditor;
const existingHandler = verticalDiffManager.getHandlerForFile(
editor?.document.uri.fsPath ?? "",
);
const previousInput = existingHandler?.input;
const config = await configHandler.loadConfig();
let defaultModelTitle =
config.experimental?.modelRoles?.inlineEdit ??
(await sidebar.webviewProtocol.request(
"getDefaultModelTitle",
undefined,
));
if (!defaultModelTitle) {
defaultModelTitle = config.models[0]?.title!;
}
const quickPickItems =
config.contextProviders
?.filter((provider) => provider.description.type === "normal")
.map((provider) => {
return {
label: provider.description.displayTitle,
description: provider.description.title,
detail: provider.description.description,
};
}) || [];
const addContextMsg = quickPickItems.length
? " (or press enter to add context first)"
: "";
const textInputOptions: vscode.InputBoxOptions = {
placeHolder: selectionEmpty
? `Type instructions to generate code${addContextMsg}`
: `Describe how to edit the highlighted code${addContextMsg}`,
title: `${getPlatform() === "mac" ? "Cmd" : "Ctrl"}+I`,
prompt: `[${defaultModelTitle}]`,
value: prompt,
ignoreFocusOut: true,
};
if (previousInput) {
textInputOptions.value = previousInput + ", ";
textInputOptions.valueSelection = [
textInputOptions.value.length,
textInputOptions.value.length,
];
}
let text = await vscode.window.showInputBox(textInputOptions);
if (text === undefined) {
return;
}
if (text.length > 0 || quickPickItems.length === 0) {
sidebar.webviewProtocol.request("incrementFtc", undefined);
await verticalDiffManager.streamEdit(
text,
defaultModelTitle,
undefined,
previousInput,
);
} else {
// Pick context first
const selectedProviders = await vscode.window.showQuickPick(
quickPickItems,
{
title: "Add Context",
canPickMany: true,
},
);
let text = await vscode.window.showInputBox(textInputOptions);
if (text) {
const llm = await configHandler.llmFromTitle();
const config = await configHandler.loadConfig();
const context = (
await Promise.all(
selectedProviders?.map((providerTitle) => {
const provider = config.contextProviders?.find(
(provider) =>
provider.description.title === providerTitle.description,
);
if (!provider) {
return [];
}
return provider.getContextItems("", {
embeddingsProvider: config.embeddingsProvider,
reranker: config.reranker,
ide,
llm,
fullInput: text || "",
selectedCode: [],
fetch: (url, init) =>
fetchwithRequestOptions(url, init, config.requestOptions),
});
}) || [],
)
).flat();
text =
context.map((item) => item.content).join("\n\n") +
"\n\n---\n\n" +
text;
sidebar.webviewProtocol.request("incrementFtc", undefined);
await verticalDiffManager.streamEdit(
text,
defaultModelTitle,
undefined,
previousInput,
);
}
}
"continue.quickEdit": (injectedPrompt?: string) => {
quickEdit.run(injectedPrompt);
},
"continue.writeCommentsForCode": async () => {
streamInlineEdit(
@ -533,17 +425,6 @@ const commandsMap: (
addEntireFileToContext(uri, false, sidebar.webviewProtocol);
}
},
"continue.updateAllReferences": (filepath: vscode.Uri) => {
// Get the cursor position in the editor
const editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
const position = editor.selection.active;
sidebar.sendMainUserInput(
`/references ${filepath.fsPath} ${position.line} ${position.character}`,
);
},
"continue.logAutocompleteOutcome": (
completionId: string,
completionProvider: CompletionProvider,
@ -678,6 +559,8 @@ const commandsMap: (
client.sendFeedback(feedback, lastLines);
}
},
"continue.quickEditHistoryUp": async () => {},
"continue.quickEditHistoryDown": async () => {},
};
};

View file

@ -196,7 +196,6 @@ export class VerticalPerLineDiffManager {
const existingHandler = this.getHandlerForFile(filepath);
if (existingHandler) {
let existingStartLine = existingHandler?.range.start.line;
if (quickEdit) {
// Previous diff was a quickEdit
// Check if user has highlighted a range