Skip to content

Commit b63185c

Browse files
committed
fix: avoid relying on session state
1 parent b5540e2 commit b63185c

File tree

3 files changed

+27
-101
lines changed

3 files changed

+27
-101
lines changed

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

Lines changed: 20 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -158,39 +158,6 @@ export class InlineCompletionManager implements Disposable {
158158
this.languageClient.sendNotification(this.logSessionResultMessageName, params)
159159
}
160160
commands.registerCommand('aws.amazonq.rejectCodeSuggestion', onInlineRejection)
161-
162-
/*
163-
We have to overwrite the prev. and next. commands because the inlineCompletionProvider only contained the current item
164-
To show prev. and next. recommendation we need to re-register a new provider with the previous or next item
165-
*/
166-
167-
const swapProviderAndShow = async () => {
168-
await commands.executeCommand('editor.action.inlineSuggest.hide')
169-
this.disposable.dispose()
170-
this.disposable = languages.registerInlineCompletionItemProvider(
171-
CodeWhispererConstants.platformLanguageIds,
172-
new AmazonQInlineCompletionItemProvider(
173-
this.languageClient,
174-
this.recommendationService,
175-
this.sessionManager,
176-
this.inlineTutorialAnnotation,
177-
false
178-
)
179-
)
180-
await commands.executeCommand('editor.action.inlineSuggest.trigger')
181-
}
182-
183-
const prevCommandHandler = async () => {
184-
this.sessionManager.decrementActiveIndex()
185-
await swapProviderAndShow()
186-
}
187-
commands.registerCommand('editor.action.inlineSuggest.showPrevious', prevCommandHandler)
188-
189-
const nextCommandHandler = async () => {
190-
this.sessionManager.incrementActiveIndex()
191-
await swapProviderAndShow()
192-
}
193-
commands.registerCommand('editor.action.inlineSuggest.showNext', nextCommandHandler)
194161
}
195162
}
196163

@@ -199,8 +166,7 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
199166
private readonly languageClient: LanguageClient,
200167
private readonly recommendationService: RecommendationService,
201168
private readonly sessionManager: SessionManager,
202-
private readonly inlineTutorialAnnotation: InlineTutorialAnnotation,
203-
private readonly isNewSession: boolean = true
169+
private readonly inlineTutorialAnnotation: InlineTutorialAnnotation
204170
) {}
205171

