@@ -50,6 +50,8 @@ import javax.swing.JLabel
50
50
import javax.swing.JPanel
51
51
import javax.swing.border.LineBorder
52
52
import cc.unitmesh.devti.AutoDevColors
53
+ import com.intellij.openapi.actionSystem.ActionUpdateThread
54
+ import kotlinx.coroutines.Job
53
55
54
56
class TerminalSketchProvider : LanguageSketchProvider {
55
57
override fun isSupported (lang : String ): Boolean = lang == " bash" || lang == " shell"
@@ -70,6 +72,10 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
70
72
71
73
private var lastExecutionResults: String = " "
72
74
private var hasExecutionResults: Boolean = false
75
+
76
+ // 添加变量追踪执行状态和执行任务
77
+ private var isExecuting = false
78
+ private var currentExecutionJob: Job ? = null
73
79
74
80
val titleLabel = JLabel (" Terminal" ).apply {
75
81
border = JBUI .Borders .empty(0 , 10 )
@@ -325,15 +331,57 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
325
331
326
332
inner class TerminalExecuteAction :
327
333
AnAction (" Execute" , AutoDevBundle .message(" sketch.terminal.execute" ), AutoDevIcons .RUN ) {
334
+ override fun getActionUpdateThread (): ActionUpdateThread = ActionUpdateThread .EDT
335
+
336
+ override fun update (e : AnActionEvent ) {
337
+ super .update(e)
338
+ // 根据当前执行状态更新图标和文本
339
+ if (isExecuting) {
340
+ e.presentation.icon = AllIcons .Actions .Suspend
341
+ e.presentation.text = " Stop"
342
+ e.presentation.description = AutoDevBundle .message(" sketch.terminal.stop" )
343
+ } else {
344
+ e.presentation.icon = AutoDevIcons .RUN
345
+ e.presentation.text = " Execute"
346
+ e.presentation.description = AutoDevBundle .message(" sketch.terminal.execute" )
347
+ }
348
+ }
349
+
328
350
override fun actionPerformed (e : AnActionEvent ) {
351
+ if (isExecuting) {
352
+ // 如果正在执行,则停止执行
353
+ currentExecutionJob?.cancel()
354
+
355
+ ApplicationManager .getApplication().invokeLater {
356
+ isExecuting = false
357
+ titleLabel.icon = null
358
+
359
+ // 更新UI以反映停止状态
360
+ resultSketch.updateViewText(lastExecutionResults + " \n\n [执行已手动停止]" , true )
361
+ setResultStatus(false , " 执行已手动停止" )
362
+
363
+ // 更新工具栏
364
+ actionGroup.update(e)
365
+ toolbar.updateActionsImmediately()
366
+ }
367
+ return
368
+ }
369
+
370
+ // 开始执行
371
+ isExecuting = true
329
372
titleLabel.icon = AllIcons .RunConfigurations .TestState .Run
373
+
374
+ // 更新工具栏以显示停止按钮
375
+ actionGroup.update(e)
376
+ toolbar.updateActionsImmediately()
330
377
331
378
hasExecutionResults = false
332
379
lastExecutionResults = " "
333
380
334
381
val stdWriter = UIUpdatingWriter (
335
382
onTextUpdate = { text, complete ->
336
383
resultSketch.updateViewText(text, complete)
384
+ lastExecutionResults = text
337
385
},
338
386
onPanelUpdate = { title, _ ->
339
387
collapsibleResultPanel.setTitle(title)
@@ -350,7 +398,7 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
350
398
stdWriter.setExecuting(true )
351
399
setResultStatus(false )
352
400
353
- AutoDevCoroutineScope .scope(project).launch {
401
+ currentExecutionJob = AutoDevCoroutineScope .scope(project).launch {
354
402
val executor = project.getService(ProcessExecutor ::class .java)
355
403
try {
356
404
val dispatcher = PooledThreadExecutor .INSTANCE .asCoroutineDispatcher()
@@ -366,6 +414,12 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
366
414
hasExecutionResults = true
367
415
368
416
titleLabel.icon = null
417
+ isExecuting = false
418
+
419
+ // 更新工具栏以显示执行按钮
420
+ actionGroup.update(e)
421
+ toolbar.updateActionsImmediately()
422
+
369
423
val success = exitCode == 0
370
424
setResultStatus(success, if (! success) " Process exited with code $exitCode " else null )
371
425
}
@@ -375,6 +429,12 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
375
429
stdWriter.setExecuting(false )
376
430
// Clear the running icon.
377
431
titleLabel.icon = null
432
+ isExecuting = false
433
+
434
+ // 更新工具栏以显示执行按钮
435
+ actionGroup.update(e)
436
+ toolbar.updateActionsImmediately()
437
+
378
438
resultSketch.updateViewText(" ${stdWriter.getContent()} \n Error: ${ex.message} " , true )
379
439
setResultStatus(false , ex.message)
380
440
}
0 commit comments