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,47 @@ 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+ if ( this . activeSession ?. suggestions && this . activeSession . suggestions . length > 0 ) {
140+ const reference = this . activeSession . suggestions [ this . _currentSuggestionIndex ] . references
141+ if ( reference && reference . length > 0 ) {
142+ ReferenceInlineProvider . instance . setInlineReference (
143+ this . activeSession . startPosition . line ,
144+ this . activeSession . suggestions [ this . _currentSuggestionIndex ] . insertText . toString ( ) ,
145+ reference
146+ )
147+ }
148+ if ( vscode . window . activeTextEditor ) {
149+ ImportAdderProvider . instance . onShowRecommendation (
150+ vscode . window . activeTextEditor . document ,
151+ this . activeSession . startPosition . line ,
152+ this . activeSession . suggestions [ this . _currentSuggestionIndex ]
153+ )
154+ }
155+ }
156+ }
106157}
0 commit comments