Search in sources :

Example 26 with Processor

use of com.intellij.util.Processor in project intellij-community by JetBrains.

the class LibraryUsageCollector method getProjectUsages.

@NotNull
@Override
public Set<UsageDescriptor> getProjectUsages(@NotNull Project project) {
    final Set<LibraryKind> usedKinds = new HashSet<>();
    final Processor<Library> processor = library -> {
        usedKinds.addAll(LibraryPresentationManagerImpl.getLibraryKinds(library, null));
        return true;
    };
    for (Module module : ModuleManager.getInstance(project).getModules()) {
        ModuleRootManager.getInstance(module).orderEntries().librariesOnly().forEachLibrary(processor);
    }
    final HashSet<UsageDescriptor> usageDescriptors = new HashSet<>();
    for (LibraryKind kind : usedKinds) {
        usageDescriptors.add(new UsageDescriptor(kind.getKindId(), 1));
    }
    return usageDescriptors;
}
Also used : GroupDescriptor(com.intellij.internal.statistic.beans.GroupDescriptor) ModuleManager(com.intellij.openapi.module.ModuleManager) UsageDescriptor(com.intellij.internal.statistic.beans.UsageDescriptor) AbstractApplicationUsagesCollector(com.intellij.internal.statistic.AbstractApplicationUsagesCollector) NonNls(org.jetbrains.annotations.NonNls) Set(java.util.Set) LibraryKind(com.intellij.openapi.roots.libraries.LibraryKind) HashSet(java.util.HashSet) Library(com.intellij.openapi.roots.libraries.Library) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) Processor(com.intellij.util.Processor) Project(com.intellij.openapi.project.Project) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull) LibraryKind(com.intellij.openapi.roots.libraries.LibraryKind) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module) UsageDescriptor(com.intellij.internal.statistic.beans.UsageDescriptor) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with Processor

use of com.intellij.util.Processor in project intellij-community by JetBrains.

the class FindUsagesTest method testNonCodeClassUsages.

public void testNonCodeClassUsages() throws Exception {
    final TempDirTestFixture tdf = IdeaTestFixtureFactory.getFixtureFactory().createTempDirTestFixture();
    tdf.setUp();
    try {
        new WriteCommandAction(getProject()) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                final ModifiableModuleModel moduleModel = ModuleManager.getInstance(getProject()).getModifiableModel();
                moduleModel.newModule("independent/independent.iml", StdModuleTypes.JAVA.getId());
                moduleModel.commit();
                tdf.createFile("plugin.xml", "<document>\n" + "  <action class=\"com.Foo\" />\n" + "  <action class=\"com.Foo.Bar\" />\n" + "  <action class=\"com.Foo$Bar\" />\n" + "</document>");
                PsiTestUtil.addContentRoot(ModuleManager.getInstance(getProject()).findModuleByName("independent"), tdf.getFile(""));
            }
        }.execute();
        GlobalSearchScope scope = GlobalSearchScope.allScope(getProject());
        PsiClass foo = myJavaFacade.findClass("com.Foo", scope);
        PsiClass bar = myJavaFacade.findClass("com.Foo.Bar", scope);
        final int[] count = { 0 };
        Processor<UsageInfo> processor = usageInfo -> {
            int navigationOffset = usageInfo.getNavigationOffset();
            assertTrue(navigationOffset > 0);
            String textAfter = usageInfo.getFile().getText().substring(navigationOffset);
            assertTrue(textAfter, textAfter.startsWith("Foo") || textAfter.startsWith("Bar") || textAfter.startsWith("com.Foo.Bar"));
            count[0]++;
            return true;
        };
        JavaFindUsagesHandler handler = new JavaFindUsagesHandler(bar, JavaFindUsagesHandlerFactory.getInstance(getProject()));
        count[0] = 0;
        handler.processUsagesInText(foo, processor, scope);
        assertEquals(3, count[0]);
        count[0] = 0;
        handler.processUsagesInText(bar, processor, scope);
        assertEquals(2, count[0]);
    } finally {
        tdf.tearDown();
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) TempDirTestFixture(com.intellij.testFramework.fixtures.TempDirTestFixture) ModuleManager(com.intellij.openapi.module.ModuleManager) JavaFindUsagesHandlerFactory(com.intellij.find.findUsages.JavaFindUsagesHandlerFactory) UsageInfo(com.intellij.usageView.UsageInfo) IntArrayList(com.intellij.util.containers.IntArrayList) ArrayList(java.util.ArrayList) MethodReferencesSearch(com.intellij.psi.search.searches.MethodReferencesSearch) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) IdeaTestUtil(com.intellij.testFramework.IdeaTestUtil) PsiTestCase(com.intellij.testFramework.PsiTestCase) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) StdModuleTypes(com.intellij.openapi.module.StdModuleTypes) StdFileTypes(com.intellij.openapi.fileTypes.StdFileTypes) PsiTestUtil(com.intellij.testFramework.PsiTestUtil) JavaTestUtil(com.intellij.JavaTestUtil) OverridingMethodsSearch(com.intellij.psi.search.searches.OverridingMethodsSearch) Collection(java.util.Collection) TextRange(com.intellij.openapi.util.TextRange) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) JavaFindUsagesHandler(com.intellij.find.findUsages.JavaFindUsagesHandler) List(java.util.List) Result(com.intellij.openapi.application.Result) Processor(com.intellij.util.Processor) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) IdeaTestFixtureFactory(com.intellij.testFramework.fixtures.IdeaTestFixtureFactory) Collections(java.util.Collections) TempDirTestFixture(com.intellij.testFramework.fixtures.TempDirTestFixture) JavaFindUsagesHandler(com.intellij.find.findUsages.JavaFindUsagesHandler) Result(com.intellij.openapi.application.Result) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) UsageInfo(com.intellij.usageView.UsageInfo)

