Skip to content

Commit 3af06a2

Browse files
committed
refactor(runner): refactor RunServiceTask to use new completion action
This commit updates the `RunServiceTask` by changing its main run method to use a new function, `runAndCollectTestResults`, which replaces the `doRun` method. It also introduces a dedicated completion action, `processRunCompletionAction`, which encapsulates the logic for handling the aftermath of a process run. This change improves the structure and readability of the code, providing a clearer separation of concerns and a more maintainable implementation.
1 parent 6a751c2 commit 3af06a2

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/main/kotlin/cc/unitmesh/devti/runner/RunServiceTask.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import com.intellij.util.text.nullize
2828
import java.util.concurrent.CountDownLatch
2929
import java.util.concurrent.TimeUnit
3030

31-
class RunServiceTask(
31+
open class RunServiceTask(
3232
private val project: Project,
3333
private val virtualFile: VirtualFile,
3434
private val testElement: PsiElement?,
@@ -42,7 +42,7 @@ class RunServiceTask(
4242
private fun runnerId() = runner?.runnerId ?: DefaultRunExecutor.EXECUTOR_ID
4343

4444
override fun run(indicator: ProgressIndicator) {
45-
doRun(indicator)
45+
runAndCollectTestResults(indicator)
4646
}
4747

4848
/**
@@ -52,7 +52,7 @@ class RunServiceTask(
5252
* @param indicator A progress indicator that is used to track the progress of the execution.
5353
* @return The check result of the executed run configuration, or `null` if no run configuration could be created.
5454
*/
55-
fun doRun(indicator: ProgressIndicator?): RunnerResult? {
55+
private fun runAndCollectTestResults(indicator: ProgressIndicator?): RunnerResult? {
5656
val settings: RunnerAndConfigurationSettings? = runService.createRunSettings(project, virtualFile, testElement)
5757
if (settings == null) {
5858
logger<RunServiceTask>().warn("No run configuration found for file: ${virtualFile.path}")
@@ -198,7 +198,7 @@ class RunServiceTask(
198198
val env =
199199
ExecutionEnvironmentBuilder.create(DefaultRunExecutor.getRunExecutorInstance(), this)
200200
.activeTarget()
201-
.build(callback(runContext))
201+
.build(processRunCompletionAction(runContext))
202202

203203
if (runner == null || env.state == null) {
204204
runContext.latch.countDown()
@@ -210,7 +210,17 @@ class RunServiceTask(
210210
return true
211211
}
212212

213-
fun callback(runContext: RunContext) = ProgramRunner.Callback { descriptor ->
213+
/**
214+
* This function defines a process run completion action to be executed once a process run by the program runner completes.
215+
* It is designed to handle the aftermath of a process execution, including stopping the process and notifying the run context.
216+
*
217+
* @param runContext The context in which the run operation is being executed. It provides the necessary information
218+
* and handles to manage the run process, including a latch to synchronize the completion of the run.
219+
* The run context is also responsible for disposing of resources once the run completes.
220+
*
221+
* Note: This function uses the 'return@Callback' syntax to exit the lambda expression early in case of a null descriptor.
222+
*/
223+
fun processRunCompletionAction(runContext: RunContext) = ProgramRunner.Callback { descriptor ->
214224
// Descriptor can be null in some cases.
215225
// For example, IntelliJ Rust's test runner provides null here if compilation fails
216226
if (descriptor == null) {

0 commit comments

Comments
 (0)