@@ -49,6 +49,7 @@ import javax.swing.BoxLayout
49
49
import javax.swing.JButton
50
50
import javax.swing.JComponent
51
51
import javax.swing.JPanel
52
+ import javax.swing.Icon
52
53
53
54
open class CodeHighlightSketch (
54
55
open val project : Project ,
@@ -62,10 +63,9 @@ open class CodeHighlightSketch(
62
63
) : JBPanel<CodeHighlightSketch>(VerticalLayout (2)), DataProvider, LangSketch, Disposable {
63
64
private val minDevinLineThreshold = 1
64
65
private var isDevIns = false
65
- private var devInsCollapsedPanel: JPanel ? = null
66
- private var devInsExpandedPanel: JPanel ? = null
67
- private var isCollapsed = false
68
- private var runButton: ActionButton ? = null
66
+ private var collapsedPanel: JPanel ? = null
67
+ private var isCollapsed = true // 默认折叠状态
68
+ private var actionButton: ActionButton ? = null
69
69
private var isComplete = isUser
70
70
71
71
private var textLanguage: String? = if (ideaLanguage != null ) ideaLanguage?.displayName else null
@@ -84,17 +84,22 @@ open class CodeHighlightSketch(
84
84
return this != null && this .trim().isNotEmpty()
85
85
}
86
86
87
+ private fun shouldUseCollapsedView (): Boolean {
88
+ val displayName = ideaLanguage?.displayName
89
+ return when {
90
+ displayName == " Markdown" -> false
91
+ ideaLanguage == PlainTextLanguage .INSTANCE -> false
92
+ displayName == plainText -> false
93
+ else -> true
94
+ }
95
+ }
96
+
87
97
private var toolbar: ActionToolbar ? = null
88
98
89
99
fun initEditor (text : String , fileName : String? = null) {
90
100
if (hasSetupAction) return
91
101
hasSetupAction = true
92
102
93
- if (isUser) {
94
- setupSimpleEditor(text, fileName)
95
- return
96
- }
97
-
98
103
val editor = EditorUtil .createCodeViewerEditor(project, text, ideaLanguage, fileName, this )
99
104
100
105
border = if (withLeftRightBorder) {
@@ -108,34 +113,24 @@ open class CodeHighlightSketch(
108
113
if (ideaLanguage?.displayName == " DevIn" ) {
109
114
isDevIns = true
110
115
editorFragment = EditorFragment (editor, minDevinLineThreshold, previewEditor)
111
- setupDevInsView(text)
112
116
} else {
113
- setupRegularEditor (editor)
117
+ editorFragment = EditorFragment (editor, editorLineThreshold, previewEditor )
114
118
}
115
119
116
- setupToolbarAndStyling(fileName, editor)
117
- }
118
-
119
- private fun setupSimpleEditor (text : String , fileName : String? ) {
120
- val editor = EditorUtil .createCodeViewerEditor(project, text, ideaLanguage, fileName, this )
121
-
122
- border = if (withLeftRightBorder) {
123
- JBEmptyBorder (4 , 4 , 4 , 4 )
120
+ // 检查是否需要折叠视图
121
+ val needsCollapsedView = shouldUseCollapsedView()
122
+ if (needsCollapsedView) {
123
+ setupCollapsedView(text)
124
124
} else {
125
- JBEmptyBorder (4 , 0 , 0 , 0 )
125
+ // 直接添加编辑器内容,不使用折叠
126
+ add(editorFragment!! .getContent())
127
+ isCollapsed = false
126
128
}
127
129
128
- editor.component.isOpaque = true
129
- editorFragment = EditorFragment (editor, editorLineThreshold, previewEditor)
130
- add(editorFragment!! .getContent())
131
-
132
130
setupToolbarAndStyling(fileName, editor)
133
131
}
134
132
135
- private fun setupRegularEditor (editor : EditorEx ) {
136
- editorFragment = EditorFragment (editor, editorLineThreshold, previewEditor)
137
- add(editorFragment!! .getContent())
138
- }
133
+
139
134
140
135
private val plainText = PlainTextLanguage .INSTANCE .displayName
141
136
@@ -147,7 +142,7 @@ open class CodeHighlightSketch(
147
142
148
143
if (textLanguage != null && lowercase != " markdown" && textLanguage != plainText) {
149
144
if (showToolbar && lowercase != devinLanguageId) {
150
- val isShowBottomBorder = devInsCollapsedPanel != null
145
+ val isShowBottomBorder = collapsedPanel != null
151
146
toolbar = setupActionBar(project, editor, isPackageFile, isShowBottomBorder)
152
147
}
153
148
} else {
@@ -163,10 +158,16 @@ open class CodeHighlightSketch(
163
158
}
164
159
}
165
160
166
- private fun setupDevInsView (text : String ) {
167
- devInsCollapsedPanel = JPanel (BorderLayout ()).apply {
161
+ private fun setupCollapsedView (text : String ) {
162
+ collapsedPanel = JPanel (BorderLayout ()).apply {
168
163
border = JBUI .Borders .empty(2 )
169
- runButton = createRunButton(text)
164
+
165
+ // 根据是否为 DevIns 创建不同的按钮
166
+ actionButton = if (isDevIns) {
167
+ createDevInsButton(text)
168
+ } else {
169
+ createGenericButton()
170
+ }
170
171
171
172
val firstLine = text.lines().firstOrNull() ? : " "
172
173
val previewLabel = JBLabel (firstLine).apply {
@@ -189,7 +190,7 @@ open class CodeHighlightSketch(
189
190
}
190
191
191
192
val leftPanel = JPanel (FlowLayout (FlowLayout .LEFT , 2 , 0 )).apply {
192
- add(runButton !! )
193
+ add(actionButton !! )
193
194
}
194
195
195
196
val rightPanel = JPanel (BorderLayout ()).apply {
@@ -201,21 +202,17 @@ open class CodeHighlightSketch(
201
202
add(rightPanel, BorderLayout .CENTER )
202
203
}
203
204
204
- devInsExpandedPanel = JPanel (VerticalLayout (0 )).apply {
205
- add(editorFragment!! .getContent())
206
-
207
- val fewerLinesLabel = createFewerLinesLabel()
208
- add(fewerLinesLabel)
209
- }
210
-
211
- add(devInsCollapsedPanel!! )
205
+ add(collapsedPanel!! )
212
206
isCollapsed = true
213
- updateRunButtonIcon ()
207
+ updateActionButtonIcon ()
214
208
}
215
209
216
- var devinRunButtonPresentation = Presentation ()
217
- private fun createRunButton (newText : String ): ActionButton {
218
- devinRunButtonPresentation?.icon = AutoDevIcons .RUN
210
+ private var actionButtonPresentation = Presentation ()
211
+
212
+ private fun createDevInsButton (newText : String ): ActionButton {
213
+ val commandIcon = getDevInsCommandIcon(newText)
214
+ actionButtonPresentation = Presentation ()
215
+ actionButtonPresentation.icon = commandIcon
219
216
return ActionButton (
220
217
DumbAwareAction .create {
221
218
if (isComplete) return @create
@@ -226,28 +223,67 @@ open class CodeHighlightSketch(
226
223
sketchService.send(newText)
227
224
}
228
225
},
229
- devinRunButtonPresentation ,
226
+ actionButtonPresentation ,
230
227
" AutoDevToolbar" ,
231
228
JBUI .size(24 , 24 )
232
229
)
233
230
}
234
231
235
- private fun updateRunButtonIcon () {
236
- runButton?.let { button: ActionButton ->
237
- val icon = if (isComplete) AutoDevIcons .RUN else AutoDevIcons .LOADING
238
- devinRunButtonPresentation?.setIcon(icon)
232
+ private fun createGenericButton (): ActionButton {
233
+ actionButtonPresentation = Presentation ()
234
+ actionButtonPresentation.icon = AllIcons .General .ArrowRight
235
+ return ActionButton (
236
+ DumbAwareAction .create {
237
+ // 普通编辑器的按钮行为,可以根据需要扩展
238
+ },
239
+ actionButtonPresentation,
240
+ " AutoDevToolbar" ,
241
+ JBUI .size(24 , 24 )
242
+ )
243
+ }
244
+
245
+ private fun getDevInsCommandIcon (text : String ): Icon {
246
+ val firstLine = text.lines().firstOrNull() ? : " "
247
+ if (firstLine.startsWith(" /" )) {
248
+ val commandName = firstLine.substring(1 ).split(" :" ).firstOrNull()?.trim()
249
+ if (commandName != null ) {
250
+ val command = BuiltinCommand .entries.find { it.commandName == commandName }
251
+ if (command != null ) {
252
+ return command.icon
253
+ }
254
+ }
255
+ }
256
+ return AutoDevIcons .RUN // 默认图标
257
+ }
258
+
259
+ private fun updateActionButtonIcon () {
260
+ actionButton?.let { button: ActionButton ->
261
+ if (isDevIns) {
262
+ val icon = if (isComplete) {
263
+ getDevInsCommandIcon(getViewText())
264
+ } else {
265
+ AutoDevIcons .LOADING
266
+ }
267
+ actionButtonPresentation.setIcon(icon)
268
+ }
239
269
button.repaint()
240
270
}
241
271
}
242
272
243
273
private fun toggleEditorVisibility () {
244
274
if (isCollapsed) {
245
- remove(devInsCollapsedPanel)
246
- add(devInsExpandedPanel!! )
275
+ // 展开:移除折叠面板,添加编辑器内容和折叠标签
276
+ remove(collapsedPanel)
277
+ add(editorFragment!! .getContent())
278
+
279
+ val fewerLinesLabel = createFewerLinesLabel()
280
+ add(fewerLinesLabel)
281
+
247
282
isCollapsed = false
248
283
} else {
249
- remove(devInsExpandedPanel)
250
- add(devInsCollapsedPanel!! )
284
+ // 折叠:移除所有内容,只显示折叠面板
285
+ removeAll()
286
+ add(collapsedPanel!! )
251
287
isCollapsed = true
252
288
}
253
289
@@ -302,16 +338,19 @@ open class CodeHighlightSketch(
302
338
try {
303
339
document?.replaceString(0 , document.textLength, normalizedText)
304
340
305
- // Update DevIns collapsed panel preview text if applicable
306
- if (isDevIns && devInsCollapsedPanel != null ) {
341
+ // Update collapsed panel preview text if applicable
342
+ if (collapsedPanel != null && shouldUseCollapsedView() ) {
307
343
val firstLine = normalizedText.lines().firstOrNull() ? : " "
308
- val components = devInsCollapsedPanel !! .components
344
+ val components = collapsedPanel !! .components
309
345
for (comp in components) {
310
346
if (comp is JPanel && comp.layout is BorderLayout ) {
311
347
val centerComp = (comp.layout as BorderLayout ).getLayoutComponent(BorderLayout .CENTER )
312
- if (centerComp is JBLabel ) {
313
- centerComp.text = firstLine
314
- break
348
+ if (centerComp is JPanel && centerComp.layout is BorderLayout ) {
349
+ val labelComp = (centerComp.layout as BorderLayout ).getLayoutComponent(BorderLayout .CENTER )
350
+ if (labelComp is JBLabel ) {
351
+ labelComp.text = firstLine
352
+ break
353
+ }
315
354
}
316
355
}
317
356
}
@@ -320,16 +359,16 @@ open class CodeHighlightSketch(
320
359
logger<CodeHighlightSketch >().error(" Error updating editor text" , e)
321
360
}
322
361
323
- // Update run button icon state for DevIns
324
- updateRunButtonIcon ()
362
+ // Update action button icon state
363
+ updateActionButtonIcon ()
325
364
326
365
val lineCount = document?.lineCount ? : 0
327
366
if (lineCount > editorLineThreshold) {
328
367
editorFragment?.updateExpandCollapseLabel()
329
368
}
330
369
331
- // Auto-collapse DevIns view when complete
332
- if (complete && isDevIns && ! isCollapsed ) {
370
+ // Auto-collapse view when complete (only for collapsible views)
371
+ if (complete && ! isCollapsed && shouldUseCollapsedView() ) {
333
372
toggleEditorVisibility()
334
373
}
335
374
}
0 commit comments