From d5466532e15bc79fff2fd290e6a34bdcf63b72d8 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Thu, 4 Jul 2024 14:03:11 -0700 Subject: [PATCH] quick input box with history --- extensions/vscode/package-lock.json | 4 +- extensions/vscode/package.json | 24 +++ extensions/vscode/src/commands.ts | 15 +- .../src/quickEdit/InputBoxWithHistory.ts | 181 ++++++++++++++++++ .../vscode/src/{ => quickEdit}/QuickEdit.ts | 22 ++- 5 files changed, 234 insertions(+), 12 deletions(-) create mode 100644 extensions/vscode/src/quickEdit/InputBoxWithHistory.ts rename extensions/vscode/src/{ => quickEdit}/QuickEdit.ts (89%) diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 47a3d36cb..6873f1d75 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -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", diff --git a/extensions/vscode/package.json b/extensions/vscode/package.json index dbd2703ef..25eca203b 100644 --- a/extensions/vscode/package.json +++ b/extensions/vscode/package.json @@ -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", diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index 0e9cdaf92..411bde18a 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -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(); + const historyDownEventEmitter = new vscode.EventEmitter(); 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(); + }, }; }; diff --git a/extensions/vscode/src/quickEdit/InputBoxWithHistory.ts b/extensions/vscode/src/quickEdit/InputBoxWithHistory.ts new file mode 100644 index 000000000..f88341882 --- /dev/null +++ b/extensions/vscode/src/quickEdit/InputBoxWithHistory.ts @@ -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, + private readonly historyDownEvent: vscode.Event, + 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 { + 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, + ); + } + } +} diff --git a/extensions/vscode/src/QuickEdit.ts b/extensions/vscode/src/quickEdit/QuickEdit.ts similarity index 89% rename from extensions/vscode/src/QuickEdit.ts rename to extensions/vscode/src/quickEdit/QuickEdit.ts index e25860f72..5e598f51f 100644 --- a/extensions/vscode/src/QuickEdit.ts +++ b/extensions/vscode/src/quickEdit/QuickEdit.ts @@ -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, + private readonly historyDownEvent: vscode.Event, ) {} private async _getDefaultModelTitle(): Promise { @@ -92,9 +96,15 @@ export class QuickEdit { async _collectTextInput( stuff: QuickEditFlowStuff, ): Promise { - 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,