22 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33 * SPDX-License-Identifier: Apache-2.0
44 */
5-
5+ import * as vscode from 'vscode'
66import {
77 CancellationToken ,
88 InlineCompletionContext ,
@@ -40,11 +40,14 @@ import { InlineGeneratingMessage } from './inlineGeneratingMessage'
4040import { LineTracker } from './stateTracker/lineTracker'
4141import { InlineTutorialAnnotation } from './tutorials/inlineTutorialAnnotation'
4242import { TelemetryHelper } from './telemetryHelper'
43- import { Experiments , getLogger } from 'aws-core-vscode/shared'
43+ import { Experiments , getLogger , sleep } from 'aws-core-vscode/shared'
4444import { debounce , messageUtils } from 'aws-core-vscode/utils'
4545import { showEdits } from './EditRendering/imageRenderer'
4646import { ICursorUpdateRecorder } from './cursorUpdateManager'
4747
48+ let lastDocumentDeleteEvent : vscode . TextDocumentChangeEvent | undefined = undefined
49+ let lastDocumentDeleteTime = 0
50+
4851export class InlineCompletionManager implements Disposable {
4952 private disposable : Disposable
5053 private inlineCompletionProvider : AmazonQInlineCompletionItemProvider
@@ -55,6 +58,7 @@ export class InlineCompletionManager implements Disposable {
5558 private incomingGeneratingMessage : InlineGeneratingMessage
5659 private inlineTutorialAnnotation : InlineTutorialAnnotation
5760 private readonly logSessionResultMessageName = 'aws/logInlineCompletionSessionResults'
61+ private documentChangeListener : Disposable
5862
5963 constructor (
6064 languageClient : LanguageClient ,
@@ -79,6 +83,13 @@ export class InlineCompletionManager implements Disposable {
7983 this . sessionManager ,
8084 this . inlineTutorialAnnotation
8185 )
86+
87+ this . documentChangeListener = vscode . workspace . onDidChangeTextDocument ( ( e ) => {
88+ if ( e . contentChanges . length === 1 && e . contentChanges [ 0 ] . text === '' ) {
89+ lastDocumentDeleteEvent = e
90+ lastDocumentDeleteTime = performance . now ( )
91+ }
92+ } )
8293 this . disposable = languages . registerInlineCompletionItemProvider (
8394 CodeWhispererConstants . platformLanguageIds ,
8495 this . inlineCompletionProvider
@@ -97,6 +108,9 @@ export class InlineCompletionManager implements Disposable {
97108 this . incomingGeneratingMessage . dispose ( )
98109 this . lineTracker . dispose ( )
99110 }
111+ if ( this . documentChangeListener ) {
112+ this . documentChangeListener . dispose ( )
113+ }
100114 }
101115
102116 public registerInlineCompletion ( ) {
@@ -221,6 +235,16 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
221235 getLogger ( ) . info ( 'Recommendations already active, returning empty' )
222236 return [ ]
223237 }
238+ // yield event loop to let the document listen catch updates
239+ await sleep ( 1 )
240+ // prevent user deletion invoking auto trigger
241+ // this is a best effort estimate of deletion
242+ const timeDiff = Math . abs ( performance . now ( ) - lastDocumentDeleteTime )
243+ if ( timeDiff < 500 && lastDocumentDeleteEvent && lastDocumentDeleteEvent . document . uri === document . uri ) {
244+ getLogger ( ) . debug ( 'Skip auto trigger when deleting code' )
245+ return [ ]
246+ }
247+
224248 let logstr = `GenerateCompletion metadata:\\n`
225249 try {
226250 const t0 = performance . now ( )
0 commit comments