Skip to content

Commit bcd2145

Browse files
committed
refactor(terminal): replace terminal text extraction method
Replace `terminalWidget.text` with a custom `getText` method to handle text extraction from `TerminalPanel` in IntelliJ 223. This change ensures compatibility with newer versions of IntelliJ where the original method is no longer available.
1 parent 882504b commit bcd2145

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import com.intellij.openapi.wm.ToolWindowManager
2323
import com.intellij.terminal.JBTerminalWidget
2424
import com.intellij.ui.components.panels.HorizontalLayout
2525
import com.intellij.util.ui.JBUI
26-
import com.jediterm.terminal.ui.TerminalWidgetListener
26+
import com.jediterm.core.compatibility.Point
27+
import com.jediterm.terminal.model.SelectionUtil
28+
import com.jediterm.terminal.model.TerminalSelection
29+
import com.jediterm.terminal.ui.TerminalPanel
2730
import org.jetbrains.plugins.terminal.LocalTerminalDirectRunner
2831
import java.awt.BorderLayout
2932
import java.awt.Dimension
@@ -70,7 +73,7 @@ class TerminalLangSketchProvider : LanguageSketchProvider {
7073
val sendButton = JButton("Send to Sketch").apply {
7174
addMouseListener(object : MouseAdapter() {
7275
override fun mouseClicked(e: MouseEvent?) {
73-
val output = terminalWidget!!.text
76+
val output = getText(terminalWidget!!.terminalPanel)
7477
sendToSketch(project, output)
7578
}
7679
})
@@ -147,6 +150,22 @@ class TerminalLangSketchProvider : LanguageSketchProvider {
147150
}
148151
}
149152

153+
/// in Intellij 223 this method is not exist, so we need to copy it.
154+
fun getText(terminalPanel: TerminalPanel): String {
155+
val buffer = terminalPanel.terminalTextBuffer
156+
buffer.lock()
157+
try {
158+
val selection = TerminalSelection(
159+
Point(0, -buffer.historyLinesCount),
160+
Point(buffer.width, buffer.screenLinesCount - 1)
161+
)
162+
val points = selection.pointsForRun(buffer.width)
163+
return SelectionUtil.getSelectionText(points.first!!, points.second!!, buffer)
164+
} finally {
165+
buffer.unlock()
166+
}
167+
}
168+
150169
private fun sendToSketch(project: Project, output: String) {
151170
val contentManager = ToolWindowManager.getInstance(project).getToolWindow("AutoDev")?.contentManager
152171
contentManager?.component?.components?.filterIsInstance<SketchToolWindow>()?.firstOrNull().let {

0 commit comments

Comments
 (0)