Skip to content
This repository was archived by the owner on Oct 24, 2021. It is now read-only.

Commit eaf1a3f

Browse files
committed
Fixing completion insertions further
1 parent 3f556f2 commit eaf1a3f

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/main/kotlin/dev/koding/copilot/completion/CopilotCompletionContributor.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)