diff --git a/src/activate/registerTerminalActions.ts b/src/activate/registerTerminalActions.ts index fbf2a0510c..74d3449039 100644 --- a/src/activate/registerTerminalActions.ts +++ b/src/activate/registerTerminalActions.ts @@ -1,6 +1,6 @@ import * as vscode from "vscode" import { ClineProvider } from "../core/webview/ClineProvider" -import { TerminalManager } from "../integrations/terminal/TerminalManager" +import { Terminal } from "../integrations/terminal/Terminal" const TERMINAL_COMMAND_IDS = { ADD_TO_CONTEXT: "roo-cline.terminalAddToContext", @@ -11,21 +11,12 @@ const TERMINAL_COMMAND_IDS = { } as const export const registerTerminalActions = (context: vscode.ExtensionContext) => { - const terminalManager = new TerminalManager() + registerTerminalAction(context, TERMINAL_COMMAND_IDS.ADD_TO_CONTEXT, "TERMINAL_ADD_TO_CONTEXT") - registerTerminalAction(context, terminalManager, TERMINAL_COMMAND_IDS.ADD_TO_CONTEXT, "TERMINAL_ADD_TO_CONTEXT") + registerTerminalActionPair(context, TERMINAL_COMMAND_IDS.FIX, "TERMINAL_FIX", "What would you like Roo to fix?") registerTerminalActionPair( context, - terminalManager, - TERMINAL_COMMAND_IDS.FIX, - "TERMINAL_FIX", - "What would you like Roo to fix?", - ) - - registerTerminalActionPair( - context, - terminalManager, TERMINAL_COMMAND_IDS.EXPLAIN, "TERMINAL_EXPLAIN", "What would you like Roo to explain?", @@ -34,7 +25,6 @@ export const registerTerminalActions = (context: vscode.ExtensionContext) => { const registerTerminalAction = ( context: vscode.ExtensionContext, - terminalManager: TerminalManager, command: string, promptType: "TERMINAL_ADD_TO_CONTEXT" | "TERMINAL_FIX" | "TERMINAL_EXPLAIN", inputPrompt?: string, @@ -43,7 +33,7 @@ const registerTerminalAction = ( vscode.commands.registerCommand(command, async (args: any) => { let content = args.selection if (!content || content === "") { - content = await terminalManager.getTerminalContents(promptType === "TERMINAL_ADD_TO_CONTEXT" ? -1 : 1) + content = await Terminal.getTerminalContents(promptType === "TERMINAL_ADD_TO_CONTEXT" ? -1 : 1) } if (!content) { @@ -69,13 +59,12 @@ const registerTerminalAction = ( const registerTerminalActionPair = ( context: vscode.ExtensionContext, - terminalManager: TerminalManager, baseCommand: string, promptType: "TERMINAL_ADD_TO_CONTEXT" | "TERMINAL_FIX" | "TERMINAL_EXPLAIN", inputPrompt?: string, ) => { // Register new task version - registerTerminalAction(context, terminalManager, baseCommand, promptType, inputPrompt) + registerTerminalAction(context, baseCommand, promptType, inputPrompt) // Register current task version - registerTerminalAction(context, terminalManager, `${baseCommand}InCurrentTask`, promptType, inputPrompt) + registerTerminalAction(context, `${baseCommand}InCurrentTask`, promptType, inputPrompt) } diff --git a/src/integrations/terminal/Terminal.ts b/src/integrations/terminal/Terminal.ts index b8a51bc08a..1b91a378cd 100644 --- a/src/integrations/terminal/Terminal.ts +++ b/src/integrations/terminal/Terminal.ts @@ -17,4 +17,57 @@ export class Terminal { this.running = false this.streamClosed = false } + + /** + * Gets the terminal contents based on the number of commands to include + * @param commands Number of previous commands to include (-1 for all) + * @returns The selected terminal contents + */ + public static async getTerminalContents(commands = -1): Promise { + // Save current clipboard content + const tempCopyBuffer = await vscode.env.clipboard.readText() + + try { + // Select terminal content + if (commands < 0) { + await vscode.commands.executeCommand("workbench.action.terminal.selectAll") + } else { + for (let i = 0; i < commands; i++) { + await vscode.commands.executeCommand("workbench.action.terminal.selectToPreviousCommand") + } + } + + // Copy selection and clear it + await vscode.commands.executeCommand("workbench.action.terminal.copySelection") + await vscode.commands.executeCommand("workbench.action.terminal.clearSelection") + + // Get copied content + let terminalContents = (await vscode.env.clipboard.readText()).trim() + + // Restore original clipboard content + await vscode.env.clipboard.writeText(tempCopyBuffer) + + if (tempCopyBuffer === terminalContents) { + // No terminal content was copied + return "" + } + + // Process multi-line content + const lines = terminalContents.split("\n") + const lastLine = lines.pop()?.trim() + if (lastLine) { + let i = lines.length - 1 + while (i >= 0 && !lines[i].trim().startsWith(lastLine)) { + i-- + } + terminalContents = lines.slice(Math.max(i, 0)).join("\n") + } + + return terminalContents + } catch (error) { + // Ensure clipboard is restored even if an error occurs + await vscode.env.clipboard.writeText(tempCopyBuffer) + throw error + } + } } diff --git a/src/integrations/terminal/TerminalManager.ts b/src/integrations/terminal/TerminalManager.ts index f693303849..c4ef4f9167 100644 --- a/src/integrations/terminal/TerminalManager.ts +++ b/src/integrations/terminal/TerminalManager.ts @@ -287,57 +287,4 @@ export class TerminalManager { this.disposables.forEach((disposable) => disposable.dispose()) this.disposables = [] } - - /** - * Gets the terminal contents based on the number of commands to include - * @param commands Number of previous commands to include (-1 for all) - * @returns The selected terminal contents - */ - public async getTerminalContents(commands = -1): Promise { - // Save current clipboard content - const tempCopyBuffer = await vscode.env.clipboard.readText() - - try { - // Select terminal content - if (commands < 0) { - await vscode.commands.executeCommand("workbench.action.terminal.selectAll") - } else { - for (let i = 0; i < commands; i++) { - await vscode.commands.executeCommand("workbench.action.terminal.selectToPreviousCommand") - } - } - - // Copy selection and clear it - await vscode.commands.executeCommand("workbench.action.terminal.copySelection") - await vscode.commands.executeCommand("workbench.action.terminal.clearSelection") - - // Get copied content - let terminalContents = (await vscode.env.clipboard.readText()).trim() - - // Restore original clipboard content - await vscode.env.clipboard.writeText(tempCopyBuffer) - - if (tempCopyBuffer === terminalContents) { - // No terminal content was copied - return "" - } - - // Process multi-line content - const lines = terminalContents.split("\n") - const lastLine = lines.pop()?.trim() - if (lastLine) { - let i = lines.length - 1 - while (i >= 0 && !lines[i].trim().startsWith(lastLine)) { - i-- - } - terminalContents = lines.slice(Math.max(i, 0)).join("\n") - } - - return terminalContents - } catch (error) { - // Ensure clipboard is restored even if an error occurs - await vscode.env.clipboard.writeText(tempCopyBuffer) - throw error - } - } }