@@ -2,42 +2,54 @@ package cc.unitmesh.terminal
2
2
3
3
import cc.unitmesh.terminal.ShellCommandSuggestAction.Companion.suggestCommand
4
4
import com.intellij.openapi.actionSystem.AnActionEvent
5
+ import com.intellij.openapi.actionSystem.DataProvider
6
+ import com.intellij.openapi.application.runInEdt
7
+ import com.intellij.openapi.editor.ex.EditorEx
5
8
import com.intellij.openapi.project.Project
6
9
import com.intellij.openapi.wm.ToolWindowManager
7
- import com.intellij.terminal.JBTerminalWidget
10
+ import com.intellij.terminal.ui.TerminalWidget
11
+ import com.intellij.ui.components.panels.Wrapper
8
12
import com.intellij.ui.content.Content
9
13
import org.jetbrains.plugins.terminal.TerminalToolWindowFactory
10
14
import org.jetbrains.plugins.terminal.TerminalToolWindowManager
11
- import org.jetbrains.plugins.terminal.exp.TerminalDataContextUtils.editor
15
+ import org.jetbrains.plugins.terminal.exp.TerminalOutputController
12
16
13
17
object TerminalUtil {
14
18
fun sendMsg (project : Project , data : String , e : AnActionEvent ) {
15
- val editor = e.editor
19
+ val content = getContent(project) ? : return
20
+ val findWidgetByContent = TerminalToolWindowManager .findWidgetByContent(content) ? : return
21
+ val editor = tryGetBlockTerminalEditor(findWidgetByContent)
16
22
if (editor == null ) {
17
- trySendMsgInOld(project, data)
23
+ trySendMsgInOld(project, data, content )
18
24
return
19
25
}
20
26
21
27
suggestCommand(data, project) { string ->
22
- editor.document.insertString(editor.caretModel.offset, string)
28
+ runInEdt {
29
+ editor.document.insertString(editor.caretModel.offset, string)
30
+ }
23
31
}
24
32
}
25
33
26
- private fun trySendMsgInOld (project : Project , data : String ): Boolean {
27
- val widget = getCurrentTerminalWidget(project) ? : return true
34
+ private fun tryGetBlockTerminalEditor (findWidgetByContent : TerminalWidget ): EditorEx ? {
35
+ val terminalView = (findWidgetByContent.component as Wrapper ).targetComponent
36
+ if (terminalView is DataProvider ) {
37
+ val controller = terminalView.getData(TerminalOutputController .KEY .name)
38
+ return (controller as ? TerminalOutputController )?.outputModel?.editor
39
+ }
40
+
41
+ return null
42
+ }
43
+
44
+ private fun trySendMsgInOld (project : Project , data : String , content : Content ): Boolean {
45
+ val widget = TerminalToolWindowManager .getWidgetByContent(content) ? : return true
28
46
suggestCommand(data, project) { string ->
29
47
widget.terminalStarter?.sendString(string, true )
30
48
}
31
49
32
50
return false
33
51
}
34
52
35
- fun getCurrentTerminalWidget (project : Project ): JBTerminalWidget ? {
36
- val content = getContent(project) ? : return null
37
- val widget = TerminalToolWindowManager .getWidgetByContent(content) ? : return null
38
- return widget
39
- }
40
-
41
53
private fun getContent (project : Project ): Content ? {
42
54
val toolWindow = ToolWindowManager .getInstance(project)
43
55
.getToolWindow(TerminalToolWindowFactory .TOOL_WINDOW_ID )
0 commit comments