@@ -290,7 +290,26 @@ private CodyProposalCollection CreateAutocompleteProposals(AutocompleteResult au
290290 var startPos = ToPosition ( snapshot , item . Range . Start . Line , item . Range . Start . Character ) ;
291291 var endPos = ToPosition ( snapshot , item . Range . End . Line , item . Range . End . Character ) ;
292292
293- if ( endPos > snapshot . Length ) throw new ArgumentOutOfRangeException ( nameof ( endPos ) ) ;
293+ if ( endPos > snapshot . Length )
294+ {
295+ var oldText = snapshot . GetText ( startPos , snapshot . Length - startPos ) ;
296+ var oldTextStartLine = snapshot . GetLineFromLineNumber ( item . Range . Start . Line ) ? . GetText ( ) ;
297+ var dic = new Dictionary < string , object > ( )
298+ {
299+ [ "endPos" ] = endPos ,
300+ [ "snapshotLength" ] = snapshot . Length ,
301+ [ "startPos" ] = startPos ,
302+ [ "insertText" ] = item . InsertText ,
303+ [ "selectedItem" ] = completionState ? . SelectedItem ,
304+ [ "oldText" ] = oldText ,
305+ [ "oldTextStartLine" ] = oldTextStartLine
306+ } ;
307+
308+ var ex = new ArgumentOutOfRangeException ( nameof ( endPos ) ) ;
309+ ex . AddSentryContext ( "autocomplete" , dic ) ;
310+
311+ throw ex ;
312+ }
294313
295314 var ( currentText , offset ) = GetLinesOfOriginalText ( snapshot , startPos , endPos ) ;
296315
@@ -371,7 +390,7 @@ private CodyProposalCollection CreateAutoeditProposals(AutocompleteResult autoco
371390 var edits = new List < ProposedEdit > ( ) ;
372391 var snapshot = caret . Position . Snapshot ;
373392
374- var range = FindRange ( snapshot , item . Range . Start . Line , item . OriginalText ) ;
393+ var range = FindRange ( snapshot , item . Range . Start . Line , item . OriginalText , item . InsertText ) ;
375394 if ( range == null )
376395 {
377396 trace . TraceEvent ( "TextMismatch" ) ;
@@ -415,26 +434,48 @@ private CodyProposalCollection CreateAutoeditProposals(AutocompleteResult autoco
415434 return new CodyProposalCollection ( proposalList ) ;
416435 }
417436
418- private TextRange FindRange ( ITextSnapshot snapshot , int startLine , string originalText )
437+ private TextRange FindRange ( ITextSnapshot snapshot , int startLine , string originalText , string newText )
419438 {
420439 var lineCount = CountLines ( originalText ) ;
421- int ? offset = null ;
422440 var textBlock = new StringBuilder ( ) ;
423- for ( int lineNum = startLine ; lineNum < startLine + lineCount ; lineNum ++ )
441+ int count = 0 ;
442+ try
424443 {
425- var line = snapshot . GetLineFromLineNumber ( lineNum ) ;
426- if ( line == null ) break ;
427- if ( ! offset . HasValue ) offset = line . Start . Position ;
428- textBlock . Append ( line . GetTextIncludingLineBreak ( ) ) ;
429- }
430444
431- var block = textBlock . ToString ( ) ;
432- var index = block . IndexOf ( originalText ) ;
433- if ( index >= 0 ) return new TextRange
445+ int ? offset = null ;
446+
447+ for ( int lineNum = startLine ; lineNum < startLine + lineCount ; lineNum ++ )
448+ {
449+ var line = snapshot . GetLineFromLineNumber ( lineNum ) ;
450+ if ( line == null ) break ;
451+ if ( ! offset . HasValue ) offset = line . Start . Position ;
452+ textBlock . Append ( line . GetTextIncludingLineBreak ( ) ) ;
453+ count ++ ;
454+ }
455+
456+ var block = textBlock . ToString ( ) ;
457+ var index = block . IndexOf ( originalText ) ;
458+ if ( index >= 0 ) return new TextRange
459+ {
460+ Start = offset . Value + index ,
461+ End = offset . Value + index + originalText . Length
462+ } ;
463+ }
464+ catch ( InvalidOperationException ex )
434465 {
435- Start = offset . Value + index ,
436- End = offset . Value + index + originalText . Length
437- } ;
466+ var dic = new Dictionary < string , object > ( )
467+ {
468+ [ "startLine" ] = startLine ,
469+ [ "originalText" ] = originalText ,
470+ [ "newText" ] = newText ,
471+ [ "lineCount" ] = lineCount ,
472+ [ "textBlock" ] = textBlock . ToString ( ) ,
473+ [ "count" ] = count ,
474+ [ "snapshotLength" ] = snapshot . Length
475+ } ;
476+ ex . AddSentryContext ( "autocomplete" , dic ) ;
477+ throw ;
478+ }
438479
439480 return null ;
440481 }
0 commit comments