@@ -0,0 +1,3396 @@
+package com.jetbrains.ther.run;%0A%0Aimport com.intellij.execution.ExecutionException;%0Aimport com.intellij.execution.configurations.CommandLineState;%0Aimport com.intellij.execution.configurations.GeneralCommandLine;%0Aimport com.intellij.execution.configurations.GeneralCommandLine.ParentEnvironmentType;%0Aimport com.intellij.execution.process.ProcessHandler;%0Aimport com.intellij.execution.process.ProcessTerminatedListener;%0Aimport com.intellij.execution.runners.ExecutionEnvironment;%0Aimport com.intellij.openapi.util.io.FileUtil;%0Aimport com.intellij.openapi.util.text.StringUtil;%0Aimport com.intellij.util.execution.ParametersListUtil;%0Aimport com.jetbrains.ther.debugger.executor.TheRProcessUtils;%0Aimport com.jetbrains.ther.interpreter.TheRInterpreterService;%0Aimport com.jetbrains.ther.run.configuration.TheRRunConfiguration;%0Aimport org.jetbrains.annotations.NotNull;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A// TODO %5Brun%5D%5Btest%5D%0Apublic abstract class TheRCommandLineState extends CommandLineState %7B%0A%0A  @NotNull%0A  private final String myInterpreterPath;%0A%0A  @NotNull%0A  private final TheRRunConfiguration myRunConfiguration;%0A%0A  public TheRCommandLineState(@NotNull final ExecutionEnvironment environment, @NotNull final TheRRunConfiguration runConfiguration) %7B%0A    super(environment);%0A%0A    myInterpreterPath = TheRInterpreterService.getInstance().getInterpreterPath();%0A    myRunConfiguration = runConfiguration;%0A  %7D%0A%0A  @NotNull%0A  @Override%0A  protected ProcessHandler startProcess() throws ExecutionException %7B%0A    checkRunConfiguration();%0A%0A    final ProcessHandler processHandler = startProcess(myRunConfiguration, calculateCommandLine());%0A%0A    ProcessTerminatedListener.attach(processHandler, myRunConfiguration.getProject());%0A%0A    return processHandler;%0A  %7D%0A%0A  @NotNull%0A  protected abstract ProcessHandler startProcess(@NotNull final TheRRunConfiguration runConfiguration,%0A                                                 @NotNull final GeneralCommandLine generalCommandLine) throws ExecutionException;%0A%0A  private void checkRunConfiguration() throws ExecutionException %7B%0A    if (StringUtil.isEmptyOrSpaces(myInterpreterPath)) %7B%0A      throw new ExecutionException(%22The R interpreter is not specified%22);%0A    %7D%0A%0A    if (StringUtil.isEmptyOrSpaces(myRunConfiguration.getScriptPath())) %7B%0A      throw new ExecutionException(%22The R script is not specified%22);%0A    %7D%0A%0A    if (StringUtil.isEmptyOrSpaces(myRunConfiguration.getWorkingDirectoryPath())) %7B%0A      throw new ExecutionException(%22The working directory is not specified%22);%0A    %7D%0A  %7D%0A%0A  @NotNull%0A  private GeneralCommandLine calculateCommandLine() %7B%0A    return new GeneralCommandLine(calculateCommand())%0A      .withWorkDirectory(myRunConfiguration.getWorkingDirectoryPath())%0A      .withEnvironment(myRunConfiguration.getEnvs())%0A      .withParentEnvironmentType(myRunConfiguration.isPassParentEnvs() ? ParentEnvironmentType.CONSOLE : ParentEnvironmentType.NONE);%0A  %7D%0A%0A  @NotNull%0A  private List%3CString%3E calculateCommand() %7B%0A    final List%3CString%3E command = new ArrayList%3CString%3E();%0A%0A    command.add(FileUtil.toSystemDependentName(myInterpreterPath));%0A    command.addAll(TheRProcessUtils.getStartOptions());%0A%0A    final String scriptArgs = myRunConfiguration.getScriptArgs();%0A%0A    if (!StringUtil.isEmptyOrSpaces(scriptArgs)) %7B%0A      command.add(%22--args%22);%0A      command.addAll(ParametersListUtil.parse(scriptArgs));%0A    %7D%0A%0A    return command;%0A  %7D%0A%7D%0A

introduce new abstract command line state. It checks run config, generates command line, start process and attaches process terminated listener