Search in sources :

Example 16 with PsiShortNamesCache

use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.

the class CreateFromUsageUtils method guessExpectedTypes.

@NotNull
static ExpectedTypeInfo[] guessExpectedTypes(@NotNull PsiExpression expression, boolean allowVoidType) {
    PsiManager manager = expression.getManager();
    GlobalSearchScope resolveScope = expression.getResolveScope();
    List<ExpectedTypeInfo[]> typesList = new ArrayList<>();
    List<String> expectedMethodNames = new ArrayList<>();
    List<String> expectedFieldNames = new ArrayList<>();
    getExpectedInformation(expression, typesList, expectedMethodNames, expectedFieldNames);
    if (typesList.size() == 1 && (!expectedFieldNames.isEmpty() || !expectedMethodNames.isEmpty())) {
        ExpectedTypeInfo[] infos = typesList.get(0);
        if (infos.length == 1 && infos[0].getKind() == ExpectedTypeInfo.TYPE_OR_SUBTYPE && infos[0].getType().equals(PsiType.getJavaLangObject(manager, resolveScope))) {
            typesList.clear();
        }
    }
    if (typesList.isEmpty()) {
        final JavaPsiFacade facade = JavaPsiFacade.getInstance(expression.getProject());
        final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(expression.getProject());
        PsiElementFactory factory = facade.getElementFactory();
        for (String fieldName : expectedFieldNames) {
            PsiField[] fields = cache.getFieldsByNameIfNotMoreThan(fieldName, resolveScope, MAX_RAW_GUESSED_MEMBERS_COUNT);
            addMemberInfo(fields, expression, typesList, factory);
        }
        for (String methodName : expectedMethodNames) {
            PsiMethod[] projectMethods = cache.getMethodsByNameIfNotMoreThan(methodName, resolveScope.intersectWith(GlobalSearchScope.projectScope(manager.getProject())), MAX_RAW_GUESSED_MEMBERS_COUNT);
            PsiMethod[] libraryMethods = cache.getMethodsByNameIfNotMoreThan(methodName, resolveScope.intersectWith(GlobalSearchScope.notScope(GlobalSearchScope.projectScope(manager.getProject()))), MAX_RAW_GUESSED_MEMBERS_COUNT);
            PsiMethod[] methods = ArrayUtil.mergeArrays(projectMethods, libraryMethods);
            addMemberInfo(methods, expression, typesList, factory);
        }
    }
    ExpectedTypeInfo[] expectedTypes = ExpectedTypeUtil.intersect(typesList);
    if (expectedTypes.length == 0 && !typesList.isEmpty()) {
        List<ExpectedTypeInfo> union = new ArrayList<>();
        for (ExpectedTypeInfo[] aTypesList : typesList) {
            ContainerUtil.addAll(union, (ExpectedTypeInfo[]) aTypesList);
        }
        expectedTypes = union.toArray(new ExpectedTypeInfo[union.size()]);
    }
    if (expectedTypes.length == 0) {
        PsiType t = allowVoidType ? PsiType.VOID : PsiType.getJavaLangObject(manager, resolveScope);
        expectedTypes = new ExpectedTypeInfo[] { ExpectedTypesProvider.createInfo(t, ExpectedTypeInfo.TYPE_OR_SUBTYPE, t, TailType.NONE) };
    }
    return expectedTypes;
}
Also used : PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with PsiShortNamesCache

use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.

the class DefaultSymbolNavigationContributor method getNames.

@Override
@NotNull
public String[] getNames(Project project, boolean includeNonProjectItems) {
    PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project);
    HashSet<String> set = new HashSet<>();
    cache.getAllMethodNames(set);
    cache.getAllFieldNames(set);
    cache.getAllClassNames(set);
    return ArrayUtil.toStringArray(set);
}
Also used : PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) HashSet(com.intellij.util.containers.HashSet) THashSet(gnu.trove.THashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with PsiShortNamesCache

use of com.intellij.psi.search.PsiShortNamesCache 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 19 with PsiShortNamesCache

use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.

the class DefaultSymbolNavigationContributor method processNames.

public void processNames(@NotNull Processor<String> processor, @NotNull GlobalSearchScope scope, @Nullable IdFilter filter) {
    PsiShortNamesCache cache = PsiShortNamesCache.getInstance(scope.getProject());
    cache.processAllClassNames(processor, scope, filter);
    cache.processAllFieldNames(processor, scope, filter);
    cache.processAllMethodNames(processor, scope, filter);
}
Also used : PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache)

Example 20 with PsiShortNamesCache

use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.

the class DefaultSymbolNavigationContributor method getItemsByName.

@Override
@NotNull
public NavigationItem[] getItemsByName(String name, final String pattern, Project project, boolean includeNonProjectItems) {
    GlobalSearchScope scope = includeNonProjectItems ? GlobalSearchScope.allScope(project) : GlobalSearchScope.projectScope(project);
    PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project);
    Condition<PsiMember> qualifiedMatcher = getQualifiedNameMatcher(pattern);
    List<PsiMember> result = new ArrayList<>();
    for (PsiMethod method : cache.getMethodsByName(name, scope)) {
        if (!method.isConstructor() && isOpenable(method) && !hasSuperMethod(method, scope, qualifiedMatcher)) {
            result.add(method);
        }
    }
    for (PsiField field : cache.getFieldsByName(name, scope)) {
        if (isOpenable(field)) {
            result.add(field);
        }
    }
    for (PsiClass aClass : cache.getClassesByName(name, scope)) {
        if (isOpenable(aClass)) {
            result.add(aClass);
        }
    }
    PsiMember[] array = result.toArray(new PsiMember[result.size()]);
    Arrays.sort(array, MyComparator.INSTANCE);
    return array;
}
Also used : PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiShortNamesCache (com.intellij.psi.search.PsiShortNamesCache)30 NotNull (org.jetbrains.annotations.NotNull)20 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)11 Project (com.intellij.openapi.project.Project)6 PsiClass (com.intellij.psi.PsiClass)5 ArrayList (java.util.ArrayList)4 Nullable (org.jetbrains.annotations.Nullable)4 com.intellij.psi (com.intellij.psi)3 Processor (com.intellij.util.Processor)3 HashSet (com.intellij.util.containers.HashSet)3 Module (com.intellij.openapi.module.Module)2 PsiField (com.intellij.psi.PsiField)2 PsiMethod (com.intellij.psi.PsiMethod)2 THashSet (gnu.trove.THashSet)2 List (java.util.List)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 NodeIterator (com.intellij.dupLocator.iterators.NodeIterator)1 DefaultPsiElementCellRenderer (com.intellij.ide.util.DefaultPsiElementCellRenderer)1