Skip to content

Commit 71393df

Browse files
committed
feat(custom): make custom prompt works
1 parent 9d7aef0 commit 71393df

File tree

7 files changed

+89
-3
lines changed

7 files changed

+89
-3
lines changed

java/src/main/kotlin/cc/unitmesh/idea/promting/JavaContextPrompter.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ open class JavaContextPrompter : ContextPrompter() {
175175

176176
ChatActionType.EXPLAIN_BUSINESS -> {
177177
}
178+
179+
ChatActionType.CUSTOM_ACTION -> TODO()
178180
}
179181

180182
return prompt

src/main/kotlin/cc/unitmesh/devti/custom/CustomIntention.kt

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package cc.unitmesh.devti.custom
22

3+
import cc.unitmesh.devti.custom.variable.*
34
import cc.unitmesh.devti.gui.chat.ChatActionType
5+
import cc.unitmesh.devti.gui.sendToChatPanel
46
import cc.unitmesh.devti.intentions.AbstractChatIntention
7+
import cc.unitmesh.devti.provider.ContextPrompter
8+
import cc.unitmesh.devti.provider.builtin.DefaultContextPrompter
59
import com.intellij.openapi.editor.Editor
610
import com.intellij.openapi.project.Project
711
import com.intellij.openapi.util.NlsSafe
812
import com.intellij.psi.PsiElement
913
import com.intellij.psi.PsiFile
14+
import org.apache.velocity.VelocityContext
15+
import org.apache.velocity.app.Velocity
16+
import java.io.StringWriter
1017

1118
class CustomIntention(private val intentionConfig: CustomIntentionConfig) : AbstractChatIntention() {
1219
val specConfig: Map<String, String> = CustomPromptConfig.load().spec
@@ -15,7 +22,7 @@ class CustomIntention(private val intentionConfig: CustomIntentionConfig) : Abst
1522

1623
override fun getFamilyName(): String = "Custom Intention"
1724

18-
override fun getActionType(): ChatActionType = super.getActionType()
25+
override fun getActionType(): ChatActionType = ChatActionType.CUSTOM_ACTION
1926

2027
override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
2128
if (editor == null || file == null) return
@@ -25,14 +32,46 @@ class CustomIntention(private val intentionConfig: CustomIntentionConfig) : Abst
2532

2633
val prompt: CustomIntentionPrompt = buildCustomPrompt(psiElement!!, selectedText, intentionConfig)
2734

35+
36+
sendToChatPanel(project, getActionType(), object : ContextPrompter() {
37+
override fun displayPrompt(): String {
38+
return prompt.displayPrompt
39+
}
40+
41+
override fun requestPrompt(): String {
42+
return prompt.requestPrompt
43+
}
44+
})
2845
}
2946

3047
private fun buildCustomPrompt(
3148
psiElement: PsiElement,
3249
selectedText: @NlsSafe String,
3350
config: CustomIntentionConfig,
3451
): CustomIntentionPrompt {
35-
return CustomIntentionPrompt(selectedText, selectedText, listOf())
52+
val stringBuilderWriter = StringWriter()
53+
val velocityContext = VelocityContext()
54+
55+
val resolverMap = LinkedHashMap<CustomIntentionVariableType, VariableResolver>(10)
56+
57+
val variableResolvers = arrayOf(
58+
MethodInputOutputVariableResolver(psiElement),
59+
SelectionVariableResolver(psiElement.language.displayName ?: "", selectedText),
60+
) + SpecResolverService.getInstance().createResolvers()
61+
62+
for (resolver in variableResolvers) {
63+
resolverMap[resolver.type] = resolver
64+
}
65+
66+
resolverMap.forEach { (variableType, resolver) ->
67+
val value = resolver.resolve()
68+
velocityContext.put(variableType.toString(), value)
69+
}
70+
71+
Velocity.evaluate(velocityContext, stringBuilderWriter, "", intentionConfig.template)
72+
val output = stringBuilderWriter.toString()
73+
74+
return CustomIntentionPrompt(output, output, listOf())
3675
}
3776

3877
companion object {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cc.unitmesh.devti.custom.variable
2+
3+
import com.intellij.openapi.components.Service
4+
5+
@Service(Service.Level.APP)
6+
class AllVariablesResolverService {
7+
8+
fun buildResolver() {
9+
10+
}
11+
12+
13+
}

src/main/kotlin/cc/unitmesh/devti/custom/variable/CustomIntentionVariableType.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ package cc.unitmesh.devti.custom.variable
33
enum class CustomIntentionVariableType(@JvmField val description: String) {
44
SELECTION("Currently selected code fragment with language name"),
55
METHOD_INPUT_OUTPUT("Method input parameters's class as code snippets"),
6+
SPEC_VARIABLE("Load from spec config, and config to items"),
67
;
78
}

src/main/kotlin/cc/unitmesh/devti/custom/variable/SelectionVariableResolver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cc.unitmesh.devti.custom.variable
22

33
class SelectionVariableResolver(
4-
private val languageName: String,
4+
val languageName: String,
55
val code: String,
66
) : VariableResolver {
77
override val type: CustomIntentionVariableType = CustomIntentionVariableType.SELECTION
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cc.unitmesh.devti.custom.variable
2+
3+
import cc.unitmesh.devti.custom.CustomPromptConfig
4+
import com.intellij.openapi.application.ApplicationManager
5+
import com.intellij.openapi.components.Service
6+
7+
@Service(Service.Level.APP)
8+
class SpecResolverService {
9+
private val specs = CustomPromptConfig.load().spec
10+
11+
fun createResolvers(): List<SpecVariableResolver> {
12+
return specs.map { (key, value) ->
13+
SpecVariableResolver(key, value)
14+
}
15+
}
16+
17+
companion object {
18+
fun getInstance(): SpecResolverService {
19+
return ApplicationManager.getApplication().getService(SpecResolverService::class.java)
20+
}
21+
}
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cc.unitmesh.devti.custom.variable
2+
3+
class SpecVariableResolver(val key: String, val value: String) : VariableResolver {
4+
override val type: CustomIntentionVariableType get() = CustomIntentionVariableType.SPEC_VARIABLE
5+
6+
override fun resolve(): String {
7+
return value
8+
}
9+
}

0 commit comments

Comments
 (0)