Skip to content

Commit 4944cbf

Browse files
committedNov 29, 2024
refactor(core): asyncify run operation in FileRunService #146
The commit updates the `FileRunService` by introducing asynchronous execution using `CompletableFuture`. It removes the temporary flag and directly sets the configuration as temporary. Additionally, the `RunServiceTask` now accepts a `CompletableFuture` and completes it with the result of the run operation. This change enhances the run operation by enabling asynchronous handling of the run task.
1 parent 3c155c2 commit 4944cbf

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed
 

‎core/src/main/kotlin/com/phodal/shirecore/provider/shire/FileRunService.kt

+6-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.intellij.psi.PsiElement
1818
import com.intellij.psi.PsiFile
1919
import com.intellij.psi.PsiManager
2020
import com.phodal.shirecore.runner.RunServiceTask
21+
import java.util.concurrent.CompletableFuture
2122

2223
interface FileRunService {
2324
private val logger: Logger get() = logger<FileRunService>()
@@ -72,11 +73,7 @@ interface FileRunService {
7273
it.name == virtualFile.nameWithoutExtension && (it.javaClass == runConfigureClass)
7374
}
7475

75-
var isTemporary = false
76-
77-
// try to create config if not founds
7876
if (testConfig == null) {
79-
isTemporary = true
8077
testConfig = createConfiguration(project, virtualFile)
8178
}
8279

@@ -91,10 +88,7 @@ interface FileRunService {
9188
return null
9289
}
9390

94-
if (isTemporary) {
95-
settings.isTemporary = true
96-
}
97-
91+
settings.isTemporary = true
9892
runManager.selectedConfiguration = settings
9993

10094
return settings
@@ -118,15 +112,17 @@ interface FileRunService {
118112
* @return The result of the run operation, or `null` if an error occurred.
119113
*/
120114
fun runFile(project: Project, virtualFile: VirtualFile, psiElement: PsiElement?): String? {
115+
val future: CompletableFuture<String> = CompletableFuture<String>()
116+
121117
try {
122-
val runTask = RunServiceTask(project, virtualFile, psiElement, this)
118+
val runTask = RunServiceTask(project, virtualFile, psiElement, this, future = future)
123119
ProgressManager.getInstance().run(runTask)
124120
} catch (e: Exception) {
125121
logger.error("Failed to run file: ${virtualFile.name}", e)
126122
return e.message
127123
}
128124

129-
return null
125+
return future.get()
130126
}
131127

132128
companion object {

‎core/src/main/kotlin/com/phodal/shirecore/runner/RunServiceTask.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@ import com.intellij.util.text.nullize
2020
import com.phodal.shirecore.ShireCoreBundle
2121
import com.phodal.shirecore.ShirelangNotifications
2222
import com.phodal.shirecore.provider.shire.FileRunService
23+
import java.util.concurrent.CompletableFuture
2324

2425
open class RunServiceTask(
2526
private val project: Project,
2627
private val virtualFile: VirtualFile,
2728
private val testElement: PsiElement?,
2829
private val fileRunService: FileRunService,
2930
private val runner: ProgramRunner<*>? = null,
31+
private val future: CompletableFuture<String>? = null,
3032
) : ConfigurationRunner, com.intellij.openapi.progress.Task.Backgroundable(
3133
project, ShireCoreBundle.message("progress.run.task"), true
3234
) {
3335
override fun runnerId() = runner?.runnerId ?: DefaultRunExecutor.EXECUTOR_ID
3436

3537
override fun run(indicator: ProgressIndicator) {
36-
runAndCollectTestResults(indicator)
38+
val runAndCollectTestResults = runAndCollectTestResults(indicator)
39+
future?.complete(runAndCollectTestResults?.status?.name ?: "Failed")
3740
}
3841

3942
/**

0 commit comments

Comments
 (0)
Please sign in to comment.