Search in sources :

Example 1 with IdFilter

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

the class DomElementClassIndex method hasStubElementsOfType.

public boolean hasStubElementsOfType(final DomFileElement domFileElement, final Class<? extends DomElement> clazz) {
    final VirtualFile file = domFileElement.getFile().getVirtualFile();
    if (!(file instanceof VirtualFileWithId))
        return false;
    final String clazzName = clazz.getName();
    final int virtualFileId = ((VirtualFileWithId) file).getId();
    CommonProcessors.FindFirstProcessor<? super PsiFile> processor = new CommonProcessors.FindFirstProcessor<>();
    StubIndex.getInstance().processElements(KEY, clazzName, domFileElement.getFile().getProject(), GlobalSearchScope.fileScope(domFileElement.getFile()), new IdFilter() {

        @Override
        public boolean containsFileId(int id) {
            return id == virtualFileId;
        }
    }, PsiFile.class, processor);
    return processor.isFound();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IdFilter(com.intellij.util.indexing.IdFilter) VirtualFileWithId(com.intellij.openapi.vfs.VirtualFileWithId) CommonProcessors(com.intellij.util.CommonProcessors)

Example 2 with IdFilter

use of com.intellij.util.indexing.IdFilter 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)

Example 3 with IdFilter

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

the class ContributorsBasedGotoByModel method getIdFilter.

IdFilter getIdFilter(boolean withLibraries) {
    IdFilter idFilter = myIdFilter;
    if (idFilter == null || myIdFilterForLibraries != withLibraries) {
        idFilter = IdFilter.getProjectIdFilter(myProject, withLibraries);
        myIdFilter = idFilter;
        myIdFilterForLibraries = withLibraries;
    }
    return idFilter;
}
Also used : IdFilter(com.intellij.util.indexing.IdFilter)

Example 4 with IdFilter

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

the class DefaultChooseByNameItemProvider method filterElements.

@Override
public boolean filterElements(@NotNull final ChooseByNameBase base, @NotNull final String pattern, boolean everywhere, @NotNull final ProgressIndicator indicator, @NotNull final Processor<Object> consumer) {
    if (base.myProject != null)
        base.myProject.putUserData(ChooseByNamePopup.CURRENT_SEARCH_PATTERN, pattern);
    String namePattern = getNamePattern(base, pattern);
    String qualifierPattern = getQualifierPattern(base, pattern);
    if (removeModelSpecificMarkup(base.getModel(), namePattern).isEmpty() && !base.canShowListForEmptyPattern())
        return true;
    final ChooseByNameModel model = base.getModel();
    String matchingPattern = convertToMatchingPattern(base, namePattern);
    if (matchingPattern == null)
        return true;
    List<MatchResult> namesList = new ArrayList<>();
    final CollectConsumer<MatchResult> collect = new SynchronizedCollectConsumer<>(namesList);
    long started;
    if (model instanceof ChooseByNameModelEx) {
        indicator.checkCanceled();
        started = System.currentTimeMillis();
        final MinusculeMatcher matcher = buildPatternMatcher(matchingPattern, NameUtil.MatchingCaseSensitivity.NONE);
        ((ChooseByNameModelEx) model).processNames(sequence -> {
            indicator.checkCanceled();
            MatchResult result = matches(base, pattern, matcher, sequence);
            if (result != null) {
                collect.consume(result);
                return true;
            }
            return false;
        }, everywhere);
        if (LOG.isDebugEnabled()) {
            LOG.debug("loaded + matched:" + (System.currentTimeMillis() - started) + "," + collect.getResult().size());
        }
    } else {
        String[] names = base.getNames(everywhere);
        started = System.currentTimeMillis();
        processNamesByPattern(base, names, matchingPattern, indicator, collect);
        if (LOG.isDebugEnabled()) {
            LOG.debug("matched:" + (System.currentTimeMillis() - started) + "," + names.length);
        }
    }
    indicator.checkCanceled();
    started = System.currentTimeMillis();
    List<MatchResult> results = (List<MatchResult>) collect.getResult();
    sortNamesList(namePattern, results);
    if (LOG.isDebugEnabled()) {
        LOG.debug("sorted:" + (System.currentTimeMillis() - started) + ",results:" + results.size());
    }
    indicator.checkCanceled();
    List<Object> sameNameElements = new SmartList<>();
    final Map<Object, MatchResult> qualifierMatchResults = ContainerUtil.newIdentityTroveMap();
    Comparator<Object> weightComparator = new Comparator<Object>() {

        @SuppressWarnings("unchecked")
        Comparator<Object> modelComparator = model instanceof Comparator ? (Comparator<Object>) model : new PathProximityComparator(myContext == null ? null : myContext.getElement());

        @Override
        public int compare(Object o1, Object o2) {
            int result = modelComparator.compare(o1, o2);
            return result != 0 ? result : qualifierMatchResults.get(o1).compareTo(qualifierMatchResults.get(o2));
        }
    };
    List<Object> qualifierMiddleMatched = new ArrayList<>();
    List<Pair<String, MinusculeMatcher>> patternsAndMatchers = getPatternsAndMatchers(qualifierPattern, base);
    boolean sortedByMatchingDegree = !(base.getModel() instanceof CustomMatcherModel);
    IdFilter idFilter = null;
    if (model instanceof ContributorsBasedGotoByModel) {
        idFilter = ((ContributorsBasedGotoByModel) model).getIdFilter(everywhere);
    }
    GlobalSearchScope searchScope = FindSymbolParameters.searchScopeFor(base.myProject, everywhere);
    FindSymbolParameters parameters = new FindSymbolParameters(pattern, namePattern, searchScope, idFilter);
    boolean afterStartMatch = false;
    for (MatchResult result : namesList) {
        indicator.checkCanceled();
        String name = result.elementName;
        boolean needSeparator = sortedByMatchingDegree && !result.startMatch && afterStartMatch;
        // use interruptible call if possible
        Object[] elements = model instanceof ContributorsBasedGotoByModel ? ((ContributorsBasedGotoByModel) model).getElementsByName(name, parameters, indicator) : model.getElementsByName(name, everywhere, namePattern);
        if (elements.length > 1) {
            sameNameElements.clear();
            qualifierMatchResults.clear();
            for (final Object element : elements) {
                indicator.checkCanceled();
                MatchResult qualifierResult = matchQualifier(element, base, patternsAndMatchers);
                if (qualifierResult != null) {
                    sameNameElements.add(element);
                    qualifierMatchResults.put(element, qualifierResult);
                }
            }
            Collections.sort(sameNameElements, weightComparator);
            for (Object element : sameNameElements) {
                if (!qualifierMatchResults.get(element).startMatch) {
                    qualifierMiddleMatched.add(element);
                    continue;
                }
                if (needSeparator && !startMiddleMatchVariants(qualifierMiddleMatched, consumer))
                    return false;
                if (!consumer.process(element))
                    return false;
                needSeparator = false;
                afterStartMatch = result.startMatch;
            }
        } else if (elements.length == 1 && matchQualifier(elements[0], base, patternsAndMatchers) != null) {
            if (needSeparator && !startMiddleMatchVariants(qualifierMiddleMatched, consumer))
                return false;
            if (!consumer.process(elements[0]))
                return false;
            afterStartMatch = result.startMatch;
        }
    }
    return ContainerUtil.process(qualifierMiddleMatched, consumer);
}
Also used : IdFilter(com.intellij.util.indexing.IdFilter) PsiProximityComparator(com.intellij.psi.util.proximity.PsiProximityComparator) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) FList(com.intellij.util.containers.FList) MinusculeMatcher(com.intellij.psi.codeStyle.MinusculeMatcher) Pair(com.intellij.openapi.util.Pair) FindSymbolParameters(com.intellij.util.indexing.FindSymbolParameters)

