Exemple #1
0
  @NotNull
  @Override
  public List<LightRef> getHierarchyRestrictedToLibraryScope(
      @NotNull LightRef baseRef,
      @NotNull PsiElement basePsi,
      @NotNull ByteArrayEnumerator names,
      @NotNull GlobalSearchScope libraryScope) {
    final PsiClass baseClass =
        ObjectUtils.notNull(
            basePsi instanceof PsiClass
                ? (PsiClass) basePsi
                : ReadAction.compute(() -> (PsiMember) basePsi).getContainingClass());

    final List<LightRef> overridden = new ArrayList<>();
    Processor<PsiClass> processor =
        c -> {
          if (c.hasModifierProperty(PsiModifier.PRIVATE)) return true;
          String qName = ReadAction.compute(() -> c.getQualifiedName());
          if (qName == null) return true;
          overridden.add(baseRef.override(id(qName, names)));
          return true;
        };

    ClassInheritorsSearch.search(
            baseClass,
            LibraryScopeCache.getInstance(baseClass.getProject()).getLibrariesOnlyScope(),
            true)
        .forEach(processor);
    return overridden;
  }
  @NotNull
  public static String buildStringToFindForIndicesFromRegExp(
      @NotNull String stringToFind, @NotNull Project project) {
    if (!Registry.is("idea.regexp.search.uses.indices")) return "";

    final AccessToken accessToken = ReadAction.start();
    try {
      final List<PsiElement> topLevelRegExpChars = getTopLevelRegExpChars("a", project);
      if (topLevelRegExpChars.size() != 1) return "";

      // leave only top level regExpChars
      return StringUtil.join(
          getTopLevelRegExpChars(stringToFind, project),
          new Function<PsiElement, String>() {
            final Class regExpCharPsiClass = topLevelRegExpChars.get(0).getClass();

            @Override
            public String fun(PsiElement element) {
              return regExpCharPsiClass.isInstance(element) ? element.getText() : " ";
            }
          },
          "");
    } finally {
      accessToken.finish();
    }
  }
    @Override
    public void navigate(Project project) {
      VirtualFile currentVirtualFile = null;

      AccessToken accessToken = ReadAction.start();

      try {
        if (!myVirtualFile.isValid()) return;

        PsiFile psiFile = PsiManager.getInstance(project).findFile(myVirtualFile);
        if (psiFile != null) {
          PsiElement navigationElement =
              psiFile.getNavigationElement(); // Sources may be downloaded.
          if (navigationElement instanceof PsiFile) {
            currentVirtualFile = ((PsiFile) navigationElement).getVirtualFile();
          }
        }

        if (currentVirtualFile == null) {
          currentVirtualFile = myVirtualFile;
        }
      } finally {
        accessToken.finish();
      }

      new OpenFileHyperlinkInfo(myProject, currentVirtualFile, myLineNumber - 1).navigate(project);
    }
  @Nullable
  public static PsiClass[] getAllTestClasses(final TestClassFilter filter, boolean sync) {
    final PsiClass[][] holder = new PsiClass[1][];
    final Runnable process =
        () -> {
          final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();

          final Collection<PsiClass> set = new LinkedHashSet<>();
          final PsiManager manager = PsiManager.getInstance(filter.getProject());
          final GlobalSearchScope projectScope =
              GlobalSearchScope.projectScope(manager.getProject());
          final GlobalSearchScope scope = projectScope.intersectWith(filter.getScope());
          for (final PsiClass psiClass : AllClassesSearch.search(scope, manager.getProject())) {
            if (filter.isAccepted(psiClass)) {
              if (indicator != null) {
                indicator.setText2(
                    "Found test class " + ReadAction.compute(psiClass::getQualifiedName));
              }
              set.add(psiClass);
            }
          }
          holder[0] = set.toArray(new PsiClass[set.size()]);
        };
    if (sync) {
      ProgressManager.getInstance()
          .runProcessWithProgressSynchronously(
              process, "Searching For Tests...", true, filter.getProject());
    } else {
      process.run();
    }
    return holder[0];
  }
