2
2
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
-
5
+ import * as vscode from 'vscode'
6
6
import {
7
7
CancellationToken ,
8
8
InlineCompletionContext ,
@@ -40,11 +40,14 @@ import { InlineGeneratingMessage } from './inlineGeneratingMessage'
40
40
import { LineTracker } from './stateTracker/lineTracker'
41
41
import { InlineTutorialAnnotation } from './tutorials/inlineTutorialAnnotation'
42
42
import { TelemetryHelper } from './telemetryHelper'
43
- import { Experiments , getLogger } from 'aws-core-vscode/shared'
43
+ import { Experiments , getLogger , sleep } from 'aws-core-vscode/shared'
44
44
import { debounce , messageUtils } from 'aws-core-vscode/utils'
45
45
import { showEdits } from './EditRendering/imageRenderer'
46
46
import { ICursorUpdateRecorder } from './cursorUpdateManager'
47
47
48
+ let lastDocumentDeleteEvent : vscode . TextDocumentChangeEvent | undefined = undefined
49
+ let lastDocumentDeleteTime = 0
50
+
48
51
export class InlineCompletionManager implements Disposable {
49
52
private disposable : Disposable
50
53
private inlineCompletionProvider : AmazonQInlineCompletionItemProvider
@@ -55,6 +58,7 @@ export class InlineCompletionManager implements Disposable {
55
58
private incomingGeneratingMessage : InlineGeneratingMessage
56
59
private inlineTutorialAnnotation : InlineTutorialAnnotation
57
60
private readonly logSessionResultMessageName = 'aws/logInlineCompletionSessionResults'
61
+ private documentChangeListener : Disposable
58
62
59
63
constructor (
60
64
languageClient : LanguageClient ,
@@ -79,6 +83,13 @@ export class InlineCompletionManager implements Disposable {
79
83
this . sessionManager ,
80
84
this . inlineTutorialAnnotation
81
85
)
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
+ } )
82
93
this . disposable = languages . registerInlineCompletionItemProvider (
83
94
CodeWhispererConstants . platformLanguageIds ,
84
95
this . inlineCompletionProvider
@@ -97,6 +108,9 @@ export class InlineCompletionManager implements Disposable {
97
108
this . incomingGeneratingMessage . dispose ( )
98
109
this . lineTracker . dispose ( )
99
110
}
111
+ if ( this . documentChangeListener ) {
112
+ this . documentChangeListener . dispose ( )
113
+ }
100
114
}
101
115
102
116
public registerInlineCompletion ( ) {
@@ -221,6 +235,16 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
221
235
getLogger ( ) . info ( 'Recommendations already active, returning empty' )
222
236
return [ ]
223
237
}
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
+
224
248
let logstr = `GenerateCompletion metadata:\\n`
225
249
try {
226
250
const t0 = performance . now ( )
0 commit comments