Skip to content

Commit 0c32ef8

Browse files
committed
fix the activation bug and simplify codes in commands.ts
1 parent 991bb70 commit 0c32ef8

File tree

4 files changed

+87
-44
lines changed

4 files changed

+87
-44
lines changed

packages/amazonq/src/extension.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import { registerCommands } from './commands'
4545
import { focusAmazonQPanel } from 'aws-core-vscode/codewhispererChat'
4646
import { activate as activateAmazonqLsp } from './lsp/activation'
4747
import { hasGlibcPatch } from './lsp/client'
48+
import { activateAutoDebug } from './lsp/autoDebug/activation'
4849

4950
export const amazonQContextPrefix = 'amazonq'
5051

@@ -131,6 +132,14 @@ export async function activateAmazonQCommon(context: vscode.ExtensionContext, is
131132
await activateAmazonqLsp(context)
132133
}
133134

135+
// Activate AutoDebug feature at extension level
136+
try {
137+
const autoDebugFeature = await activateAutoDebug(context)
138+
context.subscriptions.push(autoDebugFeature)
139+
} catch (error) {
140+
getLogger().error('Failed to activate AutoDebug feature at extension level: %s', error)
141+
}
142+
134143
// Generic extension commands
135144
registerGenericCommands(context, amazonQContextPrefix)
136145

packages/amazonq/src/lsp/autoDebug/commands.ts

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -61,56 +61,81 @@ export class AutoDebugCommands implements vscode.Disposable {
6161
}
6262

