Skip to content

Commit bf2440d

Browse files
committed
feat(custom-variable): improve variable list component and add custom variable support #51
1 parent e5993a4 commit bf2440d

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

src/main/kotlin/cc/unitmesh/devti/custom/team/TeamPromptTemplateCompiler.kt

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cc.unitmesh.devti.custom.team
22

3+
import cc.unitmesh.devti.custom.variable.CustomVariable
34
import cc.unitmesh.devti.gui.chat.ChatActionType
45
import cc.unitmesh.devti.provider.context.ChatContextProvider
56
import cc.unitmesh.devti.provider.context.ChatCreationContext
@@ -22,23 +23,24 @@ class TeamPromptTemplateCompiler(
2223
val editor: Editor,
2324
val selectedText: String = "",
2425
) {
26+
private val log = logger<TeamPromptTemplateCompiler>()
2527
private val velocityContext = VelocityContext()
2628

2729
init {
28-
this.set("selection", selectedText)
29-
this.set("beforeCursor", file.text.substring(0, editor.caretModel.offset))
30-
this.set("afterCursor", file.text.substring(editor.caretModel.offset))
30+
this.set(CustomVariable.SELECTION.variable, selectedText)
31+
this.set(CustomVariable.BEFORE_CURSOR.variable, file.text.substring(0, editor.caretModel.offset))
32+
this.set(CustomVariable.AFTER_CURSOR.variable, file.text.substring(editor.caretModel.offset))
3133
}
3234

3335
fun set(key: String, value: String) {
3436
velocityContext.put(key, value)
3537
}
3638

3739
fun compile(template: String): String {
38-
velocityContext.put("fileName", file.name)
39-
velocityContext.put("filePath", file.virtualFile?.path ?: "")
40+
velocityContext.put(CustomVariable.FILE_NAME.variable, file.name)
41+
velocityContext.put(CustomVariable.FILE_PATH.variable, file.virtualFile?.path ?: "")
4042
velocityContext.put(
41-
"methodName", when (element) {
43+
CustomVariable.METHOD_NAME.variable, when (element) {
4244
is PsiNameIdentifierOwner -> element.nameIdentifier?.text ?: ""
4345
else -> ""
4446
}
@@ -74,16 +76,16 @@ class TeamPromptTemplateCompiler(
7476
)
7577

7678
val collectChatContextList = ChatContextProvider.collectChatContextList(file.project, context)
77-
velocityContext.put("frameworkContext", collectChatContextList.joinToString("\n") {
79+
velocityContext.put(CustomVariable.FRAMEWORK_CONTEXT.variable, collectChatContextList.joinToString("\n") {
7880
it.text
7981
})
8082
}
8183
}
8284

8385
private fun configForLanguage() {
84-
velocityContext.put("language", language.displayName)
86+
velocityContext.put(CustomVariable.LANGUAGE.variable, language.displayName)
8587
velocityContext.put(
86-
"commentSymbol", when (language.displayName.lowercase()) {
88+
CustomVariable.COMMENT_SYMBOL.variable, when (language.displayName.lowercase()) {
8789
"java", "kotlin" -> "//"
8890
"python" -> "#"
8991
"javascript" -> "//"
@@ -93,8 +95,4 @@ class TeamPromptTemplateCompiler(
9395
}
9496
)
9597
}
96-
97-
companion object {
98-
val log = logger<TeamPromptTemplateCompiler>()
99-
}
10098
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cc.unitmesh.devti.custom.variable
2+
3+
enum class CustomVariable(val variable: String, val description: String) {
4+
SELECTION("selection", "The selected text"),
5+
BEFORE_CURSOR("beforeCursor", "The text before the cursor"),
6+
AFTER_CURSOR("afterCursor", "The text after the cursor"),
7+
FILE_NAME("fileName", "The name of the file"),
8+
FILE_PATH("filePath", "The path of the file"),
9+
METHOD_NAME("methodName", "The name of the method"),
10+
LANGUAGE("language", "The language of the file"),
11+
COMMENT_SYMBOL("commentSymbol", "The comment symbol of the language"),
12+
FRAMEWORK_CONTEXT("frameworkContext", "The context of the framework"),
13+
14+
;
15+
16+
companion object {
17+
fun all(): List<CustomVariable> = values().toList()
18+
}
19+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ enum class CustomVariableType(@JvmField val description: String) {
66
SPEC_VARIABLE("Load from spec config, and config to items"),
77
SIMILAR_CHUNK("Similar code chunk with element's code and recently open code"),
88
;
9-
}
9+
}

src/main/kotlin/cc/unitmesh/devti/gui/chat/AutoDevVariableList.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cc.unitmesh.devti.gui.chat
33
import com.intellij.ui.Gray
44
import com.intellij.ui.JBColor
55
import com.intellij.ui.components.JBList
6+
import java.awt.BorderLayout
67
import java.awt.Component
78
import java.awt.event.MouseEvent
89
import java.awt.event.MouseListener
@@ -15,14 +16,14 @@ class AutoDevVariableList(
1516
val callback: ((AutoDevVariableListComponent) -> Unit?)?,
1617
) : JBList<AutoDevVariableListComponent>() {
1718
init {
18-
border = BorderFactory.createEmptyBorder(0, 5, 0, 5)
19+
border = BorderFactory.createEmptyBorder(0, 4, 0, 4)
1920
setCellRenderer(VariableListCellRenderer())
2021
addMouseListener(object : MouseInputAdapter() {
2122
override fun mouseClicked(event: MouseEvent?) {
2223
val item = selectedValue ?: return
2324
callback?.invoke(item)
2425
}
25-
} as MouseListener)
26+
})
2627
}
2728
}
2829

@@ -66,9 +67,10 @@ class VariableListCellRenderer : ListCellRenderer<AutoDevVariableListComponent>
6667

6768
class AutoDevVariableListComponent : JPanel() {
6869
init {
69-
val label = JLabel("doing something")
70+
add(JLabel("$" + "selection"), BorderLayout.WEST)
71+
val label = JLabel("Used to get the currently selected text")
7072
label.border = BorderFactory.createEmptyBorder(0, 8, 0, 0)
7173
label.foreground = JBColor.namedColor("Component.infoForeground", JBColor(Gray.x99, Gray.x78))
72-
add(label, "East")
74+
add(label, BorderLayout.EAST)
7375
}
7476
}

0 commit comments

Comments
 (0)