2
2
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
- import * as vscode from 'vscode'
5
+
6
6
import {
7
7
CancellationToken ,
8
8
InlineCompletionContext ,
@@ -46,9 +46,7 @@ import { Experiments, getLogger, sleep } from 'aws-core-vscode/shared'
46
46
import { debounce , messageUtils } from 'aws-core-vscode/utils'
47
47
import { showEdits } from './EditRendering/imageRenderer'
48
48
import { ICursorUpdateRecorder } from './cursorUpdateManager'
49
-
50
- let lastDocumentDeleteEvent : vscode . TextDocumentChangeEvent | undefined = undefined
51
- let lastDocumentDeleteTime = 0
49
+ import { DocumentEventListener } from './documentEventListener'
52
50
53
51
export class InlineCompletionManager implements Disposable {
54
52
private disposable : Disposable
@@ -60,7 +58,7 @@ export class InlineCompletionManager implements Disposable {
60
58
61
59
private inlineTutorialAnnotation : InlineTutorialAnnotation
62
60
private readonly logSessionResultMessageName = 'aws/logInlineCompletionSessionResults'
63
- private documentChangeListener : Disposable
61
+ private documentEventListener : DocumentEventListener
64
62
65
63
constructor (
66
64
languageClient : LanguageClient ,
@@ -74,24 +72,19 @@ export class InlineCompletionManager implements Disposable {
74
72
this . lineTracker = lineTracker
75
73
this . recommendationService = new RecommendationService ( this . sessionManager , cursorUpdateRecorder )
76
74
this . inlineTutorialAnnotation = inlineTutorialAnnotation
75
+ this . documentEventListener = new DocumentEventListener ( )
77
76
this . inlineCompletionProvider = new AmazonQInlineCompletionItemProvider (
78
77
languageClient ,
79
78
this . recommendationService ,
80
79
this . sessionManager ,
81
- this . inlineTutorialAnnotation
80
+ this . inlineTutorialAnnotation ,
81
+ this . documentEventListener
82
82
)
83
83
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
- } )
90
84
this . disposable = languages . registerInlineCompletionItemProvider (
91
85
CodeWhispererConstants . platformLanguageIds ,
92
86
this . inlineCompletionProvider
93
87
)
94
-
95
88
this . lineTracker . ready ( )
96
89
}
97
90
@@ -104,8 +97,8 @@ export class InlineCompletionManager implements Disposable {
104
97
this . disposable . dispose ( )
105
98
this . lineTracker . dispose ( )
106
99
}
107
- if ( this . documentChangeListener ) {
108
- this . documentChangeListener . dispose ( )
100
+ if ( this . documentEventListener ) {
101
+ this . documentEventListener . dispose ( )
109
102
}
110
103
}
111
104
@@ -211,7 +204,8 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
211
204
private readonly languageClient : LanguageClient ,
212
205
private readonly recommendationService : RecommendationService ,
213
206
private readonly sessionManager : SessionManager ,
214
- private readonly inlineTutorialAnnotation : InlineTutorialAnnotation
207
+ private readonly inlineTutorialAnnotation : InlineTutorialAnnotation ,
208
+ private readonly documentEventListener : DocumentEventListener
215
209
) { }
216
210
217
211
private readonly logSessionResultMessageName = 'aws/logInlineCompletionSessionResults'
@@ -251,8 +245,7 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
251
245
await sleep ( 1 )
252
246
// prevent user deletion invoking auto trigger
253
247
// 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 ) ) {
256
249
getLogger ( ) . debug ( 'Skip auto trigger when deleting code' )
257
250
return [ ]
258
251
}
@@ -393,6 +386,20 @@ ${itemLog}
393
386
return [ ]
394
387
}
395
388
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
+
396
403
// the user typed characters from invoking suggestion cursor position to receiving suggestion position
397
404
const typeahead = document . getText ( new Range ( position , editor . selection . active ) )
398
405
0 commit comments