Skip to content

Commit e24e5b2

Browse files
author
Eric Wheeler
committed
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 <[email protected]>
1 parent daf36f3 commit e24e5b2

File tree

3 files changed

+59
-70
lines changed

3 files changed

+59
-70
lines changed

src/activate/registerTerminalActions.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from "vscode"
22
import { ClineProvider } from "../core/webview/ClineProvider"
3-
import { TerminalManager } from "../integrations/terminal/TerminalManager"
3+
import { Terminal } from "../integrations/terminal/Terminal"
44

55
const TERMINAL_COMMAND_IDS = {
66
ADD_TO_CONTEXT: "roo-cline.terminalAddToContext",
@@ -11,21 +11,12 @@ const TERMINAL_COMMAND_IDS = {
1111
} as const
1212

1313
export const registerTerminalActions = (context: vscode.ExtensionContext) => {
14-
const terminalManager = new TerminalManager()
14+
registerTerminalAction(context, TERMINAL_COMMAND_IDS.ADD_TO_CONTEXT, "TERMINAL_ADD_TO_CONTEXT")
1515

16-
registerTerminalAction(context, terminalManager, TERMINAL_COMMAND_IDS.ADD_TO_CONTEXT, "TERMINAL_ADD_TO_CONTEXT")
16+
registerTerminalActionPair(context, TERMINAL_COMMAND_IDS.FIX, "TERMINAL_FIX", "What would you like Roo to fix?")
1717

1818
registerTerminalActionPair(
1919
context,
20-
terminalManager,
21-
TERMINAL_COMMAND_IDS.FIX,
22-
"TERMINAL_FIX",
23-
"What would you like Roo to fix?",
24-
)
25-
26-
registerTerminalActionPair(
27-
context,
28-
terminalManager,
2920
TERMINAL_COMMAND_IDS.EXPLAIN,
3021
"TERMINAL_EXPLAIN",
3122
"What would you like Roo to explain?",
@@ -34,7 +25,6 @@ export const registerTerminalActions = (context: vscode.ExtensionContext) => {
3425

3526
const registerTerminalAction = (
3627
context: vscode.ExtensionContext,
37-
terminalManager: TerminalManager,
3828
command: string,
3929
promptType: "TERMINAL_ADD_TO_CONTEXT" | "TERMINAL_FIX" | "TERMINAL_EXPLAIN",
4030
inputPrompt?: string,
@@ -43,7 +33,7 @@ const registerTerminalAction = (
4333
vscode.commands.registerCommand(command, async (args: any) => {
4434
let content = args.selection
4535
if (!content || content === "") {
46-
content = await terminalManager.getTerminalContents(promptType === "TERMINAL_ADD_TO_CONTEXT" ? -1 : 1)
36+
content = await Terminal.getTerminalContents(promptType === "TERMINAL_ADD_TO_CONTEXT" ? -1 : 1)
4737
}
4838

4939
if (!content) {
@@ -69,13 +59,12 @@ const registerTerminalAction = (
6959

7060
const registerTerminalActionPair = (
7161
context: vscode.ExtensionContext,
72-
terminalManager: TerminalManager,
7362
baseCommand: string,
7463
promptType: "TERMINAL_ADD_TO_CONTEXT" | "TERMINAL_FIX" | "TERMINAL_EXPLAIN",
7564
inputPrompt?: string,
7665
) => {
7766
// Register new task version
78-
registerTerminalAction(context, terminalManager, baseCommand, promptType, inputPrompt)
67+
registerTerminalAction(context, baseCommand, promptType, inputPrompt)
7968
// Register current task version
80-
registerTerminalAction(context, terminalManager, `${baseCommand}InCurrentTask`, promptType, inputPrompt)
69+
registerTerminalAction(context, `${baseCommand}InCurrentTask`, promptType, inputPrompt)
8170
}

src/integrations/terminal/Terminal.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,57 @@ export class Terminal {
1717
this.running = false
1818
this.streamClosed = false
1919
}
20+
21+
/**
22+
* Gets the terminal contents based on the number of commands to include
23+
* @param commands Number of previous commands to include (-1 for all)
24+
* @returns The selected terminal contents
25+
*/
26+
public static async getTerminalContents(commands = -1): Promise<string> {
27+
// Save current clipboard content
28+
const tempCopyBuffer = await vscode.env.clipboard.readText()
29+
30+
try {
31+
// Select terminal content
32+
if (commands < 0) {
33+
await vscode.commands.executeCommand("workbench.action.terminal.selectAll")
34+
} else {
35+
for (let i = 0; i < commands; i++) {
36+
await vscode.commands.executeCommand("workbench.action.terminal.selectToPreviousCommand")
37+
}
38+
}
39+
40+
// Copy selection and clear it
41+
await vscode.commands.executeCommand("workbench.action.terminal.copySelection")
42+
await vscode.commands.executeCommand("workbench.action.terminal.clearSelection")
43+
44+
// Get copied content
45+
let terminalContents = (await vscode.env.clipboard.readText()).trim()
46+
47+
// Restore original clipboard content
48+
await vscode.env.clipboard.writeText(tempCopyBuffer)
49+
50+
if (tempCopyBuffer === terminalContents) {
51+
// No terminal content was copied
52+
return ""
53+
}
54+
55+
// Process multi-line content
56+
const lines = terminalContents.split("\n")
57+
const lastLine = lines.pop()?.trim()
58+
if (lastLine) {
59+
let i = lines.length - 1
60+
while (i >= 0 && !lines[i].trim().startsWith(lastLine)) {
61+
i--
62+
}
63+
terminalContents = lines.slice(Math.max(i, 0)).join("\n")
64+
}
65+
66+
return terminalContents
67+
} catch (error) {
68+
// Ensure clipboard is restored even if an error occurs
69+
await vscode.env.clipboard.writeText(tempCopyBuffer)
70+
throw error
71+
}
72+
}
2073
}

src/integrations/terminal/TerminalManager.ts

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -287,57 +287,4 @@ export class TerminalManager {
287287
this.disposables.forEach((disposable) => disposable.dispose())
288288
this.disposables = []
289289
}
290-
291-
/**
292-
* Gets the terminal contents based on the number of commands to include
293-
* @param commands Number of previous commands to include (-1 for all)
294-
* @returns The selected terminal contents
295-
*/
296-
public async getTerminalContents(commands = -1): Promise<string> {
297-
// Save current clipboard content
298-
const tempCopyBuffer = await vscode.env.clipboard.readText()
299-
300-
try {
301-
// Select terminal content
302-
if (commands < 0) {
303-
await vscode.commands.executeCommand("workbench.action.terminal.selectAll")
304-
} else {
305-
for (let i = 0; i < commands; i++) {
306-
await vscode.commands.executeCommand("workbench.action.terminal.selectToPreviousCommand")
307-
}
308-
}
309-
310-
// Copy selection and clear it
311-
await vscode.commands.executeCommand("workbench.action.terminal.copySelection")
312-
await vscode.commands.executeCommand("workbench.action.terminal.clearSelection")
313-
314-
// Get copied content
315-
let terminalContents = (await vscode.env.clipboard.readText()).trim()
316-
317-
// Restore original clipboard content
318-
await vscode.env.clipboard.writeText(tempCopyBuffer)
319-
320-
if (tempCopyBuffer === terminalContents) {
321-
// No terminal content was copied
322-
return ""
323-
}
324-
325-
// Process multi-line content
326-
const lines = terminalContents.split("\n")
327-
const lastLine = lines.pop()?.trim()
328-
if (lastLine) {
329-
let i = lines.length - 1
330-
while (i >= 0 && !lines[i].trim().startsWith(lastLine)) {
331-
i--
332-
}
333-
terminalContents = lines.slice(Math.max(i, 0)).join("\n")
334-
}
335-
336-
return terminalContents
337-
} catch (error) {
338-
// Ensure clipboard is restored even if an error occurs
339-
await vscode.env.clipboard.writeText(tempCopyBuffer)
340-
throw error
341-
}
342-
}
343290
}

0 commit comments

Comments
 (0)