Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 87f1d5d

Browse files
committed
feat(shire): add new DevIns action templates and update related configurations #379
1 parent 492f16d commit 87f1d5d

File tree

46 files changed

+1291
-66
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1291
-66
lines changed

core/src/main/resources/messages/AutoDevBundle_en.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,6 @@ shire.run.local.mode=Run Local Model
271271
devins.llm.notfound=LLM not found
272272
devins.llm.done=Done
273273
intentions.step.prepare-context=Prepare Context
274+
devins.intention=Shire intention action
275+
devins.newFile=New File
276+
devins.file=DevIns File

core/src/main/resources/messages/AutoDevBundle_zh.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,6 @@ shire.run.local.mode=Run Local Model
261261
devins.llm.notfound=LLM not found
262262
devins.llm.done=Done
263263
intentions.step.prepare-context=Prepare Context
264+
devins.intention=Shire intention action
265+
devins.newFile=New File
266+
devins.file=DevIns File

exts/devins-lang/src/223/main/kotlin/cc/unitmesh/devti/language/startup/ShireActionStartupActivity.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,10 @@ class ShireActionStartupActivity : StartupActivity {
4141
changesProvider.onUpdated(it)
4242
}
4343

44-
// changesProvider.startup { shireConfig, shireFile ->
45-
// attachCopyPasteAction(shireConfig, shireFile)
46-
// }
47-
// attachTerminalAction()
48-
// attachDatabaseAction()
49-
// attachVcsLogAction()
50-
// attachExtensionActions(project)
44+
attachTerminalAction()
45+
attachDatabaseAction()
46+
attachVcsLogAction()
47+
attachExtensionActions(project)
5148
}
5249
}
5350

exts/devins-lang/src/233/main/kotlin/cc/unitmesh/devti/language/startup/ShireActionStartupActivity.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cc.unitmesh.devti.language.startup
22

33
import cc.unitmesh.devti.custom.team.InteractionType
44
import cc.unitmesh.devti.language.DevInFileType
5+
import cc.unitmesh.devti.language.actions.copyPaste.PasteManagerService
56
import cc.unitmesh.devti.language.ast.HobbitHole
67
import cc.unitmesh.devti.language.psi.DevInFile
78
import cc.unitmesh.devti.language.startup.third.ShireSonarLintToolWindowListener
@@ -36,9 +37,9 @@ class ShireActionStartupActivity : ProjectActivity {
3637
changesProvider.onUpdated(it)
3738
}
3839

39-
// attachTerminalAction()
40-
// attachDatabaseAction()
41-
// attachVcsLogAction()
40+
attachTerminalAction()
41+
attachDatabaseAction()
42+
attachVcsLogAction()
4243
attachExtensionActions(project)
4344
}
4445
}

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/actions/DevInsRunFileAction.kt

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package cc.unitmesh.devti.language.actions
22

