Skip to content

Commit 93b6813

Browse files
committed
feat(terminal): add stop functionality to terminal sketch provider
- Implement stop functionality for terminal sketch provider - Add isExecuting and currentExecutionJob variables to track execution state - Update UI and toolbar based on execution state - Handle cancellation of ongoing execution and update UI accordingly
1 parent b992680 commit 93b6813

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

core/src/main/resources/messages/AutoDevBundle_en.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,4 @@ planner.error.no.file=No {0} file
204204
planner.error.no.after.file=No after file
205205
planner.error.create.file=Create file error {0}
206206
sketch.plan.rerun.task=Help me fix this failed task:
207+
sketch.terminal.stop=Stop Terminal

core/src/main/resources/messages/AutoDevBundle_zh.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,4 @@ planner.error.no.file=没有文件 {0}
197197
planner.error.no.after.file=No after file
198198
planner.error.create.file=Create file error {0}
199199
sketch.plan.rerun.task=Help me fix this failed task:
200+
sketch.terminal.stop=Stop Terminal

exts/ext-terminal/src/main/kotlin/cc/unitmesh/terminal/sketch/TerminalSketchProvider.kt

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ import javax.swing.JLabel
5050
import javax.swing.JPanel
5151
import javax.swing.border.LineBorder
5252
import cc.unitmesh.devti.AutoDevColors
53+
import com.intellij.openapi.actionSystem.ActionUpdateThread
54+
import kotlinx.coroutines.Job
5355

5456
class TerminalSketchProvider : LanguageSketchProvider {
5557
override fun isSupported(lang: String): Boolean = lang == "bash" || lang == "shell"
@@ -70,6 +72,10 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
7072

7173
private var lastExecutionResults: String = ""
7274
private var hasExecutionResults: Boolean = false
75+
76+
// 添加变量追踪执行状态和执行任务
77+
private var isExecuting = false
78+
private var currentExecutionJob: Job? = null
7379

7480
val titleLabel = JLabel("Terminal").apply {
7581
border = JBUI.Borders.empty(0, 10)
@@ -325,15 +331,57 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
325331

326332
inner class TerminalExecuteAction :
327333
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+
328350
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
329372
titleLabel.icon = AllIcons.RunConfigurations.TestState.Run
373+
374+
// 更新工具栏以显示停止按钮
375+
actionGroup.update(e)
376+
toolbar.updateActionsImmediately()
330377

331378
hasExecutionResults = false
332379
lastExecutionResults = ""
333380

334381
val stdWriter = UIUpdatingWriter(
335382
onTextUpdate = { text, complete ->
336383
resultSketch.updateViewText(text, complete)
384+
lastExecutionResults = text
337385
},
338386
onPanelUpdate = { title, _ ->
339387
collapsibleResultPanel.setTitle(title)
@@ -350,7 +398,7 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
350398
stdWriter.setExecuting(true)
351399
setResultStatus(false)
352400

353-
AutoDevCoroutineScope.scope(project).launch {
401+
currentExecutionJob = AutoDevCoroutineScope.scope(project).launch {
354402
val executor = project.getService(ProcessExecutor::class.java)
355403
try {
356404
val dispatcher = PooledThreadExecutor.INSTANCE.asCoroutineDispatcher()
@@ -366,6 +414,12 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
366414
hasExecutionResults = true
367415

368416
titleLabel.icon = null
417+
isExecuting = false
418+
419+
// 更新工具栏以显示执行按钮
420+
actionGroup.update(e)
421+
toolbar.updateActionsImmediately()
422+
369423
val success = exitCode == 0
370424
setResultStatus(success, if (!success) "Process exited with code $exitCode" else null)
371425
}
@@ -375,6 +429,12 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
375429
stdWriter.setExecuting(false)
376430
// Clear the running icon.
377431
titleLabel.icon = null
432+
isExecuting = false
433+
434+
// 更新工具栏以显示执行按钮
435+
actionGroup.update(e)
436+
toolbar.updateActionsImmediately()
437+
378438
resultSketch.updateViewText("${stdWriter.getContent()}\nError: ${ex.message}", true)
379439
setResultStatus(false, ex.message)
380440
}

0 commit comments

Comments
 (0)