|
| 1 | +package com.intellij.plugin.powershell.ide.run |
| 2 | + |
| 3 | +import com.intellij.execution.ExecutionException |
| 4 | +import com.intellij.execution.ExecutionResult |
| 5 | +import com.intellij.execution.configurations.RunProfile |
| 6 | +import com.intellij.execution.configurations.RunProfileState |
| 7 | +import com.intellij.execution.configurations.RunnerSettings |
| 8 | +import com.intellij.execution.executors.DefaultDebugExecutor |
| 9 | +import com.intellij.execution.runners.AsyncProgramRunner |
| 10 | +import com.intellij.execution.runners.ExecutionEnvironment |
| 11 | +import com.intellij.execution.ui.RunContentDescriptor |
| 12 | +import com.intellij.openapi.fileEditor.FileDocumentManager |
| 13 | +import com.intellij.openapi.rd.util.toPromise |
| 14 | +import com.intellij.openapi.rd.util.withUiContext |
| 15 | +import com.intellij.plugin.powershell.ide.PluginProjectRoot |
| 16 | +import com.intellij.plugin.powershell.ide.debugger.PowerShellDebugProcess |
| 17 | +import com.intellij.xdebugger.XDebugProcess |
| 18 | +import com.intellij.xdebugger.XDebugProcessStarter |
| 19 | +import com.intellij.xdebugger.XDebugSession |
| 20 | +import com.intellij.xdebugger.XDebuggerManager |
| 21 | +import kotlinx.coroutines.Dispatchers |
| 22 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 23 | +import kotlinx.coroutines.async |
| 24 | +import org.jetbrains.concurrency.Promise |
| 25 | +import java.lang.Boolean |
| 26 | +import kotlin.OptIn |
| 27 | +import kotlin.String |
| 28 | +import kotlin.Throws |
| 29 | + |
| 30 | +/** |
| 31 | + * The main purpose of this runner is to call [RunProfileState.execute] or a background thread instead of a foreground |
| 32 | + * one, as our [RunProfileState] implementation requires FS access that's only possible from the background. |
| 33 | + */ |
| 34 | +class PowerShellProgramDebugRunner : AsyncProgramRunner<RunnerSettings>() { |
| 35 | + |
| 36 | + override fun getRunnerId() = "com.intellij.plugin.powershell.ide.run.PowerShellProgramDebugRunner" |
| 37 | + |
| 38 | + override fun canRun(executorId: String, profile: RunProfile) = |
| 39 | + executorId == DefaultDebugExecutor.EXECUTOR_ID && profile is PowerShellRunConfiguration |
| 40 | + |
| 41 | + @OptIn(ExperimentalCoroutinesApi::class) |
| 42 | + override fun execute(environment: ExecutionEnvironment, state: RunProfileState): Promise<RunContentDescriptor?> = |
| 43 | + PluginProjectRoot.getInstance(environment.project).coroutineScope.async(Dispatchers.Default) { |
| 44 | + state as PowerShellScriptCommandLineState |
| 45 | + withUiContext { |
| 46 | + FileDocumentManager.getInstance().saveAllDocuments() |
| 47 | + } |
| 48 | + state.prepareExecution() |
| 49 | + val executionResult = state.execute(environment.executor, this@PowerShellProgramDebugRunner) |
| 50 | + val descriptor = withUiContext { |
| 51 | + XDebuggerManager.getInstance(environment.project).startSession(environment, object : XDebugProcessStarter() { |
| 52 | + @Throws(ExecutionException::class) |
| 53 | + override fun start(session: XDebugSession): XDebugProcess { |
| 54 | + return PowerShellDebugProcess(session, executionResult) |
| 55 | + } |
| 56 | + }).runContentDescriptor |
| 57 | + } |
| 58 | + descriptor |
| 59 | + }.toPromise() |
| 60 | +} |
0 commit comments