clipboard context provider p1

This commit is contained in:
Dallin Romney 2024-12-29 15:14:56 +01:00
parent 74035c35cc
commit caf31ed11f
6 changed files with 130 additions and 4 deletions

View file

@ -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<ContextItem[]> {
// 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<ContextSubmenuItem[]> {
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;

View file

@ -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<ToCoreProtocol, FromCoreProtocol>
@ -377,6 +378,10 @@ export class Core {
};
});
on("clipboardCache/add", (msg) => {
clipboardCache.add(uuidv4(), msg.data.content);
});
async function* llmStreamChat(
configHandler: ConfigHandler,
abortedMessageIds: Set<string>,

View file

@ -193,4 +193,5 @@ export type ToCoreFromIdeOrWebviewProtocol = {
{ toolCall: ToolCall; selectedModelTitle: string },
{ contextItems: ContextItem[] },
];
"clipboardCache/add": [{ content: string }, void];
};

View file

@ -0,0 +1,54 @@
class ClipboardCache {
private cache: Map<string, string>;
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();

View file

@ -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",

View file

@ -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(