Skip to content

Commit b4b7d17

Browse files
committed
fix(amazonq): Register invokeInlineCompletion and rejectCodeSuggestion with lsp implementation
1 parent f93a6bf commit b4b7d17

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

packages/amazonq/src/lsp/activation.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@
66
import vscode from 'vscode'
77
import { startLanguageServer } from './client'
88
import { AmazonQLSPResolver } from './lspInstaller'
9-
import { ToolkitError } from 'aws-core-vscode/shared'
9+
import { Commands, ToolkitError } from 'aws-core-vscode/shared'
1010

1111
export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
1212
try {
1313
const installResult = await new AmazonQLSPResolver().resolve()
1414
await startLanguageServer(ctx, installResult.resourcePaths)
15+
ctx.subscriptions.push(
16+
Commands.register({ id: 'aws.amazonq.invokeInlineCompletion', autoconnect: true }, async () => {
17+
await vscode.commands.executeCommand('editor.action.inlineSuggest.trigger')
18+
}),
19+
Commands.declare('aws.amazonq.rejectCodeSuggestion', () => async () => {
20+
await vscode.commands.executeCommand('editor.action.inlineSuggest.hide')
21+
}).register()
22+
)
1523
} catch (err) {
1624
const e = err as ToolkitError
1725
void vscode.window.showInformationMessage(`Unable to launch amazonq language server: ${e.message}`)

packages/core/src/codewhisperer/service/recommendationHandler.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,38 @@ import { UserWrittenCodeTracker } from '../tracker/userWrittenCodeTracker'
5050
* It does not contain UI/UX related logic
5151
*/
5252

53-
// below commands override VS Code inline completion commands
54-
const prevCommand = Commands.declare('editor.action.inlineSuggest.showPrevious', () => async () => {
55-
await RecommendationHandler.instance.showRecommendation(-1)
56-
})
57-
const nextCommand = Commands.declare('editor.action.inlineSuggest.showNext', () => async () => {
58-
await RecommendationHandler.instance.showRecommendation(1)
59-
})
60-
61-
const rejectCommand = Commands.declare('aws.amazonq.rejectCodeSuggestion', () => async () => {
62-
telemetry.record({
63-
traceId: TelemetryHelper.instance.traceId,
53+
/**
54+
* Commands as a level of indirection so that declare doesn't intercept any registrations for the
55+
* language server implementation.
56+
*
57+
* Otherwise you'll get:
58+
* "Unable to launch amazonq language server: Command "aws.amazonq.rejectCodeSuggestion" has already been declared by the Toolkit"
59+
*/
60+
function createCommands() {
61+
// below commands override VS Code inline completion commands
62+
const prevCommand = Commands.declare('editor.action.inlineSuggest.showPrevious', () => async () => {
63+
await RecommendationHandler.instance.showRecommendation(-1)
64+
})
65+
const nextCommand = Commands.declare('editor.action.inlineSuggest.showNext', () => async () => {
66+
await RecommendationHandler.instance.showRecommendation(1)
6467
})
6568

66-
await vscode.commands.executeCommand('editor.action.inlineSuggest.hide')
67-
RecommendationHandler.instance.reportUserDecisions(-1)
68-
await Commands.tryExecute('aws.amazonq.refreshAnnotation')
69-
})
69+
const rejectCommand = Commands.declare('aws.amazonq.rejectCodeSuggestion', () => async () => {
70+
telemetry.record({
71+
traceId: TelemetryHelper.instance.traceId,
72+
})
73+
74+
await vscode.commands.executeCommand('editor.action.inlineSuggest.hide')
75+
RecommendationHandler.instance.reportUserDecisions(-1)
76+
await Commands.tryExecute('aws.amazonq.refreshAnnotation')
77+
})
78+
79+
return {
80+
prevCommand,
81+
nextCommand,
82+
rejectCommand,
83+
}
84+
}
7085

7186
const lock = new AsyncLock({ maxPending: 1 })
7287

@@ -579,6 +594,7 @@ export class RecommendationHandler {
579594
// They are subscribed when suggestion starts and disposed when suggestion is accepted/rejected
580595
// to avoid impacting other plugins or user who uses this API
581596
private registerCommandOverrides() {
597+
const { prevCommand, nextCommand, rejectCommand } = createCommands()
582598
this.prev = prevCommand.register()
583599
this.next = nextCommand.register()
584600
this.reject = rejectCommand.register()

0 commit comments

Comments
 (0)