Skip to content

Commit ccf3967

Browse files
committed
feat(gui): add support for custom variables in chat input #51
This commit introduces the ability to use custom variables in the chat input by providing a list of available variables and a popup to select them. The custom variables are now rendered in the chat input section, and a popup is created to display the list of variables when the user types a dollar sign. The popup is dismissed when the user clicks outside of it or presses a key.
1 parent bf2440d commit ccf3967

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import cc.unitmesh.devti.AutoDevIcons
55
import cc.unitmesh.devti.counit.configurable.customAgentSetting
66
import cc.unitmesh.devti.counit.model.CustomAgentConfig
77
import cc.unitmesh.devti.counit.model.CustomAgentState
8+
import cc.unitmesh.devti.custom.variable.CustomVariable
89
import cc.unitmesh.devti.llms.tokenizer.Tokenizer
910
import cc.unitmesh.devti.llms.tokenizer.TokenizerImpl
1011
import cc.unitmesh.devti.settings.AutoDevSettingsState
@@ -108,6 +109,10 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
108109

109110
// check new input == $
110111
if (event.newFragment.contentEquals("$") || event.newFragment.contentEquals("¥")) {
112+
if (popup == null) {
113+
popup = createPopup()
114+
}
115+
111116
if (popup?.isVisible == true) {
112117
popup?.cancel()
113118
}
@@ -122,8 +127,6 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
122127
}
123128
}
124129

125-
popup = createPopup()
126-
127130
input.addDocumentListener(documentListener)
128131
input.recreateDocument()
129132

@@ -175,14 +178,22 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
175178
tokenizer = TokenizerImpl.INSTANCE
176179
}
177180

178-
private fun createPopup() = JBPopupFactory.getInstance()
179-
.createComponentPopupBuilder(AutoDevVariableList(listOf(
180-
AutoDevVariableListComponent(),
181-
) ,null), null )
182-
.setRequestFocus(false)
183-
.setMinSize(
184-
Dimension(this@AutoDevInputSection.width, 0)
185-
).createPopup()
181+
private fun createPopup(): JBPopup {
182+
val devVariableList = AutoDevVariableList.from(CustomVariable.all()) { item ->
183+
input.text += item.customVariable.variable
184+
this.popup?.cancel()
185+
}
186+
187+
devVariableList.selectedIndex = 0
188+
189+
val popups = JBPopupFactory.getInstance()
190+
.createComponentPopupBuilder(devVariableList, null)
191+
.setRequestFocus(false)
192+
.setMinSize(Dimension(this@AutoDevInputSection.width, 0))
193+
.createPopup()
194+
195+
return popups
196+
}
186197

187198

188199
private fun loadRagApps(): List<CustomAgentConfig> {

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,47 @@
11
package cc.unitmesh.devti.gui.chat
22

3+
import cc.unitmesh.devti.custom.variable.CustomVariable
34
import com.intellij.ui.Gray
45
import com.intellij.ui.JBColor
56
import com.intellij.ui.components.JBList
67
import java.awt.BorderLayout
78
import java.awt.Component
89
import java.awt.event.MouseEvent
9-
import java.awt.event.MouseListener
1010
import javax.swing.*
1111
import javax.swing.border.Border
1212
import javax.swing.event.MouseInputAdapter
1313

1414
class AutoDevVariableList(
15-
val list: List<AutoDevVariableListComponent>,
16-
val callback: ((AutoDevVariableListComponent) -> Unit?)?,
17-
) : JBList<AutoDevVariableListComponent>() {
15+
val list: List<AutoDevVariableListItemComponent>,
16+
val callback: ((AutoDevVariableListItemComponent) -> Unit),
17+
) : JBList<AutoDevVariableListItemComponent>(list) {
1818
init {
1919
border = BorderFactory.createEmptyBorder(0, 4, 0, 4)
2020
setCellRenderer(VariableListCellRenderer())
2121
addMouseListener(object : MouseInputAdapter() {
2222
override fun mouseClicked(event: MouseEvent?) {
2323
val item = selectedValue ?: return
24-
callback?.invoke(item)
24+
callback.invoke(item)
2525
}
2626
})
2727
}
28+
29+
companion object {
30+
fun from(all: List<CustomVariable>, function: (AutoDevVariableListItemComponent) -> Unit): AutoDevVariableList {
31+
val list = all.map {
32+
AutoDevVariableListItemComponent(it)
33+
}
34+
return AutoDevVariableList(list, function)
35+
}
36+
}
2837
}
2938

30-
class VariableListCellRenderer : ListCellRenderer<AutoDevVariableListComponent> {
39+
class VariableListCellRenderer : ListCellRenderer<AutoDevVariableListItemComponent> {
3140
private var emptyBorder: Border = BorderFactory.createEmptyBorder(1, 1, 1, 1)
3241

3342
override fun getListCellRendererComponent(
34-
jList: JList<out AutoDevVariableListComponent>,
35-
value: AutoDevVariableListComponent?,
43+
jList: JList<out AutoDevVariableListItemComponent>,
44+
value: AutoDevVariableListItemComponent?,
3645
index: Int,
3746
isSelected: Boolean,
3847
cellHasFocus: Boolean,
@@ -62,15 +71,14 @@ class VariableListCellRenderer : ListCellRenderer<AutoDevVariableListComponent>
6271
value.border = border
6372
return value
6473
}
65-
6674
}
6775

68-
class AutoDevVariableListComponent : JPanel() {
76+
class AutoDevVariableListItemComponent(val customVariable: CustomVariable) : JPanel() {
6977
init {
70-
add(JLabel("$" + "selection"), BorderLayout.WEST)
71-
val label = JLabel("Used to get the currently selected text")
78+
add(JLabel("$${customVariable.variable}"), BorderLayout.WEST)
79+
val label = JLabel(customVariable.description)
7280
label.border = BorderFactory.createEmptyBorder(0, 8, 0, 0)
7381
label.foreground = JBColor.namedColor("Component.infoForeground", JBColor(Gray.x99, Gray.x78))
7482
add(label, BorderLayout.EAST)
7583
}
76-
}
84+
}

0 commit comments

Comments
 (0)