Elffuss BraidHub

kikocis/continue public

feat: support getTerminalContents in InteliJIde.kt, which could only read content from the latest terminal tab for now
Changesetce2e334291621f5b3dcf12269e0cc0724f77f945a15d6058270b19ef9735e47f
Authorzimu <zimu@mgtv.com> · 2025-07-18 17:21 +08:00
Committerzimu <zimu@mgtv.com> · 2025-07-21 09:56 +08:00
Parents9df0fa5d79c7

Browse the files at this changeset

2 files changed

modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/IntelliJIde.kt +33 −8
@@ -27,12 +27,13 @@
2727 import com.intellij.openapi.fileEditor.FileEditorManager
2828 import com.intellij.openapi.project.Project
2929 import com.intellij.openapi.project.guessProjectDir
30-import com.intellij.openapi.util.IconLoader
3130 import com.intellij.openapi.vfs.LocalFileSystem
3231 import com.intellij.openapi.vfs.VirtualFileManager
3332 import com.intellij.psi.PsiDocumentManager
3433 import com.intellij.testFramework.LightVirtualFile
3534 import kotlinx.coroutines.*
35+import org.jetbrains.plugins.terminal.ShellTerminalWidget
36+import org.jetbrains.plugins.terminal.TerminalView
3637 import java.awt.Toolkit
3738 import java.awt.datatransfer.DataFlavor
3839 import java.io.BufferedReader
@@ -54,7 +55,7 @@
5455 init {
5556 try {
5657 val os = getOS()
57-
58+
5859 if (os == OS.LINUX || os == OS.MAC) {
5960 val file = File(ripgrep)
6061 if (!file.canExecute()) {
@@ -135,7 +136,30 @@
135136 }
136137
137138 override suspend fun getTerminalContents(): String {
138- return ""
139+ return withContext(Dispatchers.EDT) {
140+ try {
141+ val terminalView = TerminalView.getInstance(project)
142+ // Find the first terminal widget available, whatever its state.
143+ val widget =
144+ terminalView.getWidgets().filterIsInstance<ShellTerminalWidget>().firstOrNull { it.isEnabled }
145+
146+ if (widget != null) {
147+ val textBuffer = widget.terminalTextBuffer
148+ val stringBuilder = StringBuilder()
149+ // Iterate through all lines in the buffer (history + screen)
150+ for (i in 0 until textBuffer.historyLinesCount + textBuffer.screenLinesCount) {
151+ stringBuilder.append(textBuffer.getLine(i).text).append('\n')
152+ }
153+ stringBuilder.toString()
154+ } else {
155+ "" // Return empty if no terminal is available
156+ }
157+ } catch (e: Exception) {
158+ println("Error getting terminal contents: ${e.message}")
159+ e.printStackTrace()
160+ "" // Return empty on error
161+ }
162+ }
139163 }
140164
141165 override suspend fun getDebugLocals(threadIndex: Int): String {
@@ -343,7 +367,7 @@
343367 }
344368
345369 val command = GeneralCommandLine(commandArgs)
346-
370+
347371 command.setWorkDirectory(project.basePath)
348372 val results = ExecUtil.execAndGetOutput(command).stdout
349373 return results.split("\n")
@@ -357,11 +381,12 @@
357381 throw NotImplementedError("Ripgrep not supported, this workspace is remote")
358382 }
359383 }
384+
360385 override suspend fun getSearchResults(query: String, maxResults: Int?): String {
361386 val ideInfo = this.getIdeInfo()
362387 if (ideInfo.remoteName == "local") {
363388 try {
364- val commandArgs = mutableListOf(
389+ val commandArgs = mutableListOf(
365390 ripgrep,
366391 "-i",
367392 "--ignore-file",
@@ -372,20 +397,20 @@
372397 "2",
373398 "--heading"
374399 )
375-
400+
376401 // Conditionally add maxResults flag
377402 if (maxResults != null) {
378403 commandArgs.add("-m")
379404 commandArgs.add(maxResults.toString())
380405 }
381-
406+
382407 // Add the search query and path
383408 commandArgs.add("-e")
384409 commandArgs.add(query)
385410 commandArgs.add(".")
386411
387412 val command = GeneralCommandLine(commandArgs)
388-
413+
389414 command.setWorkDirectory(project.basePath)
390415 return ExecUtil.execAndGetOutput(command).stdout
391416 } catch (exception: Exception) {
modified extensions/intellij/src/main/resources/META-INF/plugin.xml +1 −0
@@ -7,6 +7,7 @@
77 <![CDATA[View the latest release notes on <a href="https://github.com/continuedev/continue/releases">GitHub</a>]]></change-notes>
88
99 <depends>com.intellij.modules.platform</depends>
10+ <depends>org.jetbrains.plugins.terminal</depends>
1011
1112 <!-- See here for why this is optional: https://github.com/continuedev/continue/issues/2775#issuecomment-2535620877-->
1213 <depends optional="true" config-file="continueintellijextension-withJSON.xml">