3-
import cc.unitmesh.devti.language.startup.DynamicDevInActionConfig
3+
import cc.unitmesh.devti.language.startup.DynamicDevInsActionConfig
44
import cc.unitmesh.devti.language.middleware.post.PostProcessorContext
55
import cc.unitmesh.devti.language.psi.DevInFile
66
import cc.unitmesh.devti.language.run.DevInsConfiguration
@@ -10,6 +10,7 @@ import cc.unitmesh.devti.language.run.runner.ShireConsoleView
1010
import cc.unitmesh.devti.language.status.DevInsRunListener
1111
import com.intellij.execution.ExecutionManager
1212
import com.intellij.execution.RunManager
13+
import com.intellij.execution.RunnerAndConfigurationSettings
1314
import com.intellij.execution.actions.ConfigurationContext
1415
import com.intellij.execution.actions.RunConfigurationProducer
1516
import com.intellij.execution.executors.DefaultRunExecutor
@@ -61,6 +62,65 @@ class DevInsRunFileAction : DumbAwareAction() {
6162
companion object {
6263
val ID: @NonNls String = "runDevInsFileAction"
6364

65+
fun createRunConfig(e: AnActionEvent): RunnerAndConfigurationSettings? {
66+
val context = ConfigurationContext.getFromContext(e.dataContext, e.place)
67+
return RunConfigurationProducer.getInstance(DevInsRunConfigurationProducer::class.java)
68+
.findExistingConfiguration(context)
69+
}
70+
71+
/**
72+
* Executes a Shire file within the specified project context.
73+
*
74+
* ```kotlin
75+
* val project = ... // IntelliJ IDEA project
76+
* val config = ... // DynamicShireActionConfig object
77+
* val runSettings = ... // Optional RunnerAndConfigurationSettings
78+
* val variables = mapOf("key1" to "value1", "key2" to "value2")
79+
*
80+
* executeFile(project, config, runSettings, variables)
81+
* ```
82+
*
83+
* @param project The IntelliJ IDEA project in which the Shire file is to be executed.
84+
* @param config The configuration object containing details about the Shire file to be executed.
85+
* @param runSettings Optional runner and configuration settings to use for execution. If null, a new configuration will be created.
86+
* @param variables A map of variables to be passed to the Shire file during execution. Defaults to an empty map.
87+
*
88+
* @throws Exception If there is an error creating the run configuration or execution environment.
89+
*/
90+
fun executeFile(
91+
project: Project,
92+
config: DynamicDevInsActionConfig,
93+
runSettings: RunnerAndConfigurationSettings?,
94+
variables: Map<String, String> = mapOf(),
95+
) {
96+
val settings = try {
97+
runSettings ?: RunManager.getInstance(project)
98+
.createConfiguration(config.name, DevInsConfigurationType::class.java)
99+
} catch (e: Exception) {
100+
logger<DevInsRunFileAction>().error("Failed to create configuration", e)
101+
return
102+
}
103+
104+
val runConfiguration = settings.configuration as DevInsConfiguration
105+
runConfiguration.setScriptPath(config.devinFile.virtualFile.path)
106+
if (variables.isNotEmpty()) {
107+
runConfiguration.setVariables(variables)
108+
PostProcessorContext.updateRunConfigVariables(variables)
109+
}
110+
111+
val executorInstance = DefaultRunExecutor.getRunExecutorInstance()
112+
val executionEnvironment = ExecutionEnvironmentBuilder
113+
.createOrNull(executorInstance, runConfiguration)
114+
?.build()
115+
116+
if (executionEnvironment == null) {
117+
logger<DevInsRunFileAction>().error("Failed to create execution environment")
118+
return
119+
}
120+
121+
ExecutionManager.getInstance(project).restartRunProfile(executionEnvironment)
122+
}
123+
64124
fun suspendExecuteFile(
65125
project: Project,
66126
file: DevInFile,
@@ -74,7 +134,7 @@ class DevInsRunFileAction : DumbAwareAction() {
74134
variables[varName] = varValue
75135
}
76136

77-
val config = DynamicDevInActionConfig.from(file)
137+
val config = DynamicDevInsActionConfig.from(file)
78138

79139
val settings = try {
80140
RunManager.getInstance(project)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cc.unitmesh.devti.language.actions.base.validator
2+
3+
import cc.unitmesh.devti.language.ast.FrontMatterType
4+
import cc.unitmesh.devti.language.ast.Statement
5+
import cc.unitmesh.devti.language.ast.variable.ConditionPsiVariable
6+
import com.intellij.psi.PsiFile
7+
8+
9+
object WhenConditionValidator {
10+
private fun buildPsiVariable(file: PsiFile): Map<String, String> {
11+
return ConditionPsiVariable.values().associate {
12+
when (it) {
13+
ConditionPsiVariable.FILE_PATH -> it.variableName to file.virtualFile.path
14+
ConditionPsiVariable.FILE_NAME -> it.variableName to file.name
15+
ConditionPsiVariable.FILE_EXTENSION -> it.variableName to (file.virtualFile.extension ?: "")
16+
ConditionPsiVariable.FILE_CONTENT -> it.variableName to file.text
17+
}
18+
}
19+
}
20+
21+
fun isAvailable(conditions: FrontMatterType.EXPRESSION, file: PsiFile): Boolean {
22+
return (conditions.value as? Statement)?.evaluate(buildPsiVariable(file)) == true
23+
}
24+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cc.unitmesh.devti.language.actions.console
2+
3+
import cc.unitmesh.devti.language.actions.DevInsRunFileAction
4+
import cc.unitmesh.devti.language.ast.config.DevInsActionLocation
5+
import cc.unitmesh.devti.language.provider.action.VariableActionEventDataHolder
6+
import cc.unitmesh.devti.language.startup.DynamicShireActionService
7+
import com.intellij.openapi.actionSystem.ActionUpdateThread
8+
import com.intellij.openapi.actionSystem.AnAction
9+
import com.intellij.openapi.actionSystem.AnActionEvent
10+
import com.intellij.openapi.project.Project
11+
12+
class ShireConsoleAction : AnAction() {
13+
override fun getActionUpdateThread() = ActionUpdateThread.EDT
14+
15+
private fun shireActionConfigs(project: Project) =
16+
DynamicShireActionService.getInstance(project).getActions(DevInsActionLocation.CONSOLE_MENU)
17+
18+
override fun update(e: AnActionEvent) {
19+
val project = e.project ?: return
20+
val isOnlyOneConfig = shireActionConfigs(project).size == 1
21+
22+
val hobbitHole = shireActionConfigs(project).firstOrNull()?.hole
23+
e.presentation.isVisible = isOnlyOneConfig
24+
e.presentation.isEnabled = hobbitHole != null && hobbitHole.enabled
25+
if (hobbitHole != null) {
26+
e.presentation.text = hobbitHole.name ?: "<Placeholder>"
27+
}
28+
}
29+
30+
override fun actionPerformed(e: AnActionEvent) {
31+
val project = e.project ?: return
32+
33+
VariableActionEventDataHolder.putData(VariableActionEventDataHolder(e.dataContext))
34+
35+
val config = shireActionConfigs(project).firstOrNull() ?: return
36+
DevInsRunFileAction.executeFile(project, config, null)
37+
}
38+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cc.unitmesh.devti.language.actions.context
2+
3+
import cc.unitmesh.devti.AutoDevIcons
4+
import cc.unitmesh.devti.language.actions.DevInsRunFileAction
5+
import com.intellij.openapi.actionSystem.ActionUpdateThread
6+
import com.intellij.openapi.actionSystem.AnActionEvent
7+
import com.intellij.openapi.actionSystem.CommonDataKeys
8+
import com.intellij.openapi.diagnostic.logger
9+
import com.intellij.openapi.project.DumbAwareAction
10+
import cc.unitmesh.devti.language.actions.base.validator.WhenConditionValidator
11+
import cc.unitmesh.devti.language.startup.DynamicDevInsActionConfig
12+
13+
class ShireContextMenuAction(private val config: DynamicDevInsActionConfig) :
14+
DumbAwareAction(config.name, config.hole?.description, AutoDevIcons.AI_COPILOT) {
15+
16+
init {
17+
templatePresentation.text = config.name.ifBlank { "Unknown" }
18+
}
19+
20+
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT
21+
22+
23+
override fun update(e: AnActionEvent) {
24+
//2024-07-13 10:32:57,277 [51307999] SEVERE - #c.i.o.a.i.Utils - Empty menu item text for ShireContextMenuAction@EditorPopup (com.phodal.shirelang.actions.context.ShireContextMenuAction). The default action text must be specified in plugin.xml or its class constructor [Plugin: com.phodal.shire]
25+
// com.intellij.diagnostic.PluginException: Empty menu item text for ShireContextMenuAction@EditorPopup (com.phodal.shirelang.actions.context.ShireContextMenuAction). The default action text must be specified in plugin.xml or its class constructor [Plugin: com.phodal.shire]
26+
try {
27+
val conditions = config.hole?.when_ ?: return
28+
val psiFile = e.getData(CommonDataKeys.PSI_FILE) ?: return
29+
30+
WhenConditionValidator.isAvailable(conditions, psiFile).let {
31+
e.presentation.isEnabled = it
32+
e.presentation.isVisible = it
33+
34+
e.presentation.text = config.hole.name
35+
}
36+
} catch (e: Exception) {
37+
logger<ShireContextMenuAction>().error("Error in ShireContextMenuAction", e)
38+
}
39+
}
40+
41+
override fun actionPerformed(e: AnActionEvent) {
42+
val project = e.project ?: return
43+
DevInsRunFileAction.executeFile(
44+
project,
45+
config,
46+
DevInsRunFileAction.createRunConfig(e)
47+
)
48+
}
49+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package cc.unitmesh.devti.language.actions.context
2+
3+
import com.intellij.openapi.actionSystem.*
4+
import cc.unitmesh.devti.language.actions.base.validator.WhenConditionValidator
5+
import cc.unitmesh.devti.language.ast.config.DevInsActionLocation
6+
import cc.unitmesh.devti.language.startup.DynamicShireActionService
7+
8+
class ShireContextMenuActionGroup : ActionGroup() {
9+
override fun getActionUpdateThread(): ActionUpdateThread {
10+
return ActionUpdateThread.BGT
11+
}
12+
13+
override fun update(e: AnActionEvent) {
14+
val project = e.project ?: return
15+
e.presentation.isPopupGroup = DynamicShireActionService.getInstance(project).getAllActions().size > 1
16+
}
17+
18+
override fun getChildren(e: AnActionEvent?): Array<AnAction> {
19+
val project = e?.project ?: return emptyArray()
20+
val actionService = DynamicShireActionService.getInstance(project)
21+
22+
return actionService.getActions(DevInsActionLocation.CONTEXT_MENU).mapNotNull { actionConfig ->
23+
if (actionConfig.hole == null) return@mapNotNull null
24+
if (!actionConfig.hole.enabled) return@mapNotNull null
25+
26+
actionConfig.hole.when_?.let {
27+
val psiFile = e.getData(CommonDataKeys.PSI_FILE) ?: return@mapNotNull null
28+
if (!WhenConditionValidator.isAvailable(it, psiFile)) return@mapNotNull null
29+
}
30+
31+
val menuAction = ShireContextMenuAction(actionConfig)
32+
if (actionConfig.hole.shortcut != null) {
33+
actionService.bindShortcutToAction(menuAction, actionConfig.hole.shortcut)
34+
}
35+
36+
menuAction
37+
}.distinctBy { it.templatePresentation.text }.toTypedArray()
38+
}
39+
}
40+

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/startup/ShireCopyPastePreProcessor.kt renamed to exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/actions/copyPaste/ShireCopyPastePreProcessor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cc.unitmesh.devti.language.startup
1+
package cc.unitmesh.devti.language.actions.copyPaste
22

33
import cc.unitmesh.devti.language.ast.HobbitHole
44
import cc.unitmesh.devti.language.ast.variable.ShireVariableTemplateCompiler

0 commit comments

Comments
 (0)