@@ -17,6 +17,7 @@ import {
1717 window ,
1818 TextEditor ,
1919 InlineCompletionTriggerKind ,
20+ Range ,
2021} from 'vscode'
2122import { LanguageClient } from 'vscode-languageclient'
2223import {
@@ -32,11 +33,13 @@ import {
3233 ReferenceLogViewProvider ,
3334 ImportAdderProvider ,
3435 CodeSuggestionsState ,
36+ vsCodeState ,
3537} from 'aws-core-vscode/codewhisperer'
3638import { InlineGeneratingMessage } from './inlineGeneratingMessage'
3739import { LineTracker } from './stateTracker/lineTracker'
3840import { InlineTutorialAnnotation } from './tutorials/inlineTutorialAnnotation'
3941import { TelemetryHelper } from './telemetryHelper'
42+ import { getLogger } from 'aws-core-vscode/shared'
4043
4144export class InlineCompletionManager implements Disposable {
4245 private disposable : Disposable
@@ -204,54 +207,68 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
204207 context : InlineCompletionContext ,
205208 token : CancellationToken
206209 ) : Promise < InlineCompletionItem [ ] | InlineCompletionList > {
207- if ( this . isNewSession ) {
208- const isAutoTrigger = context . triggerKind === InlineCompletionTriggerKind . Automatic
209- if ( isAutoTrigger && ! CodeSuggestionsState . instance . isSuggestionsEnabled ( ) ) {
210- // return early when suggestions are disabled with auto trigger
210+ try {
211+ // only exposed for testing
212+ vsCodeState . isRecommendationsActive = true
213+ if ( this . isNewSession ) {
214+ const isAutoTrigger = context . triggerKind === InlineCompletionTriggerKind . Automatic
215+ if ( isAutoTrigger && ! CodeSuggestionsState . instance . isSuggestionsEnabled ( ) ) {
216+ // return early when suggestions are disabled with auto trigger
217+ return [ ]
218+ }
219+
220+ // tell the tutorial that completions has been triggered
221+ await this . inlineTutorialAnnotation . triggered ( context . triggerKind )
222+ TelemetryHelper . instance . setInvokeSuggestionStartTime ( )
223+ TelemetryHelper . instance . setTriggerType ( context . triggerKind )
224+
225+ // make service requests if it's a new session
226+ await this . recommendationService . getAllRecommendations (
227+ this . languageClient ,
228+ document ,
229+ position ,
230+ context ,
231+ token
232+ )
233+ }
234+ // get active item from session for displaying
235+ const items = this . sessionManager . getActiveRecommendation ( )
236+ const session = this . sessionManager . getActiveSession ( )
237+ const editor = window . activeTextEditor
238+ if ( ! session || ! items . length || ! editor ) {
211239 return [ ]
212240 }
213241
214- // tell the tutorial that completions has been triggered
215- await this . inlineTutorialAnnotation . triggered ( context . triggerKind )
216- TelemetryHelper . instance . setInvokeSuggestionStartTime ( )
217- TelemetryHelper . instance . setTriggerType ( context . triggerKind )
218-
219- // make service requests if it's a new session
220- await this . recommendationService . getAllRecommendations (
221- this . languageClient ,
222- document ,
223- position ,
224- context ,
225- token
226- )
227- }
228- // get active item from session for displaying
229- const items = this . sessionManager . getActiveRecommendation ( )
230- const session = this . sessionManager . getActiveSession ( )
231- if ( ! session || ! items . length ) {
232- return [ ]
233- }
234- const editor = window . activeTextEditor
235- for ( const item of items ) {
236- item . command = {
237- command : 'aws.amazonq.acceptInline' ,
238- title : 'On acceptance' ,
239- arguments : [
240- session . sessionId ,
241- item ,
242- editor ,
243- session . requestStartTime ,
242+ const start = document . validatePosition ( editor . selection . active )
243+ const end = position
244+ for ( const item of items ) {
245+ item . command = {
246+ command : 'aws.amazonq.acceptInline' ,
247+ title : 'On acceptance' ,
248+ arguments : [
249+ session . sessionId ,
250+ item ,
251+ editor ,
252+ session . requestStartTime ,
253+ position . line ,
254+ session . firstCompletionDisplayLatency ,
255+ ] ,
256+ }
257+ item . range = new Range ( start , end )
258+ ReferenceInlineProvider . instance . setInlineReference (
244259 position . line ,
245- session . firstCompletionDisplayLatency ,
246- ] ,
260+ item . insertText as string ,
261+ item . references
262+ )
263+ ImportAdderProvider . instance . onShowRecommendation ( document , position . line , item )
247264 }
248- ReferenceInlineProvider . instance . setInlineReference (
249- position . line ,
250- item . insertText as string ,
251- item . references
252- )
253- ImportAdderProvider . instance . onShowRecommendation ( document , position . line , item )
265+ return items as InlineCompletionItem [ ]
266+ } catch ( e ) {
267+ getLogger ( 'amazonqLsp' ) . error ( 'Completion request failed: %O' , e )
268+ return [ ]
269+ } finally {
270+ // only exposed for testing
271+ vsCodeState . isRecommendationsActive = false
254272 }
255- return items as InlineCompletionItem [ ]
256273 }
257274}
0 commit comments