Skip to content

Commit 4ceed6e

Browse files
authored
fix(amazonq): handle explain, refactor, fix, optimize, sendToPrompt (#7169)
## Problem explain, refactor, fix, optimize right click menus aren't working ## Solution re-enable them through flare's genericCommand message ## TODO - handle generateUnitTests - handle explainIssue both of these need flare API changes to send generic prompts from the frontend to the backend --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent be976e1 commit 4ceed6e

File tree

3 files changed

+49
-110
lines changed

3 files changed

+49
-110
lines changed

packages/amazonq/src/lsp/chat/commands.ts

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import { Commands, globals } from 'aws-core-vscode/shared'
7-
// import { window } from 'vscode'
7+
import { window } from 'vscode'
88
import { AmazonQChatViewProvider } from './webviewProvider'
99

1010
/**
@@ -13,21 +13,21 @@ import { AmazonQChatViewProvider } from './webviewProvider'
1313
*/
1414
export function registerCommands(provider: AmazonQChatViewProvider) {
1515
globals.context.subscriptions.push(
16-
// registerGenericCommand('aws.amazonq.explainCode', 'Explain', provider),
17-
// registerGenericCommand('aws.amazonq.refactorCode', 'Refactor', provider),
18-
// registerGenericCommand('aws.amazonq.fixCode', 'Fix', provider),
19-
// registerGenericCommand('aws.amazonq.optimizeCode', 'Optimize', provider),
20-
// Commands.register('aws.amazonq.sendToPrompt', (data) => {
21-
// const triggerType = getCommandTriggerType(data)
22-
// const selection = getSelectedText()
16+
registerGenericCommand('aws.amazonq.explainCode', 'Explain', provider),
17+
registerGenericCommand('aws.amazonq.refactorCode', 'Refactor', provider),
18+
registerGenericCommand('aws.amazonq.fixCode', 'Fix', provider),
19+
registerGenericCommand('aws.amazonq.optimizeCode', 'Optimize', provider),
20+
Commands.register('aws.amazonq.sendToPrompt', (data) => {
21+
const triggerType = getCommandTriggerType(data)
22+
const selection = getSelectedText()
2323

24-
// void focusAmazonQPanel().then(() => {
25-
// void provider.webview?.postMessage({
26-
// command: 'sendToPrompt',
27-
// params: { selection: selection, triggerType },
28-
// })
29-
// })
30-
// }),
24+
void focusAmazonQPanel().then(() => {
25+
void provider.webview?.postMessage({
26+
command: 'sendToPrompt',
27+
params: { selection: selection, triggerType },
28+
})
29+
})
30+
}),
3131
Commands.register('aws.amazonq.openTab', () => {
3232
void focusAmazonQPanel().then(() => {
3333
void provider.webview?.postMessage({
@@ -39,36 +39,36 @@ export function registerCommands(provider: AmazonQChatViewProvider) {
3939
)
4040
}
4141

42-
// function getSelectedText(): string {
43-
// const editor = window.activeTextEditor
44-
// if (editor) {
45-
// const selection = editor.selection
46-
// const selectedText = editor.document.getText(selection)
47-
// return selectedText
48-
// }
42+
function getSelectedText(): string {
43+
const editor = window.activeTextEditor
44+
if (editor) {
45+
const selection = editor.selection
46+
const selectedText = editor.document.getText(selection)
47+
return selectedText
48+
}
4949

50-
// return ' '
51-
// }
50+
return ' '
51+
}
5252

53-
// function getCommandTriggerType(data: any): string {
54-
// // data is undefined when commands triggered from keybinding or command palette. Currently no
55-
// // way to differentiate keybinding and command palette, so both interactions are recorded as keybinding
56-
// return data === undefined ? 'hotkeys' : 'contextMenu'
57-
// }
53+
function getCommandTriggerType(data: any): string {
54+
// data is undefined when commands triggered from keybinding or command palette. Currently no
55+
// way to differentiate keybinding and command palette, so both interactions are recorded as keybinding
56+
return data === undefined ? 'hotkeys' : 'contextMenu'
57+
}
5858

59-
// function registerGenericCommand(commandName: string, genericCommand: string, provider: AmazonQChatViewProvider) {
60-
// return Commands.register(commandName, (data) => {
61-
// const triggerType = getCommandTriggerType(data)
62-
// const selection = getSelectedText()
59+
function registerGenericCommand(commandName: string, genericCommand: string, provider: AmazonQChatViewProvider) {
60+
return Commands.register(commandName, (data) => {
61+
const triggerType = getCommandTriggerType(data)
62+
const selection = getSelectedText()
6363

64-
// void focusAmazonQPanel().then(() => {
65-
// void provider.webview?.postMessage({
66-
// command: 'genericCommand',
67-
// params: { genericCommand, selection, triggerType },
68-
// })
69-
// })
70-
// })
71-
// }
64+
void focusAmazonQPanel().then(() => {
65+
void provider.webview?.postMessage({
66+
command: 'genericCommand',
67+
params: { genericCommand, selection, triggerType },
68+
})
69+
})
70+
})
71+
}
7272

7373
/**
7474
* Importing focusAmazonQPanel from aws-core-vscode/amazonq leads to several dependencies down the chain not resolving since AmazonQ chat

packages/core/src/codewhispererChat/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,5 +236,5 @@ export function init(appContext: AmazonQAppInitContext) {
236236

237237
appContext.registerWebViewToAppMessagePublisher(new MessagePublisher<any>(cwChatUIInputEventEmitter), 'cwc')
238238

239-
registerCommands(cwChatControllerMessagePublishers)
239+
registerCommands()
240240
}

packages/core/src/codewhispererChat/commands/registerCommands.ts

Lines changed: 7 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import { commandPalette } from '../../codewhisperer/commands/types'
77
import { CodeScanIssue } from '../../codewhisperer/models/model'
88
import { Commands, VsCodeCommandArg, placeholder } from '../../shared/vscode/commands2'
9-
import { ChatControllerMessagePublishers } from '../controllers/chat/controller'
109

1110
/**
1211
* Opens the Amazon Q panel, showing the correct View that should
@@ -37,73 +36,13 @@ export const focusAmazonQPanelKeybinding = Commands.declare('_aws.amazonq.focusC
3736
await focusAmazonQPanel.execute(placeholder, 'keybinding')
3837
})
3938

40-
const getCommandTriggerType = (data: any): EditorContextCommandTriggerType => {
41-
// data is undefined when commands triggered from keybinding or command palette. Currently no
42-
// way to differentiate keybinding and command palette, so both interactions are recorded as keybinding
43-
return data === undefined ? 'keybinding' : 'contextMenu'
44-
}
45-
46-
export function registerCommands(controllerPublishers: ChatControllerMessagePublishers) {
47-
Commands.register('aws.amazonq.explainCode', async (data) => {
48-
return focusAmazonQPanel.execute(placeholder, 'amazonq.explainCode').then(() => {
49-
controllerPublishers.processContextMenuCommand.publish({
50-
type: 'aws.amazonq.explainCode',
51-
triggerType: getCommandTriggerType(data),
52-
})
53-
})
54-
})
55-
Commands.register('aws.amazonq.refactorCode', async (data) => {
56-
return focusAmazonQPanel.execute(placeholder, 'amazonq.refactorCode').then(() => {
57-
controllerPublishers.processContextMenuCommand.publish({
58-
type: 'aws.amazonq.refactorCode',
59-
triggerType: getCommandTriggerType(data),
60-
})
61-
})
62-
})
63-
Commands.register('aws.amazonq.fixCode', async (data) => {
64-
return focusAmazonQPanel.execute(placeholder, 'amazonq.fixCode').then(() => {
65-
controllerPublishers.processContextMenuCommand.publish({
66-
type: 'aws.amazonq.fixCode',
67-
triggerType: getCommandTriggerType(data),
68-
})
69-
})
70-
})
71-
Commands.register('aws.amazonq.optimizeCode', async (data) => {
72-
return focusAmazonQPanel.execute(placeholder, 'amazonq.optimizeCode').then(() => {
73-
controllerPublishers.processContextMenuCommand.publish({
74-
type: 'aws.amazonq.optimizeCode',
75-
triggerType: getCommandTriggerType(data),
76-
})
77-
})
78-
})
79-
Commands.register('aws.amazonq.sendToPrompt', async (data) => {
80-
return focusAmazonQPanel.execute(placeholder, 'amazonq.sendToPrompt').then(() => {
81-
controllerPublishers.processContextMenuCommand.publish({
82-
type: 'aws.amazonq.sendToPrompt',
83-
triggerType: getCommandTriggerType(data),
84-
})
85-
})
86-
})
87-
Commands.register('aws.amazonq.explainIssue', async (issue) => {
88-
return focusAmazonQPanel.execute(placeholder, 'amazonq.explainIssue').then(() => {
89-
controllerPublishers.processContextMenuCommand.publish({
90-
type: 'aws.amazonq.explainIssue',
91-
triggerType: 'click',
92-
issue,
93-
})
94-
})
95-
})
96-
Commands.register('aws.amazonq.generateUnitTests', async (data) => {
97-
return focusAmazonQPanel.execute(placeholder, 'amazonq.generateUnitTests').then(() => {
98-
controllerPublishers.processContextMenuCommand.publish({
99-
type: 'aws.amazonq.generateUnitTests',
100-
triggerType: getCommandTriggerType(data),
101-
})
102-
})
103-
})
104-
Commands.register('aws.amazonq.updateContextCommandItems', () => {
105-
controllerPublishers.processContextCommandUpdateMessage.publish()
106-
})
39+
export function registerCommands() {
40+
/**
41+
* make these no-ops, since theres still callers that need to be deprecated
42+
*/
43+
Commands.register('aws.amazonq.explainIssue', async (issue) => {})
44+
Commands.register('aws.amazonq.generateUnitTests', async (data) => {})
45+
Commands.register('aws.amazonq.updateContextCommandItems', () => {})
10746
}
10847

10948
export type EditorContextBaseCommandType =

0 commit comments

Comments
 (0)