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

Commit d08ba1b

Browse files
committed
Made some completion stuff properly work with the progress indicator
1 parent eaf1a3f commit d08ba1b

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

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

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@ package dev.koding.copilot.completion
22

33
import com.intellij.codeInsight.completion.*
44
import 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
68
import com.intellij.openapi.progress.ProgressManager
79
import com.intellij.openapi.util.TextRange
810
import dev.koding.copilot.completion.api.CompletionRequest
11+
import dev.koding.copilot.completion.api.CompletionResponse
912
import 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
1116
import kotlin.math.max
1217

1318

1419
class 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

src/main/kotlin/dev/koding/copilot/completion/api/CompletionData.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ data class CompletionRequest(
3131
header("Content-Type", "application/json")
3232
header("Accept", "application/json")
3333
header("Openai-Organization", "github-copilot")
34+
header("OpenAI-Intent", "copilot-ghost")
3435

3536
body = this@CompletionRequest
3637
}

0 commit comments

Comments
 (0)