Exemple #5
0
 @Override
 @NotNull
 public DocInfo getInfo() {
   AccessToken token = ReadAction.start();
   try {
     return generateInfo(myTargetElement, myElementAtPointer);
   } catch (IndexNotReadyException e) {
     showDumbModeNotification(myTargetElement.getProject());
     return DocInfo.EMPTY;
   } finally {
     token.finish();
   }
 }
    @Override
    public void run() {
      waitingForSmartMode = false;
      final Editor selectedTextEditor;
      final AccessToken token = ReadAction.start();
      try {
        selectedTextEditor = fileEditorManager.getSelectedTextEditor();
      } finally {
        token.finish();
      }

      if (selectedTextEditor != null && isApplicableEditor(selectedTextEditor)) {
        processFileEditorChange(selectedTextEditor);
      }
    }
  @Nullable
  @Override
  public Element getSerializedState(
      @NotNull Boolean storageData,
      Object component,
      @NotNull String componentName,
      boolean archive) {
    if (storageData) {
      return null;
    }

    Element element = new Element("component");
    ModifiableRootModel model = null;
    AccessToken token = ReadAction.start();
    try {
      model = ((ModuleRootManagerImpl) component).getModifiableModel();
      // IDEA-137969 Eclipse integration: external remove of classpathentry is not synchronized
      model.clear();
      try {
        myConverter.readClasspath(model);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
      ((RootModelImpl) model).writeExternal(element);
    } catch (WriteExternalException e) {
      LOG.error(e);
    } finally {
      try {
        token.finish();
      } finally {
        if (model != null) {
          model.dispose();
        }
      }
    }

    if (myPathMacroSubstitutor != null) {
      myPathMacroSubstitutor.expandPaths(element);
      myPathMacroSubstitutor.addUnknownMacros(
          "NewModuleRootManager", PathMacrosCollector.getMacroNames(element));
    }

    getStorageDataRef().set(true);
    return element;
  }
  @NotNull
  public List<RefElement> getSortedElements() {
    List<RefElement> answer;
    synchronized (myRefTable) {
      if (mySortedRefs != null) return mySortedRefs;

      answer = new ArrayList<>(myRefTable.values());
    }
    ReadAction.run(
        () ->
            ContainerUtil.quickSort(
                answer,
                (o1, o2) -> {
                  VirtualFile v1 = ((RefElementImpl) o1).getVirtualFile();
                  VirtualFile v2 = ((RefElementImpl) o2).getVirtualFile();
                  return (v1 != null ? v1.hashCode() : 0) - (v2 != null ? v2.hashCode() : 0);
                }));
    synchronized (myRefTable) {
      return mySortedRefs = Collections.unmodifiableList(answer);
    }
  }
  public void addModuleDependency(
      @NotNull String moduleName, @NotNull DependencyScope scope, boolean testJar) {
    Module m = findModuleByName(moduleName);

    ModuleOrderEntry e;
    if (m != null) {
      e = myRootModel.addModuleOrderEntry(m);
    } else {
      AccessToken accessToken = ReadAction.start();
      try {
        e = myRootModel.addInvalidModuleEntry(moduleName);
      } finally {
        accessToken.finish();
      }
    }

    e.setScope(scope);
    if (testJar) {
      ((ModuleOrderEntryImpl) e).setProductionOnTestDependency(true);
    }
  }
  private void configSurefirePlugin() {
    List<String> urls = new ArrayList<String>();

    AccessToken accessToken = ReadAction.start();
    try {
      MavenDomProjectModel domModel = null;

      Element config =
          myMavenProject.getPluginConfiguration(
              "org.apache.maven.plugins", "maven-surefire-plugin");
      for (String each :
          MavenJDOMUtil.findChildrenValuesByPath(
              config, "additionalClasspathElements", "additionalClasspathElement")) {
        String url = VfsUtil.pathToUrl(each);

        if (domModel == null) {
          domModel =
              MavenDomUtil.getMavenDomProjectModel(myModule.getProject(), myMavenProject.getFile());
        }

        if (domModel != null) {
          url = MavenPropertyResolver.resolve(url, domModel);
        }

        urls.add(url);
      }
    } finally {
      accessToken.finish();
    }

    LibraryTable moduleLibraryTable = myRootModelAdapter.getRootModel().getModuleLibraryTable();

    Library library = moduleLibraryTable.getLibraryByName(SUREFIRE_PLUGIN_LIBRARY_NAME);
    if (library == null) {
      if (urls.isEmpty()) {
        return;
      }

      library = moduleLibraryTable.createLibrary(SUREFIRE_PLUGIN_LIBRARY_NAME);
      LibraryOrderEntry orderEntry =
          myRootModelAdapter.getRootModel().findLibraryOrderEntry(library);
      orderEntry.setScope(DependencyScope.TEST);
    } else {
      if (urls.isEmpty()) {
        moduleLibraryTable.removeLibrary(library);
        return;
      }
    }

    String[] oldUrls = library.getUrls(OrderRootType.CLASSES);
    if (!urls.equals(Arrays.asList(oldUrls))) {
      Library.ModifiableModel modifiableModel = library.getModifiableModel();

      for (String url : oldUrls) {
        modifiableModel.removeRoot(url, OrderRootType.CLASSES);
      }

      for (String url : urls) {
        modifiableModel.addRoot(url, OrderRootType.CLASSES);
      }

      modifiableModel.commit();
    }
  }
  @Override
  public Component getListCellRendererComponent(
      final JList list, Object value, int index, boolean isSelected, boolean hasFocus) {

    boolean nonFocusedSelection =
        isSelected && myLookup.getFocusDegree() == LookupImpl.FocusDegree.SEMI_FOCUSED;
    if (!myLookup.isFocused()) {
      isSelected = false;
    }

    myIsSelected = isSelected;
    final LookupElement item = (LookupElement) value;
    final Color foreground = getForegroundColor(isSelected);
    final Color background =
        nonFocusedSelection
            ? SELECTED_NON_FOCUSED_BACKGROUND_COLOR
            : isSelected ? SELECTED_BACKGROUND_COLOR : BACKGROUND_COLOR;

    int allowedWidth = list.getWidth() - AFTER_TAIL - AFTER_TYPE - getIconIndent();

    FontMetrics normalMetrics = getRealFontMetrics(item, false);
    FontMetrics boldMetrics = getRealFontMetrics(item, true);
    final LookupElementPresentation presentation =
        new RealLookupElementPresentation(
            isSelected ? getMaxWidth() : allowedWidth, normalMetrics, boldMetrics, myLookup);
    AccessToken token = ReadAction.start();
    try {
      if (item.isValid()) {
        try {
          item.renderElement(presentation);
        } catch (Exception e) {
          LOG.error(e);
        } catch (Error e) {
          LOG.error(e);
        }
      } else {
        presentation.setItemTextForeground(JBColor.RED);
        presentation.setItemText("Invalid");
      }
    } finally {
      token.finish();
    }

    myNameComponent.clear();
    myNameComponent.setIcon(augmentIcon(presentation.getIcon(), myEmptyIcon));
    myNameComponent.setBackground(background);
    allowedWidth -=
        setItemTextLabel(
            item,
            new JBColor(
                isSelected ? SELECTED_FOREGROUND_COLOR : presentation.getItemTextForeground(),
                foreground),
            isSelected,
            presentation,
            allowedWidth);

    Font customFont = myLookup.getCustomFont(item, false);
    myTailComponent.setFont(customFont != null ? customFont : myNormalFont);
    myTypeLabel.setFont(customFont != null ? customFont : myNormalFont);

    myTypeLabel.clear();
    if (allowedWidth > 0) {
      allowedWidth -=
          setTypeTextLabel(
              item,
              background,
              foreground,
              presentation,
              isSelected ? getMaxWidth() : allowedWidth,
              isSelected,
              nonFocusedSelection,
              normalMetrics);
    }

    myTailComponent.clear();
    myTailComponent.setBackground(background);
    if (isSelected || allowedWidth >= 0) {
      setTailTextLabel(
          isSelected,
          presentation,
          foreground,
          isSelected ? getMaxWidth() : allowedWidth,
          nonFocusedSelection,
          normalMetrics);
    }

    if (mySelected.containsKey(index)) {
      if (!isSelected && mySelected.get(index)) {
        myPanel.setUpdateExtender(true);
      }
    }
    mySelected.put(index, isSelected);

    final double w =
        myNameComponent.getPreferredSize().getWidth()
            + myTailComponent.getPreferredSize().getWidth()
            + myTypeLabel.getPreferredSize().getWidth();

    boolean useBoxLayout =
        isSelected
            && w > list.getWidth()
            && ((JBList) list).getExpandableItemsHandler().isEnabled();
    if (useBoxLayout != myPanel.getLayout() instanceof BoxLayout) {
      myPanel.removeAll();
      if (useBoxLayout) {
        myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.X_AXIS));
        myPanel.add(myNameComponent);
        myPanel.add(myTailComponent);
        myPanel.add(myTypeLabel);
      } else {
        myPanel.setLayout(new BorderLayout());
        myPanel.add(myNameComponent, BorderLayout.WEST);
        myPanel.add(myTailComponent, BorderLayout.CENTER);
        myPanel.add(myTypeLabel, BorderLayout.EAST);
      }
    }

    return myPanel;
  }