Example 28 with Processor

use of com.intellij.util.Processor in project intellij-community by JetBrains.

the class UpdateHighlightersUtil method setHighlightersOutsideRange.

// set highlights inside startOffset,endOffset but outside priorityRange
static void setHighlightersOutsideRange(@NotNull final Project project, @NotNull final Document document, @NotNull final PsiFile psiFile, @NotNull final List<HighlightInfo> infos, @Nullable final EditorColorsScheme colorsScheme, // if null global scheme will be used
final int startOffset, final int endOffset, @NotNull final ProperTextRange priorityRange, final int group) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    final DaemonCodeAnalyzerEx codeAnalyzer = DaemonCodeAnalyzerEx.getInstanceEx(project);
    if (startOffset == 0 && endOffset == document.getTextLength()) {
        codeAnalyzer.cleanFileLevelHighlights(project, group, psiFile);
    }
    final MarkupModel markup = DocumentMarkupModel.forDocument(document, project, true);
    assertMarkupConsistent(markup, project);
    final SeverityRegistrar severityRegistrar = SeverityRegistrar.getSeverityRegistrar(project);
    final HighlightersRecycler infosToRemove = new HighlightersRecycler();
    ContainerUtil.quickSort(infos, BY_START_OFFSET_NODUPS);
    Set<HighlightInfo> infoSet = new THashSet<>(infos);
    Processor<HighlightInfo> processor = info -> {
        if (info.getGroup() == group) {
            RangeHighlighter highlighter = info.getHighlighter();
            int hiStart = highlighter.getStartOffset();
            int hiEnd = highlighter.getEndOffset();
            if (!info.isFromInjection() && hiEnd < document.getTextLength() && (hiEnd <= startOffset || hiStart >= endOffset)) {
                return true;
            }
            boolean toRemove = infoSet.contains(info) || !priorityRange.containsRange(hiStart, hiEnd) && (hiEnd != document.getTextLength() || priorityRange.getEndOffset() != document.getTextLength());
            if (toRemove) {
                infosToRemove.recycleHighlighter(highlighter);
                info.setHighlighter(null);
            }
        }
        return true;
    };
    DaemonCodeAnalyzerEx.processHighlightsOverlappingOutside(document, project, null, priorityRange.getStartOffset(), priorityRange.getEndOffset(), processor);
    final Map<TextRange, RangeMarker> ranges2markersCache = new THashMap<>(10);
    final boolean[] changed = { false };
    RangeMarkerTree.sweep((RangeMarkerTree.Generator<HighlightInfo>) processor1 -> ContainerUtil.process(infos, processor1), (offset, info, atStart, overlappingIntervals) -> {
        if (!atStart)
            return true;
        // injections are oblivious to restricting range
        if (!info.isFromInjection() && info.getEndOffset() < document.getTextLength() && (info.getEndOffset() <= startOffset || info.getStartOffset() >= endOffset))
            return true;
        if (info.isFileLevelAnnotation()) {
            codeAnalyzer.addFileLevelHighlight(project, group, info, psiFile);
            changed[0] = true;
            return true;
        }
        if (isWarningCoveredByError(info, overlappingIntervals, severityRegistrar)) {
            return true;
        }
        if (info.getStartOffset() < priorityRange.getStartOffset() || info.getEndOffset() > priorityRange.getEndOffset()) {
            createOrReuseHighlighterFor(info, colorsScheme, document, group, psiFile, (MarkupModelEx) markup, infosToRemove, ranges2markersCache, severityRegistrar);
            changed[0] = true;
        }
        return true;
    });
    for (RangeHighlighter highlighter : infosToRemove.forAllInGarbageBin()) {
        highlighter.dispose();
        changed[0] = true;
    }
    if (changed[0]) {
        clearWhiteSpaceOptimizationFlag(document);
    }
    assertMarkupConsistent(markup, project);
}
Also used : com.intellij.openapi.util(com.intellij.openapi.util) java.util(java.util) GutterMark(com.intellij.codeInsight.daemon.GutterMark) HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) Document(com.intellij.openapi.editor.Document) THashSet(gnu.trove.THashSet) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) ContainerUtil(com.intellij.util.containers.ContainerUtil) THashMap(gnu.trove.THashMap) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) RangeMarker(com.intellij.openapi.editor.RangeMarker) RedBlackTree(com.intellij.openapi.editor.impl.RedBlackTree) com.intellij.openapi.editor.markup(com.intellij.openapi.editor.markup) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) NotNull(org.jetbrains.annotations.NotNull) Consumer(com.intellij.util.Consumer) RangeMarkerTree(com.intellij.openapi.editor.impl.RangeMarkerTree) RangeMarkerTree(com.intellij.openapi.editor.impl.RangeMarkerTree) RangeMarker(com.intellij.openapi.editor.RangeMarker) THashSet(gnu.trove.THashSet) THashMap(gnu.trove.THashMap) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel)

