quick input box with history

This commit is contained in:
Nate Sesti 2024-07-04 14:03:11 -07:00
parent b226eb8d25
commit d5466532e1
5 changed files with 234 additions and 12 deletions

View file

@ -1,12 +1,12 @@
{
"name": "continue",
"version": "0.9.171",
"version": "0.9.172",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "continue",
"version": "0.9.171",
"version": "0.9.172",
"license": "Apache-2.0",
"dependencies": {
"@electron/rebuild": "^3.2.10",

View file

@ -230,6 +230,18 @@
"category": "Continue",
"title": "Fix Grammar / Spelling",
"group": "Continue"
},
{
"command": "continue.quickEditHistoryUp",
"category": "Continue",
"title": "Quick Edit History Up",
"group": "Continue"
},
{
"command": "continue.quickEditHistoryDown",
"category": "Continue",
"title": "Quick Edit History Down",
"group": "Continue"
}
],
"keybindings": [
@ -259,6 +271,18 @@
"key": "ctrl+z",
"when": "continue.diffVisible"
},
{
"command": "continue.quickEditHistoryUp",
"mac": "up",
"key": "up",
"when": "continue.quickEditFocused"
},
{
"command": "continue.quickEditHistoryDown",
"mac": "down",
"key": "down",
"when": "continue.quickEditFocused"
},
{
"command": "continue.acceptVerticalDiffBlock",
"mac": "alt+cmd+y",

View file

@ -21,7 +21,7 @@ import {
import { ContinueGUIWebviewViewProvider } from "./debugPanel";
import { DiffManager } from "./diff/horizontal";
import { VerticalPerLineDiffManager } from "./diff/verticalPerLine/manager";
import { QuickEdit } from "./QuickEdit";
import { QuickEdit } from "./quickEdit/QuickEdit";
import { Battery } from "./util/battery";
import type { VsCodeWebviewProtocol } from "./webviewProtocol";
@ -188,11 +188,16 @@ const commandsMap: (
);
}
const historyUpEventEmitter = new vscode.EventEmitter<void>();
const historyDownEventEmitter = new vscode.EventEmitter<void>();
const quickEdit = new QuickEdit(
verticalDiffManager,
configHandler,
sidebar.webviewProtocol,
ide,
extensionContext,
historyUpEventEmitter.event,
historyDownEventEmitter.event,
);
return {
@ -559,8 +564,12 @@ const commandsMap: (
client.sendFeedback(feedback, lastLines);
}
},
"continue.quickEditHistoryUp": async () => {},
"continue.quickEditHistoryDown": async () => {},
"continue.quickEditHistoryUp": async () => {
historyUpEventEmitter.fire();
},
"continue.quickEditHistoryDown": async () => {
historyDownEventEmitter.fire();
},
};
};

View file

