44 */
55import * as vscode from 'vscode'
66import { 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'
813
914// TODO: add more needed data to the session interface
1015export interface CodeWhispererSession {
@@ -25,7 +30,7 @@ export class SessionManager {
2530 private activeSession ?: CodeWhispererSession
2631 private _acceptedSuggestionCount : number = 0
2732 private _refreshedSessions = new Set < string > ( )
28-
33+ private _currentSuggestionIndex = 0
2934 constructor ( ) { }
3035
3136 public startSession (
@@ -45,6 +50,7 @@ export class SessionManager {
4550 firstCompletionDisplayLatency,
4651 diagnosticsBeforeAccept,
4752 }
53+ this . _currentSuggestionIndex = 0
4854 }
4955
5056 public closeSession ( ) {
@@ -86,6 +92,8 @@ export class SessionManager {
8692
8793 public clear ( ) {
8894 this . activeSession = undefined
95+ this . _currentSuggestionIndex = 0
96+ this . clearReferenceInlineHintsAndImportHints ( )
8997 }
9098
9199 // re-render the session ghost text to display paginated responses once per completed session
@@ -103,4 +111,60 @@ export class SessionManager {
103111 this . _refreshedSessions . add ( this . activeSession . sessionId )
104112 }
105113 }
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+ }
106170}
0 commit comments