Skip to content

Commit a568577

Browse files
authored
Merge pull request RooCodeInc#702 from RooVetGit/cte/activation-cleanup
2 parents edd0ac8 + aa806fd commit a568577

File tree

6 files changed

+247
-213
lines changed

6 files changed

+247
-213
lines changed

src/activate/handleUri.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as vscode from "vscode"
2+
3+
import { ClineProvider } from "../core/webview/ClineProvider"
4+
5+
export const handleUri = async (uri: vscode.Uri) => {
6+
const path = uri.path
7+
const query = new URLSearchParams(uri.query.replace(/\+/g, "%2B"))
8+
const visibleProvider = ClineProvider.getVisibleInstance()
9+
10+
if (!visibleProvider) {
11+
return
12+
}
13+
14+
switch (path) {
15+
case "/glama": {
16+
const code = query.get("code")
17+
if (code) {
18+
await visibleProvider.handleGlamaCallback(code)
19+
}
20+
break
21+
}
22+
case "/openrouter": {
23+
const code = query.get("code")
24+
if (code) {
25+
await visibleProvider.handleOpenRouterCallback(code)
26+
}
27+
break
28+
}
29+
default:
30+
break
31+
}
32+
}

src/activate/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { handleUri } from "./handleUri"
2+
export { registerCommands } from "./registerCommands"
3+
export { registerCodeActions } from "./registerCodeActions"
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import * as vscode from "vscode"
2+
3+
import { ACTION_NAMES, COMMAND_IDS } from "../core/CodeActionProvider"
4+
import { EditorUtils } from "../core/EditorUtils"
5+
import { ClineProvider } from "../core/webview/ClineProvider"
6+
7+
export const registerCodeActions = (context: vscode.ExtensionContext) => {
8+
registerCodeActionPair(
9+
context,
10+
COMMAND_IDS.EXPLAIN,
11+
"EXPLAIN",
12+
"What would you like Roo to explain?",
13+
"E.g. How does the error handling work?",
14+
)
15+
16+
registerCodeActionPair(
17+
context,
18+
COMMAND_IDS.FIX,
19+
"FIX",
20+
"What would you like Roo to fix?",
21+
"E.g. Maintain backward compatibility",
22+
)
23+
24+
registerCodeActionPair(
25+
context,
26+
COMMAND_IDS.IMPROVE,
27+
"IMPROVE",
28+
"What would you like Roo to improve?",
29+
"E.g. Focus on performance optimization",
30+
)
31+
32+
registerCodeAction(context, COMMAND_IDS.ADD_TO_CONTEXT, "ADD_TO_CONTEXT")
33+
}
34+
35+
const registerCodeAction = (
36+
context: vscode.ExtensionContext,
37+
command: string,
38+
promptType: keyof typeof ACTION_NAMES,
39+
inputPrompt?: string,
40+
inputPlaceholder?: string,
41+
) => {
42+
let userInput: string | undefined
43+
44+
context.subscriptions.push(
45+
vscode.commands.registerCommand(command, async (...args: any[]) => {
46+
if (inputPrompt) {
47+
userInput = await vscode.window.showInputBox({
48+
prompt: inputPrompt,
49+
placeHolder: inputPlaceholder,
50+
})
51+
}
52+
53+
// Handle both code action and direct command cases.
54+
let filePath: string
55+
let selectedText: string
56+
let diagnostics: any[] | undefined
57+
58+
if (args.length > 1) {
59+
// Called from code action.
60+
;[filePath, selectedText, diagnostics] = args
61+
} else {
62+
// Called directly from command palette.
63+
const context = EditorUtils.getEditorContext()
64+
if (!context) return
65+
;({ filePath, selectedText, diagnostics } = context)
66+
}
67+
68+
const params = {
69+
...{ filePath, selectedText },
70+
...(diagnostics ? { diagnostics } : {}),
71+
...(userInput ? { userInput } : {}),
72+
}
73+
74+
await ClineProvider.handleCodeAction(command, promptType, params)
75+
}),
76+
)
77+
}
78+
79+
const registerCodeActionPair = (
80+
context: vscode.ExtensionContext,
81+
baseCommand: string,
82+
promptType: keyof typeof ACTION_NAMES,
83+
inputPrompt?: string,
84+
inputPlaceholder?: string,
85+
) => {
86+
// Register new task version.
87+
registerCodeAction(context, baseCommand, promptType, inputPrompt, inputPlaceholder)
88+
89+
// Register current task version.
90+
registerCodeAction(context, `${baseCommand}InCurrentTask`, promptType, inputPrompt, inputPlaceholder)
91+
}

src/activate/registerCommands.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

src/core/CodeActionProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const ACTION_NAMES = {
99
ADD_TO_CONTEXT: "Roo Code: Add to Context",
1010
} as const
1111

12-
const COMMAND_IDS = {
12+
export const COMMAND_IDS = {
1313
EXPLAIN: "roo-cline.explainCode",
1414
FIX: "roo-cline.fixCode",
1515
IMPROVE: "roo-cline.improveCode",

0 commit comments

Comments
 (0)