continue-custom/extensions/vscode/src/commands.ts
2024-09-26 20:07:25 -07:00

741 lines
23 KiB
TypeScript

/* eslint-disable @typescript-eslint/naming-convention */
import { ContextMenuConfig, IDE } from "core";
import { CompletionProvider } from "core/autocomplete/completionProvider";
import { ConfigHandler } from "core/config/ConfigHandler";
import { getModelByRole } from "core/config/util";
import { ContinueServerClient } from "core/continueServer/stubs/client";
import { Core } from "core/core";
import { walkDirAsync } from "core/indexing/walkDir";
import { GlobalContext } from "core/util/GlobalContext";
import { getConfigJsonPath, getDevDataFilePath } from "core/util/paths";
import { Telemetry } from "core/util/posthog";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import readLastLines from "read-last-lines";
import * as vscode from "vscode";
import {
StatusBarStatus,
getAutocompleteStatusBarDescription,
getAutocompleteStatusBarTitle,
getStatusBarStatus,
getStatusBarStatusFromQuickPickItemLabel,
quickPickStatusText,
setupStatusBar,
} from "./autocomplete/statusBar";
import { ContinueGUIWebviewViewProvider } from "./ContinueGUIWebviewViewProvider";
import { DiffManager } from "./diff/horizontal";
import { VerticalDiffManager } from "./diff/vertical/manager";
import { QuickEdit, QuickEditShowParams } from "./quickEdit/QuickEditQuickPick";
import { Battery } from "./util/battery";
import { EXTENSION_NAME } from "./util/constants";
import { getFullyQualifiedPath } from "./util/util";
import { uriFromFilePath } from "./util/vscode";
import type { VsCodeWebviewProtocol } from "./webviewProtocol";
let fullScreenPanel: vscode.WebviewPanel | undefined;
function getFullScreenTab() {
const tabs = vscode.window.tabGroups.all.flatMap((tabGroup) => tabGroup.tabs);
return tabs.find((tab) =>
(tab.input as any)?.viewType?.endsWith("continue.continueGUIView"),
);
}
type TelemetryCaptureParams = Parameters<typeof Telemetry.capture>;
/**
* Helper method to add the `isCommandEvent` to all telemetry captures
*/
function captureCommandTelemetry(
commandName: TelemetryCaptureParams[0],
properties: TelemetryCaptureParams[1] = {},
) {
Telemetry.capture(commandName, { isCommandEvent: true, ...properties });
}
function addCodeToContextFromRange(
range: vscode.Range,
webviewProtocol: VsCodeWebviewProtocol,
prompt?: string,
) {
const document = vscode.window.activeTextEditor?.document;
if (!document) {
return;
}
const rangeInFileWithContents = {
filepath: document.uri.fsPath,
contents: document.getText(range),
range: {
start: {
line: range.start.line,
character: range.start.character,
},
end: {
line: range.end.line,
character: range.end.character,
},
},
};
webviewProtocol?.request("highlightedCode", {
rangeInFileWithContents,
prompt,
// Assume `true` since range selection is currently only used for quick actions/fixes
shouldRun: true,
});
}
async function addHighlightedCodeToContext(
webviewProtocol: VsCodeWebviewProtocol | undefined,
) {
const editor = vscode.window.activeTextEditor;
if (editor) {
const selection = editor.selection;
if (selection.isEmpty) {
return;
}
// adjust starting position to include indentation
const start = new vscode.Position(selection.start.line, 0);
const range = new vscode.Range(start, selection.end);
const contents = editor.document.getText(range);
const rangeInFileWithContents = {
filepath: editor.document.uri.fsPath,
contents,
range: {
start: {
line: selection.start.line,
character: selection.start.character,
},
end: {
line: selection.end.line,
character: selection.end.character,
},
},
};
webviewProtocol?.request("highlightedCode", {
rangeInFileWithContents,
});
}
}
async function addEntireFileToContext(
filepath: vscode.Uri,
edit: boolean,
webviewProtocol: VsCodeWebviewProtocol | undefined,
) {
// If a directory, add all files in the directory
const stat = await vscode.workspace.fs.stat(filepath);
if (stat.type === vscode.FileType.Directory) {
const files = await vscode.workspace.fs.readDirectory(filepath);
for (const [filename, type] of files) {
if (type === vscode.FileType.File) {
addEntireFileToContext(
vscode.Uri.joinPath(filepath, filename),
edit,
webviewProtocol,
);
}
}
return;
}
// Get the contents of the file
const contents = (await vscode.workspace.fs.readFile(filepath)).toString();
const rangeInFileWithContents = {
filepath: filepath.fsPath,
contents: contents,
range: {
start: {
line: 0,
character: 0,
},
end: {
line: contents.split(os.EOL).length - 1,
character: 0,
},
},
};
webviewProtocol?.request("highlightedCode", {
rangeInFileWithContents,
});
}
// Copy everything over from extension.ts
const commandsMap: (
ide: IDE,
extensionContext: vscode.ExtensionContext,
sidebar: ContinueGUIWebviewViewProvider,
configHandler: ConfigHandler,
diffManager: DiffManager,
verticalDiffManager: VerticalDiffManager,
continueServerClientPromise: Promise<ContinueServerClient>,
battery: Battery,
quickEdit: QuickEdit,
core: Core,
) => { [command: string]: (...args: any) => any } = (
ide,
extensionContext,
sidebar,
configHandler,
diffManager,
verticalDiffManager,
continueServerClientPromise,
battery,
quickEdit,
core,
) => {
/**
* Streams an inline edit to the vertical diff manager.
*
* This function retrieves the configuration, determines the appropriate model title,
* increments the FTC count, and then streams an edit to the
* vertical diff manager.
*
* @param promptName - The key for the prompt in the context menu configuration.
* @param fallbackPrompt - The prompt to use if the configured prompt is not available.
* @param [onlyOneInsertion] - Optional. If true, only one insertion will be made.
* @param [range] - Optional. The range to edit if provided.
* @returns
*/
async function streamInlineEdit(
promptName: keyof ContextMenuConfig,
fallbackPrompt: string,
onlyOneInsertion?: boolean,
range?: vscode.Range,
) {
const config = await configHandler.loadConfig();
const defaultModelTitle = await sidebar.webviewProtocol.request(
"getDefaultModelTitle",
undefined,
);
const modelTitle =
getModelByRole(config, "inlineEdit")?.title ?? defaultModelTitle;
sidebar.webviewProtocol.request("incrementFtc", undefined);
await verticalDiffManager.streamEdit(
config.experimental?.contextMenuPrompts?.[promptName] ?? fallbackPrompt,
modelTitle,
undefined,
onlyOneInsertion,
undefined,
range,
);
}
return {
"continue.acceptDiff": async (newFilepath?: string | vscode.Uri) => {
captureCommandTelemetry("acceptDiff");
let fullPath = newFilepath;
if (fullPath instanceof vscode.Uri) {
fullPath = fullPath.fsPath;
} else {
fullPath = getFullyQualifiedPath(fullPath);
}
verticalDiffManager.clearForFilepath(fullPath, true);
await diffManager.acceptDiff(fullPath);
},
"continue.rejectDiff": async (newFilepath?: string | vscode.Uri) => {
captureCommandTelemetry("rejectDiff");
let fullPath = newFilepath;
if (fullPath instanceof vscode.Uri) {
fullPath = fullPath.fsPath;
} else {
fullPath = getFullyQualifiedPath(fullPath);
}
verticalDiffManager.clearForFilepath(fullPath, false);
await diffManager.rejectDiff(fullPath);
},
"continue.acceptVerticalDiffBlock": (filepath?: string, index?: number) => {
captureCommandTelemetry("acceptVerticalDiffBlock");
verticalDiffManager.acceptRejectVerticalDiffBlock(true, filepath, index);
},
"continue.rejectVerticalDiffBlock": (filepath?: string, index?: number) => {
captureCommandTelemetry("rejectVerticalDiffBlock");
verticalDiffManager.acceptRejectVerticalDiffBlock(false, filepath, index);
},
"continue.quickFix": async (
range: vscode.Range,
diagnosticMessage: string,
) => {
captureCommandTelemetry("quickFix");
const prompt = `How do I fix the following problem in the above code?: ${diagnosticMessage}`;
addCodeToContextFromRange(range, sidebar.webviewProtocol, prompt);
vscode.commands.executeCommand("continue.continueGUIView.focus");
},
// Passthrough for telemetry purposes
"continue.defaultQuickAction": async (args: QuickEditShowParams) => {
captureCommandTelemetry("defaultQuickAction");
vscode.commands.executeCommand("continue.quickEdit", args);
},
"continue.customQuickActionSendToChat": async (
prompt: string,
range: vscode.Range,
) => {
captureCommandTelemetry("customQuickActionSendToChat");
addCodeToContextFromRange(range, sidebar.webviewProtocol, prompt);
vscode.commands.executeCommand("continue.continueGUIView.focus");
},
"continue.customQuickActionStreamInlineEdit": async (
prompt: string,
range: vscode.Range,
) => {
captureCommandTelemetry("customQuickActionStreamInlineEdit");
streamInlineEdit("docstring", prompt, false, range);
},
"continue.codebaseForceReIndex": async () => {
core.invoke("index/forceReIndex", undefined);
},
"continue.rebuildCodebaseIndex": async () => {
core.invoke("index/forceReIndex", { shouldClearIndexes: true });
},
"continue.docsIndex": async () => {
core.invoke("context/indexDocs", { reIndex: false });
},
"continue.docsReIndex": async () => {
core.invoke("context/indexDocs", { reIndex: true });
},
"continue.focusContinueInput": async () => {
const fullScreenTab = getFullScreenTab();
if (!fullScreenTab) {
// focus sidebar
vscode.commands.executeCommand("continue.continueGUIView.focus");
} else {
// focus fullscreen
fullScreenPanel?.reveal();
}
sidebar.webviewProtocol?.request("focusContinueInput", undefined);
await addHighlightedCodeToContext(sidebar.webviewProtocol);
},
"continue.focusContinueInputWithoutClear": async () => {
const fullScreenTab = getFullScreenTab();
const isContinueInputFocused = await sidebar.webviewProtocol.request(
"isContinueInputFocused",
undefined,
);
if (isContinueInputFocused) {
// Handle closing the GUI only if we are focused on the input
if (fullScreenTab) {
fullScreenPanel?.dispose();
}
} else {
// Handle opening the GUI otherwise
if (!fullScreenTab) {
// focus sidebar
vscode.commands.executeCommand("continue.continueGUIView.focus");
} else {
// focus fullscreen
fullScreenPanel?.reveal();
}
sidebar.webviewProtocol?.request(
"focusContinueInputWithoutClear",
undefined,
);
await addHighlightedCodeToContext(sidebar.webviewProtocol);
}
},
"continue.quickEdit": async (args: QuickEditShowParams) => {
captureCommandTelemetry("quickEdit");
quickEdit.show(args);
},
"continue.writeCommentsForCode": async () => {
captureCommandTelemetry("writeCommentsForCode");
streamInlineEdit(
"comment",
"Write comments for this code. Do not change anything about the code itself.",
);
},
"continue.writeDocstringForCode": async () => {
captureCommandTelemetry("writeDocstringForCode");
streamInlineEdit(
"docstring",
"Write a docstring for this code. Do not change anything about the code itself.",
true,
);
},
"continue.fixCode": async () => {
captureCommandTelemetry("fixCode");
streamInlineEdit(
"fix",
"Fix this code. If it is already 100% correct, simply rewrite the code.",
);
},
"continue.optimizeCode": async () => {
captureCommandTelemetry("optimizeCode");
streamInlineEdit("optimize", "Optimize this code");
},
"continue.fixGrammar": async () => {
captureCommandTelemetry("fixGrammar");
streamInlineEdit(
"fixGrammar",
"If there are any grammar or spelling mistakes in this writing, fix them. Do not make other large changes to the writing.",
);
},
"continue.viewLogs": async () => {
captureCommandTelemetry("viewLogs");
// Open ~/.continue/continue.log
const logFile = path.join(os.homedir(), ".continue", "continue.log");
// Make sure the file/directory exist
if (!fs.existsSync(logFile)) {
fs.mkdirSync(path.dirname(logFile), { recursive: true });
fs.writeFileSync(logFile, "");
}
const uri = vscode.Uri.file(logFile);
await vscode.window.showTextDocument(uri);
},
"continue.debugTerminal": async () => {
captureCommandTelemetry("debugTerminal");
const terminalContents = await ide.getTerminalContents();
vscode.commands.executeCommand("continue.continueGUIView.focus");
sidebar.webviewProtocol?.request("userInput", {
input: `I got the following error, can you please help explain how to fix it?\n\n${terminalContents.trim()}`,
});
},
"continue.hideInlineTip": () => {
vscode.workspace
.getConfiguration(EXTENSION_NAME)
.update("showInlineTip", false, vscode.ConfigurationTarget.Global);
},
// Commands without keyboard shortcuts
"continue.addModel": () => {
captureCommandTelemetry("addModel");
vscode.commands.executeCommand("continue.continueGUIView.focus");
sidebar.webviewProtocol?.request("addModel", undefined);
},
"continue.openSettingsUI": () => {
vscode.commands.executeCommand("continue.continueGUIView.focus");
sidebar.webviewProtocol?.request("openSettings", undefined);
},
"continue.sendMainUserInput": (text: string) => {
sidebar.webviewProtocol?.request("userInput", {
input: text,
});
},
"continue.selectRange": (startLine: number, endLine: number) => {
if (!vscode.window.activeTextEditor) {
return;
}
vscode.window.activeTextEditor.selection = new vscode.Selection(
startLine,
0,
endLine,
0,
);
},
"continue.foldAndUnfold": (
foldSelectionLines: number[],
unfoldSelectionLines: number[],
) => {
vscode.commands.executeCommand("editor.unfold", {
selectionLines: unfoldSelectionLines,
});
vscode.commands.executeCommand("editor.fold", {
selectionLines: foldSelectionLines,
});
},
"continue.sendToTerminal": (text: string) => {
captureCommandTelemetry("sendToTerminal");
ide.runCommand(text);
},
"continue.newSession": () => {
sidebar.webviewProtocol?.request("newSession", undefined);
},
"continue.viewHistory": () => {
sidebar.webviewProtocol?.request("viewHistory", undefined);
},
"continue.applyCodeFromChat": () => {
sidebar.webviewProtocol.request("applyCodeFromChat", undefined);
},
"continue.toggleFullScreen": () => {
// Check if full screen is already open by checking open tabs
const fullScreenTab = getFullScreenTab();
// Check if the active editor is the Continue GUI View
if (fullScreenTab && fullScreenTab.isActive) {
//Full screen open and focused - close it
vscode.commands.executeCommand("workbench.action.closeActiveEditor"); //this will trigger the onDidDispose listener below
return;
}
if (fullScreenTab && fullScreenPanel) {
//Full screen open, but not focused - focus it
fullScreenPanel.reveal();
return;
}
//Full screen not open - open it
captureCommandTelemetry("openFullScreen");
//create the full screen panel
let panel = vscode.window.createWebviewPanel(
"continue.continueGUIView",
"Continue",
vscode.ViewColumn.One,
{
retainContextWhenHidden: true,
},
);
fullScreenPanel = panel;
//Add content to the panel
panel.webview.html = sidebar.getSidebarContent(
extensionContext,
panel,
undefined,
undefined,
true,
);
//When panel closes, reset the webview and focus
panel.onDidDispose(
() => {
sidebar.resetWebviewProtocolWebview();
vscode.commands.executeCommand("continue.focusContinueInput");
},
null,
extensionContext.subscriptions,
);
},
"continue.openConfigJson": () => {
ide.openFile(getConfigJsonPath());
},
"continue.selectFilesAsContext": async (
firstUri: vscode.Uri,
uris: vscode.Uri[],
) => {
if (uris === undefined) {
throw new Error("No files were selected");
}
vscode.commands.executeCommand("continue.continueGUIView.focus");
for (const uri of uris) {
// If it's a folder, add the entire folder contents recursively by using walkDir (to ignore ignored files)
const isDirectory = await vscode.workspace.fs
.stat(uri)
?.then((stat) => stat.type === vscode.FileType.Directory);
if (isDirectory) {
for await (const filepath of walkDirAsync(uri.fsPath, ide)) {
addEntireFileToContext(
uriFromFilePath(filepath),
false,
sidebar.webviewProtocol,
);
}
} else {
addEntireFileToContext(uri, false, sidebar.webviewProtocol);
}
}
},
"continue.logAutocompleteOutcome": (
completionId: string,
completionProvider: CompletionProvider,
) => {
completionProvider.accept(completionId);
},
"continue.toggleTabAutocompleteEnabled": () => {
captureCommandTelemetry("toggleTabAutocompleteEnabled");
const config = vscode.workspace.getConfiguration(EXTENSION_NAME);
const enabled = config.get("enableTabAutocomplete");
const pauseOnBattery = config.get<boolean>(
"pauseTabAutocompleteOnBattery",
);
if (!pauseOnBattery || battery.isACConnected()) {
config.update(
"enableTabAutocomplete",
!enabled,
vscode.ConfigurationTarget.Global,
);
} else {
if (enabled) {
const paused = getStatusBarStatus() === StatusBarStatus.Paused;
if (paused) {
setupStatusBar(StatusBarStatus.Enabled);
} else {
config.update(
"enableTabAutocomplete",
false,
vscode.ConfigurationTarget.Global,
);
}
} else {
setupStatusBar(StatusBarStatus.Paused);
config.update(
"enableTabAutocomplete",
true,
vscode.ConfigurationTarget.Global,
);
}
}
},
"continue.openTabAutocompleteConfigMenu": async () => {
captureCommandTelemetry("openTabAutocompleteConfigMenu");
const config = vscode.workspace.getConfiguration(EXTENSION_NAME);
const quickPick = vscode.window.createQuickPick();
const autocompleteModels =
(await configHandler.loadConfig())?.tabAutocompleteModels ?? [];
let selected = new GlobalContext().get("selectedTabAutocompleteModel");
if (
!selected ||
!autocompleteModels.some((model) => model.title === selected)
) {
selected = autocompleteModels[0].title;
}
// Toggle between Disabled, Paused, and Enabled
const pauseOnBattery =
config.get<boolean>("pauseTabAutocompleteOnBattery") &&
!battery.isACConnected();
const currentStatus = getStatusBarStatus();
let targetStatus: StatusBarStatus | undefined;
if (pauseOnBattery) {
// Cycle from Disabled -> Paused -> Enabled
targetStatus =
currentStatus === StatusBarStatus.Paused
? StatusBarStatus.Enabled
: currentStatus === StatusBarStatus.Disabled
? StatusBarStatus.Paused
: StatusBarStatus.Disabled;
} else {
// Toggle between Disabled and Enabled
targetStatus =
currentStatus === StatusBarStatus.Disabled
? StatusBarStatus.Enabled
: StatusBarStatus.Disabled;
}
quickPick.items = [
{
label: quickPickStatusText(targetStatus),
},
{
label: "$(gear) Configure autocomplete options",
},
{
label: "$(feedback) Give feedback",
},
{
kind: vscode.QuickPickItemKind.Separator,
label: "Switch model",
},
...autocompleteModels.map((model) => ({
label: getAutocompleteStatusBarTitle(selected, model),
description: getAutocompleteStatusBarDescription(selected, model),
})),
];
quickPick.onDidAccept(() => {
const selectedOption = quickPick.selectedItems[0].label;
const targetStatus =
getStatusBarStatusFromQuickPickItemLabel(selectedOption);
if (targetStatus !== undefined) {
setupStatusBar(targetStatus);
config.update(
"enableTabAutocomplete",
targetStatus === StatusBarStatus.Enabled,
vscode.ConfigurationTarget.Global,
);
} else if (
selectedOption === "$(gear) Configure autocomplete options"
) {
ide.openFile(getConfigJsonPath());
} else if (
autocompleteModels.some((model) => model.title === selectedOption)
) {
new GlobalContext().update(
"selectedTabAutocompleteModel",
selectedOption,
);
configHandler.reloadConfig();
} else if (selectedOption === "$(feedback) Give feedback") {
vscode.commands.executeCommand("continue.giveAutocompleteFeedback");
}
quickPick.dispose();
});
quickPick.show();
},
"continue.giveAutocompleteFeedback": async () => {
const feedback = await vscode.window.showInputBox({
ignoreFocusOut: true,
prompt:
"Please share what went wrong with the last completion. The details of the completion as well as this message will be sent to the Continue team in order to improve.",
});
if (feedback) {
const client = await continueServerClientPromise;
const completionsPath = getDevDataFilePath("autocomplete");
const lastLines = await readLastLines.read(completionsPath, 2);
client.sendFeedback(feedback, lastLines);
}
},
};
};
export function registerAllCommands(
context: vscode.ExtensionContext,
ide: IDE,
extensionContext: vscode.ExtensionContext,
sidebar: ContinueGUIWebviewViewProvider,
configHandler: ConfigHandler,
diffManager: DiffManager,
verticalDiffManager: VerticalDiffManager,
continueServerClientPromise: Promise<ContinueServerClient>,
battery: Battery,
quickEdit: QuickEdit,
core: Core,
) {
for (const [command, callback] of Object.entries(
commandsMap(
ide,
extensionContext,
sidebar,
configHandler,
diffManager,
verticalDiffManager,
continueServerClientPromise,
battery,
quickEdit,
core,
),
)) {
context.subscriptions.push(
vscode.commands.registerCommand(command, callback),
);
}
}