Skip to content

Commit 5538584

Browse files
committed
feat(terminal): add shell tool detection and context-aware command suggestions #135
1 parent c37ef91 commit 5538584

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.jetbrains.plugins.terminal.TerminalView
88

99
object TerminalUtil {
1010
fun getCurrentTerminalWidget(project: Project): JBTerminalWidget? {
11+
// TODO: test with `e.dataContext.getData(JBTerminalWidget.TERMINAL_DATA_KEY) ?: return`
1112
val toolWindow = ToolWindowManager.getInstance(project)
1213
.getToolWindow(TerminalToolWindowFactory.TOOL_WINDOW_ID) ?: return null
1314
val content = toolWindow.contentManager.selectedContent ?: return null

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent
1313
import com.intellij.openapi.actionSystem.AnActionHolder
1414
import com.intellij.openapi.actionSystem.PlatformCoreDataKeys
1515
import com.intellij.openapi.project.Project
16+
import com.intellij.openapi.project.guessProjectDir
1617
import com.intellij.openapi.ui.popup.Balloon
1718
import com.intellij.openapi.ui.popup.JBPopupFactory
1819
import com.intellij.openapi.ui.popup.JBPopupListener
@@ -27,6 +28,7 @@ import com.intellij.util.ui.SwingHelper
2728
import com.intellij.util.ui.UIUtil
2829
import kotlinx.coroutines.flow.*
2930
import kotlinx.coroutines.launch
31+
import org.jetbrains.plugins.terminal.TerminalProjectOptionsProvider
3032
import java.awt.Component
3133
import java.awt.Font
3234
import java.awt.Point
@@ -42,23 +44,24 @@ private const val ERROR_VALUE = "error"
4244
class ShellCommandSuggestAction : AnAction() {
4345
override fun actionPerformed(e: AnActionEvent) {
4446
val project = e.project ?: return
45-
46-
val popupPoint = getPreferredPopupPoint(e)
47-
4847
val contextComponent = e.getData(PlatformCoreDataKeys.CONTEXT_COMPONENT) ?: return
4948

50-
showContentRenamePopup(contextComponent, popupPoint) { data ->
49+
showContentRenamePopup(contextComponent, getPreferredPopupPoint(e)) { data ->
5150
val widget = TerminalUtil.getCurrentTerminalWidget(project) ?: return@showContentRenamePopup
5251
suggestCommand(widget, data, project)
5352
}
5453
}
5554

56-
data class ShellSuggestions(val question: String)
57-
5855
private fun suggestCommand(widget: JBTerminalWidget, data: String, project: Project) {
5956
val templateRender = TemplateRender(GENIUS_PRACTISES)
6057
val template = templateRender.getTemplate("shell-suggest.vm")
61-
templateRender.context = ShellSuggestions(data)
58+
59+
val options = TerminalProjectOptionsProvider.getInstance(project)
60+
61+
templateRender.context = ShellSuggestContext(
62+
data, options.shellPath, options.startingDirectory
63+
?: project.guessProjectDir()?.path ?: System.getProperty("user.home")
64+
)
6265
val promptText = templateRender.renderTemplate(template)
6366

6467
val llm = LlmFactory.instance.create(project)
@@ -156,3 +159,4 @@ class ShellCommandSuggestAction : AnAction() {
156159
}
157160
}
158161

162+
data class ShellSuggestContext(val question: String, val shellPath: String, val cwd: String)

src/main/resources/genius/en/practises/shell-suggest.vm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Return only the command to be executed as a raw string, no string delimiters
22
wrapping it, no yapping, no markdown, no fenced code blocks, what you return
33
will be passed to subprocess.check_output() directly.
44

5+
User current directory is: ${context.cwd}, user use is: ${context.shellPath}, according the tool to create the command.
6+
57
For example, if the user asks: undo last git commit
68

79
You return only line command: git reset --soft HEAD~1

src/main/resources/genius/zh/practises/shell-suggest.vm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Return only the command to be executed as a raw string, no string delimiters
22
wrapping it, no yapping, no markdown, no fenced code blocks, what you return
33
will be passed to subprocess.check_output() directly.
44

5+
User current directory is: ${context.cwd}, user use is: ${context.shellPath}, according the tool to create the command.
6+
57
For example, if the user asks: undo last git commit
68

79
You return only line command: git reset --soft HEAD~1

0 commit comments

Comments
 (0)