5
5
6
6
import vscode from 'vscode'
7
7
import {
8
+ acceptSuggestion ,
8
9
AuthUtil ,
10
+ CodeSuggestionsState ,
11
+ CodeWhispererCodeCoverageTracker ,
9
12
CodeWhispererConstants ,
13
+ CodeWhispererSettings ,
14
+ ConfigurationEntry ,
15
+ DefaultCodeWhispererClient ,
16
+ invokeRecommendation ,
10
17
isInlineCompletionEnabled ,
18
+ KeyStrokeHandler ,
19
+ RecommendationHandler ,
11
20
runtimeLanguageContext ,
21
+ TelemetryHelper ,
12
22
UserWrittenCodeTracker ,
13
23
vsCodeState ,
14
24
} 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'
16
27
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 ( )
21
31
32
+ if ( isInlineCompletionEnabled ( ) ) {
22
33
await setSubscriptionsforInlineCompletion ( )
23
34
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
+ }
24
56
}
25
57
26
58
async function setSubscriptionsforInlineCompletion ( ) {
59
+ RecommendationHandler . instance . subscribeSuggestionCommands ( )
60
+
27
61
/**
28
62
* Automated trigger
29
63
*/
30
64
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
+ } ) ,
31
75
vscode . workspace . onDidChangeTextDocument ( async ( e ) => {
32
76
const editor = vscode . window . activeTextEditor
33
77
if ( ! editor ) {
@@ -40,6 +84,7 @@ export async function activate() {
40
84
return
41
85
}
42
86
87
+ CodeWhispererCodeCoverageTracker . getTracker ( e . document . languageId ) ?. countTotalTokens ( e )
43
88
UserWrittenCodeTracker . instance . onTextDocumentChange ( e )
44
89
/**
45
90
* Handle this keystroke event only when
@@ -51,6 +96,11 @@ export async function activate() {
51
96
return
52
97
}
53
98
99
+ if ( vsCodeState . lastUserModificationTime ) {
100
+ TelemetryHelper . instance . setTimeSinceLastModification (
101
+ performance . now ( ) - vsCodeState . lastUserModificationTime
102
+ )
103
+ }
54
104
vsCodeState . lastUserModificationTime = performance . now ( )
55
105
/**
56
106
* Important: Doing this sleep(10) is to make sure
@@ -59,6 +109,19 @@ export async function activate() {
59
109
* Then this event can be processed by our code.
60
110
*/
61
111
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
+ } )
62
125
} )
63
126
)
64
127
}
0 commit comments