| 1 | import { QuickPickItem, window, ExtensionContext } from "vscode"; |
| 2 | |
| 3 | const HISTORY_KEY = "quickEditHistory"; |
| 4 | const MAX_HISTORY_LENGTH = 50; |
| 5 | |
| 6 | export function appendToHistory( |
| 7 | prompt: string, |
| 8 | { globalState }: ExtensionContext, |
| 9 | ) { |
| 10 | let history: string[] = globalState.get(HISTORY_KEY, []); |
| 11 | |
| 12 | // Remove duplicate if exists |
| 13 | if (history[history.length - 1] === prompt) { |
| 14 | history = history.slice(0, -1); |
| 15 | } |
| 16 | |
| 17 | // Add new item |
| 18 | history.push(prompt); |
| 19 | |
| 20 | // Truncate if over max size |
| 21 | if (history.length > MAX_HISTORY_LENGTH) { |
| 22 | history = history.slice(-MAX_HISTORY_LENGTH); |
| 23 | } |
| 24 | |
| 25 | globalState.update(HISTORY_KEY, history); |
| 26 | } |
| 27 | |
| 28 | export async function getHistoryQuickPickVal({ |
| 29 | globalState, |
| 30 | }: ExtensionContext): Promise<string | undefined> { |
| 31 | const historyItems: QuickPickItem[] = globalState |
| 32 | .get(HISTORY_KEY, []) |
| 33 | .map((item) => ({ label: item })) |
| 34 | .reverse(); |
| 35 | |
| 36 | const selectedItem = await window.showQuickPick(historyItems, { |
| 37 | title: "History", |
| 38 | placeHolder: "Select a previous prompt", |
| 39 | }); |
| 40 | |
| 41 | return selectedItem?.label; |
| 42 | } |