|
| 1 | +import * as vscode from "vscode" |
| 2 | +import delay from "delay" |
| 3 | + |
| 4 | +import { ClineProvider } from "../core/webview/ClineProvider" |
| 5 | + |
| 6 | +export type RegisterCommandOptions = { |
| 7 | + context: vscode.ExtensionContext |
| 8 | + outputChannel: vscode.OutputChannel |
| 9 | + provider: ClineProvider |
| 10 | +} |
| 11 | + |
| 12 | +export const registerCommands = (options: RegisterCommandOptions) => { |
| 13 | + const { context, outputChannel } = options |
| 14 | + |
| 15 | + for (const [command, callback] of Object.entries(getCommandsMap(options))) { |
| 16 | + context.subscriptions.push(vscode.commands.registerCommand(command, callback)) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +const getCommandsMap = ({ context, outputChannel, provider }: RegisterCommandOptions) => { |
| 21 | + return { |
| 22 | + "roo-cline.plusButtonClicked": async () => { |
| 23 | + await provider.clearTask() |
| 24 | + await provider.postStateToWebview() |
| 25 | + await provider.postMessageToWebview({ type: "action", action: "chatButtonClicked" }) |
| 26 | + }, |
| 27 | + "roo-cline.mcpButtonClicked": () => { |
| 28 | + provider.postMessageToWebview({ type: "action", action: "mcpButtonClicked" }) |
| 29 | + }, |
| 30 | + "roo-cline.promptsButtonClicked": () => { |
| 31 | + provider.postMessageToWebview({ type: "action", action: "promptsButtonClicked" }) |
| 32 | + }, |
| 33 | + "roo-cline.popoutButtonClicked": () => openClineInNewTab({ context, outputChannel }), |
| 34 | + "roo-cline.openInNewTab": () => openClineInNewTab({ context, outputChannel }), |
| 35 | + "roo-cline.settingsButtonClicked": () => { |
| 36 | + provider.postMessageToWebview({ type: "action", action: "settingsButtonClicked" }) |
| 37 | + }, |
| 38 | + "roo-cline.historyButtonClicked": () => { |
| 39 | + provider.postMessageToWebview({ type: "action", action: "historyButtonClicked" }) |
| 40 | + }, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +const openClineInNewTab = async ({ context, outputChannel }: Omit<RegisterCommandOptions, "provider">) => { |
| 45 | + outputChannel.appendLine("Opening Roo Code in new tab") |
| 46 | + |
| 47 | + // (This example uses webviewProvider activation event which is necessary to |
| 48 | + // deserialize cached webview, but since we use retainContextWhenHidden, we |
| 49 | + // don't need to use that event). |
| 50 | + // https://github.com/microsoft/vscode-extension-samples/blob/main/webview-sample/src/extension.ts |
| 51 | + const tabProvider = new ClineProvider(context, outputChannel) |
| 52 | + // const column = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.viewColumn : undefined |
| 53 | + const lastCol = Math.max(...vscode.window.visibleTextEditors.map((editor) => editor.viewColumn || 0)) |
| 54 | + |
| 55 | + // Check if there are any visible text editors, otherwise open a new group |
| 56 | + // to the right. |
| 57 | + const hasVisibleEditors = vscode.window.visibleTextEditors.length > 0 |
| 58 | + |
| 59 | + if (!hasVisibleEditors) { |
| 60 | + await vscode.commands.executeCommand("workbench.action.newGroupRight") |
| 61 | + } |
| 62 | + |
| 63 | + const targetCol = hasVisibleEditors ? Math.max(lastCol + 1, 1) : vscode.ViewColumn.Two |
| 64 | + |
| 65 | + const panel = vscode.window.createWebviewPanel(ClineProvider.tabPanelId, "Roo Code", targetCol, { |
| 66 | + enableScripts: true, |
| 67 | + retainContextWhenHidden: true, |
| 68 | + localResourceRoots: [context.extensionUri], |
| 69 | + }) |
| 70 | + |
| 71 | + // TODO: use better svg icon with light and dark variants (see |
| 72 | + // https://stackoverflow.com/questions/58365687/vscode-extension-iconpath). |
| 73 | + panel.iconPath = { |
| 74 | + light: vscode.Uri.joinPath(context.extensionUri, "assets", "icons", "rocket.png"), |
| 75 | + dark: vscode.Uri.joinPath(context.extensionUri, "assets", "icons", "rocket.png"), |
| 76 | + } |
| 77 | + |
| 78 | + tabProvider.resolveWebviewView(panel) |
| 79 | + |
| 80 | + // Lock the editor group so clicking on files doesn't open them over the panel |
| 81 | + await delay(100) |
| 82 | + await vscode.commands.executeCommand("workbench.action.lockEditorGroup") |
| 83 | +} |
0 commit comments