@@ -186,11 +186,16 @@ export class InlineCompletionItem extends InlineSuggestionItemBase {
186
186
textModel : ITextModel ,
187
187
) : InlineCompletionItem {
188
188
const identity = new InlineSuggestionIdentity ( ) ;
189
- const textEdit = new TextReplacement ( data . range , data . insertText ) ;
190
- const edit = getPositionOffsetTransformerFromTextModel ( textModel ) . getStringReplacement ( textEdit ) ;
189
+ const transformer = getPositionOffsetTransformerFromTextModel ( textModel ) ;
190
+
191
+ const insertText = data . insertText . replace ( / \r \n | \r | \n / g, textModel . getEOL ( ) ) ;
192
+
193
+ const edit = reshapeInlineCompletion ( new StringReplacement ( transformer . getOffsetRange ( data . range ) , insertText ) , textModel ) ;
194
+ const textEdit = transformer . getSingleTextEdit ( edit ) ;
195
+
191
196
const displayLocation = data . displayLocation ? InlineSuggestDisplayLocation . create ( data . displayLocation , textModel ) : undefined ;
192
197
193
- return new InlineCompletionItem ( edit , textEdit , data . range , data . snippetInfo , data . additionalTextEdits , data , identity , displayLocation ) ;
198
+ return new InlineCompletionItem ( edit , textEdit , textEdit . range , data . snippetInfo , data . additionalTextEdits , data , identity , displayLocation ) ;
194
199
}
195
200
196
201
public readonly isInlineEdit = false ;
@@ -456,7 +461,7 @@ function getStringEdit(textModel: ITextModel, editRange: Range, replaceText: str
456
461
const edit = new StringReplacement ( originalRange , replaceText ) ;
457
462
458
463
const originalText = textModel . getValueInRange ( rangeInModel ) ;
459
- return reshapeEdit ( edit , originalText , innerChanges . length , textModel ) ;
464
+ return reshapeInlineEdit ( edit , originalText , innerChanges . length , textModel ) ;
460
465
} )
461
466
) ;
462
467
@@ -591,7 +596,18 @@ class SingleUpdatedNextEdit {
591
596
}
592
597
}
593
598
594
- function reshapeEdit ( edit : StringReplacement , originalText : string , totalInnerEdits : number , textModel : ITextModel ) : StringReplacement {
599
+ function reshapeInlineCompletion ( edit : StringReplacement , textModel : ITextModel ) : StringReplacement {
600
+ // If the insertion is a multi line insertion starting on the next line
601
+ // Move it forwards so that the multi line insertion starts on the current line
602
+ const eol = textModel . getEOL ( ) ;
603
+ if ( edit . replaceRange . isEmpty && edit . newText . includes ( eol ) ) {
604
+ edit = reshapeMultiLineInsertion ( edit , textModel ) ;
605
+ }
606
+
607
+ return edit ;
608
+ }
609
+
610
+ function reshapeInlineEdit ( edit : StringReplacement , originalText : string , totalInnerEdits : number , textModel : ITextModel ) : StringReplacement {
595
611
// TODO: EOL are not properly trimmed by the diffAlgorithm #12680
596
612
const eol = textModel . getEOL ( ) ;
597
613
if ( edit . newText . endsWith ( eol ) && originalText . endsWith ( eol ) ) {
@@ -602,7 +618,11 @@ function reshapeEdit(edit: StringReplacement, originalText: string, totalInnerEd
602
618
// If the insertion ends with a new line and is inserted at the start of a line which has text,
603
619
// we move the insertion to the end of the previous line if possible
604
620
if ( totalInnerEdits === 1 && edit . replaceRange . isEmpty && edit . newText . includes ( eol ) ) {
605
- edit = reshapeMultiLineInsertion ( edit , textModel ) ;
621
+ const startPosition = textModel . getPositionAt ( edit . replaceRange . start ) ;
622
+ const hasTextOnInsertionLine = textModel . getLineLength ( startPosition . lineNumber ) !== 0 ;
623
+ if ( hasTextOnInsertionLine ) {
624
+ edit = reshapeMultiLineInsertion ( edit , textModel ) ;
625
+ }
606
626
}
607
627
608
628
// The diff algorithm extended a simple edit to the entire word
@@ -641,7 +661,7 @@ function reshapeMultiLineInsertion(edit: StringReplacement, textModel: ITextMode
641
661
642
662
// If the insertion ends with a new line and is inserted at the start of a line which has text,
643
663
// we move the insertion to the end of the previous line if possible
644
- if ( startColumn === 1 && startLineNumber > 1 && textModel . getLineLength ( startLineNumber ) !== 0 && edit . newText . endsWith ( eol ) && ! edit . newText . startsWith ( eol ) ) {
664
+ if ( startColumn === 1 && startLineNumber > 1 && edit . newText . endsWith ( eol ) && ! edit . newText . startsWith ( eol ) ) {
645
665
return new StringReplacement ( edit . replaceRange . delta ( - 1 ) , eol + edit . newText . slice ( 0 , - eol . length ) ) ;
646
666
}
647
667
0 commit comments