@@ -3,6 +3,7 @@ package com.phodal.shirelang.editor
3
3
import com.intellij.icons.AllIcons
4
4
import com.intellij.openapi.actionSystem.*
5
5
import com.intellij.openapi.actionSystem.ex.ActionUtil
6
+ import com.intellij.openapi.actionSystem.impl.ActionButton
6
7
import com.intellij.openapi.application.ApplicationManager
7
8
import com.intellij.openapi.editor.Editor
8
9
import com.intellij.openapi.editor.ScrollType
@@ -15,8 +16,8 @@ import com.intellij.openapi.editor.impl.EditorImpl
15
16
import com.intellij.openapi.fileEditor.*
16
17
import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider
17
18
import com.intellij.openapi.fileTypes.FileTypeRegistry
19
+ import com.intellij.openapi.ide.CopyPasteManager
18
20
import com.intellij.openapi.project.Project
19
- import com.intellij.openapi.ui.DialogPanel
20
21
import com.intellij.openapi.util.UserDataHolder
21
22
import com.intellij.openapi.util.UserDataHolderBase
22
23
import com.intellij.openapi.vfs.VirtualFile
@@ -25,6 +26,7 @@ import com.intellij.psi.PsiManager
25
26
import com.intellij.testFramework.LightVirtualFile
26
27
import com.intellij.ui.JBColor
27
28
import com.intellij.ui.components.JBLabel
29
+ import com.intellij.ui.components.JBPanel
28
30
import com.intellij.ui.components.JBScrollPane
29
31
import com.intellij.ui.dsl.builder.Align
30
32
import com.intellij.ui.dsl.builder.panel
@@ -39,7 +41,12 @@ import kotlinx.coroutines.flow.MutableStateFlow
39
41
import kotlinx.coroutines.runBlocking
40
42
import org.intellij.plugins.markdown.lang.MarkdownLanguage
41
43
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
42
48
import java.beans.PropertyChangeListener
49
+ import javax.swing.Box
43
50
import javax.swing.JComponent
44
51
import javax.swing.JPanel
45
52
import javax.swing.ScrollPaneConstants
@@ -98,7 +105,7 @@ open class ShirePreviewEditor(
98
105
)
99
106
100
107
private var shireRunnerContext: ShireRunnerContext ? = null
101
- private var variableDebugPanel: JPanel = panel {}
108
+ private var variableDebugPanel: JPanel = JPanel ( BorderLayout ())
102
109
103
110
private var highlightSketch: CodeHighlightSketch ? = null
104
111
@@ -137,25 +144,81 @@ open class ShirePreviewEditor(
137
144
}
138
145
139
146
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 )
144
180
}
181
+ varPanel.add(keyLabel)
145
182
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 )
151
185
}
152
- }
186
+ varPanel.add(colonLabel)
153
187
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)
156
213
}
157
- }
158
214
215
+ // 填充剩余空间
216
+ gbc.gridy++
217
+ gbc.weighty = 1.0
218
+ panel.add(Box .createVerticalGlue(), gbc)
219
+
220
+ return panel
221
+ }
159
222
160
223
private inner class ReparseContentDocumentListener : DocumentListener {
161
224
override fun documentChanged (event : DocumentEvent ) {
@@ -173,12 +236,16 @@ open class ShirePreviewEditor(
173
236
174
237
val variables = shireRunnerContext?.compiledVariables
175
238
if (variables != null ) {
176
- variableDebugPanel = variablePanel(variables)
177
- mainPanel.repaint()
239
+ variableDebugPanel.removeAll()
240
+ val panel = variablePanel(variables)
241
+ variableDebugPanel.add(panel, BorderLayout .CENTER )
178
242
}
179
243
180
244
highlightSketch?.updateViewText(shireRunnerContext!! .finalPrompt)
181
245
highlightSketch?.repaint()
246
+
247
+ mainPanel.revalidate()
248
+ mainPanel.repaint()
182
249
} catch (e: Exception ) {
183
250
e.printStackTrace()
184
251
}
0 commit comments