Example 29 with Processor

use of com.intellij.util.Processor in project intellij-community by JetBrains.

the class UpdateHighlightersUtil method setHighlightersInRange.

static void setHighlightersInRange(@NotNull final Project project, @NotNull final Document document, @NotNull final TextRange range, // if null global scheme will be used
@Nullable final EditorColorsScheme colorsScheme, @NotNull final List<HighlightInfo> infos, @NotNull final MarkupModelEx markup, final int group) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    final SeverityRegistrar severityRegistrar = SeverityRegistrar.getSeverityRegistrar(project);
    final HighlightersRecycler infosToRemove = new HighlightersRecycler();
    DaemonCodeAnalyzerEx.processHighlights(document, project, null, range.getStartOffset(), range.getEndOffset(), info -> {
        if (info.getGroup() == group) {
            RangeHighlighter highlighter = info.getHighlighter();
            int hiStart = highlighter.getStartOffset();
            int hiEnd = highlighter.getEndOffset();
            boolean willBeRemoved = hiEnd == document.getTextLength() && range.getEndOffset() == document.getTextLength() || range.containsRange(hiStart, hiEnd);
            if (willBeRemoved) {
                infosToRemove.recycleHighlighter(highlighter);
                info.setHighlighter(null);
            }
        }
        return true;
    });
    ContainerUtil.quickSort(infos, BY_START_OFFSET_NODUPS);
    final Map<TextRange, RangeMarker> ranges2markersCache = new THashMap<>(10);
    final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
    final DaemonCodeAnalyzerEx codeAnalyzer = DaemonCodeAnalyzerEx.getInstanceEx(project);
    final boolean[] changed = { false };
    RangeMarkerTree.sweep((RangeMarkerTree.Generator<HighlightInfo>) processor -> ContainerUtil.process(infos, processor), (offset, info, atStart, overlappingIntervals) -> {
        if (!atStart) {
            return true;
        }
        if (info.isFileLevelAnnotation() && psiFile != null && psiFile.getViewProvider().isPhysical()) {
            codeAnalyzer.addFileLevelHighlight(project, group, info, psiFile);
            changed[0] = true;
            return true;
        }
        if (isWarningCoveredByError(info, overlappingIntervals, severityRegistrar)) {
            return true;
        }
        if (info.getStartOffset() >= range.getStartOffset() && info.getEndOffset() <= range.getEndOffset() && psiFile != null) {
            createOrReuseHighlighterFor(info, colorsScheme, document, group, psiFile, markup, infosToRemove, ranges2markersCache, severityRegistrar);
            changed[0] = true;
        }
        return true;
    });
    for (RangeHighlighter highlighter : infosToRemove.forAllInGarbageBin()) {
        highlighter.dispose();
        changed[0] = true;
    }
    if (changed[0]) {
        clearWhiteSpaceOptimizationFlag(document);
    }
    assertMarkupConsistent(markup, project);
}
Also used : com.intellij.openapi.util(com.intellij.openapi.util) java.util(java.util) GutterMark(com.intellij.codeInsight.daemon.GutterMark) HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) Document(com.intellij.openapi.editor.Document) THashSet(gnu.trove.THashSet) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) ContainerUtil(com.intellij.util.containers.ContainerUtil) THashMap(gnu.trove.THashMap) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) RangeMarker(com.intellij.openapi.editor.RangeMarker) RedBlackTree(com.intellij.openapi.editor.impl.RedBlackTree) com.intellij.openapi.editor.markup(com.intellij.openapi.editor.markup) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) NotNull(org.jetbrains.annotations.NotNull) Consumer(com.intellij.util.Consumer) RangeMarkerTree(com.intellij.openapi.editor.impl.RangeMarkerTree) RangeMarkerTree(com.intellij.openapi.editor.impl.RangeMarkerTree) RangeMarker(com.intellij.openapi.editor.RangeMarker) THashMap(gnu.trove.THashMap) PsiFile(com.intellij.psi.PsiFile)

