@NotNull
 public static Collection<ErlangFunction> findFunctionTestElements(@Nullable PsiElement element) {
   if (element != null) {
     PsiFile containingFile = element.getContainingFile();
     if (getFileTestElement(containingFile) == null) {
       return ContainerUtil.emptyList();
     }
   }
   return ContainerUtil.createMaybeSingletonList(getZeroArityFunction(element));
 }
 @NotNull
 @Override
 public Collection<BooleanOptionDescription> getOptions(@Nullable Project project) {
   if (project == null) return ContainerUtil.emptyList();
   List<BooleanOptionDescription> result = ContainerUtil.newArrayList();
   List<Tools> tools =
       InspectionProjectProfileManager.getInstance(project)
           .getCurrentProfile()
           .getAllEnabledInspectionTools(project);
   for (Tools tool : tools) {
     result.add(new ToolOptionDescription(tool, project));
   }
   return result;
 }
  private static List<PsiClassType> getSuperTypes(PsiClass clazz) {
    if (clazz instanceof GrTypeDefinition) {
      final GrExtendsClause elist = ((GrTypeDefinition) clazz).getExtendsClause();
      final GrImplementsClause ilist = ((GrTypeDefinition) clazz).getImplementsClause();

      if (elist == null && ilist == null) return ContainerUtil.emptyList();

      final ArrayList<PsiClassType> types = new ArrayList<PsiClassType>();
      if (elist != null) ContainerUtil.addAll(types, elist.getReferencedTypes());
      if (ilist != null) ContainerUtil.addAll(types, ilist.getReferencedTypes());
      return types;
    } else {
      final PsiReferenceList elist = clazz.getExtendsList();
      final PsiReferenceList ilist = clazz.getImplementsList();

      if (elist == null && ilist == null) return ContainerUtil.emptyList();

      final ArrayList<PsiClassType> types = new ArrayList<PsiClassType>();
      if (elist != null) ContainerUtil.addAll(types, elist.getReferencedTypes());
      if (ilist != null) ContainerUtil.addAll(types, ilist.getReferencedTypes());
      return types;
    }
  }
  public List<RecentTestsPopupEntry> getTestsToShow() {
    if (myRecords == null) return ContainerUtil.emptyList();

    RecentTestsData data = new RecentTestsData();
    for (Map.Entry<String, TestStateStorage.Record> entry : myRecords.entrySet()) {
      String url = entry.getKey();
      TestStateStorage.Record record = entry.getValue();

      if (TestLocator.canLocate(url)) {
        handleUrl(data, url, record);
      }
    }

    return data.getTestsToShow();
  }
  void submitPasses(
      @NotNull Map<FileEditor, HighlightingPass[]> passesMap,
      @NotNull DaemonProgressIndicator updateProgress) {
    if (isDisposed()) return;

    // null keys are ok
    MultiMap<Document, FileEditor> documentToEditors = MultiMap.createSet();
    MultiMap<FileEditor, TextEditorHighlightingPass> documentBoundPasses = MultiMap.createSmart();
    MultiMap<FileEditor, EditorBoundHighlightingPass> editorBoundPasses = MultiMap.createSmart();
    List<Pair<FileEditor, TextEditorHighlightingPass>> passesWithNoDocuments = new ArrayList<>();
    Set<VirtualFile> vFiles = new HashSet<>();

    for (Map.Entry<FileEditor, HighlightingPass[]> entry : passesMap.entrySet()) {
      FileEditor fileEditor = entry.getKey();
      HighlightingPass[] passes = entry.getValue();
      Document document;
      if (fileEditor instanceof TextEditor) {
        Editor editor = ((TextEditor) fileEditor).getEditor();
        LOG.assertTrue(!(editor instanceof EditorWindow));
        document = editor.getDocument();
      } else {
        VirtualFile virtualFile =
            ((FileEditorManagerEx) FileEditorManager.getInstance(myProject)).getFile(fileEditor);
        document =
            virtualFile == null ? null : FileDocumentManager.getInstance().getDocument(virtualFile);
      }
      if (document != null) {
        vFiles.add(FileDocumentManager.getInstance().getFile(document));
      }

      int prevId = 0;
      for (final HighlightingPass pass : passes) {
        if (pass instanceof EditorBoundHighlightingPass) {
          EditorBoundHighlightingPass editorPass = (EditorBoundHighlightingPass) pass;
          editorPass.setId(
              nextPassId.incrementAndGet()); // have to make ids unique for this document
          editorBoundPasses.putValue(fileEditor, editorPass);
        } else {
          TextEditorHighlightingPass textEditorHighlightingPass =
              convertToTextHighlightingPass(pass, document, nextPassId, prevId);
          document = textEditorHighlightingPass.getDocument();
          documentBoundPasses.putValue(fileEditor, textEditorHighlightingPass);
          if (document == null) {
            passesWithNoDocuments.add(Pair.create(fileEditor, textEditorHighlightingPass));
          } else {
            documentToEditors.putValue(document, fileEditor);
          }
          prevId = textEditorHighlightingPass.getId();
        }
      }
    }

    List<ScheduledPass> freePasses = new ArrayList<>(documentToEditors.size() * 5);
    List<ScheduledPass> dependentPasses = new ArrayList<>(documentToEditors.size() * 10);
    // (fileEditor, passId) -> created pass
    Map<Pair<FileEditor, Integer>, ScheduledPass> toBeSubmitted = new THashMap<>(passesMap.size());

    final AtomicInteger threadsToStartCountdown = new AtomicInteger(0);
    for (Map.Entry<Document, Collection<FileEditor>> entry : documentToEditors.entrySet()) {
      Collection<FileEditor> fileEditors = entry.getValue();
      Document document = entry.getKey();
      FileEditor preferredFileEditor = getPreferredFileEditor(document, fileEditors);
      List<TextEditorHighlightingPass> passes =
          (List<TextEditorHighlightingPass>) documentBoundPasses.get(preferredFileEditor);
      if (passes.isEmpty()) {
        continue;
      }
      sortById(passes);
      for (TextEditorHighlightingPass currentPass : passes) {
        createScheduledPass(
            preferredFileEditor,
            currentPass,
            toBeSubmitted,
            passes,
            freePasses,
            dependentPasses,
            updateProgress,
            threadsToStartCountdown);
      }
    }

    for (Map.Entry<FileEditor, Collection<EditorBoundHighlightingPass>> entry :
        editorBoundPasses.entrySet()) {
      FileEditor fileEditor = entry.getKey();
      Collection<EditorBoundHighlightingPass> createdEditorBoundPasses = entry.getValue();
      List<TextEditorHighlightingPass> createdDocumentBoundPasses =
          (List<TextEditorHighlightingPass>) documentBoundPasses.get(fileEditor);
      List<TextEditorHighlightingPass> allCreatedPasses =
          new ArrayList<>(createdDocumentBoundPasses);
      allCreatedPasses.addAll(createdEditorBoundPasses);

      for (EditorBoundHighlightingPass pass : createdEditorBoundPasses) {
        createScheduledPass(
            fileEditor,
            pass,
            toBeSubmitted,
            allCreatedPasses,
            freePasses,
            dependentPasses,
            updateProgress,
            threadsToStartCountdown);
      }
    }

    for (Pair<FileEditor, TextEditorHighlightingPass> pair : passesWithNoDocuments) {
      FileEditor fileEditor = pair.first;
      TextEditorHighlightingPass pass = pair.second;
      createScheduledPass(
          fileEditor,
          pass,
          toBeSubmitted,
          ContainerUtil.emptyList(),
          freePasses,
          dependentPasses,
          updateProgress,
          threadsToStartCountdown);
    }

    if (CHECK_CONSISTENCY && !ApplicationInfoImpl.isInPerformanceTest()) {
      assertConsistency(freePasses, toBeSubmitted, threadsToStartCountdown);
    }

    log(
        updateProgress,
        null,
        vFiles + " ----- starting " + threadsToStartCountdown.get(),
        freePasses);

    for (ScheduledPass dependentPass : dependentPasses) {
      mySubmittedPasses.put(dependentPass, Job.NULL_JOB);
    }
    for (ScheduledPass freePass : freePasses) {
      submit(freePass);
    }
  }
  @NotNull
  static List<String> getTasksToRun(Module module) {
    final List<String> result;
    final String externalProjectId = ExternalSystemApiUtil.getExternalProjectId(module);
    if (externalProjectId == null) return ContainerUtil.emptyList();
    final String projectPath = ExternalSystemApiUtil.getExternalProjectPath(module);
    if (projectPath == null) return ContainerUtil.emptyList();
    final ExternalProjectInfo externalProjectInfo =
        ExternalSystemUtil.getExternalProjectInfo(
            module.getProject(), GradleConstants.SYSTEM_ID, projectPath);
    if (externalProjectInfo == null) return ContainerUtil.emptyList();

    if (StringUtil.endsWith(externalProjectId, ":test")
        || StringUtil.endsWith(externalProjectId, ":main")) {
      result = TEST_SOURCE_SET_TASKS;
    } else {
      final DataNode<ModuleData> moduleNode =
          GradleProjectResolverUtil.findModule(
              externalProjectInfo.getExternalProjectStructure(), projectPath);
      if (moduleNode == null) return ContainerUtil.emptyList();
      final String sourceSetId =
          StringUtil.substringAfter(
              externalProjectId, moduleNode.getData().getExternalName() + ':');
      if (sourceSetId == null) return ContainerUtil.emptyList();

      final DataNode<TaskData> taskNode =
          ExternalSystemApiUtil.find(
              moduleNode,
              ProjectKeys.TASK,
              new BooleanFunction<DataNode<TaskData>>() {
                @Override
                public boolean fun(DataNode<TaskData> node) {
                  return GradleCommonClassNames.GRADLE_API_TASKS_TESTING_TEST.equals(
                          node.getData().getType())
                      && StringUtil.startsWith(sourceSetId, node.getData().getName());
                }
              });

      if (taskNode == null) return ContainerUtil.emptyList();
      final String taskName = taskNode.getData().getName();
      result = ContainerUtil.list("clean" + StringUtil.capitalize(taskName), taskName);
    }

    final String path;
    if (!externalProjectId.startsWith(":")) {
      path = ":";
    } else {
      final List<String> pathParts = StringUtil.split(externalProjectId, ":");
      if (!pathParts.isEmpty()) pathParts.remove(pathParts.size() - 1);
      final String join = StringUtil.join(pathParts, ":");
      path = ":" + join + (!join.isEmpty() ? ":" : "");
    }
    return ContainerUtil.map(
        result,
        new Function<String, String>() {
          @Override
          public String fun(String s) {
            return path + s;
          }
        });
  }
  public KeymapGroup createGroup(Condition<AnAction> condition, final Project project) {
    KeymapGroup result =
        KeymapGroupFactory.getInstance()
            .createGroup(
                ExternalSystemBundle.message("external.system.keymap.group"),
                ExternalSystemIcons.TaskGroup);
    if (project == null) return result;

    MultiMap<ProjectSystemId, String> projectToActionsMapping = MultiMap.create();
    for (ExternalSystemManager<?, ?, ?, ?, ?> manager : ExternalSystemApiUtil.getAllManagers()) {
      projectToActionsMapping.putValues(manager.getSystemId(), ContainerUtil.<String>emptyList());
    }

    ActionManager actionManager = ActionManager.getInstance();
    if (actionManager != null) {
      for (String eachId : actionManager.getActionIds(getActionPrefix(project, null))) {
        AnAction eachAction = actionManager.getAction(eachId);

        if (!(eachAction instanceof MyExternalSystemAction)) continue;
        if (condition != null && !condition.value(actionManager.getActionOrStub(eachId))) continue;

        MyExternalSystemAction taskAction = (MyExternalSystemAction) eachAction;
        projectToActionsMapping.putValue(taskAction.getSystemId(), eachId);
      }
    }

    Map<ProjectSystemId, KeymapGroup> keymapGroupMap = ContainerUtil.newHashMap();
    for (ProjectSystemId systemId : projectToActionsMapping.keySet()) {
      if (!keymapGroupMap.containsKey(systemId)) {
        final Icon projectIcon = ExternalSystemUiUtil.getUiAware(systemId).getProjectIcon();
        KeymapGroup group =
            KeymapGroupFactory.getInstance().createGroup(systemId.getReadableName(), projectIcon);
        result.addGroup(group);
        keymapGroupMap.put(systemId, group);
      }
    }

    for (Map.Entry<ProjectSystemId, Collection<String>> each : projectToActionsMapping.entrySet()) {
      Collection<String> tasks = each.getValue();
      final ProjectSystemId systemId = each.getKey();
      final KeymapGroup systemGroup = keymapGroupMap.get(systemId);
      for (String actionId : tasks) {
        systemGroup.addActionId(actionId);
      }
      if (systemGroup instanceof Group) {
        Icon icon =
            SystemInfoRt.isMac ? AllIcons.ToolbarDecorator.Mac.Add : AllIcons.ToolbarDecorator.Add;
        ((Group) systemGroup)
            .addHyperlink(
                new Hyperlink(icon, "Choose a task to assign a shortcut") {
                  @Override
                  public void onClick(MouseEvent e) {
                    SelectExternalTaskDialog dialog =
                        new SelectExternalTaskDialog(systemId, project);
                    if (dialog.showAndGet() && dialog.getResult() != null) {
                      TaskData taskData = dialog.getResult().second;
                      String ownerModuleName = dialog.getResult().first;
                      ExternalSystemTaskAction externalSystemAction =
                          (ExternalSystemTaskAction)
                              getOrRegisterAction(project, ownerModuleName, taskData);

                      ApplicationManager.getApplication()
                          .getMessageBus()
                          .syncPublisher(KeymapListener.CHANGE_TOPIC)
                          .processCurrentKeymapChanged();

                      Settings allSettings =
                          Settings.KEY.getData(
                              DataManager.getInstance().getDataContext(e.getComponent()));
                      KeymapPanel keymapPanel =
                          allSettings != null ? allSettings.find(KeymapPanel.class) : null;
                      if (keymapPanel != null) {
                        // clear actions filter
                        keymapPanel.showOption("");
                        keymapPanel.selectAction(externalSystemAction.myId);
                      }
                    }
                  }
                });
      }
    }

    for (ActionsProvider extension : ActionsProvider.EP_NAME.getExtensions()) {
      KeymapGroup group = extension.createGroup(condition, project);
      if (group != null) {
        result.addGroup(group);
      }
    }

    return result;
  }