From caf31ed11f55d7be40cb74225dfd1068594deaec Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Sun, 29 Dec 2024 15:14:56 +0100 Subject: [PATCH] clipboard context provider p1 --- .../providers/ClipboardContextProvider.ts | 57 +++++++++++++++++++ core/core.ts | 5 ++ core/protocol/core.ts | 1 + core/util/clipboardCache.ts | 54 ++++++++++++++++++ extensions/vscode/package-lock.json | 4 +- extensions/vscode/src/commands.ts | 13 ++++- 6 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 core/context/providers/ClipboardContextProvider.ts create mode 100644 core/util/clipboardCache.ts diff --git a/core/context/providers/ClipboardContextProvider.ts b/core/context/providers/ClipboardContextProvider.ts new file mode 100644 index 000000000..eb128ea7f --- /dev/null +++ b/core/context/providers/ClipboardContextProvider.ts @@ -0,0 +1,57 @@ +import { BaseContextProvider } from ".."; +import { + ContextItem, + ContextProviderDescription, + ContextProviderExtras, + ContextSubmenuItem, + LoadSubmenuItemsArgs, +} from "../.."; +import { clipboardCache } from "../../util/clipboardCache"; + +const MAX_CLIPBOARD_ITEMS = 10; + +class ClipboardContextProvider extends BaseContextProvider { + static description: ContextProviderDescription = { + title: "clipboard", + displayTitle: "Clipboard", + description: "Use recent copies", + type: "submenu", + }; + + async getContextItems( + query: string, + extras: ContextProviderExtras, + ): Promise { + // Assume the query is a cache id + const id = query.trim(); + const content = clipboardCache.get(id); + + if (content) { + clipboardCache.select(id); + return [ + { + name: "Clipboard item", + description: content.slice(0, 20), + content, + }, + ]; + } + return []; + } + + async loadSubmenuItems( + args: LoadSubmenuItemsArgs, + ): Promise { + const recentClipboardItems = clipboardCache.getNItems(MAX_CLIPBOARD_ITEMS); + + return recentClipboardItems.map((item, index) => { + return { + id: item.id, + title: item.content.slice(0, 20), + description: `#${index + 1}`, + }; + }); + } +} + +export default ClipboardContextProvider; diff --git a/core/core.ts b/core/core.ts index 14389910c..90a6246c3 100644 --- a/core/core.ts +++ b/core/core.ts @@ -46,6 +46,7 @@ import * as URI from "uri-js"; import { SYSTEM_PROMPT_DOT_FILE } from "./config/getSystemPromptDotFile"; import type { IMessenger, Message } from "./protocol/messenger"; import { localPathToUri } from "./util/pathToUri"; +import { clipboardCache } from "./util/clipboardCache"; export class Core { // implements IMessenger @@ -377,6 +378,10 @@ export class Core { }; }); + on("clipboardCache/add", (msg) => { + clipboardCache.add(uuidv4(), msg.data.content); + }); + async function* llmStreamChat( configHandler: ConfigHandler, abortedMessageIds: Set, diff --git a/core/protocol/core.ts b/core/protocol/core.ts index e05ce1bc4..d0c84121f 100644 --- a/core/protocol/core.ts +++ b/core/protocol/core.ts @@ -193,4 +193,5 @@ export type ToCoreFromIdeOrWebviewProtocol = { { toolCall: ToolCall; selectedModelTitle: string }, { contextItems: ContextItem[] }, ]; + "clipboardCache/add": [{ content: string }, void]; }; diff --git a/core/util/clipboardCache.ts b/core/util/clipboardCache.ts new file mode 100644 index 000000000..4ecd16afc --- /dev/null +++ b/core/util/clipboardCache.ts @@ -0,0 +1,54 @@ +class ClipboardCache { + private cache: Map; + private order: string[]; + private readonly maxSize = 30; + + constructor() { + this.cache = new Map(); + this.order = []; + } + + add(id: string, content: string): void { + if (!content) { + return; + } + if (this.order.length >= this.maxSize) { + const oldest = this.order.pop(); + if (oldest) { + this.cache.delete(oldest); + } + } + + this.cache.set(id, content); + this.order.unshift(id); + } + + getNItems(count: number): { + id: string; + content: string; + }[] { + return this.order.slice(0, count).map((id) => ({ + id, + content: this.cache.get(id) || "", + })); + } + + get(id: string): string | undefined { + return this.cache.get(id); + } + + select(id: string): void { + const index = this.order.indexOf(id); + if (index > -1) { + this.order.splice(index, 1); + this.order.unshift(id); + } + } + + clear(): void { + this.cache.clear(); + this.order = []; + } +} + +export const clipboardCache = new ClipboardCache(); diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 8f5cad8a0..0a2dd8330 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -1,12 +1,12 @@ { "name": "continue", - "version": "0.9.247", + "version": "0.9.248", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "continue", - "version": "0.9.247", + "version": "0.9.248", "license": "Apache-2.0", "dependencies": { "@continuedev/fetch": "^1.0.3", diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index b83d3d380..147a1f118 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -1001,7 +1001,10 @@ const getCommandsMap: ( }; }; -const registerCopyBufferSpy = (context: vscode.ExtensionContext) => { +const registerCopyBufferSpy = ( + context: vscode.ExtensionContext, + core: Core, +) => { const typeDisposable = vscode.commands.registerCommand( "editor.action.clipboardCopyAction", async (arg) => doCopy(typeDisposable), @@ -1014,6 +1017,12 @@ const registerCopyBufferSpy = (context: vscode.ExtensionContext) => { const clipboardText = await vscode.env.clipboard.readText(); + if (clipboardText) { + core.invoke("clipboardCache/add", { + content: clipboardText, + }); + } + await context.workspaceState.update("continue.copyBuffer", { text: clipboardText, copiedAt: new Date().toISOString(), @@ -1043,7 +1052,7 @@ export function registerAllCommands( core: Core, editDecorationManager: EditDecorationManager, ) { - registerCopyBufferSpy(context); + registerCopyBufferSpy(context, core); for (const [command, callback] of Object.entries( getCommandsMap(