Skip to content

Commit 0102385

Browse files
authored
feat(amazonq): Add separate activation code path (#7980)
## Problem ## Solution --- - 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 1f691a8 commit 0102385

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed

packages/amazonq/src/app/inline/activation.ts

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,68 @@
66
import vscode from 'vscode'
77
import {
88
AuthUtil,
9+
CodeSuggestionsState,
10+
CodeWhispererCodeCoverageTracker,
911
CodeWhispererConstants,
12+
CodeWhispererSettings,
13+
ConfigurationEntry,
14+
DefaultCodeWhispererClient,
15+
invokeRecommendation,
1016
isInlineCompletionEnabled,
17+
KeyStrokeHandler,
18+
RecommendationHandler,
1119
runtimeLanguageContext,
20+
TelemetryHelper,
1221
UserWrittenCodeTracker,
1322
vsCodeState,
1423
} from 'aws-core-vscode/codewhisperer'
15-
import { globals, sleep } from 'aws-core-vscode/shared'
24+
import { Commands, getLogger, globals, sleep } from 'aws-core-vscode/shared'
1625

1726
export async function activate() {
18-
if (isInlineCompletionEnabled()) {
19-
// Debugging purpose: only initialize NextEditPredictionPanel when development
20-
// NextEditPredictionPanel.getInstance()
27+
const codewhispererSettings = CodeWhispererSettings.instance
28+
const client = new DefaultCodeWhispererClient()
2129

30+
if (isInlineCompletionEnabled()) {
2231
await setSubscriptionsforInlineCompletion()
2332
await AuthUtil.instance.setVscodeContextProps()
2433
}
2534

35+
function getAutoTriggerStatus(): boolean {
36+
return CodeSuggestionsState.instance.isSuggestionsEnabled()
37+
}
38+
39+
async function getConfigEntry(): Promise<ConfigurationEntry> {
40+
const isShowMethodsEnabled: boolean =
41+
vscode.workspace.getConfiguration('editor').get('suggest.showMethods') || false
42+
const isAutomatedTriggerEnabled: boolean = getAutoTriggerStatus()
43+
const isManualTriggerEnabled: boolean = true
44+
const isSuggestionsWithCodeReferencesEnabled = codewhispererSettings.isSuggestionsWithCodeReferencesEnabled()
45+
46+
// TODO:remove isManualTriggerEnabled
47+
return {
48+
isShowMethodsEnabled,
49+
isManualTriggerEnabled,
50+
isAutomatedTriggerEnabled,
51+
isSuggestionsWithCodeReferencesEnabled,
52+
}
53+
}
54+
2655
async function setSubscriptionsforInlineCompletion() {
56+
RecommendationHandler.instance.subscribeSuggestionCommands()
57+
2758
/**
2859
* Automated trigger
2960
*/
3061
globals.context.subscriptions.push(
62+
vscode.window.onDidChangeActiveTextEditor(async (editor) => {
63+
await RecommendationHandler.instance.onEditorChange()
64+
}),
65+
vscode.window.onDidChangeWindowState(async (e) => {
66+
await RecommendationHandler.instance.onFocusChange()
67+
}),
68+
vscode.window.onDidChangeTextEditorSelection(async (e) => {
69+
await RecommendationHandler.instance.onCursorChange(e)
70+
}),
3171
vscode.workspace.onDidChangeTextDocument(async (e) => {
3272
const editor = vscode.window.activeTextEditor
3373
if (!editor) {
@@ -40,6 +80,7 @@ export async function activate() {
4080
return
4181
}
4282

83+
CodeWhispererCodeCoverageTracker.getTracker(e.document.languageId)?.countTotalTokens(e)
4384
UserWrittenCodeTracker.instance.onTextDocumentChange(e)
4485
/**
4586
* Handle this keystroke event only when
@@ -51,6 +92,11 @@ export async function activate() {
5192
return
5293
}
5394

95+
if (vsCodeState.lastUserModificationTime) {
96+
TelemetryHelper.instance.setTimeSinceLastModification(
97+
performance.now() - vsCodeState.lastUserModificationTime
98+
)
99+
}
54100
vsCodeState.lastUserModificationTime = performance.now()
55101
/**
56102
* Important: Doing this sleep(10) is to make sure
@@ -59,6 +105,19 @@ export async function activate() {
59105
* Then this event can be processed by our code.
60106
*/
61107
await sleep(CodeWhispererConstants.vsCodeCursorUpdateDelay)
108+
if (!RecommendationHandler.instance.isSuggestionVisible()) {
109+
await KeyStrokeHandler.instance.processKeyStroke(e, editor, client, await getConfigEntry())
110+
}
111+
}),
112+
// manual trigger
113+
Commands.register({ id: 'aws.amazonq.invokeInlineCompletion', autoconnect: true }, async () => {
114+
invokeRecommendation(
115+
vscode.window.activeTextEditor as vscode.TextEditor,
116+
client,
117+
await getConfigEntry()
118+
).catch((e: Error) => {
119+
getLogger().error('invokeRecommendation failed: %s', (e as Error).message)
120+
})
62121
})
63122
)
64123
}

packages/amazonq/src/lsp/client.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
} from 'aws-core-vscode/shared'
4646
import { processUtils } from 'aws-core-vscode/shared'
4747
import { activate } from './chat/activation'
48+
import { activate as activateInline } from '../app/inline/activation'
4849
import { AmazonQResourcePaths } from './lspInstaller'
4950
import { ConfigSection, isValidConfigSection, pushConfigUpdate, toAmazonQLSPLogLevel } from './config'
5051
import { activate as activateInlineChat } from '../inlineChat/activation'
@@ -338,8 +339,17 @@ async function onLanguageServerReady(
338339
// tutorial for inline chat
339340
const inlineChatTutorialAnnotation = new InlineChatTutorialAnnotation(inlineTutorialAnnotation)
340341

341-
const inlineManager = new InlineCompletionManager(client, sessionManager, lineTracker, inlineTutorialAnnotation)
342-
inlineManager.registerInlineCompletion()
342+
const enableInlineRollback = false
343+
if (enableInlineRollback) {
344+
// use VSC inline
345+
await activateInline()
346+
} else {
347+
// use language server for inline completion
348+
const inlineManager = new InlineCompletionManager(client, sessionManager, lineTracker, inlineTutorialAnnotation)
349+
inlineManager.registerInlineCompletion()
350+
toDispose.push(inlineManager)
351+
}
352+
343353
activateInlineChat(extensionContext, client, encryptionKey, inlineChatTutorialAnnotation)
344354

345355
if (Experiments.instance.get('amazonqChatLSP', true)) {
@@ -354,7 +364,6 @@ async function onLanguageServerReady(
354364
await initializeLanguageServerConfiguration(client, 'startup')
355365

356366
toDispose.push(
357-
inlineManager,
358367
Commands.register('aws.amazonq.showPrev', async () => {
359368
await sessionManager.maybeRefreshSessionUx()
360369
await vscode.commands.executeCommand('editor.action.inlineSuggest.showPrevious')

packages/core/src/codewhisperer/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,7 @@ export * from './util/gitUtil'
8787
export * from './ui/prompters'
8888
export { UserWrittenCodeTracker } from './tracker/userWrittenCodeTracker'
8989
export { RegionProfileManager, defaultServiceConfig } from './region/regionProfileManager'
90+
export { DocumentChangedSource, KeyStrokeHandler, DefaultDocumentChangedType } from './service/keyStrokeHandler'
91+
export { RecommendationHandler } from './service/recommendationHandler'
92+
export { CodeWhispererCodeCoverageTracker } from './tracker/codewhispererCodeCoverageTracker'
93+
export { invokeRecommendation } from './commands/invokeRecommendation'

0 commit comments

Comments
 (0)