22 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33 * SPDX-License-Identifier: Apache-2.0
44 */
5- import * as vscode from 'vscode'
5+
66import {
77 CancellationToken ,
88 InlineCompletionContext ,
@@ -46,9 +46,7 @@ import { Experiments, getLogger, sleep } from 'aws-core-vscode/shared'
4646import { debounce , messageUtils } from 'aws-core-vscode/utils'
4747import { showEdits } from './EditRendering/imageRenderer'
4848import { ICursorUpdateRecorder } from './cursorUpdateManager'
49-
50- let lastDocumentDeleteEvent : vscode . TextDocumentChangeEvent | undefined = undefined
51- let lastDocumentDeleteTime = 0
49+ import { DocumentEventListener } from './documentEventListener'
5250
5351export class InlineCompletionManager implements Disposable {
5452 private disposable : Disposable
@@ -60,7 +58,7 @@ export class InlineCompletionManager implements Disposable {
6058
6159 private inlineTutorialAnnotation : InlineTutorialAnnotation
6260 private readonly logSessionResultMessageName = 'aws/logInlineCompletionSessionResults'
63- private documentChangeListener : Disposable
61+ private documentEventListener : DocumentEventListener
6462
6563 constructor (
6664 languageClient : LanguageClient ,
@@ -74,24 +72,19 @@ export class InlineCompletionManager implements Disposable {
7472 this . lineTracker = lineTracker
7573 this . recommendationService = new RecommendationService ( this . sessionManager , cursorUpdateRecorder )
7674 this . inlineTutorialAnnotation = inlineTutorialAnnotation
75+ this . documentEventListener = new DocumentEventListener ( )
7776 this . inlineCompletionProvider = new AmazonQInlineCompletionItemProvider (
7877 languageClient ,
7978 this . recommendationService ,
8079 this . sessionManager ,
81- this . inlineTutorialAnnotation
80+ this . inlineTutorialAnnotation ,
81+ this . documentEventListener
8282 )
8383
84- this . documentChangeListener = vscode . workspace . onDidChangeTextDocument ( ( e ) => {
85- if ( e . contentChanges . length === 1 && e . contentChanges [ 0 ] . text === '' ) {
86- lastDocumentDeleteEvent = e
87- lastDocumentDeleteTime = performance . now ( )
88- }
89- } )
9084 this . disposable = languages . registerInlineCompletionItemProvider (
9185 CodeWhispererConstants . platformLanguageIds ,
9286 this . inlineCompletionProvider
9387 )
94-
9588 this . lineTracker . ready ( )
9689 }
9790
@@ -104,8 +97,8 @@ export class InlineCompletionManager implements Disposable {
10497 this . disposable . dispose ( )
10598 this . lineTracker . dispose ( )
10699 }
107- if ( this . documentChangeListener ) {
108- this . documentChangeListener . dispose ( )
100+ if ( this . documentEventListener ) {
101+ this . documentEventListener . dispose ( )
109102 }
110103 }
111104
@@ -211,7 +204,8 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
211204 private readonly languageClient : LanguageClient ,
212205 private readonly recommendationService : RecommendationService ,
213206 private readonly sessionManager : SessionManager ,
214- private readonly inlineTutorialAnnotation : InlineTutorialAnnotation
207+ private readonly inlineTutorialAnnotation : InlineTutorialAnnotation ,
208+ private readonly documentEventListener : DocumentEventListener
215209 ) { }
216210
217211 private readonly logSessionResultMessageName = 'aws/logInlineCompletionSessionResults'
@@ -251,8 +245,7 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
251245 await sleep ( 1 )
252246 // prevent user deletion invoking auto trigger
253247 // this is a best effort estimate of deletion
254- const timeDiff = Math . abs ( performance . now ( ) - lastDocumentDeleteTime )
255- if ( timeDiff < 500 && lastDocumentDeleteEvent && lastDocumentDeleteEvent . document . uri === document . uri ) {
248+ if ( this . documentEventListener . isLastEventDeletion ( document . uri . fsPath ) ) {
256249 getLogger ( ) . debug ( 'Skip auto trigger when deleting code' )
257250 return [ ]
258251 }
@@ -393,6 +386,20 @@ ${itemLog}
393386 return [ ]
394387 }
395388
389+ // delay the suggestion rendeing if user is actively typing
390+ // see https://github.com/aws/aws-toolkit-vscode/commit/a537602a96f498f372ed61ec9d82cf8577a9d854
391+ for ( let i = 0 ; i < 30 ; i ++ ) {
392+ const lastDocumentChange = this . documentEventListener . getLastDocumentChangeEvent ( document . uri . fsPath )
393+ if (
394+ lastDocumentChange &&
395+ performance . now ( ) - lastDocumentChange . timestamp < CodeWhispererConstants . inlineSuggestionShowDelay
396+ ) {
397+ await sleep ( CodeWhispererConstants . showRecommendationTimerPollPeriod )
398+ } else {
399+ break
400+ }
401+ }
402+
396403 // the user typed characters from invoking suggestion cursor position to receiving suggestion position
397404 const typeahead = document . getText ( new Range ( position , editor . selection . active ) )
398405
0 commit comments