Skip to content

Commit a67d4dc

Browse files
committed
update import adder hints
1 parent 5e256b9 commit a67d4dc

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

packages/amazonq/src/app/inline/completion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,6 @@ ${itemLog}
434434
}
435435
item.range = new Range(cursorPosition, cursorPosition)
436436
itemsMatchingTypeahead.push(item)
437-
ImportAdderProvider.instance.onShowRecommendation(document, cursorPosition.line, item)
438437
}
439438
}
440439

@@ -458,6 +457,7 @@ ${itemLog}
458457
return []
459458
}
460459

460+
this.sessionManager.updateCodeReferenceAndImports()
461461
// suggestions returned here will be displayed on screen
462462
return itemsMatchingTypeahead as InlineCompletionItem[]
463463
} catch (e) {

packages/amazonq/src/app/inline/sessionManager.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
*/
55
import * as vscode from 'vscode'
66
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'
813

914
// TODO: add more needed data to the session interface
1015
export 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
}

packages/amazonq/src/lsp/client.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,12 @@ async function onLanguageServerReady(
309309
Commands.register('aws.amazonq.showPrev', async () => {
310310
await sessionManager.maybeRefreshSessionUx()
311311
await vscode.commands.executeCommand('editor.action.inlineSuggest.showPrevious')
312+
await sessionManager.onPrevSuggestion()
312313
}),
313314
Commands.register('aws.amazonq.showNext', async () => {
314315
await sessionManager.maybeRefreshSessionUx()
315316
await vscode.commands.executeCommand('editor.action.inlineSuggest.showNext')
317+
await sessionManager.onNextSuggestion()
316318
}),
317319
Commands.register({ id: 'aws.amazonq.invokeInlineCompletion', autoconnect: true }, async () => {
318320
await vscode.commands.executeCommand('editor.action.inlineSuggest.trigger')

0 commit comments

Comments
 (0)