refactor: move getTerminalContents to Terminal class

This commit partially addresses the issue of duplicate handler calls by
removing an unnecessary instantiation of TerminalManager in
registerTerminalActions.ts.

Move the getTerminalContents method from TerminalManager to Terminal
class as a static method and update all references to use the new
location.

Fixes: #1380

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>
This commit is contained in:
Eric Wheeler 2025-03-03 21:25:46 -08:00
parent daf36f38ea
commit e24e5b24d4
3 changed files with 59 additions and 70 deletions

View file

@ -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)
}

View file

@ -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<string> {
// 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
}
}
}

View file

@ -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<string> {
// 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
}
}
}