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

Commit c19aed5

Browse files
committed
Added settings menu & plugin icon
1 parent aabbac1 commit c19aed5

File tree

7 files changed

+176
-15
lines changed

7 files changed

+176
-15
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import com.intellij.openapi.progress.ProgressManager
99
import com.intellij.openapi.util.TextRange
1010
import dev.koding.copilot.completion.api.CompletionRequest
1111
import dev.koding.copilot.completion.api.CompletionResponse
12+
import dev.koding.copilot.config.settings
1213
import dev.koding.copilot.copilotIcon
13-
import dev.koding.copilot.copilotToken
1414
import io.ktor.client.features.*
1515
import kotlinx.coroutines.GlobalScope
1616
import kotlinx.coroutines.launch
@@ -24,7 +24,7 @@ class CopilotCompletionContributor : CompletionContributor() {
2424
override fun fillCompletionVariants(parameters: CompletionParameters, result: CompletionResultSet) {
2525
if (parameters.isAutoPopup) return
2626

27-
if (copilotToken == null) {
27+
if (settings.token == null) {
2828
if (notified) return
2929
@Suppress("DialogTitleCapitalization")
3030
Notification(
@@ -39,22 +39,22 @@ class CopilotCompletionContributor : CompletionContributor() {
3939
val prompt = """
4040
// Language: ${parameters.originalFile.language.displayName}
4141
// Path: ${parameters.originalFile.name}
42-
${getPrompt(parameters)}
42+
${parameters.prompt}
4343
""".trimIndent()
4444

4545
val (prefix, suffix) = parameters.prefixSuffix
4646

4747
var response: CompletionResponse? = null
4848
val job = GlobalScope.launch {
4949
try {
50-
response = CompletionRequest(prompt).send(copilotToken)
50+
response = CompletionRequest(prompt).send(settings.token!!)
5151
} catch (e: ClientRequestException) {
5252
if (!notified) {
5353
@Suppress("DialogTitleCapitalization")
5454
Notification(
5555
"Error Report",
5656
"GitHub Copilot",
57-
"Failed to fetch response. Is your <code>GITHUB_COPILOT_TOKEN</code> environment variable up to date?",
57+
"Failed to fetch response. Is your copilot token valid?",
5858
NotificationType.ERROR
5959
).notify(parameters.editor.project)
6060
notified = true
@@ -111,11 +111,13 @@ class CopilotCompletionContributor : CompletionContributor() {
111111
})
112112
}
113113

114-
private fun getPrompt(parameters: CompletionParameters): String {
115-
val lineNumber = parameters.editor.document.getLineNumber(parameters.offset)
116-
val startOffset = parameters.editor.document.getLineStartOffset(max(0, lineNumber - 15))
117-
return parameters.editor.document.getText(TextRange(startOffset, parameters.offset))
118-
}
114+
private val CompletionParameters.prompt: String
115+
get() {
116+
val document = editor.document
117+
val lineNumber = document.getLineNumber(offset)
118+
val startOffset = document.getLineStartOffset(max(0, lineNumber - settings.contentLines))
119+
return document.getText(TextRange(startOffset, offset))
120+
}
119121

120122
private val CompletionParameters.prefixSuffix: Pair<String, String>
121123
get() {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="dev.koding.copilot.config.ApplicationConfigurable">
3+
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="3" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
4+
<margin top="15" left="15" bottom="15" right="15"/>
5+
<constraints>
6+
<xy x="20" y="20" width="614" height="400"/>
7+
</constraints>
8+
<properties/>
9+
<border type="none"/>
10+
<children>
11+
<component id="66007" class="javax.swing.JLabel">
12+
<constraints>
13+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
14+
</constraints>
15+
<properties>
16+
<font size="18"/>
17+
<text value="GitHub Copilot Settings"/>
18+
</properties>
19+
</component>
20+
<vspacer id="38819">
21+
<constraints>
22+
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
23+
</constraints>
24+
</vspacer>
25+
<grid id="351a0" layout-manager="GridLayoutManager" row-count="3" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="5" vgap="5">
26+
<margin top="5" left="5" bottom="5" right="5"/>
27+
<constraints>
28+
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
29+
</constraints>
30+
<properties/>
31+
<border type="none"/>
32+
<children>
33+
<vspacer id="f465c">
34+
<constraints>
35+
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
36+
</constraints>
37+
</vspacer>
38+
<component id="abee8" class="javax.swing.JLabel">
39+
<constraints>
40+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
41+
</constraints>
42+
<properties>
43+
<text value="Token"/>
44+
</properties>
45+
</component>
46+
<component id="f7d07" class="javax.swing.JLabel">
47+
<constraints>
48+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
49+
</constraints>
50+
<properties>
51+
<text value="Context Lines"/>
52+
</properties>
53+
</component>
54+
<hspacer id="45144">
55+
<constraints>
56+
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
57+
</constraints>
58+
</hspacer>
59+
<component id="35d3c" class="javax.swing.JFormattedTextField" binding="contextLines">
60+
<constraints>
61+
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
62+
<preferred-size width="150" height="-1"/>
63+
</grid>
64+
</constraints>
65+
<properties/>
66+
</component>
67+
<component id="759be" class="javax.swing.JTextField" binding="copilotToken">
68+
<constraints>
69+
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
70+
<preferred-size width="150" height="-1"/>
71+
</grid>
72+
</constraints>
73+
<properties/>
74+
</component>
75+
</children>
76+
</grid>
77+
</children>
78+
</grid>
79+
</form>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dev.koding.copilot.config
2+
3+
import com.intellij.openapi.options.Configurable
4+
import java.text.NumberFormat
5+
import javax.swing.JFormattedTextField
6+
import javax.swing.JPanel
7+
import javax.swing.JTextField
8+
import javax.swing.text.DefaultFormatterFactory
9+
import javax.swing.text.NumberFormatter
10+
11+
class ApplicationConfigurable : Configurable {
12+
13+
private lateinit var panel: JPanel
14+
private lateinit var copilotToken: JTextField
15+
private lateinit var contextLines: JFormattedTextField
16+
17+
override fun apply() {
18+
settings.token = copilotToken.text
19+
settings.contentLines = contextLines.text.toIntOrNull() ?: 10
20+
}
21+
22+
override fun createComponent(): JPanel {
23+
contextLines.formatterFactory = DefaultFormatterFactory(NumberFormatter(NumberFormat.getIntegerInstance()))
24+
return panel
25+
}
26+
27+
override fun reset() {
28+
copilotToken.text = settings.token ?: ""
29+
contextLines.text = settings.contentLines.toString()
30+
}
31+
32+
override fun getDisplayName() = "GitHub Copilot"
33+
override fun isModified() = settings.token != copilotToken.text ||
34+
settings.contentLines != (contextLines.text.toIntOrNull() ?: 10)
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package dev.koding.copilot.config
2+
3+
import com.intellij.openapi.components.PersistentStateComponent
4+
import com.intellij.openapi.components.State
5+
import com.intellij.openapi.components.Storage
6+
import com.intellij.openapi.components.service
7+
8+
val settings: ApplicationSettings
9+
get() = service()
10+
11+
@State(name = "GitHubCopilotSettings", storages = [Storage("copilot.xml")])
12+
class ApplicationSettings : PersistentStateComponent<ApplicationSettings.State> {
13+
14+
data class State(
15+
var token: String? = null,
16+
var contentLines: Int = 15
17+
)
18+
19+
private var state = State()
20+
21+
// TODO: Change this to delegation
22+
var token: String?
23+
get() = state.token
24+
set(value) = value.let { state.token = it }
25+
26+
var contentLines: Int
27+
get() = state.contentLines
28+
set(value) = value.let { state.contentLines = it }
29+
30+
override fun getState() = state
31+
override fun loadState(state: State) = state.let { this.state = it }
32+
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ 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-
97
val httpClient = HttpClient {
108
install(JsonFeature) {
119
serializer = GsonSerializer()

src/main/resources/META-INF/plugin.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
This <b>IS NOT</b> an official product of GitHub and is meant for specialized use only.
99
]]></description>
1010

11-
<!-- please see https://plugins.jetbrains.com/docs/intellij/plugin-compatibility.html
12-
on how to target different products -->
1311
<depends>com.intellij.modules.lang</depends>
14-
1512
<idea-version since-build="201.4515.24"/>
1613

1714
<extensions defaultExtensionNs="com.intellij">
15+
<applicationService serviceImplementation="dev.koding.copilot.config.ApplicationSettings"/>
16+
<applicationConfigurable displayName="GitHub Copilot"
17+
id="Settings.Copilot"
18+
groupId="tools"
19+
instance="dev.koding.copilot.config.ApplicationConfigurable"/>
20+
1821
<completion.contributor language="any"
1922
implementationClass="dev.koding.copilot.completion.CopilotCompletionContributor"
2023
order="first"/>
Lines changed: 12 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)