Elffuss BraidHub

kikocis/continue public

feat: reuse terminal and find by terminalName before running command
Changeset74029cf4f4f0bd61d6697b99e59f8970d53893e756f5e5e6572d92069fd6a028
Authorzimu <zimu@mgtv.com> · 2025-07-18 21:25 +08:00
Committerzimu <zimu@mgtv.com> · 2025-07-21 09:58 +08:00
Parentsd9595d31f99e

Browse the files at this changeset

3 files changed

modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/IntelliJIde.kt +37 −18
@@ -248,43 +248,62 @@
248248 }
249249 }
250250
251- override suspend fun runCommand(command: String, options: TerminalOptions) {
252- println("Continue-Debug: runCommand called with command: $command")
251+ override suspend fun runCommand(command: String, options: TerminalOptions?) {
252+ val terminalOptions =
253+ options ?: TerminalOptions(reuseTerminal = true, terminalName = null, waitForCompletion = false)
254+
253255 ApplicationManager.getApplication().invokeLater {
254256 try {
255- println("Continue-Debug: Activating Terminal tool window.")
256257 val toolWindow = ToolWindowManager.getInstance(project).getToolWindow("Terminal")
257258 toolWindow?.activate({
258259 try {
259260 val terminalView = TerminalView.getInstance(project)
260- println("Continue-Debug: Searching for an existing, non-busy terminal widget.")
261-
262- var widget = terminalView.getWidgets().filterIsInstance<ShellTerminalWidget>()
263- .firstOrNull { !it.hasRunningCommands() }
261+ var widget: ShellTerminalWidget? = null
262+
263+ // 1. Handle reuseTerminal option
264+ if (terminalOptions.reuseTerminal == true && terminalView.getWidgets().isNotEmpty()) {
265+ // 2. Find by terminalName if provided
266+ if (terminalOptions.terminalName != null) {
267+ widget = terminalView.getWidgets().filterIsInstance<ShellTerminalWidget>()
268+ .firstOrNull {
269+ toolWindow.contentManager.getContent(it).tabName == terminalOptions.terminalName
270+ && !it.hasRunningCommands()
271+ }
272+ } else {
273+ // 3. Find active terminal, or fall back to the first one
274+ widget = terminalView.getWidgets().filterIsInstance<ShellTerminalWidget>()
275+ .firstOrNull { toolWindow.contentManager.getContent(it).isSelected }
276+ ?: terminalView.getWidgets().filterIsInstance<ShellTerminalWidget>().firstOrNull {
277+ !it.hasRunningCommands()
278+ }
279+ }
280+ }
264281
282+ // 4. Create a new terminal if needed
265283 if (widget == null) {
266- println("Continue-Debug: No non-busy widget found. Creating a new one.")
267- widget = terminalView.createLocalShellWidget(project.basePath, "Continue Terminal", true)
284+ widget = terminalView.createLocalShellWidget(
285+ project.basePath,
286+ terminalOptions.terminalName,
287+ true
288+ )
268289 } else {
269- println("Continue-Debug: Found an existing widget. Bringing it to the front.")
270- val contentManager = toolWindow.contentManager
271- val content = contentManager.getContent(widget)
290+ // Ensure the found widget is visible
291+ val content = toolWindow.contentManager.getContent(widget)
272292 if (content != null) {
273- contentManager.setSelectedContent(content, true)
293+ toolWindow.contentManager.setSelectedContent(content, true)
274294 }
275295 }
276296
277- println("Continue-Debug: Writing command to terminal: $command")
297+ // 5. Show and send text
278298 widget.ttyConnector?.write(command)
279- println("Continue-Debug: Command successfully written to terminal.")
280299
281300 } catch (e: Exception) {
282- println("Continue-Debug: Error during terminal widget handling: ${e.message}")
301+ println("Error during terminal widget handling: ${e.message}")
283302 e.printStackTrace()
284303 }
285304 }, true)
286305 } catch (e: Exception) {
287- println("Continue-Debug: Error activating terminal tool window: ${e.message}")
306+ println("Error activating terminal tool window: ${e.message}")
288307 e.printStackTrace()
289308 }
290309 }
@@ -666,7 +685,7 @@
666685 }
667686
668687 private fun setFileOpen(filepath: String, open: Boolean = true) {
669- val file = LocalFileSystem.getInstance().findFileByPath(UriUtils.uriToFile(filepath).path)
688+ val file = LocalFileSystem.getInstance().findFileByPath(UriUtils.parseUri(filepath).path)
670689
671690 file?.let {
672691 if (open) {
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/protocol/ide.kt +1 −1
@@ -62,4 +62,4 @@
6262
6363 data class GetFileStatsParams(val files: List<String>)
6464
65-data class RunCommandParams(val command: String, val options: TerminalOptions)
65+data class RunCommandParams(val command: String, val options: TerminalOptions?)
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/types.kt +4 −4
@@ -92,9 +92,9 @@
9292 )
9393
9494 data class TerminalOptions(
95- val reuseTerminal: Boolean,
96- val terminalName: String,
97- val waitForCompletion: Boolean
95+ val reuseTerminal: Boolean?,
96+ val terminalName: String?,
97+ val waitForCompletion: Boolean?
9898 )
9999
100100 interface IDE {
@@ -139,7 +139,7 @@
139139
140140 suspend fun openUrl(url: String)
141141
142- suspend fun runCommand(command: String, options: TerminalOptions)
142+ suspend fun runCommand(command: String, options: TerminalOptions?)
143143
144144 suspend fun saveFile(filepath: String)
145145