Skip to content

Commit 9d7aef0

Browse files
committed
feat(custom): add basic variable
1 parent 6ce7391 commit 9d7aef0

File tree

6 files changed

+83
-0
lines changed

6 files changed

+83
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import cc.unitmesh.devti.gui.chat.ChatActionType
44
import cc.unitmesh.devti.intentions.AbstractChatIntention
55
import com.intellij.openapi.editor.Editor
66
import com.intellij.openapi.project.Project
7+
import com.intellij.openapi.util.NlsSafe
8+
import com.intellij.psi.PsiElement
79
import com.intellij.psi.PsiFile
810

911
class CustomIntention(private val intentionConfig: CustomIntentionConfig) : AbstractChatIntention() {
12+
val specConfig: Map<String, String> = CustomPromptConfig.load().spec
1013

1114
override fun getText(): String = intentionConfig.title
1215

@@ -15,7 +18,21 @@ class CustomIntention(private val intentionConfig: CustomIntentionConfig) : Abst
1518
override fun getActionType(): ChatActionType = super.getActionType()
1619

1720
override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
21+
if (editor == null || file == null) return
22+
val withRange = elementWithRange(editor, file, project) ?: return
23+
val selectedText = withRange.first
24+
val psiElement = withRange.second
1825

26+
val prompt: CustomIntentionPrompt = buildCustomPrompt(psiElement!!, selectedText, intentionConfig)
27+
28+
}
29+
30+
private fun buildCustomPrompt(
31+
psiElement: PsiElement,
32+
selectedText: @NlsSafe String,
33+
config: CustomIntentionConfig,
34+
): CustomIntentionPrompt {
35+
return CustomIntentionPrompt(selectedText, selectedText, listOf())
1936
}
2037

2138
companion object {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cc.unitmesh.devti.custom
2+
3+
import cc.unitmesh.devti.provider.context.ChatContextItem
4+
5+
class CustomIntentionPrompt(
6+
val displayPrompt: String,
7+
val requestPrompt: String,
8+
val contextItems: List<ChatContextItem> = listOf()
9+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cc.unitmesh.devti.custom.variable
2+
3+
enum class CustomIntentionVariableType(@JvmField val description: String) {
4+
SELECTION("Currently selected code fragment with language name"),
5+
METHOD_INPUT_OUTPUT("Method input parameters's class as code snippets"),
6+
;
7+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cc.unitmesh.devti.custom.variable
2+
3+
import cc.unitmesh.devti.context.MethodContextProvider
4+
import com.intellij.psi.PsiElement
5+
6+
class MethodInputOutputVariableResolver(val element: PsiElement) : VariableResolver {
7+
override val type: CustomIntentionVariableType = CustomIntentionVariableType.METHOD_INPUT_OUTPUT
8+
9+
override fun resolve(): String {
10+
var result = ""
11+
val methodContext = MethodContextProvider(false, false).from(element)
12+
if (methodContext.name == null) {
13+
return ""
14+
}
15+
16+
val input = methodContext.signature ?: ""
17+
val output = methodContext.returnType ?: ""
18+
19+
// skip input if it is empty, skip output if it is void
20+
if (input.isNotEmpty()) {
21+
result += "Input: $input\n"
22+
}
23+
if (output.isNotEmpty() && output != "void") {
24+
result += "Output: $output\n"
25+
}
26+
27+
return result
28+
}
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cc.unitmesh.devti.custom.variable
2+
3+
class SelectionVariableResolver(
4+
private val languageName: String,
5+
val code: String,
6+
) : VariableResolver {
7+
override val type: CustomIntentionVariableType = CustomIntentionVariableType.SELECTION
8+
9+
override fun resolve(): String {
10+
return """
11+
|```$languageName
12+
|$code
13+
|```""".trimMargin()
14+
}
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package cc.unitmesh.devti.custom.variable
2+
3+
interface VariableResolver {
4+
val type: CustomIntentionVariableType
5+
fun resolve(): String
6+
}

0 commit comments

Comments
 (0)