-
Notifications
You must be signed in to change notification settings - Fork 796
Expand file tree
/
Copy pathcodeActionsProvider.ts
More file actions
128 lines (104 loc) · 4.3 KB
/
codeActionsProvider.ts
File metadata and controls
128 lines (104 loc) · 4.3 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
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import * as vscode from 'vscode'
/**
* Provides code actions for Amazon Q Auto Debug features.
* Integrates with VS Code's quick fix system to offer debugging assistance.
*/
export class AutoDebugCodeActionsProvider implements vscode.CodeActionProvider, vscode.Disposable {
private readonly disposables: vscode.Disposable[] = []
public static readonly providedCodeActionKinds = [vscode.CodeActionKind.QuickFix, vscode.CodeActionKind.Refactor]
constructor() {
this.registerProvider()
}
private registerProvider(): void {
// Register for all file types
const selector: vscode.DocumentSelector = [{ scheme: 'file' }]
this.disposables.push(
vscode.languages.registerCodeActionsProvider(selector, this, {
providedCodeActionKinds: AutoDebugCodeActionsProvider.providedCodeActionKinds,
})
)
}
/**
* Provides code actions for the given document and range
*/
public provideCodeActions(
document: vscode.TextDocument,
range: vscode.Range | vscode.Selection,
context: vscode.CodeActionContext,
token: vscode.CancellationToken
): vscode.ProviderResult<(vscode.CodeAction | vscode.Command)[]> {
if (token.isCancellationRequested) {
return []
}
const actions: vscode.CodeAction[] = []
// Get diagnostics for the current range
const diagnostics = context.diagnostics.filter(
(diagnostic) => diagnostic.range.intersection(range) !== undefined
)
if (diagnostics.length > 0) {
// Add "Fix with Amazon Q" action
actions.push(this.createFixWithQAction(document, range, diagnostics))
// Check if any diagnostic is error or warning to show "Fix All Issues"
const hasErrorOrWarning = diagnostics.some(
(d) =>
d.severity === vscode.DiagnosticSeverity.Error || d.severity === vscode.DiagnosticSeverity.Warning
)
if (hasErrorOrWarning) {
// If triggered from warning, include warnings; if from error, only errors
const hasWarning = diagnostics.some((d) => d.severity === vscode.DiagnosticSeverity.Warning)
actions.push(this.createFixAllWithQAction(document, hasWarning))
}
// Add "Explain Problem" action
actions.push(this.createExplainProblemAction(document, range, diagnostics))
}
return actions
}
private createFixWithQAction(
document: vscode.TextDocument,
range: vscode.Range,
diagnostics: vscode.Diagnostic[]
): vscode.CodeAction {
const action = new vscode.CodeAction(
`Amazon Q: Fix Problem (${diagnostics.length} issue${diagnostics.length !== 1 ? 's' : ''})`,
vscode.CodeActionKind.QuickFix
)
action.command = {
command: 'amazonq.01.fixWithQ',
title: 'Amazon Q: Fix Problem',
arguments: [range, diagnostics],
}
action.diagnostics = diagnostics
action.isPreferred = true // Make this the preferred quick fix
return action
}
private createFixAllWithQAction(document: vscode.TextDocument, includeWarnings: boolean): vscode.CodeAction {
const action = new vscode.CodeAction('Amazon Q: Fix All Issues', vscode.CodeActionKind.QuickFix)
action.command = {
command: 'amazonq.02.fixAllWithQ',
title: 'Amazon Q: Fix All Issues',
arguments: [includeWarnings],
}
return action
}
private createExplainProblemAction(
document: vscode.TextDocument,
range: vscode.Range,
diagnostics: vscode.Diagnostic[]
): vscode.CodeAction {
const action = new vscode.CodeAction('Amazon Q: Explain Problem', vscode.CodeActionKind.QuickFix)
action.command = {
command: 'amazonq.03.explainProblem',
title: 'Amazon Q: Explain Problem',
arguments: [range, diagnostics],
}
action.diagnostics = diagnostics
return action
}
public dispose(): void {
vscode.Disposable.from(...this.disposables).dispose()
}
}