Example 5 with IdFilter

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

the class DefaultFileNavigationContributor method getNames.

@Override
@NotNull
public String[] getNames(Project project, boolean includeNonProjectItems) {
    if (FileBasedIndex.ourEnableTracingOfKeyHashToVirtualFileMapping) {
        final THashSet<String> names = new THashSet<>(1000);
        IdFilter filter = IdFilter.getProjectIdFilter(project, includeNonProjectItems);
        processNames(s -> {
            names.add(s);
            return true;
        }, FindSymbolParameters.searchScopeFor(project, includeNonProjectItems), filter);
        if (IdFilter.LOG.isDebugEnabled()) {
            IdFilter.LOG.debug("All names retrieved2:" + names.size());
        }
        return ArrayUtil.toStringArray(names);
    } else {
        return FilenameIndex.getAllFilenames(project);
    }
}
Also used : IdFilter(com.intellij.util.indexing.IdFilter) THashSet(gnu.trove.THashSet) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

IdFilter (com.intellij.util.indexing.IdFilter)14 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)10 NotNull (org.jetbrains.annotations.NotNull)8 GoIdFilter (com.goide.stubs.index.GoIdFilter)6 Project (com.intellij.openapi.project.Project)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 StringUtil (com.intellij.openapi.util.text.StringUtil)3 VirtualFileWithId (com.intellij.openapi.vfs.VirtualFileWithId)3 CommonProcessors (com.intellij.util.CommonProcessors)3 GoFunctionIndex (com.goide.stubs.index.GoFunctionIndex)2 Logger (com.intellij.openapi.diagnostic.Logger)2 Module (com.intellij.openapi.module.Module)2 ProgressManager (com.intellij.openapi.progress.ProgressManager)2 Condition (com.intellij.openapi.util.Condition)2 PsiElement (com.intellij.psi.PsiElement)2 MinusculeMatcher (com.intellij.psi.codeStyle.MinusculeMatcher)2 Processor (com.intellij.util.Processor)2 FindSymbolParameters (com.intellij.util.indexing.FindSymbolParameters)2 THashSet (gnu.trove.THashSet)2 GoConstants (com.goide.GoConstants)1