Skip to content

Commit eeb1b36

Browse files
authored
Merge pull request #150 from ymli0215/master
add support for flow control in DevInsProcessProcessor
2 parents 110fab2 + 7c38f33 commit eeb1b36

File tree

1 file changed

+60
-5
lines changed

1 file changed

+60
-5
lines changed

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/run/flow/DevInsProcessProcessor.kt

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cc.unitmesh.devti.language.run.flow
22

3+
import cc.unitmesh.devti.AutoDevNotifications
34
import cc.unitmesh.devti.gui.chat.ChatActionType
45
import cc.unitmesh.devti.gui.sendToChatWindow
56
import cc.unitmesh.devti.language.DevInLanguage
@@ -17,6 +18,10 @@ import com.intellij.openapi.project.Project
1718
import com.intellij.psi.PsiComment
1819
import com.intellij.psi.PsiElement
1920
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+
2025

2126
@Service(Service.Level.PROJECT)
2227
class DevInsProcessProcessor(val project: Project) {
@@ -88,17 +93,67 @@ class DevInsProcessProcessor(val project: Project) {
8893
* @param newScript The new script to be run.
8994
*/
9095
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+
}
93101

94-
if (compiledResult.hasError) {
102+
if (result.hasError) {
95103
sendToChatWindow(project, ChatActionType.CHAT) { panel, service ->
96104
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
99107
}, null, true)
100108
}
101109
}
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
102157
}
103158

104159
/**

0 commit comments

Comments
 (0)