@@ -16,12 +16,6 @@ import { applyPatch, createPatch } from 'diff'
1616import { EditSuggestionState } from '../editSuggestionState'
1717import { debounce } from 'aws-core-vscode/utils'
1818
19- function logSuggestionFailure ( type : 'REJECT' , reason : string , suggestionContent : string ) {
20- getLogger ( 'nextEditPrediction' ) . debug (
21- `Auto ${ type } edit suggestion with reason=${ reason } , suggetion: ${ suggestionContent } `
22- )
23- }
24-
2519const autoRejectEditCursorDistance = 25
2620const maxPrefixRetryCharDiff = 5
2721const rerenderDeboucneInMs = 500
@@ -39,7 +33,10 @@ export class EditsSuggestionSvg {
3933
4034 private startLine = 0
4135
42- private docChanged : string = ''
36+ private documentChangeTrace = {
37+ contentChanged : '' ,
38+ count : 0 ,
39+ }
4340
4441 constructor (
4542 private suggestion : InlineCompletionItemWithReferences ,
@@ -114,12 +111,7 @@ export class EditsSuggestionSvg {
114111 const currentPosition = e . selections [ 0 ] . active
115112 const distance = Math . abs ( currentPosition . line - this . startLine )
116113 if ( distance > autoRejectEditCursorDistance ) {
117- logSuggestionFailure (
118- 'REJECT' ,
119- `cursor position move too far away off ${ autoRejectEditCursorDistance } lines` ,
120- this . suggestion . insertText as string
121- )
122- void vscode . commands . executeCommand ( 'aws.amazonq.inline.rejectEdit' )
114+ this . autoReject ( `cursor position move too far away off ${ autoRejectEditCursorDistance } lines` )
123115 }
124116 }
125117
@@ -140,7 +132,10 @@ export class EditsSuggestionSvg {
140132 // TODO: handle multi-contentChanges scenario
141133 const diff = e . contentChanges [ 0 ] ? e . contentChanges [ 0 ] . text : ''
142134 this . logger . info ( `docChange sessionId=${ this . session . sessionId } , contentChange=${ diff } ` )
143- this . docChanged += e . contentChanges [ 0 ] . text
135+
136+ // Track document changes because we might need to hide/reject suggestions while users are typing for better UX
137+ this . documentChangeTrace . contentChanged += e . contentChanges [ 0 ] . text
138+ this . documentChangeTrace . count ++
144139 /**
145140 * 1. Take the diff returned by the model and apply it to the code we originally sent to the model
146141 * 2. Do a diff between the above code and what's currently in the editor
@@ -153,14 +148,17 @@ export class EditsSuggestionSvg {
153148 if ( appliedToOriginal ) {
154149 const updatedPatch = this . patchSuggestion ( appliedToOriginal )
155150
156- if ( this . docChanged . length > maxPrefixRetryCharDiff ) {
157- this . logger . info ( `docChange: ${ this . docChanged } ` )
151+ if (
152+ this . documentChangeTrace . contentChanged . length > maxPrefixRetryCharDiff ||
153+ this . documentChangeTrace . count > maxPrefixRetryCharDiff
154+ ) {
155+ // Reject the suggestion if users've typed over 5 characters while the suggestion is shown
158156 this . autoReject ( RejectReason . MaxRetry )
159157 } else if ( applyPatch ( this . editor . document . getText ( ) , updatedPatch . insertText as string ) === false ) {
160158 this . autoReject ( RejectReason . DocumentChange )
161159 } else {
162160 // Close the previoius popup and rerender it
163- this . logger . info ( `calling rerender with suggestion\n ${ updatedPatch . insertText as string } ` )
161+ this . logger . debug ( `calling rerender with suggestion\n ${ updatedPatch . insertText as string } ` )
164162 await this . debouncedRerender ( updatedPatch )
165163 }
166164 } else {
@@ -190,6 +188,12 @@ export class EditsSuggestionSvg {
190188 }
191189
192190 private autoReject ( reason : string ) {
191+ function logSuggestionFailure ( type : 'REJECT' , reason : string , suggestionContent : string ) {
192+ getLogger ( 'nextEditPrediction' ) . debug (
193+ `Auto ${ type } edit suggestion with reason=${ reason } , suggetion: ${ suggestionContent } `
194+ )
195+ }
196+
193197 logSuggestionFailure ( 'REJECT' , reason , this . suggestion . insertText as string )
194198 void vscode . commands . executeCommand ( 'aws.amazonq.inline.rejectEdit' )
195199 }
0 commit comments