From 782fcd9f67036d2fcedd25e517f31ae1ba67b3bc Mon Sep 17 00:00:00 2001 From: eugene yevhen andruszczenko Date: Tue, 17 Sep 2024 17:43:57 -0400 Subject: [PATCH 1/2] feat(amazonq): enable code actions for awesome lightbulb --- packages/amazonq/package.json | 8 ++++ packages/amazonq/src/codeActions.ts | 67 +++++++++++++++++++++++++++++ packages/amazonq/src/extension.ts | 8 +++- packages/core/package.nls.json | 1 + 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 packages/amazonq/src/codeActions.ts diff --git a/packages/amazonq/package.json b/packages/amazonq/package.json index 3848f5352cd..b82ea9bdeb0 100644 --- a/packages/amazonq/package.json +++ b/packages/amazonq/package.json @@ -358,6 +358,10 @@ { "submenu": "amazonqEditorContextSubmenu", "group": "cw_chat" + }, + { + "command": "aws.amazonq.codeActions", + "when": "editorHasSelection && !editorReadonly && aws.codewhisperer.connected" } ], "aws.amazonq.submenu.feedback": [ @@ -560,6 +564,10 @@ "title": "%AWS.amazonq.toggleCodeScan%", "category": "%AWS.amazonq.title%", "enablement": "aws.codewhisperer.connected" + }, + { + "command": "aws.amazonq.codeActions", + "title": "%AWS.command.amazonq.codeActions%" } ], "keybindings": [ diff --git a/packages/amazonq/src/codeActions.ts b/packages/amazonq/src/codeActions.ts new file mode 100644 index 00000000000..3355997f3d9 --- /dev/null +++ b/packages/amazonq/src/codeActions.ts @@ -0,0 +1,67 @@ +/*! + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +import * as vscode from 'vscode' + +export class CodeActions implements vscode.CodeActionProvider { + public static readonly providedCodeActionKinds = [ + vscode.CodeActionKind.QuickFix, + vscode.CodeActionKind.RefactorRewrite, + vscode.CodeActionKind.Empty, + ] + + private suffix: string = 'using Amazon Q' + + private commands: Map = new Map< + string, + { title: string; kind: vscode.CodeActionKind } + >([ + ['aws.amazonq.explainCode', { title: `Explain ${this.suffix}`, kind: vscode.CodeActionKind.Empty }], + ['aws.amazonq.refactorCode', { title: `Refactor ${this.suffix}`, kind: vscode.CodeActionKind.RefactorRewrite }], + ['aws.amazonq.fixCode', { title: `Fix ${this.suffix}`, kind: vscode.CodeActionKind.QuickFix }], + ['aws.amazonq.optimizeCode', { title: `Optimize ${this.suffix}`, kind: vscode.CodeActionKind.RefactorRewrite }], + [ + 'aws.amazonq.sendToPrompt', + { title: `Add selection to ${this.suffix} prompt`, kind: vscode.CodeActionKind.Empty }, + ], + ]) + + provideCodeActions(): vscode.CodeAction[] { + const editor = vscode.window.activeTextEditor + if (!editor) { + return [] + } + + const selectedText = editor.document.getText(editor.selection) + if (!selectedText) { + return [] + } + + const codeActions: vscode.CodeAction[] = [] + + this.commands.forEach(({ title, kind }, command) => { + codeActions.push(this.createCodeAction(command, title, kind, selectedText)) + }) + + return codeActions + } + + private createCodeAction( + command: string, + title: string, + kind: vscode.CodeActionKind, + selectedText: string, + isPreferred = false + ): vscode.CodeAction { + const action = new vscode.CodeAction(title, kind) + action.command = { + command, + title, + arguments: [selectedText], + } + action.isPreferred = isPreferred + return action + } +} diff --git a/packages/amazonq/src/extension.ts b/packages/amazonq/src/extension.ts index 095cc4b3df4..25555aaf487 100644 --- a/packages/amazonq/src/extension.ts +++ b/packages/amazonq/src/extension.ts @@ -48,7 +48,7 @@ import * as semver from 'semver' import * as vscode from 'vscode' import { registerCommands } from './commands' import { focusAmazonQPanel } from 'aws-core-vscode/codewhispererChat' - +import { CodeActions } from './codeActions' export const amazonQContextPrefix = 'amazonq' /** @@ -56,7 +56,7 @@ export const amazonQContextPrefix = 'amazonq' */ export async function activateAmazonQCommon(context: vscode.ExtensionContext, isWeb: boolean) { initialize(context, isWeb) - const homeDirLogs = await fs.init(context, (homeDir) => { + const homeDirLogs = await fs.init(context, (homeDir: string) => { void messages.showViewLogsMessage(`Invalid home directory (check $HOME): "${homeDir}"`) }) errors.init(fs.getUsername(), env.isAutomation()) @@ -193,6 +193,10 @@ export async function activateAmazonQCommon(context: vscode.ExtensionContext, is }) }) .catch((err) => getLogger().error('Error collecting telemetry for auth_userState: %s', err)) + + vscode.languages.registerCodeActionsProvider({ scheme: 'file' }, new CodeActions(), { + providedCodeActionKinds: CodeActions.providedCodeActionKinds, + }) } export async function deactivateCommon() { diff --git a/packages/core/package.nls.json b/packages/core/package.nls.json index 125b3fc6056..d226fa27088 100644 --- a/packages/core/package.nls.json +++ b/packages/core/package.nls.json @@ -112,6 +112,7 @@ "AWS.command.amazonq.optimizeCode": "Optimize", "AWS.command.amazonq.sendToPrompt": "Send to prompt", "AWS.command.amazonq.security.scan": "Run Project Scan", + "AWS.command.amazonq.codeActions": "Code Actions", "AWS.command.deploySamApplication": "Deploy SAM Application", "AWS.command.aboutToolkit": "About", "AWS.command.downloadLambda": "Download...", From cb6bd3006eeeaba061c51b6a30e032232402b6c1 Mon Sep 17 00:00:00 2001 From: eugene yevhen andruszczenko Date: Wed, 18 Sep 2024 09:50:28 -0400 Subject: [PATCH 2/2] feat(amazonq): enable code actions for awesome lightbulb --- .../Feature-b771fe9a-7f29-47b0-b39c-1282224069dd.json | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 packages/amazonq/.changes/next-release/Feature-b771fe9a-7f29-47b0-b39c-1282224069dd.json diff --git a/packages/amazonq/.changes/next-release/Feature-b771fe9a-7f29-47b0-b39c-1282224069dd.json b/packages/amazonq/.changes/next-release/Feature-b771fe9a-7f29-47b0-b39c-1282224069dd.json new file mode 100644 index 00000000000..e71ef899992 --- /dev/null +++ b/packages/amazonq/.changes/next-release/Feature-b771fe9a-7f29-47b0-b39c-1282224069dd.json @@ -0,0 +1,4 @@ +{ + "type": "Feature", + "description": "feat(amazonq): enable code actions for awesome lightbulb" +}