Skip to content

Commit eafc193

Browse files
committed
feat(coroutines): refactor processIfClause to be suspend function and update related calls #379
1 parent c23d5da commit eafc193

File tree

3 files changed

+46
-34
lines changed

3 files changed

+46
-34
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ class DevInsCompiler(
205205
handleNextSiblingForChild(next) {
206206
when (it) {
207207
is DevInIfClause, is DevInElseifClause, is DevInElseClause -> {
208-
handleNextSiblingForChild(it, ::processIfClause)
208+
handleNextSiblingForChild(it) {
209+
runBlocking { processIfClause(it) }
210+
}
209211
}
210212

211213
else -> output.append(it.text)
@@ -225,7 +227,7 @@ class DevInsCompiler(
225227
}
226228
}
227229

228-
private fun processIfClause(clauseContent: PsiElement) {
230+
suspend fun processIfClause(clauseContent: PsiElement) {
229231
when (clauseContent) {
230232
is DevInExpr -> {
231233
addVariable(clauseContent)
@@ -234,7 +236,7 @@ class DevInsCompiler(
234236

235237
is DevInVelocityBlock -> {
236238
DevInFile.fromString(myProject, clauseContent.text).let { file ->
237-
val compile = runBlocking { DevInsCompiler(myProject, file).compile() }
239+
val compile = DevInsCompiler(myProject, file).compile()
238240
compile.let {
239241
output.append(it.output)
240242
variableTable.addVariable(it.variableTable)

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

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,67 @@
11
package cc.unitmesh.devti.language.run
22

3+
import cc.unitmesh.devti.language.run.flow.DevInsProcessProcessor
4+
import cc.unitmesh.devti.language.run.runner.ShireConsoleView
5+
import cc.unitmesh.devti.language.status.DevInsRunListener
6+
import cc.unitmesh.devti.util.AutoDevCoroutineScope
7+
import com.intellij.execution.ExecutionResult
38
import com.intellij.execution.configurations.RunProfile
49
import com.intellij.execution.configurations.RunProfileState
510
import com.intellij.execution.configurations.RunnerSettings
11+
import com.intellij.execution.process.ProcessEvent
612
import com.intellij.execution.runners.ExecutionEnvironment
713
import com.intellij.execution.runners.GenericProgramRunner
814
import com.intellij.execution.runners.showRunContent
915
import com.intellij.execution.ui.RunContentDescriptor
1016
import com.intellij.openapi.Disposable
1117
import com.intellij.openapi.application.ApplicationManager
1218
import com.intellij.openapi.fileEditor.FileDocumentManager
19+
import kotlinx.coroutines.launch
1320
import java.util.concurrent.atomic.AtomicReference
1421

1522
class DevInsProgramRunner : GenericProgramRunner<RunnerSettings>(), Disposable {
1623
private val RUNNER_ID: String = "DevInsProgramRunner"
1724

1825
private val connection = ApplicationManager.getApplication().messageBus.connect(this)
1926

20-
// private var isSubscribed = false
27+
private var isSubscribed = false
2128

2229
override fun getRunnerId(): String = RUNNER_ID
2330

2431
override fun canRun(executorId: String, profile: RunProfile) = profile is DevInsConfiguration
2532

2633
override fun doExecute(state: RunProfileState, environment: ExecutionEnvironment): RunContentDescriptor? {
2734
if (environment.runProfile !is DevInsConfiguration) return null
28-
val configuration = environment.runProfile as DevInsConfiguration
29-
val devInState = state as DevInsRunConfigurationProfileState
35+
val shireState = state as DevInsRunConfigurationProfileState
3036

31-
FileDocumentManager.getInstance().saveAllDocuments()
37+
var executeResult: ExecutionResult?
3238

3339
val result = AtomicReference<RunContentDescriptor>()
3440

35-
// if (!isSubscribed) {
36-
// connection.subscribe(DevInsRunListener.TOPIC, object : DevInsRunListener {
37-
// override fun runFinish(string: String, event: ProcessEvent, scriptPath: String) {
38-
// environment.project.service<DevInsProcessProcessor>().process(string, event, scriptPath)
39-
// }
40-
// })
41-
//
42-
// isSubscribed = true
43-
// }
44-
4541
ApplicationManager.getApplication().invokeAndWait {
46-
val executionResult = devInState.execute(environment.executor, this)
47-
if (configuration.showConsole) {
48-
val showRunContent = showRunContent(executionResult, environment)
49-
result.set(showRunContent)
50-
} else {
51-
result.set(null)
42+
if (!isSubscribed) {
43+
connection.subscribe(DevInsRunListener.TOPIC, object : DevInsRunListener {
44+
override fun runFinish(
45+
allOutput: String,
46+
llmOutput: String,
47+
event: ProcessEvent,
48+
scriptPath: String,
49+
consoleView: ShireConsoleView?,
50+
) {
51+
AutoDevCoroutineScope.scope(environment.project).launch {
52+
environment.project.getService(DevInsProcessProcessor::class.java)
53+
.process(allOutput, event, scriptPath, consoleView)
54+
}
55+
}
56+
})
57+
58+
isSubscribed = true
59+
}
60+
61+
executeResult = shireState.execute(environment.executor, this)
62+
63+
if (shireState.isShowRunContent) {
64+
result.set(showRunContent(executeResult, environment))
5265
}
5366
}
5467

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ open class DevInsRunConfigurationProfileState(
6464

6565
console.attachToProcess(processHandler)
6666

67-
val shireFile: DevInFile? =DevInFile.lookup(myProject, configuration.getScriptPath())
67+
val shireFile: DevInFile? = DevInFile.lookup(myProject, configuration.getScriptPath())
6868
if (shireFile == null) {
6969
console.print("File not found: ${configuration.getScriptPath()}", ConsoleViewContentType.ERROR_OUTPUT)
7070
processHandler.exitWithError()
@@ -222,11 +222,10 @@ open class DevInsRunConfigurationProfileState(
222222
if (stringFlow != null) {
223223
AutoDevCoroutineScope.scope(myProject).launch {
224224
val llmResult = StringBuilder()
225-
runBlocking {
226-
stringFlow.collect {
227-
llmResult.append(it)
228-
console.print(it, ConsoleViewContentType.NORMAL_OUTPUT)
229-
}
225+
226+
stringFlow.collect {
227+
llmResult.append(it)
228+
console.print(it, ConsoleViewContentType.NORMAL_OUTPUT)
230229
}
231230

232231
console.print("\nDone!", ConsoleViewContentType.SYSTEM_OUTPUT)
@@ -258,11 +257,9 @@ open class DevInsRunConfigurationProfileState(
258257

259258
AutoDevCoroutineScope.scope(myProject).launch {
260259
val llmResult = StringBuilder()
261-
runBlocking {
262-
llm.stream(output, "", true).collect {
263-
llmResult.append(it)
264-
console.print(it, ConsoleViewContentType.NORMAL_OUTPUT)
265-
}
260+
llm.stream(output, "", true).collect {
261+
llmResult.append(it)
262+
console.print(it, ConsoleViewContentType.NORMAL_OUTPUT)
266263
}
267264

268265
console.print("\nDone!", ConsoleViewContentType.SYSTEM_OUTPUT)

0 commit comments

Comments
 (0)