Example 30 with Processor

use of com.intellij.util.Processor in project intellij-community by JetBrains.

the class DefaultSymbolNavigationContributor method processElementsWithName.

@Override
public void processElementsWithName(@NotNull String name, @NotNull final Processor<NavigationItem> processor, @NotNull final FindSymbolParameters parameters) {
    GlobalSearchScope scope = parameters.getSearchScope();
    IdFilter filter = parameters.getIdFilter();
    PsiShortNamesCache cache = PsiShortNamesCache.getInstance(scope.getProject());
    String completePattern = parameters.getCompletePattern();
    final Condition<PsiMember> qualifiedMatcher = getQualifiedNameMatcher(completePattern);
    //noinspection UnusedDeclaration
    final Set<PsiMethod> collectedMethods = new THashSet<>();
    boolean success = cache.processFieldsWithName(name, field -> {
        if (isOpenable(field) && qualifiedMatcher.value(field))
            return processor.process(field);
        return true;
    }, scope, filter) && cache.processClassesWithName(name, aClass -> {
        if (isOpenable(aClass) && qualifiedMatcher.value(aClass))
            return processor.process(aClass);
        return true;
    }, scope, filter) && cache.processMethodsWithName(name, method -> {
        if (!method.isConstructor() && isOpenable(method) && qualifiedMatcher.value(method)) {
            collectedMethods.add(method);
        }
        return true;
    }, scope, filter);
    if (success) {
        // hashSuperMethod accesses index and can not be invoked without risk of the deadlock in processMethodsWithName
        Iterator<PsiMethod> iterator = collectedMethods.iterator();
        while (iterator.hasNext()) {
            PsiMethod method = iterator.next();
            if (!hasSuperMethod(method, scope, qualifiedMatcher) && !processor.process(method))
                return;
            ProgressManager.checkCanceled();
            iterator.remove();
        }
    }
}
Also used : NavigationItem(com.intellij.navigation.NavigationItem) java.util(java.util) ArrayUtil(com.intellij.util.ArrayUtil) MinusculeMatcher(com.intellij.psi.codeStyle.MinusculeMatcher) HashSet(com.intellij.util.containers.HashSet) THashSet(gnu.trove.THashSet) IdFilter(com.intellij.util.indexing.IdFilter) ChooseByNameContributorEx(com.intellij.navigation.ChooseByNameContributorEx) NameUtil(com.intellij.psi.codeStyle.NameUtil) InheritanceUtil(com.intellij.psi.util.InheritanceUtil) Project(com.intellij.openapi.project.Project) PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) PsiUtil(com.intellij.psi.util.PsiUtil) Logger(com.intellij.openapi.diagnostic.Logger) ProgressManager(com.intellij.openapi.progress.ProgressManager) StringUtil(com.intellij.openapi.util.text.StringUtil) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) DefaultPsiElementCellRenderer(com.intellij.ide.util.DefaultPsiElementCellRenderer) Nullable(org.jetbrains.annotations.Nullable) PsiSearchScopeUtil(com.intellij.psi.search.PsiSearchScopeUtil) Processor(com.intellij.util.Processor) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) FindSymbolParameters(com.intellij.util.indexing.FindSymbolParameters) Condition(com.intellij.openapi.util.Condition) IdFilter(com.intellij.util.indexing.IdFilter) PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) THashSet(gnu.trove.THashSet)

Aggregations

Processor (com.intellij.util.Processor)83 NotNull (org.jetbrains.annotations.NotNull)65 Project (com.intellij.openapi.project.Project)49 Nullable (org.jetbrains.annotations.Nullable)49 ContainerUtil (com.intellij.util.containers.ContainerUtil)42 com.intellij.psi (com.intellij.psi)31 List (java.util.List)28 ApplicationManager (com.intellij.openapi.application.ApplicationManager)25 StringUtil (com.intellij.openapi.util.text.StringUtil)25 VirtualFile (com.intellij.openapi.vfs.VirtualFile)25 java.util (java.util)25 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)24 ProgressManager (com.intellij.openapi.progress.ProgressManager)21 Logger (com.intellij.openapi.diagnostic.Logger)20 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)20 NonNls (org.jetbrains.annotations.NonNls)18 Ref (com.intellij.openapi.util.Ref)16 Collection (java.util.Collection)16 SmartList (com.intellij.util.SmartList)14 Document (com.intellij.openapi.editor.Document)13