@@ -40,15 +40,21 @@ class CopilotCompletionContributor : CompletionContributor() {
4040
4141 set.restartCompletionOnAnyPrefixChange()
4242 set.addAllElements(choices.map { choice ->
43- val completion = choice.text.removePrefix(prefix).removeSuffix(suffix)
44- .let {
45- val split = prefix.split(" ." )
46- if (split.size >= 2 ) " ${split.last()}$it " else it
47- }
43+ val completion = choice.text.removePrefix(prefix.trim()).removeSuffix(suffix.trim())
44+ val insert = " $prefix$completion \n "
4845
4946 PrioritizedLookupElement .withPriority(
50- LookupElementBuilder .create(choice, completion)
51- .withPresentableText(prefix)
47+ LookupElementBuilder .create(choice, " " )
48+ .withInsertHandler { context, _ ->
49+ val caret = context.editor.caretModel
50+ val startOffset = caret.visualLineStart
51+ val endOffset = caret.visualLineEnd
52+
53+ context.document.deleteString(startOffset, endOffset)
54+ context.document.insertString(startOffset, insert)
55+ caret.moveToOffset(startOffset + insert.length - 1 )
56+ }
57+ .withPresentableText(prefix.split(" ." ).last().trim())
5258 .withTailText(completion, true )
5359 .withTypeText(" GitHub Copilot" )
5460 .withIcon(copilotIcon)
@@ -73,7 +79,7 @@ class CopilotCompletionContributor : CompletionContributor() {
7379 val lineEnd = document.getLineEndOffset(lineNumber)
7480
7581 val start = document.getText(TextRange .create(lineStart, offset))
76- val end = document.getText(TextRange .create(offset, lineEnd)).trim()
82+ val end = document.getText(TextRange .create(offset, lineEnd))
7783 return start to end
7884 }
7985
0 commit comments