Skip to content

Commit 41b4b7e

Browse files
committed
feat(ext-terminal): refactor suggestCommand method to support 241 new UI context and add TerminalUtil class for message sending #135.
1 parent 7d87fd0 commit 41b4b7e

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

exts/ext-terminal/src/222/main/kotlin/cc/unitmesh/terminal/TerminalUtil.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import org.jetbrains.plugins.terminal.TerminalView
1111
object TerminalUtil {
1212
fun sendMsg(project: Project, data: String, e: AnActionEvent) {
1313
val widget = getCurrentTerminalWidget(project) ?: return
14-
suggestCommand(data, project) { string ->
14+
suggestCommand(data, project, { string ->
1515
widget.terminalStarter?.sendString(string, true)
16-
}
16+
}, {})
1717
}
1818

1919
fun getCurrentTerminalWidget(project: Project): JBTerminalWidget? {

exts/ext-terminal/src/233/main/kotlin/cc/unitmesh/terminal/TerminalUtil.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import org.jetbrains.plugins.terminal.TerminalToolWindowManager
1111
object TerminalUtil {
1212
fun sendMsg(project: Project, data: String, e: AnActionEvent) {
1313
val widget = getCurrentTerminalWidget(project) ?: return
14-
suggestCommand(data, project) { string ->
14+
suggestCommand(data, project, { string ->
1515
widget.terminalStarter?.sendString(string, true)
16-
}
16+
}, {})
1717
}
1818

1919
fun getCurrentTerminalWidget(project: Project): JBTerminalWidget? {

exts/ext-terminal/src/241/main/kotlin/cc/unitmesh/terminal/TerminalUtil.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ object TerminalUtil {
2525
}
2626

2727
val sb = StringBuilder()
28-
suggestCommand(data, project) { string ->
28+
suggestCommand(data, project, { string ->
2929
sb.append(string)
3030
null
31-
}
32-
33-
runInEdt {
34-
CopyPasteManager.copyTextToClipboard(sb.toString())
35-
controller.performPaste(e.dataContext)
36-
}
31+
}, {
32+
runInEdt {
33+
CopyPasteManager.copyTextToClipboard(sb.toString())
34+
controller.performPaste(e.dataContext)
35+
}
36+
})
3737
}
3838

3939
private fun tryGetBlockTerminalEditor(findWidgetByContent: TerminalWidget): TerminalPromptController? {
40-
val terminalView = (findWidgetByContent.component as Wrapper).targetComponent
40+
val terminalView = (findWidgetByContent.component as? Wrapper)?.targetComponent ?: return null
4141
if (terminalView is DataProvider) {
4242
val controller = terminalView.getData(TerminalPromptController.KEY.name)
4343
return (controller as? TerminalPromptController)
@@ -48,9 +48,9 @@ object TerminalUtil {
4848

4949
private fun trySendMsgInOld(project: Project, data: String, content: Content): Boolean {
5050
val widget = TerminalToolWindowManager.getWidgetByContent(content) ?: return true
51-
suggestCommand(data, project) { string ->
51+
suggestCommand(data, project, { string ->
5252
widget.terminalStarter?.sendString(string, true)
53-
}
53+
}, {})
5454

5555
return false
5656
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ class ShellCommandSuggestAction : DumbAwareAction() {
125125
}
126126

127127
companion object {
128-
fun suggestCommand(data: String, project: Project, function: (str: String) -> Unit?) {
128+
fun suggestCommand(data: String, project: Project,
129+
chunk: (str: String) -> Unit?,
130+
done: ((str: String) -> Unit?)?
131+
) {
129132
val templateRender = TemplateRender(GENIUS_PRACTISES)
130133
val template = templateRender.getTemplate("shell-suggest.vm")
131134

@@ -145,13 +148,17 @@ class ShellCommandSuggestAction : DumbAwareAction() {
145148
AutoDevStatusService.notifyApplication(AutoDevStatus.InProgress)
146149

147150
try {
151+
val sb = StringBuilder()
148152
stringFlow.collect {
153+
sb.append(it)
149154
if (it.contains("\n")) {
150155
throw Exception("Shell command suggestion failed")
151156
}
152157

153-
function(it)
158+
chunk(it)
154159
}
160+
161+
done?.invoke(sb.toString())
155162
} finally {
156163
AutoDevStatusService.notifyApplication(AutoDevStatus.Ready)
157164
}

0 commit comments

Comments
 (0)