@@ -10,6 +10,7 @@ import com.intellij.openapi.util.TextRange
1010import dev.koding.copilot.completion.api.CompletionRequest
1111import dev.koding.copilot.completion.api.CompletionResponse
1212import dev.koding.copilot.copilotIcon
13+ import dev.koding.copilot.copilotToken
1314import io.ktor.client.features.*
1415import kotlinx.coroutines.GlobalScope
1516import kotlinx.coroutines.launch
@@ -23,6 +24,18 @@ class CopilotCompletionContributor : CompletionContributor() {
2324 override fun fillCompletionVariants (parameters : CompletionParameters , result : CompletionResultSet ) {
2425 if (parameters.isAutoPopup) return
2526
27+ if (copilotToken == null ) {
28+ if (notified) return
29+ @Suppress(" DialogTitleCapitalization" )
30+ Notification (
31+ " Error Report" ,
32+ " GitHub Copilot" ,
33+ " You have not set a token for GitHub Copilot." ,
34+ NotificationType .ERROR
35+ ).notify(parameters.editor.project)
36+ return run { notified = true }
37+ }
38+
2639 val prompt = """
2740 // Language: ${parameters.originalFile.language.displayName}
2841 // Path: ${parameters.originalFile.name}
@@ -34,7 +47,7 @@ class CopilotCompletionContributor : CompletionContributor() {
3447 var response: CompletionResponse ? = null
3548 val job = GlobalScope .launch {
3649 try {
37- response = CompletionRequest (prompt).send(System .getenv( " GITHUB_COPILOT_TOKEN " ) )
50+ response = CompletionRequest (prompt).send(copilotToken )
3851 } catch (e: ClientRequestException ) {
3952 if (! notified) {
4053 @Suppress(" DialogTitleCapitalization" )
@@ -76,7 +89,7 @@ class CopilotCompletionContributor : CompletionContributor() {
7689 set.restartCompletionOnAnyPrefixChange()
7790 set.addAllElements(choices.map { choice ->
7891 val completion = choice.text.removePrefix(prefix.trim()).removeSuffix(suffix.trim())
79- val insert = " $prefix$completion \n "
92+ val insert = " $prefix${ completion.trim()} \n "
8093
8194 PrioritizedLookupElement .withPriority(
8295 LookupElementBuilder .create(choice, " " )
@@ -89,7 +102,7 @@ class CopilotCompletionContributor : CompletionContributor() {
89102 context.document.insertString(startOffset, insert)
90103 caret.moveToOffset(startOffset + insert.length - 1 )
91104 }
92- .withPresentableText(prefix.split(" ." ).last().trim() )
105+ .withPresentableText(prefix.split(" ." ).last())
93106 .withTailText(completion, true )
94107 .withTypeText(" GitHub Copilot" )
95108 .withIcon(copilotIcon)
@@ -99,7 +112,6 @@ class CopilotCompletionContributor : CompletionContributor() {
99112 }
100113
101114 private fun getPrompt (parameters : CompletionParameters ): String {
102- // Using the parameters, get the last 10 lines of the current editor document and return their text
103115 val lineNumber = parameters.editor.document.getLineNumber(parameters.offset)
104116 val startOffset = parameters.editor.document.getLineStartOffset(max(0 , lineNumber - 15 ))
105117 return parameters.editor.document.getText(TextRange (startOffset, parameters.offset))
0 commit comments