Search in sources :

Example 1 with FindSymbolParameters

use of com.intellij.util.indexing.FindSymbolParameters 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 2 with FindSymbolParameters

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

MinusculeMatcher (com.intellij.psi.codeStyle.MinusculeMatcher)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 FindSymbolParameters (com.intellij.util.indexing.FindSymbolParameters)2 IdFilter (com.intellij.util.indexing.IdFilter)2 DefaultPsiElementCellRenderer (com.intellij.ide.util.DefaultPsiElementCellRenderer)1 ChooseByNameContributorEx (com.intellij.navigation.ChooseByNameContributorEx)1 NavigationItem (com.intellij.navigation.NavigationItem)1 Logger (com.intellij.openapi.diagnostic.Logger)1 ProgressManager (com.intellij.openapi.progress.ProgressManager)1 Project (com.intellij.openapi.project.Project)1 Condition (com.intellij.openapi.util.Condition)1 Pair (com.intellij.openapi.util.Pair)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 com.intellij.psi (com.intellij.psi)1 NameUtil (com.intellij.psi.codeStyle.NameUtil)1 PsiSearchScopeUtil (com.intellij.psi.search.PsiSearchScopeUtil)1 PsiShortNamesCache (com.intellij.psi.search.PsiShortNamesCache)1 InheritanceUtil (com.intellij.psi.util.InheritanceUtil)1 PsiUtil (com.intellij.psi.util.PsiUtil)1 PsiProximityComparator (com.intellij.psi.util.proximity.PsiProximityComparator)1