summaryrefslogtreecommitdiff
path: root/platform/lang-impl/src/com/intellij/execution/runners/RerunTestsAction.java
blob: 8d3bf86635986b16816fdd774540816697541eac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.intellij.execution.runners;

import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.ui.RunContentDescriptor;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.util.Disposer;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Reruns all registered execution sessions.<p>
 * The difference between this action and {@code Rerun} action (Ctrl+F5) is that this action reruns
 * only explicitly registered execution sessions. For example, their tabs can be hidden by other tabs, it doesn't matter.
 * <p>
 * Thus it can be convenient for rerunning tests, because running non-test execution session after
 * running test execution session won't hide the latter.
 *
 * @author Sergey Simonchik
 */
public class RerunTestsAction extends DumbAwareAction implements AnAction.TransparentUpdate {
  public static final String ID = "RerunTests";
  private static final List<ExecutionEnvironment> REGISTRY = ContainerUtil.createLockFreeCopyOnWriteList();

  public static void register(@NotNull final ExecutionEnvironment environment) {
    REGISTRY.add(environment);
    Disposer.register(environment, new Disposable() {
      @Override
      public void dispose() {
        REGISTRY.remove(environment);
      }
    });
  }

  @Override
  public void actionPerformed(AnActionEvent e) {
    for (ExecutionEnvironment environment : REGISTRY) {
      if (!Disposer.isDisposed(environment)) {
        RunContentDescriptor descriptor = environment.getContentToReuse();
        ProcessHandler processHandler = descriptor == null ? null : descriptor.getProcessHandler();
        if (processHandler != null && processHandler.isProcessTerminated()) {
          ExecutionUtil.restart(environment);
        }
      }
    }
  }

  @Override
  public void update(AnActionEvent e) {
    e.getPresentation().setEnabled(true);
  }
}