forked from aws/aws-toolkit-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.ts
More file actions
178 lines (161 loc) · 6.1 KB
/
commands.ts
File metadata and controls
178 lines (161 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import * as vscode from 'vscode'
import { Commands, getLogger, messages } from 'aws-core-vscode/shared'
import { AutoDebugController } from './controller'
import { autoDebugTelemetry } from './telemetry'
/**
* Auto Debug commands for Amazon Q
* Handles all command registrations and implementations
*/
export class AutoDebugCommands implements vscode.Disposable {
private readonly logger = getLogger()
private readonly disposables: vscode.Disposable[] = []
private controller!: AutoDebugController
/**
* Register all auto debug commands
*/
registerCommands(context: vscode.ExtensionContext, controller: AutoDebugController): void {
this.controller = controller
this.disposables.push(
// Fix with Amazon Q command
Commands.register(
{
id: 'amazonq.01.fixWithQ',
name: 'Amazon Q: Fix Problem',
},
async (range?: vscode.Range, diagnostics?: vscode.Diagnostic[]) => {
await this.fixWithAmazonQ(range, diagnostics)
}
),
// Fix All with Amazon Q command
Commands.register(
{
id: 'amazonq.02.fixAllWithQ',
name: 'Amazon Q: Fix All Issues',
},
async (includeWarnings?: boolean) => {
await this.fixAllWithAmazonQ(includeWarnings)
}
),
// Explain Problem with Amazon Q command
Commands.register(
{
id: 'amazonq.03.explainProblem',
name: 'Amazon Q: Explain Problem',
},
async (range?: vscode.Range, diagnostics?: vscode.Diagnostic[]) => {
await this.explainProblem(range, diagnostics)
}
)
)
// Add all disposables to context
context.subscriptions.push(...this.disposables)
}
/**
* Generic error handling wrapper for command execution
*/
private async executeWithErrorHandling<T>(
action: () => Promise<T>,
errorMessage: string,
logContext: string
): Promise<T | void> {
try {
return await action()
} catch (error) {
this.logger.error(`AutoDebugCommands: Error in ${logContext}: %s`, error)
// Record telemetry failure based on context
const commandType =
logContext === 'fixWithAmazonQ'
? 'fixWithQ'
: logContext === 'fixAllWithAmazonQ'
? 'fixAllWithQ'
: 'explainProblem'
autoDebugTelemetry.recordCommandFailure(commandType, String(error))
void messages.showMessage('error', 'Amazon Q was not able to fix or explain the problem. Try again shortly')
}
}
/**
* Check if there's an active editor and log warning if not
*/
private checkActiveEditor(): vscode.TextEditor | undefined {
const editor = vscode.window.activeTextEditor
if (!editor) {
this.logger.warn('AutoDebugCommands: No active editor found')
}
return editor
}
/**
* Fix with Amazon Q - fixes only the specific issues the user selected
*/
private async fixWithAmazonQ(range?: vscode.Range, diagnostics?: vscode.Diagnostic[]): Promise<void> {
const problemCount = diagnostics?.length
autoDebugTelemetry.recordCommandInvocation('fixWithQ', problemCount)
await this.executeWithErrorHandling(
async () => {
const editor = this.checkActiveEditor()
if (!editor) {
return
}
const saved = await editor.document.save()
if (!saved) {
throw new Error('Failed to save document')
}
await this.controller.fixSpecificProblems(range, diagnostics)
autoDebugTelemetry.recordCommandSuccess('fixWithQ', problemCount)
},
'Fix with Amazon Q',
'fixWithAmazonQ'
)
}
/**
* Fix All with Amazon Q - processes issues in the current file
* @param includeWarnings - if true, fix errors and warnings; if false, fix only errors
*/
private async fixAllWithAmazonQ(includeWarnings: boolean = false): Promise<void> {
autoDebugTelemetry.recordCommandInvocation('fixAllWithQ')
await this.executeWithErrorHandling(
async () => {
const editor = this.checkActiveEditor()
if (!editor) {
return
}
const saved = await editor.document.save()
if (!saved) {
throw new Error('Failed to save document')
}
const problemCount = await this.controller.fixAllProblemsInFile(includeWarnings)
autoDebugTelemetry.recordCommandSuccess('fixAllWithQ', problemCount)
},
'Fix All with Amazon Q',
'fixAllWithAmazonQ'
)
}
/**
* Explains the problem using Amazon Q
*/
private async explainProblem(range?: vscode.Range, diagnostics?: vscode.Diagnostic[]): Promise<void> {
const problemCount = diagnostics?.length
autoDebugTelemetry.recordCommandInvocation('explainProblem', problemCount)
await this.executeWithErrorHandling(
async () => {
const editor = this.checkActiveEditor()
if (!editor) {
return
}
await this.controller.explainProblems(range, diagnostics)
autoDebugTelemetry.recordCommandSuccess('explainProblem', problemCount)
},
'Explain Problem',
'explainProblem'
)
}
/**
* Dispose of all resources
*/
dispose(): void {
vscode.Disposable.from(...this.disposables).dispose()
}
}