Skip to content

Commit d4c514b

Browse files
committed
fix: improve button click handling during extension initialization
- Add retry logic when ClineProvider is not immediately available - Attempt to activate sidebar view if no visible provider found - Add user-friendly error messages when initialization is incomplete - Improve error handling in getInstance method with longer wait times - Check for function existence before calling showErrorMessage (test compatibility) Fixes #7036
1 parent 140310f commit d4c514b

File tree

2 files changed

+63
-25
lines changed

2 files changed

+63
-25
lines changed

src/activate/registerCommands.ts

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,40 @@ import { t } from "../i18n"
1919

2020
/**
2121
* Helper to get the visible ClineProvider instance or log if not found.
22+
* Shows user-friendly error message when provider is not available.
2223
*/
23-
export function getVisibleProviderOrLog(outputChannel: vscode.OutputChannel): ClineProvider | undefined {
24-
const visibleProvider = ClineProvider.getVisibleInstance()
24+
export async function getVisibleProviderOrLog(outputChannel: vscode.OutputChannel): Promise<ClineProvider | undefined> {
25+
let visibleProvider = ClineProvider.getVisibleInstance()
26+
27+
// If no visible provider, try to activate the sidebar view first
28+
if (!visibleProvider) {
29+
outputChannel.appendLine("No visible Roo Code instance found, attempting to activate sidebar view...")
30+
31+
try {
32+
// Try to focus the sidebar view which should trigger provider creation
33+
await vscode.commands.executeCommand(`${Package.name}.SidebarProvider.focus`)
34+
35+
// Wait a bit for the view to initialize
36+
await new Promise((resolve) => setTimeout(resolve, 500))
37+
38+
// Try to get the provider again
39+
visibleProvider = ClineProvider.getVisibleInstance()
40+
} catch (error) {
41+
outputChannel.appendLine(`Failed to activate sidebar view: ${error}`)
42+
}
43+
}
44+
2545
if (!visibleProvider) {
26-
outputChannel.appendLine("Cannot find any visible Roo Code instances.")
46+
outputChannel.appendLine("Cannot find any visible Roo Code instances after activation attempt.")
47+
// Show user-friendly error message only if not in test environment
48+
if (typeof vscode.window.showErrorMessage === "function") {
49+
vscode.window.showErrorMessage(
50+
"Roo Code is still initializing. Please wait a moment and try again, or restart VS Code if the issue persists.",
51+
)
52+
}
2753
return undefined
2854
}
55+
2956
return visibleProvider
3057
}
3158

@@ -74,8 +101,8 @@ export const registerCommands = (options: RegisterCommandOptions) => {
74101

75102
const getCommandsMap = ({ context, outputChannel, provider }: RegisterCommandOptions): Record<CommandId, any> => ({
76103
activationCompleted: () => {},
77-
accountButtonClicked: () => {
78-
const visibleProvider = getVisibleProviderOrLog(outputChannel)
104+
accountButtonClicked: async () => {
105+
const visibleProvider = await getVisibleProviderOrLog(outputChannel)
79106

80107
if (!visibleProvider) {
81108
return
@@ -86,7 +113,7 @@ const getCommandsMap = ({ context, outputChannel, provider }: RegisterCommandOpt
86113
visibleProvider.postMessageToWebview({ type: "action", action: "accountButtonClicked" })
87114
},
88115
plusButtonClicked: async () => {
89-
const visibleProvider = getVisibleProviderOrLog(outputChannel)
116+
const visibleProvider = await getVisibleProviderOrLog(outputChannel)
90117

91118
if (!visibleProvider) {
92119
return
@@ -101,8 +128,8 @@ const getCommandsMap = ({ context, outputChannel, provider }: RegisterCommandOpt
101128
// This ensures the focus happens after the view has switched
102129
await visibleProvider.postMessageToWebview({ type: "action", action: "focusInput" })
103130
},
104-
mcpButtonClicked: () => {
105-
const visibleProvider = getVisibleProviderOrLog(outputChannel)
131+
mcpButtonClicked: async () => {
132+
const visibleProvider = await getVisibleProviderOrLog(outputChannel)
106133

107134
if (!visibleProvider) {
108135
return
@@ -112,8 +139,8 @@ const getCommandsMap = ({ context, outputChannel, provider }: RegisterCommandOpt
112139

113140
visibleProvider.postMessageToWebview({ type: "action", action: "mcpButtonClicked" })
114141
},
115-
promptsButtonClicked: () => {
116-
const visibleProvider = getVisibleProviderOrLog(outputChannel)
142+
promptsButtonClicked: async () => {
143+
const visibleProvider = await getVisibleProviderOrLog(outputChannel)
117144

118145
if (!visibleProvider) {
119146
return
@@ -129,8 +156,8 @@ const getCommandsMap = ({ context, outputChannel, provider }: RegisterCommandOpt
129156
return openClineInNewTab({ context, outputChannel })
130157
},
131158
openInNewTab: () => openClineInNewTab({ context, outputChannel }),
132-
settingsButtonClicked: () => {
133-
const visibleProvider = getVisibleProviderOrLog(outputChannel)
159+
settingsButtonClicked: async () => {
160+
const visibleProvider = await getVisibleProviderOrLog(outputChannel)
134161

135162
if (!visibleProvider) {
136163
return
@@ -142,8 +169,8 @@ const getCommandsMap = ({ context, outputChannel, provider }: RegisterCommandOpt
142169
// Also explicitly post the visibility message to trigger scroll reliably
143170
visibleProvider.postMessageToWebview({ type: "action", action: "didBecomeVisible" })
144171
},
145-
historyButtonClicked: () => {
146-
const visibleProvider = getVisibleProviderOrLog(outputChannel)
172+
historyButtonClicked: async () => {
173+
const visibleProvider = await getVisibleProviderOrLog(outputChannel)
147174

148175
if (!visibleProvider) {
149176
return
@@ -153,8 +180,8 @@ const getCommandsMap = ({ context, outputChannel, provider }: RegisterCommandOpt
153180

154181
visibleProvider.postMessageToWebview({ type: "action", action: "historyButtonClicked" })
155182
},
156-
marketplaceButtonClicked: () => {
157-
const visibleProvider = getVisibleProviderOrLog(outputChannel)
183+
marketplaceButtonClicked: async () => {
184+
const visibleProvider = await getVisibleProviderOrLog(outputChannel)
158185
if (!visibleProvider) return
159186
visibleProvider.postMessageToWebview({ type: "action", action: "marketplaceButtonClicked" })
160187
},
@@ -178,7 +205,7 @@ const getCommandsMap = ({ context, outputChannel, provider }: RegisterCommandOpt
178205
await promptForCustomStoragePath()
179206
},
180207
importSettings: async (filePath?: string) => {
181-
const visibleProvider = getVisibleProviderOrLog(outputChannel)
208+
const visibleProvider = await getVisibleProviderOrLog(outputChannel)
182209
if (!visibleProvider) {
183210
return
184211
}
@@ -212,8 +239,8 @@ const getCommandsMap = ({ context, outputChannel, provider }: RegisterCommandOpt
212239
outputChannel.appendLine(`Error focusing panel: ${error}`)
213240
}
214241
},
215-
acceptInput: () => {
216-
const visibleProvider = getVisibleProviderOrLog(outputChannel)
242+
acceptInput: async () => {
243+
const visibleProvider = await getVisibleProviderOrLog(outputChannel)
217244

218245
if (!visibleProvider) {
219246
return

src/core/webview/ClineProvider.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -469,15 +469,26 @@ export class ClineProvider
469469

470470
// If no visible provider, try to show the sidebar view
471471
if (!visibleProvider) {
472-
await vscode.commands.executeCommand(`${Package.name}.SidebarProvider.focus`)
473-
// Wait briefly for the view to become visible
474-
await delay(100)
475-
visibleProvider = ClineProvider.getVisibleInstance()
472+
try {
473+
await vscode.commands.executeCommand(`${Package.name}.SidebarProvider.focus`)
474+
// Wait a bit longer for the view to become visible and initialize
475+
await delay(500)
476+
visibleProvider = ClineProvider.getVisibleInstance()
477+
478+
// If still not visible, wait a bit more as initialization might be slow
479+
if (!visibleProvider) {
480+
await delay(500)
481+
visibleProvider = ClineProvider.getVisibleInstance()
482+
}
483+
} catch (error) {
484+
console.error(`Failed to activate Roo Code sidebar: ${error}`)
485+
}
476486
}
477487

478-
// If still no visible provider, return
488+
// If still no visible provider, return undefined
479489
if (!visibleProvider) {
480-
return
490+
console.warn("Could not get or create a visible ClineProvider instance")
491+
return undefined
481492
}
482493

483494
return visibleProvider

0 commit comments

Comments
 (0)