206172
provideInlineCompletionItems = debounce(
@@ -215,29 +181,28 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
215181
context: InlineCompletionContext,
216182
token: CancellationToken
217183
): Promise<InlineCompletionItem[]> {
184+
getLogger().debug(`provideInlineCompletionItems: ${context.triggerKind}`)
218185
try {
219186
vsCodeState.isRecommendationsActive = true
220-
if (this.isNewSession) {
221-
const isAutoTrigger = context.triggerKind === InlineCompletionTriggerKind.Automatic
222-
if (isAutoTrigger && !CodeSuggestionsState.instance.isSuggestionsEnabled()) {
223-
// return early when suggestions are disabled with auto trigger
224-
return []
225-
}
226-
227-
// tell the tutorial that completions has been triggered
228-
await this.inlineTutorialAnnotation.triggered(context.triggerKind)
229-
TelemetryHelper.instance.setInvokeSuggestionStartTime()
230-
TelemetryHelper.instance.setTriggerType(context.triggerKind)
231-
232-
// make service requests if it's a new session
233-
await this.recommendationService.getAllRecommendations(
234-
this.languageClient,
235-
document,
236-
position,
237-
context,
238-
token
239-
)
187+
getLogger().debug(`provideInlineCompletionItems triggered`)
188+
const isAutoTrigger = context.triggerKind === InlineCompletionTriggerKind.Automatic
189+
if (isAutoTrigger && !CodeSuggestionsState.instance.isSuggestionsEnabled()) {
190+
// return early when suggestions are disabled with auto trigger
191+
return []
240192
}
193+
194+
// tell the tutorial that completions has been triggered
195+
await this.inlineTutorialAnnotation.triggered(context.triggerKind)
196+
TelemetryHelper.instance.setInvokeSuggestionStartTime()
197+
TelemetryHelper.instance.setTriggerType(context.triggerKind)
198+
199+
await this.recommendationService.getAllRecommendations(
200+
this.languageClient,
201+
document,
202+
position,
203+
context,
204+
token
205+
)
241206
// get active item from session for displaying
242207
const items = this.sessionManager.getActiveRecommendation()
243208
const session = this.sessionManager.getActiveSession()

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

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,10 @@ export class SessionManager {
7474
*/
7575

7676
public getActiveRecommendation(): InlineCompletionItemWithReferences[] {
77-
let suggestionCount = this.activeSession?.suggestions.length
78-
if (!suggestionCount) {
79-
return []
80-
}
81-
if (suggestionCount === 1 && this.activeSession?.isRequestInProgress) {
82-
suggestionCount += 1
83-
}
84-
85-
const activeSuggestion = this.activeSession?.suggestions[this.activeIndex]
86-
if (!activeSuggestion) {
77+
if (!this.activeSession) {
8778
return []
8879
}
89-
const items = [activeSuggestion]
90-
// to make the total number of suggestions match the actual number
91-
for (let i = 1; i < suggestionCount; i++) {
92-
items.push({
93-
...activeSuggestion,
94-
insertText: `${i}`,
95-
})
96-
}
97-
return items
80+
return this.activeSession.suggestions
9881
}
9982

10083
public get acceptedSuggestionCount(): number {

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

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -320,30 +320,12 @@ describe('InlineCompletionManager', () => {
320320
assert(getAllRecommendationsStub.calledOnce)
321321
assert.deepStrictEqual(items, mockSuggestions)
322322
}),
323-
it('should not call recommendation service for existing sessions', async () => {
324-
provider = new AmazonQInlineCompletionItemProvider(
325-
languageClient,
326-
recommendationService,
327-
mockSessionManager,
328-
inlineTutorialAnnotation,
329-
false
330-
)
331-
const items = await provider.provideInlineCompletionItems(
332-
mockDocument,
333-
mockPosition,
334-
mockContext,
335-
mockToken
336-
)
337-
assert(getAllRecommendationsStub.notCalled)
338-
assert.deepStrictEqual(items, mockSuggestions)
339-
}),
340323
it('should handle reference if there is any', async () => {
341324
provider = new AmazonQInlineCompletionItemProvider(
342325
languageClient,
343326
recommendationService,
344327
mockSessionManager,
345-
inlineTutorialAnnotation,
346-
false
328+
inlineTutorialAnnotation
347329
)
348330
await provider.provideInlineCompletionItems(mockDocument, mockPosition, mockContext, mockToken)
349331
assert(setInlineReferenceStub.calledOnce)
@@ -360,8 +342,7 @@ describe('InlineCompletionManager', () => {
360342
languageClient,
361343
recommendationService,
362344
mockSessionManager,
363-
inlineTutorialAnnotation,
364-
true
345+
inlineTutorialAnnotation
365346
)
366347
getActiveRecommendationStub.returns([
367348
{
@@ -391,8 +372,7 @@ describe('InlineCompletionManager', () => {
391372
languageClient,
392373
recommendationService,
393374
mockSessionManager,
394-
inlineTutorialAnnotation,
395-
true
375+
inlineTutorialAnnotation
396376
)
397377
const expectedText = 'this is my text'
398378
getActiveRecommendationStub.returns([
@@ -415,8 +395,7 @@ describe('InlineCompletionManager', () => {
415395
languageClient,
416396
recommendationService,
417397
mockSessionManager,
418-
inlineTutorialAnnotation,
419-
true
398+
inlineTutorialAnnotation
420399
)
421400
getActiveRecommendationStub.returns([])
422401
const messageShown = new Promise((resolve) =>
@@ -449,8 +428,7 @@ describe('InlineCompletionManager', () => {
449428
languageClient,
450429
recommendationService,
451430
mockSessionManager,
452-
inlineTutorialAnnotation,
453-
false
431+
inlineTutorialAnnotation
454432
)
455433
const p1 = provider.provideInlineCompletionItems(mockDocument, mockPosition, mockContext, mockToken)
456434
const p2 = provider.provideInlineCompletionItems(mockDocument, mockPosition, mockContext, mockToken)

0 commit comments

Comments
 (0)