@NotNull
  public TIntObjectHashMap<T> preLoadCommitData(@NotNull TIntHashSet commits) throws VcsException {
    TIntObjectHashMap<T> result = new TIntObjectHashMap<>();
    final MultiMap<VirtualFile, String> rootsAndHashes = MultiMap.create();
    commits.forEach(
        commit -> {
          CommitId commitId = myHashMap.getCommitId(commit);
          if (commitId != null) {
            rootsAndHashes.putValue(commitId.getRoot(), commitId.getHash().asString());
          }
          return true;
        });

    for (Map.Entry<VirtualFile, Collection<String>> entry : rootsAndHashes.entrySet()) {
      VcsLogProvider logProvider = myLogProviders.get(entry.getKey());
      if (logProvider != null) {
        List<? extends T> details =
            readDetails(logProvider, entry.getKey(), ContainerUtil.newArrayList(entry.getValue()));
        for (T data : details) {
          int index = myHashMap.getCommitIndex(data.getId(), data.getRoot());
          result.put(index, data);
        }
        saveInCache(result);
      } else {
        LOG.error(
            "No log provider for root "
                + entry.getKey().getPath()
                + ". All known log providers "
                + myLogProviders);
      }
    }

    return result;
  }
  // made public for Upsource
  public RootIndex(@NotNull Project project, @NotNull InfoCache cache) {
    myProject = project;
    myInfoCache = cache;
    final RootInfo info = buildRootInfo(project);

    MultiMap<String, VirtualFile> rootsByPackagePrefix = MultiMap.create();
    Set<VirtualFile> allRoots = info.getAllRoots();
    for (VirtualFile root : allRoots) {
      List<VirtualFile> hierarchy = getHierarchy(root, allRoots, info);
      Pair<DirectoryInfo, String> pair =
          hierarchy != null
              ? calcDirectoryInfo(root, hierarchy, info)
              : new Pair<DirectoryInfo, String>(NonProjectDirectoryInfo.IGNORED, null);
      cacheInfos(root, root, pair.first);
      rootsByPackagePrefix.putValue(pair.second, root);
      myPackagePrefixByRoot.put(root, pair.second);
    }
    myPackageDirectoryCache =
        new PackageDirectoryCache(rootsByPackagePrefix) {
          @Override
          protected boolean isPackageDirectory(
              @NotNull VirtualFile dir, @NotNull String packageName) {
            return getInfoForFile(dir).isInProject() && packageName.equals(getPackageName(dir));
          }
        };
  }
  private void refreshLogOnVcsEvents(@NotNull Map<VirtualFile, VcsLogProvider> logProviders) {
    MultiMap<VcsLogProvider, VirtualFile> providers2roots = MultiMap.create();
    for (Map.Entry<VirtualFile, VcsLogProvider> entry : logProviders.entrySet()) {
      providers2roots.putValue(entry.getValue(), entry.getKey());
    }

    for (Map.Entry<VcsLogProvider, Collection<VirtualFile>> entry : providers2roots.entrySet()) {
      entry.getKey().subscribeToRootRefreshEvents(entry.getValue(), myLogRefresher);
    }
  }
 @NotNull
 private static MultiMap<String, GoImportSpec> collectImportsWithSameString(
     @NotNull Collection<GoImportSpec> importsWithSameName) {
   MultiMap<String, GoImportSpec> importsWithSameString = MultiMap.create();
   for (PsiElement duplicateCandidate : importsWithSameName) {
     GoImportSpec importSpec = getImportSpec(duplicateCandidate);
     if (importSpec != null) {
       importsWithSameString.putValue(importSpec.getPath(), importSpec);
     }
   }
   return importsWithSameString;
 }
