Skip to content

Commit ff9d816

Browse files
authored
fix(amazonq): correctly update the on show inlay hints for code reference and imports (#7709)
## Problem After Flare migration, the code references and imports are not properly render on suggestion being shown. ## Solution correctly update the on show inlay hints for code reference and imports --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 9562ccc commit ff9d816

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-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: 66 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,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
}

packages/amazonq/src/lsp/client.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,12 @@ async function onLanguageServerReady(
355355
Commands.register('aws.amazonq.showPrev', async () => {
356356
await sessionManager.maybeRefreshSessionUx()
357357
await vscode.commands.executeCommand('editor.action.inlineSuggest.showPrevious')
358+
sessionManager.onPrevSuggestion()
358359
}),
359360
Commands.register('aws.amazonq.showNext', async () => {
360361
await sessionManager.maybeRefreshSessionUx()
361362
await vscode.commands.executeCommand('editor.action.inlineSuggest.showNext')
363+
sessionManager.onNextSuggestion()
362364
}),
363365
Commands.register({ id: 'aws.amazonq.invokeInlineCompletion', autoconnect: true }, async () => {
364366
await vscode.commands.executeCommand('editor.action.inlineSuggest.trigger')

packages/amazonq/test/unit/amazonq/apps/inline/completion.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ describe('InlineCompletionManager', () => {
256256
getActiveSession: getActiveSessionStub,
257257
getActiveRecommendation: getActiveRecommendationStub,
258258
clear: () => {},
259+
updateCodeReferenceAndImports: () => {},
259260
} as unknown as SessionManager
260261

261262
getActiveSessionStub.returns({

0 commit comments

Comments
 (0)