fun splitAndExecute()

in src/org/jetbrains/r/console/RConsoleExecuteActionHandler.kt [389:428]


  fun splitAndExecute(
    code: String,
    isDebug: Boolean = false,
    sourceFile: VirtualFile? = null,
    sourceStartOffset: Int? = null,
    firstDebugCommand: ExecuteCodeRequest.DebugCommand = ExecuteCodeRequest.DebugCommand.CONTINUE,
  ): Promise<Unit> = runReadAction {
    splitCodeForExecution(consoleView.project, code)
      .mapIndexed { index, (text, range) ->
        val doExecute = if (sourceFile == null || sourceStartOffset == null) {
          ({ consoleView.rInterop.replExecute(text, setLastValue = true, debug = isDebug) })
        }
        else {
          val newRange = TextRange(range.startOffset + sourceStartOffset, range.endOffset + sourceStartOffset)
          val debugCommand = if (index == 0) firstDebugCommand else ExecuteCodeRequest.DebugCommand.KEEP_PREVIOUS
          val request = consoleView.rInterop.prepareReplSourceFileRequest(sourceFile, newRange, isDebug, debugCommand)
          ({ consoleView.rInterop.replSourceFile(request) })
        }
        // block is lambda!
        {
          executeLater {
            if (isDebug && state == State.DEBUG_PROMPT) {
              return@executeLater resolvedPromise(false)
            }
            fireBeforeExecution()
            consoleView.appendCommandText(text.trim { it <= ' ' })
            fireBusy()
            val prepare = if (index == 0) consoleView.interpreter.prepareForExecutionAsync() else resolvedPromise()
            prepare.thenAsync {
              doExecute().then { it.exception == null }
            }
          }.thenAsync { it }
        }
      }.let {
        PromiseUtil.runChain(it).then {
          refreshLocalFileSystem()
          Unit
        }
      }
  }