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,11 +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
52-
53- let lastDocumentChangeEventMap : Map < string , vscode . TextDocumentChangeEvent > = new Map ( )
49+ import { DocumentEventListener } from './documentEventListener'
5450
5551export class InlineCompletionManager implements Disposable {
5652 private disposable : Disposable
@@ -62,7 +58,7 @@ export class InlineCompletionManager implements Disposable {
6258
6359 private inlineTutorialAnnotation : InlineTutorialAnnotation
6460 private readonly logSessionResultMessageName = 'aws/logInlineCompletionSessionResults'
65- private documentChangeListener : Disposable
61+ private documentEventListener : DocumentEventListener
6662
6763 constructor (
6864 languageClient : LanguageClient ,
@@ -76,27 +72,19 @@ export class InlineCompletionManager implements Disposable {
7672 this . lineTracker = lineTracker
7773 this . recommendationService = new RecommendationService ( this . sessionManager , cursorUpdateRecorder )
7874 this . inlineTutorialAnnotation = inlineTutorialAnnotation
75+ this . documentEventListener = new DocumentEventListener ( )
7976 this . inlineCompletionProvider = new AmazonQInlineCompletionItemProvider (
8077 languageClient ,
8178 this . recommendationService ,
8279 this . sessionManager ,
83- this . inlineTutorialAnnotation
80+ this . inlineTutorialAnnotation ,
81+ this . documentEventListener
8482 )
8583
86- this . documentChangeListener = vscode . workspace . onDidChangeTextDocument ( ( e ) => {
87- if ( e . contentChanges . length === 1 && e . contentChanges [ 0 ] . text === '' ) {
88- lastDocumentDeleteEvent = e
89- lastDocumentDeleteTime = performance . now ( )
90- }
91- if ( e . contentChanges . length > 0 ) {
92- lastDocumentChangeEventMap . set ( e . document . uri . fsPath , e )
93- }
94- } )
9584 this . disposable = languages . registerInlineCompletionItemProvider (
9685 CodeWhispererConstants . platformLanguageIds ,
9786 this . inlineCompletionProvider
9887 )
99-
10088 this . lineTracker . ready ( )
10189 }
10290
@@ -109,8 +97,8 @@ export class InlineCompletionManager implements Disposable {
10997 this . disposable . dispose ( )
11098 this . lineTracker . dispose ( )
11199 }
112- if ( this . documentChangeListener ) {
113- this . documentChangeListener . dispose ( )
100+ if ( this . documentEventListener ) {
101+ this . documentEventListener . dispose ( )
114102 }
115103 }
116104
@@ -216,7 +204,8 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
216204 private readonly languageClient : LanguageClient ,
217205 private readonly recommendationService : RecommendationService ,
218206 private readonly sessionManager : SessionManager ,
219- private readonly inlineTutorialAnnotation : InlineTutorialAnnotation
207+ private readonly inlineTutorialAnnotation : InlineTutorialAnnotation ,
208+ private readonly documentEventListener : DocumentEventListener
220209 ) { }
221210
222211 private readonly logSessionResultMessageName = 'aws/logInlineCompletionSessionResults'
@@ -256,14 +245,11 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
256245 await sleep ( 1 )
257246 // prevent user deletion invoking auto trigger
258247 // this is a best effort estimate of deletion
259- const timeDiff = Math . abs ( performance . now ( ) - lastDocumentDeleteTime )
260- if ( timeDiff < 500 && lastDocumentDeleteEvent && lastDocumentDeleteEvent . document . uri === document . uri ) {
248+ if ( this . documentEventListener . isLastEventDeletion ( document . uri . fsPath ) ) {
261249 getLogger ( ) . debug ( 'Skip auto trigger when deleting code' )
262250 return [ ]
263251 }
264252
265- const event = lastDocumentChangeEventMap . get ( document . uri . fsPath ) || undefined
266- console . log ( event )
267253 let logstr = `GenerateCompletion metadata:\\n`
268254 try {
269255 const t0 = performance . now ( )
0 commit comments