Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(amazonq): enable code actions for awesome lightbulb #5620

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "Feature",
"description": "feat(amazonq): enable code actions for awesome lightbulb"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for including a changelog. Please review the changelog guidelines. Is this the most meaningful thing we can say about this improvement for customers that will be interested in it?

}
8 changes: 8 additions & 0 deletions packages/amazonq/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@
{
"submenu": "amazonqEditorContextSubmenu",
"group": "cw_chat"
},
{
"command": "aws.amazonq.codeActions",
"when": "editorHasSelection && !editorReadonly && aws.codewhisperer.connected"
}
],
"aws.amazonq.submenu.feedback": [
Expand Down Expand Up @@ -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": [
Expand Down
67 changes: 67 additions & 0 deletions packages/amazonq/src/codeActions.ts
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice that there is a app/chat/ for application code for AmazonQ.

If this CodeActions module is generally useful for all Q features, it could live in packages/amazonq/src/util/ or packages/amazonq/src/shared/. If it's for a particular Q feature, it should live in app/<feature>/.

Or it could live in app/ directly.

The main point is that the top-level src/ directory isn't the right place for most code files.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Will review

public static readonly providedCodeActionKinds = [
vscode.CodeActionKind.QuickFix,
vscode.CodeActionKind.RefactorRewrite,
vscode.CodeActionKind.Empty,
]

private suffix: string = 'using Amazon Q'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be parametrized as well so that the code can be shared by Amazon Q and Toolkits.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a preference as to where to add consts in the source code?


private commands: Map<string, { title: string; kind: vscode.CodeActionKind }> = new Map<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we create an abstract class (or a common class) CodeActions in shared and pass the following commands during Amazon Q activation. That way, we can share most of the logic in Toolkits and the Q Extension.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. We can itemize as a fast follow

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
}
}
8 changes: 6 additions & 2 deletions packages/amazonq/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ 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'

/**
* Activation code for Amazon Q that will we want in all environments (eg Node.js, web mode)
*/
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())
Expand Down Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -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...",
Expand Down
Loading