Skip to content

Commit c51b388

Browse files
committedJan 4, 2025
feat(editor): enhance variable debug panel UI and functionality #157
- Refactor variable panel layout using `JBPanel` and `GridBagLayout`. - Add copy button to each variable for easy value copying. - Improve styling with custom fonts, colors, and borders. - Ensure proper panel updates with `revalidate()` and `repaint()`.
1 parent 6cf0cda commit c51b388

File tree

1 file changed

+84
-17
lines changed

1 file changed

+84
-17
lines changed
 

‎shirelang/src/main/kotlin/com/phodal/shirelang/editor/ShireSplitEditorProvider.kt

+84-17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.phodal.shirelang.editor
33
import com.intellij.icons.AllIcons
44
import com.intellij.openapi.actionSystem.*
55
import com.intellij.openapi.actionSystem.ex.ActionUtil
6+
import com.intellij.openapi.actionSystem.impl.ActionButton
67
import com.intellij.openapi.application.ApplicationManager
78
import com.intellij.openapi.editor.Editor
89
import com.intellij.openapi.editor.ScrollType
@@ -15,8 +16,8 @@ import com.intellij.openapi.editor.impl.EditorImpl
1516
import com.intellij.openapi.fileEditor.*
1617
import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider
1718
import com.intellij.openapi.fileTypes.FileTypeRegistry
19+
import com.intellij.openapi.ide.CopyPasteManager
1820
import com.intellij.openapi.project.Project
19-
import com.intellij.openapi.ui.DialogPanel
2021
import com.intellij.openapi.util.UserDataHolder
2122
import com.intellij.openapi.util.UserDataHolderBase
2223
import com.intellij.openapi.vfs.VirtualFile
@@ -25,6 +26,7 @@ import com.intellij.psi.PsiManager
2526
import com.intellij.testFramework.LightVirtualFile
2627
import com.intellij.ui.JBColor
2728
import com.intellij.ui.components.JBLabel
29+
import com.intellij.ui.components.JBPanel
2830
import com.intellij.ui.components.JBScrollPane
2931
import com.intellij.ui.dsl.builder.Align
3032
import com.intellij.ui.dsl.builder.panel
@@ -39,7 +41,12 @@ import kotlinx.coroutines.flow.MutableStateFlow
3941
import kotlinx.coroutines.runBlocking
4042
import org.intellij.plugins.markdown.lang.MarkdownLanguage
4143
import java.awt.BorderLayout
44+
import java.awt.FlowLayout
45+
import java.awt.GridBagConstraints
46+
import java.awt.GridBagLayout
47+
import java.awt.datatransfer.StringSelection
4248
import java.beans.PropertyChangeListener
49+
import javax.swing.Box
4350
import javax.swing.JComponent
4451
import javax.swing.JPanel
4552
import javax.swing.ScrollPaneConstants
@@ -98,7 +105,7 @@ open class ShirePreviewEditor(
98105
)
99106

100107
private var shireRunnerContext: ShireRunnerContext? = null
101-
private var variableDebugPanel: JPanel = panel {}
108+
private var variableDebugPanel: JPanel = JPanel(BorderLayout())
102109

103110
private var highlightSketch: CodeHighlightSketch? = null
104111

@@ -137,25 +144,81 @@ open class ShirePreviewEditor(
137144
}
138145

