55
66import vscode from 'vscode'
77import {
8+ acceptSuggestion ,
89 AuthUtil ,
10+ CodeSuggestionsState ,
11+ CodeWhispererCodeCoverageTracker ,
912 CodeWhispererConstants ,
13+ CodeWhispererSettings ,
14+ ConfigurationEntry ,
15+ DefaultCodeWhispererClient ,
16+ invokeRecommendation ,
1017 isInlineCompletionEnabled ,
18+ KeyStrokeHandler ,
19+ RecommendationHandler ,
1120 runtimeLanguageContext ,
21+ TelemetryHelper ,
1222 UserWrittenCodeTracker ,
1323 vsCodeState ,
1424} from 'aws-core-vscode/codewhisperer'
15- import { globals , sleep } from 'aws-core-vscode/shared'
25+ import { Commands , getLogger , globals , sleep } from 'aws-core-vscode/shared'
26+ import { LanguageClient } from 'vscode-languageclient'
1627
17- export async function activate ( ) {
18- if ( isInlineCompletionEnabled ( ) ) {
19- // Debugging purpose: only initialize NextEditPredictionPanel when development
20- // NextEditPredictionPanel.getInstance()
28+ export async function activate ( languageClient : LanguageClient ) {
29+ const codewhispererSettings = CodeWhispererSettings . instance
30+ const client = new DefaultCodeWhispererClient ( )
2131
32+ if ( isInlineCompletionEnabled ( ) ) {
2233 await setSubscriptionsforInlineCompletion ( )
2334 await AuthUtil . instance . setVscodeContextProps ( )
35+ RecommendationHandler . instance . setLanguageClient ( languageClient )
36+ }
37+
38+ function getAutoTriggerStatus ( ) : boolean {
39+ return CodeSuggestionsState . instance . isSuggestionsEnabled ( )
40+ }
41+
42+ async function getConfigEntry ( ) : Promise < ConfigurationEntry > {
43+ const isShowMethodsEnabled : boolean =
44+ vscode . workspace . getConfiguration ( 'editor' ) . get ( 'suggest.showMethods' ) || false
45+ const isAutomatedTriggerEnabled : boolean = getAutoTriggerStatus ( )
46+ const isManualTriggerEnabled : boolean = true
47+ const isSuggestionsWithCodeReferencesEnabled = codewhispererSettings . isSuggestionsWithCodeReferencesEnabled ( )
48+
49+ // TODO:remove isManualTriggerEnabled
50+ return {
51+ isShowMethodsEnabled,
52+ isManualTriggerEnabled,
53+ isAutomatedTriggerEnabled,
54+ isSuggestionsWithCodeReferencesEnabled,
55+ }
2456 }
2557
2658 async function setSubscriptionsforInlineCompletion ( ) {
59+ RecommendationHandler . instance . subscribeSuggestionCommands ( )
60+
2761 /**
2862 * Automated trigger
2963 */
3064 globals . context . subscriptions . push (
65+ acceptSuggestion . register ( globals . context ) ,
66+ vscode . window . onDidChangeActiveTextEditor ( async ( editor ) => {
67+ await RecommendationHandler . instance . onEditorChange ( )
68+ } ) ,
69+ vscode . window . onDidChangeWindowState ( async ( e ) => {
70+ await RecommendationHandler . instance . onFocusChange ( )
71+ } ) ,
72+ vscode . window . onDidChangeTextEditorSelection ( async ( e ) => {
73+ await RecommendationHandler . instance . onCursorChange ( e )
74+ } ) ,
3175 vscode . workspace . onDidChangeTextDocument ( async ( e ) => {
3276 const editor = vscode . window . activeTextEditor
3377 if ( ! editor ) {
@@ -40,6 +84,7 @@ export async function activate() {
4084 return
4185 }
4286
87+ CodeWhispererCodeCoverageTracker . getTracker ( e . document . languageId ) ?. countTotalTokens ( e )
4388 UserWrittenCodeTracker . instance . onTextDocumentChange ( e )
4489 /**
4590 * Handle this keystroke event only when
@@ -51,6 +96,11 @@ export async function activate() {
5196 return
5297 }
5398
99+ if ( vsCodeState . lastUserModificationTime ) {
100+ TelemetryHelper . instance . setTimeSinceLastModification (
101+ performance . now ( ) - vsCodeState . lastUserModificationTime
102+ )
103+ }
54104 vsCodeState . lastUserModificationTime = performance . now ( )
55105 /**
56106 * Important: Doing this sleep(10) is to make sure
@@ -59,6 +109,19 @@ export async function activate() {
59109 * Then this event can be processed by our code.
60110 */
61111 await sleep ( CodeWhispererConstants . vsCodeCursorUpdateDelay )
112+ if ( ! RecommendationHandler . instance . isSuggestionVisible ( ) ) {
113+ await KeyStrokeHandler . instance . processKeyStroke ( e , editor , client , await getConfigEntry ( ) )
114+ }
115+ } ) ,
116+ // manual trigger
117+ Commands . register ( { id : 'aws.amazonq.invokeInlineCompletion' , autoconnect : true } , async ( ) => {
118+ invokeRecommendation (
119+ vscode . window . activeTextEditor as vscode . TextEditor ,
120+ client ,
121+ await getConfigEntry ( )
122+ ) . catch ( ( e : Error ) => {
123+ getLogger ( ) . error ( 'invokeRecommendation failed: %s' , ( e as Error ) . message )
124+ } )
62125 } )
63126 )
64127 }
0 commit comments