4
4
*/
5
5
import * as vscode from 'vscode'
6
6
import { InlineCompletionItemWithReferences } from '@aws/language-server-runtimes-types'
7
- import { FileDiagnostic , getDiagnosticsOfCurrentFile } from 'aws-core-vscode/codewhisperer'
7
+ import {
8
+ FileDiagnostic ,
9
+ getDiagnosticsOfCurrentFile ,
10
+ ImportAdderProvider ,
11
+ ReferenceInlineProvider ,
12
+ } from 'aws-core-vscode/codewhisperer'
8
13
9
14
// TODO: add more needed data to the session interface
10
15
export interface CodeWhispererSession {
@@ -25,7 +30,7 @@ export class SessionManager {
25
30
private activeSession ?: CodeWhispererSession
26
31
private _acceptedSuggestionCount : number = 0
27
32
private _refreshedSessions = new Set < string > ( )
28
-
33
+ private _currentSuggestionIndex = 0
29
34
constructor ( ) { }
30
35
31
36
public startSession (
@@ -45,6 +50,7 @@ export class SessionManager {
45
50
firstCompletionDisplayLatency,
46
51
diagnosticsBeforeAccept,
47
52
}
53
+ this . _currentSuggestionIndex = 0
48
54
}
49
55
50
56
public closeSession ( ) {
@@ -86,6 +92,8 @@ export class SessionManager {
86
92
87
93
public clear ( ) {
88
94
this . activeSession = undefined
95
+ this . _currentSuggestionIndex = 0
96
+ this . clearReferenceInlineHintsAndImportHints ( )
89
97
}
90
98
91
99
// re-render the session ghost text to display paginated responses once per completed session
@@ -103,4 +111,60 @@ export class SessionManager {
103
111
this . _refreshedSessions . add ( this . activeSession . sessionId )
104
112
}
105
113
}
114
+
115
+ public onNextSuggestion ( ) {
116
+ if ( this . activeSession ?. suggestions && this . activeSession ?. suggestions . length > 0 ) {
117
+ this . _currentSuggestionIndex = ( this . _currentSuggestionIndex + 1 ) % this . activeSession . suggestions . length
118
+ this . updateCodeReferenceAndImports ( )
119
+ }
120
+ }
121
+
122
+ public onPrevSuggestion ( ) {
123
+ if ( this . activeSession ?. suggestions && this . activeSession . suggestions . length > 0 ) {
124
+ this . _currentSuggestionIndex =
125
+ ( this . _currentSuggestionIndex - 1 + this . activeSession . suggestions . length ) %
126
+ this . activeSession . suggestions . length
127
+ this . updateCodeReferenceAndImports ( )
128
+ }
129
+ }
130
+
131
+ private clearReferenceInlineHintsAndImportHints ( ) {
132
+ ReferenceInlineProvider . instance . removeInlineReference ( )
133
+ ImportAdderProvider . instance . clear ( )
134
+ }
135
+
136
+ // Ideally use this API handleDidShowCompletionItem
137
+ // https://github.com/microsoft/vscode/blob/main/src/vscode-dts/vscode.proposed.inlineCompletionsAdditions.d.ts#L83
138
+ updateCodeReferenceAndImports ( ) {
139
+ try {
140
+ this . clearReferenceInlineHintsAndImportHints ( )
141
+ if (
142
+ this . activeSession ?. suggestions &&
143
+ this . activeSession . suggestions [ this . _currentSuggestionIndex ] &&
144
+ this . activeSession . suggestions . length > 0
145
+ ) {
146
+ const reference = this . activeSession . suggestions [ this . _currentSuggestionIndex ] . references
147
+ const insertText = this . activeSession . suggestions [ this . _currentSuggestionIndex ] . insertText
148
+ if ( reference && reference . length > 0 ) {
149
+ const insertTextStr =
150
+ typeof insertText === 'string' ? insertText : ( insertText . value ?? String ( insertText ) )
151
+
152
+ ReferenceInlineProvider . instance . setInlineReference (
153
+ this . activeSession . startPosition . line ,
154
+ insertTextStr ,
155
+ reference
156
+ )
157
+ }
158
+ if ( vscode . window . activeTextEditor ) {
159
+ ImportAdderProvider . instance . onShowRecommendation (
160
+ vscode . window . activeTextEditor . document ,
161
+ this . activeSession . startPosition . line ,
162
+ this . activeSession . suggestions [ this . _currentSuggestionIndex ]
163
+ )
164
+ }
165
+ }
166
+ } catch {
167
+ // do nothing as this is not critical path
168
+ }
169
+ }
106
170
}
0 commit comments