|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import * as vscode from 'vscode' |
| 7 | + |
| 8 | +/** |
| 9 | + * Provides code actions for Amazon Q Auto Debug features. |
| 10 | + * Integrates with VS Code's quick fix system to offer debugging assistance. |
| 11 | + */ |
| 12 | +export class AutoDebugCodeActionsProvider implements vscode.CodeActionProvider, vscode.Disposable { |
| 13 | + private readonly disposables: vscode.Disposable[] = [] |
| 14 | + |
| 15 | + public static readonly providedCodeActionKinds = [vscode.CodeActionKind.QuickFix, vscode.CodeActionKind.Refactor] |
| 16 | + |
| 17 | + constructor() { |
| 18 | + this.registerProvider() |
| 19 | + } |
| 20 | + |
| 21 | + private registerProvider(): void { |
| 22 | + // Register for all file types |
| 23 | + const selector: vscode.DocumentSelector = [{ scheme: 'file' }] |
| 24 | + |
| 25 | + this.disposables.push( |
| 26 | + vscode.languages.registerCodeActionsProvider(selector, this, { |
| 27 | + providedCodeActionKinds: AutoDebugCodeActionsProvider.providedCodeActionKinds, |
| 28 | + }) |
| 29 | + ) |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Provides code actions for the given document and range |
| 34 | + */ |
| 35 | + public provideCodeActions( |
| 36 | + document: vscode.TextDocument, |
| 37 | + range: vscode.Range | vscode.Selection, |
| 38 | + context: vscode.CodeActionContext, |
| 39 | + token: vscode.CancellationToken |
| 40 | + ): vscode.ProviderResult<(vscode.CodeAction | vscode.Command)[]> { |
| 41 | + if (token.isCancellationRequested) { |
| 42 | + return [] |
| 43 | + } |
| 44 | + |
| 45 | + const actions: vscode.CodeAction[] = [] |
| 46 | + |
| 47 | + // Get diagnostics for the current range |
| 48 | + const diagnostics = context.diagnostics.filter( |
| 49 | + (diagnostic) => diagnostic.range.intersection(range) !== undefined |
| 50 | + ) |
| 51 | + |
| 52 | + if (diagnostics.length > 0) { |
| 53 | + // Add "Fix with Amazon Q" action |
| 54 | + actions.push(this.createFixWithQAction(document, range, diagnostics)) |
| 55 | + |
| 56 | + // Add "Fix All with Amazon Q" action |
| 57 | + actions.push(this.createFixAllWithQAction(document)) |
| 58 | + |
| 59 | + // Add "Explain Problem" action |
| 60 | + actions.push(this.createExplainProblemAction(document, range, diagnostics)) |
| 61 | + } |
| 62 | + return actions |
| 63 | + } |
| 64 | + |
| 65 | + private createFixWithQAction( |
| 66 | + document: vscode.TextDocument, |
| 67 | + range: vscode.Range, |
| 68 | + diagnostics: vscode.Diagnostic[] |
| 69 | + ): vscode.CodeAction { |
| 70 | + const action = new vscode.CodeAction( |
| 71 | + `Amazon Q: Fix Problem (${diagnostics.length} issue${diagnostics.length !== 1 ? 's' : ''})`, |
| 72 | + vscode.CodeActionKind.QuickFix |
| 73 | + ) |
| 74 | + |
| 75 | + action.command = { |
| 76 | + command: 'amazonq.01.fixWithQ', |
| 77 | + title: 'Amazon Q: Fix Problem', |
| 78 | + arguments: [range, diagnostics], |
| 79 | + } |
| 80 | + |
| 81 | + action.diagnostics = diagnostics |
| 82 | + action.isPreferred = true // Make this the preferred quick fix |
| 83 | + |
| 84 | + return action |
| 85 | + } |
| 86 | + |
| 87 | + private createFixAllWithQAction(document: vscode.TextDocument): vscode.CodeAction { |
| 88 | + const action = new vscode.CodeAction('Amazon Q: Fix All Errors', vscode.CodeActionKind.QuickFix) |
| 89 | + |
| 90 | + action.command = { |
| 91 | + command: 'amazonq.02.fixAllWithQ', |
| 92 | + title: 'Amazon Q: Fix All Errors', |
| 93 | + } |
| 94 | + |
| 95 | + return action |
| 96 | + } |
| 97 | + |
| 98 | + private createExplainProblemAction( |
| 99 | + document: vscode.TextDocument, |
| 100 | + range: vscode.Range, |
| 101 | + diagnostics: vscode.Diagnostic[] |
| 102 | + ): vscode.CodeAction { |
| 103 | + const action = new vscode.CodeAction('Amazon Q: Explain Problem', vscode.CodeActionKind.QuickFix) |
| 104 | + |
| 105 | + action.command = { |
| 106 | + command: 'amazonq.03.explainProblem', |
| 107 | + title: 'Amazon Q: Explain Problem', |
| 108 | + arguments: [range, diagnostics], |
| 109 | + } |
| 110 | + |
| 111 | + action.diagnostics = diagnostics |
| 112 | + |
| 113 | + return action |
| 114 | + } |
| 115 | + |
| 116 | + public dispose(): void { |
| 117 | + vscode.Disposable.from(...this.disposables).dispose() |
| 118 | + } |
| 119 | +} |
0 commit comments