6363
/**
64-
* Fix with Amazon Q - fixes only the specific issues the user selected
64+
* Generic error handling wrapper for command execution
6565
*/
66-
private async fixWithAmazonQ(range?: vscode.Range, diagnostics?: vscode.Diagnostic[]): Promise<void> {
66+
private async executeWithErrorHandling<T>(
67+
action: () => Promise<T>,
68+
errorMessage: string,
69+
logContext: string
70+
): Promise<T | void> {
6771
try {
68-
const editor = vscode.window.activeTextEditor
69-
if (!editor) {
70-
return
71-
}
72-
73-
// Controller handles panel focusing and message sending
74-
await this.controller.fixSpecificProblems(range, diagnostics)
72+
return await action()
7573
} catch (error) {
76-
this.logger.error('AutoDebugCommands: Error in Fix with Amazon Q: %s', error)
74+
this.logger.error(`AutoDebugCommands: Error in ${logContext}: %s`, error)
7775
void messages.showMessage('error', 'Amazon Q was not able to fix or explain the problem. Try again shortly')
7876
}
7977
}
8078

79+
/**
80+
* Check if there's an active editor and log warning if not
81+
*/
82+
private checkActiveEditor(): vscode.TextEditor | undefined {
83+
const editor = vscode.window.activeTextEditor
84+
if (!editor) {
85+
this.logger.warn('AutoDebugCommands: No active editor found')
86+
}
87+
return editor
88+
}
89+
90+
/**
91+
* Fix with Amazon Q - fixes only the specific issues the user selected
92+
*/
93+
private async fixWithAmazonQ(range?: vscode.Range, diagnostics?: vscode.Diagnostic[]): Promise<void> {
94+
await this.executeWithErrorHandling(
95+
async () => {
96+
const editor = this.checkActiveEditor()
97+
if (!editor) {
98+
return
99+
}
100+
await this.controller.fixSpecificProblems(range, diagnostics)
101+
},
102+
'Fix with Amazon Q',
103+
'fixWithAmazonQ'
104+
)
105+
}
106+
81107
/**
82108
* Fix All with Amazon Q - processes all errors in the current file
83109
*/
84110
private async fixAllWithAmazonQ(): Promise<void> {
85-
try {
86-
const editor = vscode.window.activeTextEditor
87-
if (!editor) {
88-
return
89-
}
90-
91-
await this.controller.fixAllProblemsInFile(10) // 10 errors per batch
92-
} catch (error) {
93-
this.logger.error('AutoDebugCommands: Error in Fix All with Amazon Q: %s', error)
94-
void messages.showMessage('error', 'Amazon Q was not able to fix or explain the problem. Try again shortly')
95-
}
111+
await this.executeWithErrorHandling(
112+
async () => {
113+
const editor = this.checkActiveEditor()
114+
if (!editor) {
115+
return
116+
}
117+
await this.controller.fixAllProblemsInFile(10) // 10 errors per batch
118+
},
119+
'Fix All with Amazon Q',
120+
'fixAllWithAmazonQ'
121+
)
96122
}
97123

98124
/**
99125
* Explains the problem using Amazon Q
100126
*/
101127
private async explainProblem(range?: vscode.Range, diagnostics?: vscode.Diagnostic[]): Promise<void> {
102-
try {
103-
const editor = vscode.window.activeTextEditor
104-
if (!editor) {
105-
this.logger.warn('AutoDebugCommands: No active editor for explainProblem')
106-
return
107-
}
108-
109-
await this.controller.explainProblems(range, diagnostics)
110-
} catch (error) {
111-
this.logger.error('AutoDebugCommands: Error explaining problem: %s', error)
112-
void messages.showMessage('error', 'Amazon Q was not able to fix or explain the problem. Try again shortly')
113-
}
128+
await this.executeWithErrorHandling(
129+
async () => {
130+
const editor = this.checkActiveEditor()
131+
if (!editor) {
132+
return
133+
}
134+
await this.controller.explainProblems(range, diagnostics)
135+
},
136+
'Explain Problem',
137+
'explainProblem'
138+
)
114139
}
115140

116141
/**

packages/amazonq/src/lsp/autoDebug/lsp/autoDebugLspClient.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5-
65
import { getLogger, placeholder } from 'aws-core-vscode/shared'
76
import { focusAmazonQPanel } from 'aws-core-vscode/codewhispererChat'
87

@@ -19,11 +18,16 @@ export class AutoDebugLspClient {
1918

2019
public async sendChatMessage(params: { message: string; triggerType: string; eventId: string }): Promise<boolean> {
2120
try {
21+
// Ensure the chat view provider and webview are available
22+
await this.ensureWebviewReady()
23+
2224
// Get the webview provider from the static reference
2325
const amazonQChatViewProvider = AutoDebugLspClient.chatViewProvider
2426

2527
if (!amazonQChatViewProvider?.webview) {
26-
this.logger.error('AutoDebugLspClient: Amazon Q Chat View Provider not available')
28+
this.logger.error(
29+
'AutoDebugLspClient: Amazon Q Chat View Provider webview not available after initialization'
30+
)
2731
return false
2832
}
2933

@@ -50,4 +54,18 @@ export class AutoDebugLspClient {
5054
return false
5155
}
5256
}
57+
58+
/**
59+
* Ensures that the chat view provider and its webview are ready for use
60+
*/
61+
private async ensureWebviewReady(): Promise<void> {
62+
if (!AutoDebugLspClient.chatViewProvider) {
63+
await focusAmazonQPanel.execute(placeholder, 'autoDebug')
64+
}
65+
66+
// Now ensure the webview is created
67+
if (!AutoDebugLspClient.chatViewProvider.webview) {
68+
await focusAmazonQPanel.execute(placeholder, 'autoDebug')
69+
}
70+
}
5371
}

packages/amazonq/src/lsp/client.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ import { LineTracker } from '../app/inline/stateTracker/lineTracker'
5353
import { InlineTutorialAnnotation } from '../app/inline/tutorials/inlineTutorialAnnotation'
5454
import { InlineChatTutorialAnnotation } from '../app/inline/tutorials/inlineChatTutorialAnnotation'
5555
import { codeReviewInChat } from '../app/amazonqScan/models/constants'
56-
import { activateAutoDebug } from './autoDebug/activation'
5756

5857
const localize = nls.loadMessageBundle()
5958
const logger = getLogger('amazonqLsp.lspClient')
@@ -343,14 +342,6 @@ async function onLanguageServerReady(
343342
await activate(client, encryptionKey, resourcePaths.ui)
344343
}
345344

346-
// Activate AutoDebug feature with direct LSP client connection
347-
try {
348-
const autoDebugFeature = await activateAutoDebug(extensionContext, client, encryptionKey)
349-
toDispose.push(autoDebugFeature)
350-
} catch (error) {
351-
getLogger('amazonqLsp').error('Failed to activate AutoDebug feature: %s', error)
352-
}
353-
354345
const refreshInterval = auth.startTokenRefreshInterval(10 * oneSecond)
355346

356347
// We manually push the cached values the first time since event handlers, which should push, may not have been setup yet.

0 commit comments

Comments
 (0)