139146
private fun variablePanel(variables: Map<String, Any>): JPanel {
140-
val variablesPanel: DialogPanel = panel {
141-
row {
142-
val label = JBLabel("Variables")
143-
cell(label).align(Align.FILL).resizableColumn()
147+
val panel = JBPanel<JBPanel<*>>(GridBagLayout())
148+
panel.background = JBColor(0xF5F5F5, 0x2B2D30)
149+
panel.border = JBUI.Borders.empty(4)
150+
151+
val gbc = GridBagConstraints().apply {
152+
fill = GridBagConstraints.HORIZONTAL
153+
anchor = GridBagConstraints.NORTHWEST
154+
weightx = 1.0
155+
gridx = 0
156+
insets = JBUI.insets(2)
157+
}
158+
159+
// 添加标题
160+
val titleLabel = JBLabel("Variables").apply {
161+
font = JBUI.Fonts.label(14f).asBold()
162+
border = JBUI.Borders.empty(4, 8)
163+
}
164+
panel.add(titleLabel, gbc)
165+
166+
// 添加变量列表
167+
variables.forEach { (key, value) ->
168+
gbc.gridy++
169+
170+
val varPanel = JBPanel<JBPanel<*>>(FlowLayout(FlowLayout.LEFT, 0, 0))
171+
varPanel.isOpaque = false
172+
varPanel.border = JBUI.Borders.compound(
173+
JBUI.Borders.customLine(JBColor(0xE6E6E6, 0x3C3F41), 0, 0, 1, 0),
174+
JBUI.Borders.empty(6, 8)
175+
)
176+
177+
val keyLabel = JBLabel(key).apply {
178+
font = JBUI.Fonts.label(11f)
179+
foreground = JBColor(0x666666, 0x999999)
144180
}
181+
varPanel.add(keyLabel)
145182

146-
variables.forEach { (key, value) ->
147-
row {
148-
val label = JBLabel("$key: $value")
149-
cell(label).align(Align.FILL).resizableColumn()
150-
}
183+
val colonLabel = JBLabel(": ").apply {
184+
foreground = JBColor(0x666666, 0x999999)
151185
}
152-
}
186+
varPanel.add(colonLabel)
153187

154-
return JPanel(BorderLayout()).apply {
155-
add(variablesPanel, BorderLayout.CENTER)
188+
val valueStr = value.toString()
189+
val valueLabel = JBLabel(valueStr).apply {
190+
font = JBUI.Fonts.label(11f)
191+
foreground = JBColor(0x000000, 0xCCCCCC)
192+
}
193+
varPanel.add(valueLabel)
194+
195+
val copyButton = ActionButton(
196+
object : AnAction(AllIcons.Actions.Copy) {
197+
override fun actionPerformed(e: AnActionEvent) {
198+
val clipboardManager = CopyPasteManager.getInstance()
199+
clipboardManager.setContents(StringSelection(valueStr))
200+
}
201+
},
202+
Presentation().apply { icon = AllIcons.Actions.Copy },
203+
"Variables",
204+
JBUI.size(16)
205+
).apply {
206+
toolTipText = "Copy value"
207+
border = JBUI.Borders.empty(2)
208+
}
209+
varPanel.add(Box.createHorizontalStrut(4))
210+
varPanel.add(copyButton)
211+
212+
panel.add(varPanel, gbc)
156213
}
157-
}
158214

215+
// 填充剩余空间
216+
gbc.gridy++
217+
gbc.weighty = 1.0
218+
panel.add(Box.createVerticalGlue(), gbc)
219+
220+
return panel
221+
}
159222

160223
private inner class ReparseContentDocumentListener : DocumentListener {
161224
override fun documentChanged(event: DocumentEvent) {
@@ -173,12 +236,16 @@ open class ShirePreviewEditor(
173236

174237
val variables = shireRunnerContext?.compiledVariables
175238
if (variables != null) {
176-
variableDebugPanel = variablePanel(variables)
177-
mainPanel.repaint()
239+
variableDebugPanel.removeAll()
240+
val panel = variablePanel(variables)
241+
variableDebugPanel.add(panel, BorderLayout.CENTER)
178242
}
179243

180244
highlightSketch?.updateViewText(shireRunnerContext!!.finalPrompt)
181245
highlightSketch?.repaint()
246+
247+
mainPanel.revalidate()
248+
mainPanel.repaint()
182249
} catch (e: Exception) {
183250
e.printStackTrace()
184251
}

0 commit comments

Comments
 (0)
Please sign in to comment.