Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved command input #4

Merged
merged 2 commits into from
Feb 11, 2023
Merged
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
6 changes: 2 additions & 4 deletions vs-code-extension/src/editSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from "vscode";
import { EditSelectionRequest } from "../../app/api/types/api";
import { createEdit } from "./client";
import recordVoiceCommand from "./recordVoiceCommand";
import { getInstructionText } from "./getInstructionText";

/**
* Get an instruction from the user.
Expand All @@ -16,10 +17,7 @@ async function getInstruction(): Promise<EditSelectionRequest["instruction"] | n
? await recordVoiceCommand()
: {
type: "text",
contents: await vscode.window.showInputBox({
title: "Instructions for Clippy",
value: "",
}),
contents: await getInstructionText()
};

if (!instruction?.contents) return null;
Expand Down
40 changes: 40 additions & 0 deletions vs-code-extension/src/getInstructionText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as vscode from "vscode";
import getContext from "./context";

const BUILD_IN_INSTRUCTIONS = [
"Fix syntax errors",
"Add type hints to all function definitions",
"Remove logging statements",
"Remove dead code"
];

const lastInstructionStateKey = "lastInstruction";


export async function getInstructionText() {
const context = getContext();
const lastInstructions = context.globalState.get(lastInstructionStateKey, []) as Array<string>;
const quickPick = vscode.window.createQuickPick();
quickPick.items = [
{ label: "Recent", kind: vscode.QuickPickItemKind.Separator },
...lastInstructions.filter(Boolean).map(label => ({ label })),

{ label: "Builtin", kind: vscode.QuickPickItemKind.Separator },
...BUILD_IN_INSTRUCTIONS.map(label => ({ label })),
];

quickPick.title = "Instructions for Clippy";
quickPick.placeholder = "What do you want to do?";
quickPick.canSelectMany = false;
const inputResult = new Promise<string | undefined>((resolve, reject) => {
quickPick.onDidAccept(() => resolve(quickPick.value));
quickPick.onDidChangeSelection(selection => resolve(selection[0].label));
quickPick.onDidHide(() => resolve(undefined));
});

quickPick.show();
const result = await inputResult.finally(() => quickPick.dispose());
const uniqueHistory = [...new Set([result, ...lastInstructions])].slice(0, 5);
context.globalState.update(lastInstructionStateKey, uniqueHistory);
return result;
}