Skip to content

Commit 633e393

Browse files
committed
feat(ext-terminal): refactor suggestCommand method to support new UI context and add TerminalUtil class for message sending #135
The commit introduces a new class, `TerminalUtil`, which provides a utility method for sending messages to the terminal. It also refactors the `suggestCommand` method to use the new `TerminalUtil` class and to support the new UI context. Additionally, the commit updates the build script to include the new source and resource directories for the `241` platform version, and renames the `NewTerminalUiUtil` class to `TerminalUtil`.
1 parent 3533b1d commit 633e393

File tree

6 files changed

+122
-50
lines changed

6 files changed

+122
-50
lines changed

build.gradle.kts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ allprojects {
181181

182182
val testOutput = configurations.create("testOutput")
183183

184-
184+
if(this.name != "ext-terminal") {
185185
sourceSets {
186186
main {
187187
java.srcDirs("src/gen")
@@ -208,6 +208,7 @@ allprojects {
208208
}
209209
}
210210
}
211+
}
211212

212213
dependencies {
213214
compileOnly(kotlin("stdlib-jdk8"))
@@ -606,6 +607,25 @@ project(":exts:ext-terminal") {
606607
dependencies {
607608
implementation(project(":"))
608609
}
610+
611+
sourceSets {
612+
main {
613+
resources.srcDirs("src/$platformVersion/main/resources")
614+
}
615+
test {
616+
resources.srcDirs("src/$platformVersion/test/resources")
617+
}
618+
}
619+
kotlin {
620+
sourceSets {
621+
main {
622+
kotlin.srcDirs("src/$platformVersion/main/kotlin")
623+
}
624+
test {
625+
kotlin.srcDirs("src/$platformVersion/test/kotlin")
626+
}
627+
}
628+
}
609629
}
610630

611631
project(":exts:devins-lang") {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
package cc.unitmesh.terminal
22

3+
import cc.unitmesh.terminal.ShellCommandSuggestAction.Companion.suggestCommand
34
import com.intellij.openapi.project.Project
45
import com.intellij.openapi.wm.ToolWindowManager
56
import com.intellij.terminal.JBTerminalWidget
67
import org.jetbrains.plugins.terminal.TerminalToolWindowFactory
78
import org.jetbrains.plugins.terminal.TerminalView
89

910
object TerminalUtil {
11+
fun sendMsg(project: Project, data: String, e: AnActionEvent) {
12+
val widget = getCurrentTerminalWidget(project) ?: return
13+
suggestCommand(data, project) { string ->
14+
widget.terminalStarter?.sendString(string, true)
15+
}
16+
}
17+
1018
fun getCurrentTerminalWidget(project: Project): JBTerminalWidget? {
1119
// TODO: test with `e.dataContext.getData(JBTerminalWidget.TERMINAL_DATA_KEY) ?: return`
1220
val toolWindow = ToolWindowManager.getInstance(project)

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
package cc.unitmesh.terminal
22

3+
import cc.unitmesh.terminal.ShellCommandSuggestAction.Companion.suggestCommand
34
import com.intellij.openapi.project.Project
45
import com.intellij.openapi.wm.ToolWindowManager
56
import com.intellij.terminal.JBTerminalWidget
67
import org.jetbrains.plugins.terminal.TerminalToolWindowFactory
78
import org.jetbrains.plugins.terminal.TerminalToolWindowManager
89

910
object TerminalUtil {
11+
fun sendMsg(project: Project, data: String, e: AnActionEvent) {
12+
val widget = getCurrentTerminalWidget(project) ?: return
13+
suggestCommand(data, project) { string ->
14+
widget.terminalStarter?.sendString(string, true)
15+
}
16+
}
17+
1018
fun getCurrentTerminalWidget(project: Project): JBTerminalWidget? {
1119
val toolWindow = ToolWindowManager.getInstance(project)
1220
.getToolWindow(TerminalToolWindowFactory.TOOL_WINDOW_ID) ?: return null

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

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cc.unitmesh.terminal
2+
3+
import cc.unitmesh.terminal.ShellCommandSuggestAction.Companion.suggestCommand
4+
import com.intellij.openapi.actionSystem.AnActionEvent
5+
import com.intellij.openapi.project.Project
6+
import com.intellij.openapi.wm.ToolWindowManager
7+
import com.intellij.terminal.JBTerminalWidget
8+
import com.intellij.ui.content.Content
9+
import org.jetbrains.plugins.terminal.TerminalToolWindowFactory
10+
import org.jetbrains.plugins.terminal.TerminalToolWindowManager
11+
import org.jetbrains.plugins.terminal.exp.TerminalDataContextUtils.editor
12+
13+
object TerminalUtil {
14+
fun sendMsg(project: Project, data: String, e: AnActionEvent) {
15+
val editor = e.editor
16+
if (editor == null) {
17+
trySendMsgInOld(project, data)
18+
return
19+
}
20+
21+
suggestCommand(data, project) { string ->
22+
editor.document.insertString(editor.caretModel.offset, string)
23+
}
24+
}
25+
26+
private fun trySendMsgInOld(project: Project, data: String): Boolean {
27+
val widget = getCurrentTerminalWidget(project) ?: return true
28+
suggestCommand(data, project) { string ->
29+
widget.terminalStarter?.sendString(string, true)
30+
}
31+
32+
return false
33+
}
34+
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+
private fun getContent(project: Project): Content? {
42+
val toolWindow = ToolWindowManager.getInstance(project)
43+
.getToolWindow(TerminalToolWindowFactory.TOOL_WINDOW_ID)
44+
val content = toolWindow?.contentManager?.selectedContent
45+
return content
46+
}
47+
}

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

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -45,44 +45,9 @@ class ShellCommandSuggestAction : AnAction() {
4545
val project = e.project ?: return
4646
val contextComponent = e.getData(PlatformCoreDataKeys.CONTEXT_COMPONENT) ?: return
4747

48-
showContentRenamePopup(contextComponent, getPreferredPopupPoint(e)) { data ->
49-
val widget = TerminalUtil.getCurrentTerminalWidget(project) ?: return@showContentRenamePopup
50-
suggestCommand(data, project) { string ->
51-
widget.terminalStarter?.sendString(string, true)
52-
}
53-
}
54-
}
55-
56-
open fun suggestCommand(data: String, project: Project, function: (str: String) -> Unit?) {
57-
val templateRender = TemplateRender(GENIUS_PRACTISES)
58-
val template = templateRender.getTemplate("shell-suggest.vm")
59-
60-
val options = TerminalProjectOptionsProvider.getInstance(project)
61-
62-
templateRender.context = ShellSuggestContext(
63-
data, options.shellPath,
64-
options.startingDirectory
65-
?: project.guessProjectDir()?.path ?: System.getProperty("user.home")
66-
)
67-
val promptText = templateRender.renderTemplate(template)
68-
69-
val llm = LlmFactory.instance.create(project)
70-
val stringFlow: Flow<String> = llm.stream(promptText, "", false)
71-
72-
LLMCoroutineScope.scope(project).launch {
73-
AutoDevStatusService.notifyApplication(AutoDevStatus.InProgress)
74-
75-
try {
76-
stringFlow.collect {
77-
if (it.contains("\n")) {
78-
throw Exception("Shell command suggestion failed")
79-
}
80-
81-
function(it)
82-
}
83-
} finally {
84-
AutoDevStatusService.notifyApplication(AutoDevStatus.Ready)
85-
}
48+
showContentRenamePopup(contextComponent, getPreferredPopupPoint(e)) { string ->
49+
TerminalUtil.sendMsg(project, string, e)
50+
return@showContentRenamePopup
8651
}
8752
}
8853

@@ -159,5 +124,40 @@ class ShellCommandSuggestAction : AnAction() {
159124
}
160125
})
161126
}
127+
128+
companion object {
129+
fun suggestCommand(data: String, project: Project, function: (str: String) -> Unit?) {
130+
val templateRender = TemplateRender(GENIUS_PRACTISES)
131+
val template = templateRender.getTemplate("shell-suggest.vm")
132+
133+
val options = TerminalProjectOptionsProvider.getInstance(project)
134+
135+
templateRender.context = ShellSuggestContext(
136+
data, options.shellPath,
137+
options.startingDirectory
138+
?: project.guessProjectDir()?.path ?: System.getProperty("user.home")
139+
)
140+
val promptText = templateRender.renderTemplate(template)
141+
142+
val llm = LlmFactory.instance.create(project)
143+
val stringFlow: Flow<String> = llm.stream(promptText, "", false)
144+
145+
LLMCoroutineScope.scope(project).launch {
146+
AutoDevStatusService.notifyApplication(AutoDevStatus.InProgress)
147+
148+
try {
149+
stringFlow.collect {
150+
if (it.contains("\n")) {
151+
throw Exception("Shell command suggestion failed")
152+
}
153+
154+
function(it)
155+
}
156+
} finally {
157+
AutoDevStatusService.notifyApplication(AutoDevStatus.Ready)
158+
}
159+
}
160+
}
161+
}
162162
}
163163

0 commit comments

Comments
 (0)