1
1
package cc.unitmesh.devti.language.run.flow
2
2
3
+ import cc.unitmesh.devti.AutoDevNotifications
3
4
import cc.unitmesh.devti.gui.chat.ChatActionType
4
5
import cc.unitmesh.devti.gui.sendToChatWindow
5
6
import cc.unitmesh.devti.language.DevInLanguage
@@ -17,6 +18,10 @@ import com.intellij.openapi.project.Project
17
18
import com.intellij.psi.PsiComment
18
19
import com.intellij.psi.PsiElement
19
20
import org.jetbrains.kotlin.psi.psiUtil.startOffset
21
+ import com.intellij.psi.PsiWhiteSpace
22
+ import com.intellij.psi.util.PsiUtilBase
23
+ import com.intellij.openapi.fileEditor.FileEditorManager
24
+
20
25
21
26
@Service(Service .Level .PROJECT )
22
27
class DevInsProcessProcessor (val project : Project ) {
@@ -88,17 +93,67 @@ class DevInsProcessProcessor(val project: Project) {
88
93
* @param newScript The new script to be run.
89
94
*/
90
95
fun executeTask (newScript : DevInFile ) {
91
- val compiledResult = DevInsCompiler (project, newScript).compile()
92
- val prompt = compiledResult.output
96
+ val devInsCompiler = createCompiler(project, newScript)
97
+ val result = devInsCompiler.compile()
98
+ if (result.output != " " ) {
99
+ AutoDevNotifications .notify(project, result.output)
100
+ }
93
101
94
- if (compiledResult .hasError) {
102
+ if (result .hasError) {
95
103
sendToChatWindow(project, ChatActionType .CHAT ) { panel, service ->
96
104
service.handlePromptAndResponse(panel, object : ContextPrompter () {
97
- override fun displayPrompt (): String = prompt
98
- override fun requestPrompt (): String = prompt
105
+ override fun displayPrompt (): String = result.output
106
+ override fun requestPrompt (): String = result.output
99
107
}, null , true )
100
108
}
101
109
}
110
+ else {
111
+ if (result.nextJob != null ) {
112
+ val nextJob = result.nextJob!!
113
+ val nextResult = createCompiler(project, nextJob).compile()
114
+ if (nextResult.output != " " ) {
115
+ AutoDevNotifications .notify(project, nextResult.output)
116
+ }
117
+ }
118
+ }
119
+ }
120
+
121
+ /* *
122
+ * Creates a new instance of `DevInsCompiler`.
123
+ *
124
+ * @param project The current project.
125
+ * @param text The source code text.
126
+ * @return A new instance of `DevInsCompiler`.
127
+ */
128
+ private fun createCompiler (
129
+ project : Project ,
130
+ text : String
131
+ ): DevInsCompiler {
132
+ val devInFile = DevInFile .fromString(project, text)
133
+ return createCompiler(project, devInFile)
134
+ }
135
+
136
+ private fun createCompiler (
137
+ project : Project ,
138
+ devInFile : DevInFile
139
+ ): DevInsCompiler {
140
+ val editor = FileEditorManager .getInstance(project).selectedTextEditor
141
+ val element: PsiElement ? = editor?.caretModel?.currentCaret?.offset?.let {
142
+ val psiFile = PsiUtilBase .getPsiFileInEditor(editor, project) ? : return @let null
143
+ getElementAtOffset(psiFile, it)
144
+ }
145
+
146
+ return DevInsCompiler (project, devInFile, editor, element)
147
+ }
148
+
149
+ private fun getElementAtOffset (psiFile : PsiElement , offset : Int ): PsiElement ? {
150
+ var element = psiFile.findElementAt(offset) ? : return null
151
+
152
+ if (element is PsiWhiteSpace ) {
153
+ element = element.getParent()
154
+ }
155
+
156
+ return element
102
157
}
103
158
104
159
/* *
0 commit comments