@ -0,0 +1,181 @@
import * as vscode from "vscode";
// Because we have to hide and show the input box again to allow history, this class can act like a more contiguous input box.
export default class InputBoxWithHistory implements vscode.Disposable {
private static MaxSize = 50;
private static HistoryKey = "quickEditHistory";
private currentIndex: number;
private history: string[] = [];
private cancellationReason: "up" | "down" | undefined = undefined;
private readonly completionEventEmitter = new vscode.EventEmitter<
string | undefined
>();
private currentInputBoxCancellationTokenSource:
| vscode.CancellationTokenSource
| undefined = undefined;
private disposables: vscode.Disposable[] = [];
constructor(
private readonly context: vscode.ExtensionContext,
private readonly historyUpEvent: vscode.Event<void>,
private readonly historyDownEvent: vscode.Event<void>,
private readonly options: vscode.InputBoxOptions,
) {
this.history = this.context.globalState.get(
InputBoxWithHistory.HistoryKey,
[],
);
this.currentIndex = this.history.length;
this.disposables.push(
this.historyUpEvent(() => {
const value = this.up();
this.cancellationReason = "up";
if (typeof value === "undefined") {
return;
}
this._displayInputBox(value);
}),
);
this.disposables.push(
this.historyDownEvent(() => {
const value = this.down();
this.cancellationReason = "down";
if (typeof value === "undefined") {
return;
}
this._displayInputBox(value);
}),
);
}
dispose() {
if (this.currentInputBoxCancellationTokenSource) {
this.currentInputBoxCancellationTokenSource.dispose();
}
this.completionEventEmitter.dispose();
this.disposables.forEach((disposable) => disposable.dispose());
vscode.commands.executeCommand(
"setContext",
"continue.quickEditHistoryFocused",
false,
);
}
appendToHistory(item: string) {
let history = this.history;
// Remove duplicate if exists
if (history[history.length - 1] === item) {
history = history.slice(0, -1);
}
// Add new item
history.push(item);
// Truncate if over max size
if (history.length > InputBoxWithHistory.MaxSize) {
history = history.slice(-InputBoxWithHistory.MaxSize);
}
this.context.globalState.update(InputBoxWithHistory.HistoryKey, history);
}
up(): string | undefined {
if (this.currentIndex > 0) {
this.currentIndex--;
return this.history[this.currentIndex];
}
return undefined;
}
down(): string | undefined {
if (this.currentIndex < this.history.length - 1) {
this.currentIndex++;
return this.history[this.currentIndex];
} else if (this.currentIndex === this.history.length - 1) {
this.currentIndex++;
return "";
}
return undefined;
}
private _displayInputBox(value?: string) {
if (this.currentInputBoxCancellationTokenSource) {
this.currentInputBoxCancellationTokenSource.cancel();
}
this.currentInputBoxCancellationTokenSource =
new vscode.CancellationTokenSource();
this.currentInputBoxCancellationTokenSource.token.onCancellationRequested(
() => {
// Up/down will cancel the input box, but we don't want that to trigger everything to be disposed
if (
this.cancellationReason === "up" ||
this.cancellationReason === "down"
) {
return;
}
this.completionEventEmitter.fire(undefined);
},
);
const options = {
...this.options,
};
if (value) {
options.value = value;
}
vscode.window
.showInputBox(options, this.currentInputBoxCancellationTokenSource.token)
.then((input) => {
if (
(!input && this.cancellationReason === "up") ||
this.cancellationReason === "down"
) {
return;
}
this.completionEventEmitter.fire(input);
});
setTimeout(() => {
this.cancellationReason = undefined;
}, 100);
}
async getInput(): Promise<string | undefined> {
try {
vscode.commands.executeCommand(
"setContext",
"continue.quickEditFocused",
true,
);
return new Promise((resolve, reject) => {
const completionDisposable = this.completionEventEmitter.event(
(input) => {
resolve(input);
completionDisposable.dispose();
if (input) {
this.appendToHistory(input);
}
this.dispose();
},
);
this._displayInputBox();
});
} catch (e) {
console.error(e);
} finally {
vscode.commands.executeCommand(
"setContext",
"continue.quickEditHistoryFocused",
false,
);
}
}
}

View file

@ -2,9 +2,10 @@ 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";
import { VerticalPerLineDiffManager } from "../diff/verticalPerLine/manager";
import { getPlatform } from "../util/util";
import { VsCodeWebviewProtocol } from "../webviewProtocol";
import InputBoxWithHistory from "./InputBoxWithHistory";
interface QuickEditFlowStuff {
defaultModelTitle: string;
@ -19,6 +20,9 @@ export class QuickEdit {
private readonly configHandler: ConfigHandler,
private readonly webviewProtocol: VsCodeWebviewProtocol,
private readonly ide: IDE,
private readonly context: vscode.ExtensionContext,
private readonly historyUpEvent: vscode.Event<void>,
private readonly historyDownEvent: vscode.Event<void>,
) {}
private async _getDefaultModelTitle(): Promise<string> {
@ -92,9 +96,15 @@ export class QuickEdit {
async _collectTextInput(
stuff: QuickEditFlowStuff,
): Promise<string | undefined> {
return await vscode.window.showInputBox(
const inputBox = new InputBoxWithHistory(
this.context,
this.historyUpEvent,
this.historyDownEvent,
await this._getTextInputOptions(stuff),
);
const input = await inputBox.getInput();
inputBox.dispose();
return input;
}
private async _runWithContextItems(stuff: QuickEditFlowStuff) {
@ -106,9 +116,7 @@ export class QuickEdit {
},
);
const input = await vscode.window.showInputBox(
await this._getTextInputOptions(stuff),
);
const input = await this._collectTextInput(stuff);
if (input) {
const inputWithContext = await this._addSelectedProvidersToInput(
input,