@@ -2,17 +2,24 @@ package dev.koding.copilot.completion
22
33import com.intellij.codeInsight.completion.*
44import com.intellij.codeInsight.lookup.LookupElementBuilder
5- import com.intellij.openapi.application.ex.ApplicationUtil
5+ import com.intellij.notification.Notification
6+ import com.intellij.notification.NotificationType
7+ import com.intellij.openapi.progress.ProcessCanceledException
68import com.intellij.openapi.progress.ProgressManager
79import com.intellij.openapi.util.TextRange
810import dev.koding.copilot.completion.api.CompletionRequest
11+ import dev.koding.copilot.completion.api.CompletionResponse
912import dev.koding.copilot.copilotIcon
10- import kotlinx.coroutines.runBlocking
13+ import io.ktor.client.features.*
14+ import kotlinx.coroutines.GlobalScope
15+ import kotlinx.coroutines.launch
1116import kotlin.math.max
1217
1318
1419class CopilotCompletionContributor : CompletionContributor () {
1520
21+ private var notified = false
22+
1623 override fun fillCompletionVariants (parameters : CompletionParameters , result : CompletionResultSet ) {
1724 if (parameters.isAutoPopup) return
1825
@@ -23,11 +30,39 @@ class CopilotCompletionContributor : CompletionContributor() {
2330 """ .trimIndent()
2431
2532 val (prefix, suffix) = parameters.prefixSuffix
26- val response = ApplicationUtil .runWithCheckCanceled({
27- return @runWithCheckCanceled runBlocking { CompletionRequest (prompt).send(System .getenv(" GITHUB_COPILOT_TOKEN" )) }
28- }, ProgressManager .getInstance().progressIndicator) ? : return
2933
30- val choices = response.choices.filter { it.text.isNotBlank() }
34+ var response: CompletionResponse ? = null
35+ val job = GlobalScope .launch {
36+ try {
37+ response = CompletionRequest (prompt).send(System .getenv(" GITHUB_COPILOT_TOKEN" ))
38+ } catch (e: ClientRequestException ) {
39+ if (! notified) {
40+ @Suppress(" DialogTitleCapitalization" )
41+ Notification (
42+ " Error Report" ,
43+ " GitHub Copilot" ,
44+ " Failed to fetch response. Is your <code>GITHUB_COPILOT_TOKEN</code> environment variable up to date?" ,
45+ NotificationType .ERROR
46+ ).notify(parameters.editor.project)
47+ notified = true
48+ }
49+
50+ return @launch result.stopHere()
51+ }
52+ }
53+
54+ if (result.isStopped) return
55+ while (response == null ) {
56+ try {
57+ ProgressManager .getInstance().progressIndicator.checkCanceled()
58+ Thread .sleep(10 )
59+ } catch (e: ProcessCanceledException ) {
60+ job.cancel()
61+ return result.stopHere()
62+ }
63+ }
64+
65+ val choices = response!! .choices.filter { it.text.isNotBlank() }
3166 if (choices.isEmpty()) return
3267
3368 val originalMatcher = result.prefixMatcher
@@ -66,7 +101,7 @@ class CopilotCompletionContributor : CompletionContributor() {
66101 private fun getPrompt (parameters : CompletionParameters ): String {
67102 // Using the parameters, get the last 10 lines of the current editor document and return their text
68103 val lineNumber = parameters.editor.document.getLineNumber(parameters.offset)
69- val startOffset = parameters.editor.document.getLineStartOffset(max(0 , lineNumber - 10 ))
104+ val startOffset = parameters.editor.document.getLineStartOffset(max(0 , lineNumber - 15 ))
70105 return parameters.editor.document.getText(TextRange (startOffset, parameters.offset))
71106 }
72107
0 commit comments