Skip to content

Commit d354989

Browse files
committed
feat(devins-lang): add SHELL command and related functionality #101
Add a new SHELL command to the DevIns language, along with supporting functionality such as command execution and completion provider for code insight.
1 parent fab54c0 commit d354989

File tree

6 files changed

+80
-3
lines changed

6 files changed

+80
-3
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ project(":exts:devins-lang") {
591591

592592
intellij {
593593
version.set(ideaVersion)
594-
plugins.set((ideaPlugins + "org.intellij.plugins.markdown"))
594+
plugins.set((ideaPlugins + "org.intellij.plugins.markdown" + "com.jetbrains.sh"))
595595
}
596596

597597
dependencies {

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/DevInsCompiler.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ class DevInsCompiler(
183183
result.isLocalCommand = true
184184
FileFuncInsCommand(myProject, prop)
185185
}
186+
187+
BuiltinCommand.SHELL -> {
188+
result.isLocalCommand = true
189+
ShellInsCommand(myProject, prop)
190+
}
186191
}
187192

188193
val execResult = command.execute()
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package cc.unitmesh.devti.language.compiler.exec
2+
3+
import cc.unitmesh.devti.language.utils.lookupFile
4+
import com.intellij.execution.DefaultExecutionResult
5+
import com.intellij.execution.ExecutionException
6+
import com.intellij.execution.configurations.GeneralCommandLine
7+
import com.intellij.execution.configurations.PtyCommandLine
8+
import com.intellij.execution.process.KillableProcessHandler
9+
import com.intellij.execution.process.ProcessHandler
10+
import com.intellij.execution.process.ProcessTerminatedListener
11+
import com.intellij.execution.ui.ConsoleView
12+
import com.intellij.openapi.application.ApplicationManager
13+
import com.intellij.openapi.project.Project
14+
import com.intellij.sh.run.ShConfigurationType
15+
import com.intellij.sh.run.ShRunner
16+
import com.intellij.terminal.TerminalExecutionConsole
17+
import com.intellij.util.io.BaseOutputReader
18+
19+
class ShellInsCommand(val myProject: Project, val prop: String) : InsCommand {
20+
override fun execute(): String? {
21+
val virtualFile = myProject.lookupFile(prop.trim()) ?: return "<DevInsError>: File not found: $prop"
22+
23+
val workingDirectory = virtualFile.parent.path
24+
val shRunner = ApplicationManager.getApplication().getService(
25+
ShRunner::class.java
26+
)
27+
if (shRunner != null && shRunner.isAvailable(myProject)) {
28+
shRunner.run(myProject, virtualFile.path, workingDirectory, "RunDevInsShell", true)
29+
}
30+
31+
// TODO: after run done
32+
// runInTerminal(virtualFile.path, workingDirectory, myProject)
33+
34+
return ""
35+
}
36+
37+
@Throws(ExecutionException::class)
38+
private fun runInTerminal(command: String, workingDirectory: String, project: Project): DefaultExecutionResult {
39+
val commandLine = createCommandLineForScript(project, workingDirectory, command)
40+
val processHandler = createProcessHandler(commandLine)
41+
ProcessTerminatedListener.attach(processHandler)
42+
val console: ConsoleView = TerminalExecutionConsole(project, processHandler)
43+
console.attachToProcess(processHandler)
44+
return DefaultExecutionResult(console, processHandler)
45+
}
46+
47+
private fun createCommandLineForScript(
48+
project: Project,
49+
workingDirectory: String,
50+
command: String
51+
): GeneralCommandLine {
52+
val commandLine = PtyCommandLine()
53+
commandLine.withConsoleMode(false)
54+
commandLine.withInitialColumns(120)
55+
commandLine.withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE)
56+
commandLine.setWorkDirectory(workingDirectory)
57+
commandLine.withExePath(ShConfigurationType.getDefaultShell(project))
58+
commandLine.withParameters("-c")
59+
commandLine.withParameters(command)
60+
return commandLine
61+
}
62+
63+
@Throws(ExecutionException::class)
64+
private fun createProcessHandler(commandLine: GeneralCommandLine): ProcessHandler {
65+
return object : KillableProcessHandler(commandLine) {
66+
override fun readerOptions(): BaseOutputReader.Options {
67+
return BaseOutputReader.Options.forTerminalPtyProcess()
68+
}
69+
}
70+
}
71+
}

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/completion/provider/BuiltinCommandCompletion.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.intellij.codeInsight.AutoPopupController
55
import com.intellij.codeInsight.completion.*
66
import com.intellij.codeInsight.lookup.LookupElementBuilder
77
import com.intellij.util.ProcessingContext
8-
import org.jetbrains.kotlin.idea.completion.weighers.VariableOrParameterNameWithTypeWeigher.nameWithTypePriority
98

109
class BuiltinCommandCompletion : CompletionProvider<CompletionParameters>() {
1110
override fun addCompletions(

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/dataprovider/BuiltinCommand.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ enum class BuiltinCommand(
2323
WRITE("write", "Write content to a file, /write:path/to/file:L1-L2", AllIcons.Actions.Edit, true, true),
2424
PATCH("patch", "Apply patch to a file, /patch:path/to/file", AllIcons.Vcs.Patch_file, false),
2525
RUN("run", "Run the content of a file", AllIcons.Actions.Execute, true, true),
26+
SHELL("shell", "Run shell command", AllIcons.Actions.Execute, true, true),
27+
COMMIT("commit", "Commit the content of a file", AllIcons.Vcs.CommitNode, false),
2628
FILE_FUNC("file-func", "Read the name of a file", AllIcons.Actions.GroupByFile, true, true),
27-
COMMIT("commit", "Commit the content of a file", AllIcons.Vcs.CommitNode, false)
2829
;
2930

3031
companion object {

exts/devins-lang/src/main/resources/cc.unitmesh.devti.language.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<idea-plugin package="cc.unitmesh.devti.language">
22
<dependencies>
33
<plugin id="org.intellij.plugins.markdown"/>
4+
<plugin id="com.jetbrains.sh"/>
45
</dependencies>
56

67
<extensions defaultExtensionNs="com.intellij">

0 commit comments

Comments
 (0)