feat: optimize getTerminalContents in InteliJIde.kt, which reads content from the selected terminal panel

This commit is contained in:
zimu 2025-07-18 17:51:53 +08:00
parent 1862344605
commit 10e4c325a2

View file

@ -29,6 +29,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.openapi.wm.ToolWindowManager
import com.intellij.psi.PsiDocumentManager
import com.intellij.testFramework.LightVirtualFile
import kotlinx.coroutines.*
@ -138,10 +139,13 @@ class IntelliJIDE(
override suspend fun getTerminalContents(): String {
return withContext(Dispatchers.EDT) {
try {
val toolWindow = ToolWindowManager.getInstance(project).getToolWindow("Terminal")
val terminalView = TerminalView.getInstance(project)
// Find the first terminal widget available, whatever its state.
val widget =
terminalView.getWidgets().filterIsInstance<ShellTerminalWidget>().firstOrNull { it.isEnabled }
// Find the first terminal widget selected, whatever its state, running command or not.
val widget = terminalView.getWidgets().filterIsInstance<ShellTerminalWidget>().firstOrNull {
toolWindow?.getContentManager()?.getContent(it)?.isSelected ?: false
}
if (widget != null) {
val textBuffer = widget.terminalTextBuffer