Esempio n. 5
0
  @NotNull
  public static Map<PsiMethod, SiblingInfo> getSiblingInheritanceInfos(
      @NotNull final Collection<PsiMethod> methods) {
    MultiMap<PsiClass, PsiMethod> byClass = MultiMap.create();
    for (PsiMethod method : methods) {
      PsiClass containingClass = method.getContainingClass();
      if (canHaveSiblingSuper(method, containingClass)) {
        byClass.putValue(containingClass, method);
      }
    }

    Map<PsiMethod, SiblingInfo> result = new HashMap<>();
    for (PsiClass psiClass : byClass.keySet()) {
      SiblingInheritorSearcher searcher =
          new SiblingInheritorSearcher(byClass.get(psiClass), psiClass);
      ClassInheritorsSearch.search(psiClass, psiClass.getUseScope(), true, true, false)
          .forEach(searcher);
      result.putAll(searcher.getResult());
    }
    return result;
  }
  public static MultiMap<String, GoImportSpec> filterUnusedImports(
      @NotNull PsiFile file, @NotNull MultiMap<String, GoImportSpec> importMap) {
    MultiMap<String, GoImportSpec> result = MultiMap.create();
    result.putAllValues(importMap);
    result.remove("_"); // imports for side effects are always used

    Collection<GoImportSpec> implicitImports = ContainerUtil.newArrayList(result.get("."));
    for (GoImportSpec importEntry : implicitImports) {
      GoImportSpec spec = getImportSpec(importEntry);
      if (spec != null && spec.isDot() && hasImportUsers(spec)) {
        result.remove(".", importEntry);
      }
    }

    file.accept(
        new GoRecursiveVisitor() {
          @Override
          public void visitTypeReferenceExpression(@NotNull GoTypeReferenceExpression o) {
            GoTypeReferenceExpression lastQualifier = o.getQualifier();
            if (lastQualifier != null) {
              GoTypeReferenceExpression previousQualifier;
              while ((previousQualifier = lastQualifier.getQualifier()) != null) {
                lastQualifier = previousQualifier;
              }
              markAsUsed(lastQualifier.getIdentifier(), lastQualifier.getReference());
            }
          }

          @Override
          public void visitReferenceExpression(@NotNull GoReferenceExpression o) {
            GoReferenceExpression lastQualifier = o.getQualifier();
            if (lastQualifier != null) {
              GoReferenceExpression previousQualifier;
              while ((previousQualifier = lastQualifier.getQualifier()) != null) {
                lastQualifier = previousQualifier;
              }
              markAsUsed(lastQualifier.getIdentifier(), lastQualifier.getReference());
            }
          }

          private void markAsUsed(@NotNull PsiElement qualifier, @NotNull PsiReference reference) {
            String qualifierText = qualifier.getText();
            if (!result.containsKey(qualifierText)) {
              // already marked
              return;
            }
            PsiElement resolve = reference.resolve();
            if (!(resolve instanceof PsiDirectory
                || resolve instanceof GoImportSpec
                || resolve instanceof PsiDirectoryContainer)) {
              return;
            }
            Collection<String> qualifiersToDelete = ContainerUtil.newHashSet();
            for (GoImportSpec spec : result.get(qualifierText)) {
              for (Map.Entry<String, Collection<GoImportSpec>> entry : result.entrySet()) {
                for (GoImportSpec importSpec : entry.getValue()) {
                  if (importSpec == spec) {
                    qualifiersToDelete.add(entry.getKey());
                  }
                }
              }
            }
            for (String qualifierToDelete : qualifiersToDelete) {
              result.remove(qualifierToDelete);
            }
          }
        });
    return result;
  }
  public static MultiMap<String, GoImportSpec> filterUnusedImports(
      @NotNull PsiFile file, @NotNull final MultiMap<String, GoImportSpec> importMap) {
    final MultiMap<String, GoImportSpec> result = MultiMap.create();
    result.putAllValues(importMap);
    result.remove("_"); // imports for side effects are always used

    Collection<GoImportSpec> implicitImports = ContainerUtil.newArrayList(result.get("."));
    for (GoImportSpec importEntry : implicitImports) {
      GoImportSpec spec = getImportSpec(importEntry);
      if (spec != null && spec.isDot()) {
        List<? extends PsiElement> list = spec.getUserData(GoReference.IMPORT_USERS);
        if (list != null) {
          for (PsiElement e : list) {
            if (e.isValid()) {
              result.remove(".", importEntry);
            }
          }
        }
      }
    }

    file.accept(
        new GoRecursiveVisitor() {
          @Override
          public void visitTypeReferenceExpression(@NotNull GoTypeReferenceExpression o) {
            GoTypeReferenceExpression lastQualifier = o.getQualifier();
            if (lastQualifier != null) {
              GoTypeReferenceExpression previousQualifier;
              while ((previousQualifier = lastQualifier.getQualifier()) != null) {
                lastQualifier = previousQualifier;
              }
              markAsUsed(lastQualifier.getIdentifier());
            }
          }

          @Override
          public void visitReferenceExpression(@NotNull GoReferenceExpression o) {
            GoReferenceExpression lastQualifier = o.getQualifier();
            if (lastQualifier != null) {
              GoReferenceExpression previousQualifier;
              while ((previousQualifier = lastQualifier.getQualifier()) != null) {
                lastQualifier = previousQualifier;
              }
              markAsUsed(lastQualifier.getIdentifier());
            }
          }

          private void markAsUsed(@NotNull PsiElement qualifier) {
            Collection<String> qualifiersToDelete = ContainerUtil.newHashSet();
            for (GoImportSpec spec : result.get(qualifier.getText())) {
              for (Map.Entry<String, Collection<GoImportSpec>> entry : result.entrySet()) {
                for (GoImportSpec importSpec : entry.getValue()) {
                  if (importSpec == spec) {
                    qualifiersToDelete.add(entry.getKey());
                  }
                }
              }
            }
            for (String qualifierToDelete : qualifiersToDelete) {
              result.remove(qualifierToDelete);
            }
          }
        });
    return result;
  }
  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;
  }