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

Commit aabbac1

Browse files
committed
Some minor cleanup
1 parent d08ba1b commit aabbac1

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.intellij.openapi.util.TextRange
1010
import dev.koding.copilot.completion.api.CompletionRequest
1111
import dev.koding.copilot.completion.api.CompletionResponse
1212
import dev.koding.copilot.copilotIcon
13+
import dev.koding.copilot.copilotToken
1314
import io.ktor.client.features.*
1415
import kotlinx.coroutines.GlobalScope
1516
import 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))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ data class CompletionRequest(
2525
val logProbability: Int = 2,
2626
val stop: List<String> = listOf("\n")
2727
) {
28-
suspend fun send(token: String): CompletionResponse? =
29-
httpClient.post<CompletionResponse>("https://copilot.githubassets.com/v1/engines/github-multi-stochbpe-cushman-pii/completions") {
28+
suspend fun send(token: String): CompletionResponse =
29+
httpClient.post("https://copilot.githubassets.com/v1/engines/github-multi-stochbpe-cushman-pii/completions") {
3030
header("Authorization", "Bearer $token")
3131
header("Content-Type", "application/json")
3232
header("Accept", "application/json")

src/main/kotlin/dev/koding/copilot/const.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import com.intellij.openapi.util.IconLoader
44
import io.ktor.client.*
55
import io.ktor.client.features.json.*
66

7+
val copilotToken: String? = System.getenv("GITHUB_COPILOT_TOKEN") ?: System.getProperty("copilot.token")
8+
79
val httpClient = HttpClient {
810
install(JsonFeature) {
911
serializer = GsonSerializer()

0 commit comments

Comments
 (0)