Search in sources :

Example 11 with PsiSearchHelper

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

the class InlineOptionsDialog method initOccurrencesNumber.

protected static int initOccurrencesNumber(PsiNameIdentifierOwner nameIdentifierOwner) {
    final ProgressManager progressManager = ProgressManager.getInstance();
    final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(nameIdentifierOwner.getProject());
    final GlobalSearchScope scope = GlobalSearchScope.projectScope(nameIdentifierOwner.getProject());
    final String name = nameIdentifierOwner.getName();
    final boolean isCheapToSearch = name != null && searchHelper.isCheapEnoughToSearch(name, scope, null, progressManager.getProgressIndicator()) != PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES;
    return isCheapToSearch ? ReferencesSearch.search(nameIdentifierOwner, scope).findAll().size() : -1;
}
Also used : PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ProgressManager(com.intellij.openapi.progress.ProgressManager)

Example 12 with PsiSearchHelper

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

the class UnusedSymbolUtil method processUsages.

// return false if can't process usages (weird member of too may usages) or processor returned false
public static boolean processUsages(@NotNull Project project, @NotNull PsiFile containingFile, @NotNull PsiMember member, @NotNull ProgressIndicator progress, @Nullable PsiFile ignoreFile, @NotNull Processor<UsageInfo> usageInfoProcessor) {
    String name = member.getName();
    if (name == null) {
        log("* " + member.getName() + " no name; false");
        return false;
    }
    SearchScope useScope = member.getUseScope();
    PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(project);
    if (useScope instanceof GlobalSearchScope) {
        // some classes may have references from within XML outside dependent modules, e.g. our actions
        if (member instanceof PsiClass) {
            useScope = GlobalSearchScope.projectScope(project).uniteWith((GlobalSearchScope) useScope);
        }
        // if we've resolved all references, find usages will be fast
        PsiSearchHelper.SearchCostResult cheapEnough = RefResolveService.ENABLED && RefResolveService.getInstance(project).isUpToDate() ? PsiSearchHelper.SearchCostResult.FEW_OCCURRENCES : searchHelper.isCheapEnoughToSearch(name, (GlobalSearchScope) useScope, ignoreFile, progress);
        if (cheapEnough == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES) {
            log("* " + member.getName() + " too many usages; false");
            return false;
        }
        //if count is 0 there is no usages since we've called myRefCountHolder.isReferenced() before
        if (cheapEnough == PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES && !canBeReferencedViaWeirdNames(member, containingFile)) {
            log("* " + member.getName() + " 0 usages; true");
            return true;
        }
        if (member instanceof PsiMethod) {
            String propertyName = PropertyUtil.getPropertyName(member);
            if (propertyName != null) {
                SearchScope fileScope = containingFile.getUseScope();
                if (fileScope instanceof GlobalSearchScope && searchHelper.isCheapEnoughToSearch(propertyName, (GlobalSearchScope) fileScope, ignoreFile, progress) == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES) {
                    log("* " + member.getName() + " too many prop usages; false");
                    return false;
                }
            }
        }
    }
    FindUsagesOptions options;
    if (member instanceof PsiPackage) {
        options = new JavaPackageFindUsagesOptions(useScope);
        options.isSearchForTextOccurrences = true;
    } else if (member instanceof PsiClass) {
        options = new JavaClassFindUsagesOptions(useScope);
        options.isSearchForTextOccurrences = true;
    } else if (member instanceof PsiMethod) {
        PsiMethod method = (PsiMethod) member;
        options = new JavaMethodFindUsagesOptions(useScope);
        options.isSearchForTextOccurrences = method.isConstructor();
    } else if (member instanceof PsiVariable) {
        options = new JavaVariableFindUsagesOptions(useScope);
        options.isSearchForTextOccurrences = false;
    } else {
        options = new FindUsagesOptions(useScope);
        options.isSearchForTextOccurrences = true;
    }
    options.isUsages = true;
    return JavaFindUsagesHelper.processElementUsages(member, options, usageInfoProcessor);
}
Also used : PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope)

Example 13 with PsiSearchHelper

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

the class DeprecatedIsStillUsedInspection method checkMember.

private static void checkMember(@NotNull PsiMember member, @NotNull PsiIdentifier identifier, @NotNull ProblemsHolder holder) {
    if (!(member instanceof PsiDocCommentOwner) || !isDeprecated((PsiDocCommentOwner) member)) {
        return;
    }
    PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(member.getProject());
    String name = member.getName();
    if (name != null && hasUsages(member, name, searchHelper, member.getResolveScope())) {
        holder.registerProblem(identifier, "Deprecated member '" + name + "' is still used");
    }
}
Also used : PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper)

Example 14 with PsiSearchHelper

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

the class DfaVariableValue method isCheapEnoughToSearch.

private static boolean isCheapEnoughToSearch(PsiField field, String name) {
    SearchScope scope = field.getUseScope();
    if (!(scope instanceof GlobalSearchScope))
        return true;
    PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(field.getProject());
    PsiSearchHelper.SearchCostResult result = helper.isCheapEnoughToSearch(name, (GlobalSearchScope) scope, field.getContainingFile(), null);
    return result != PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES;
}
Also used : PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope)

Example 15 with PsiSearchHelper

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

the class UnusedDeclarationInspectionBase method runInspection.

@Override
public void runInspection(@NotNull final AnalysisScope scope, @NotNull InspectionManager manager, @NotNull final GlobalInspectionContext globalContext, @NotNull ProblemDescriptionsProcessor problemDescriptionsProcessor) {
    globalContext.getRefManager().iterate(new RefJavaVisitor() {

        @Override
        public void visitElement(@NotNull final RefEntity refEntity) {
            if (refEntity instanceof RefElementImpl) {
                final RefElementImpl refElement = (RefElementImpl) refEntity;
                if (!refElement.isSuspicious())
                    return;
                PsiFile file = refElement.getContainingFile();
                if (file == null)
                    return;
                final boolean isSuppressed = refElement.isSuppressed(getShortName(), ALTERNATIVE_ID);
                if (isSuppressed || !((GlobalInspectionContextBase) globalContext).isToCheckFile(file, UnusedDeclarationInspectionBase.this)) {
                    if (isSuppressed || !scope.contains(file)) {
                        getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
                    }
                }
            }
        }
    });
    if (isAddNonJavaUsedEnabled()) {
        checkForReachableRefs(globalContext);
        final StrictUnreferencedFilter strictUnreferencedFilter = new StrictUnreferencedFilter(this, globalContext);
        ProgressManager.getInstance().runProcess(new Runnable() {

            @Override
            public void run() {
                final RefManager refManager = globalContext.getRefManager();
                final PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(refManager.getProject());
                refManager.iterate(new RefJavaVisitor() {

                    @Override
                    public void visitElement(@NotNull final RefEntity refEntity) {
                        if (refEntity instanceof RefClass && strictUnreferencedFilter.accepts((RefClass) refEntity)) {
                            findExternalClassReferences((RefClass) refEntity);
                        } else if (refEntity instanceof RefMethod) {
                            RefMethod refMethod = (RefMethod) refEntity;
                            if (refMethod.isConstructor() && strictUnreferencedFilter.accepts(refMethod)) {
                                findExternalClassReferences(refMethod.getOwnerClass());
                            }
                        }
                    }

                    private void findExternalClassReferences(final RefClass refElement) {
                        final PsiClass psiClass = refElement.getElement();
                        String qualifiedName = psiClass != null ? psiClass.getQualifiedName() : null;
                        if (qualifiedName != null) {
                            final GlobalSearchScope projectScope = GlobalSearchScope.projectScope(globalContext.getProject());
                            final PsiNonJavaFileReferenceProcessor processor = (file, startOffset, endOffset) -> {
                                getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
                                return false;
                            };
                            final DelegatingGlobalSearchScope globalSearchScope = new DelegatingGlobalSearchScope(projectScope) {

                                @Override
                                public boolean contains(@NotNull VirtualFile file) {
                                    return file.getFileType() != JavaFileType.INSTANCE && super.contains(file);
                                }
                            };
                            if (helper.processUsagesInNonJavaFiles(qualifiedName, processor, globalSearchScope)) {
                                final PsiReference reference = ReferencesSearch.search(psiClass, globalSearchScope).findFirst();
                                if (reference != null) {
                                    getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
                                    for (PsiMethod method : psiClass.getMethods()) {
                                        final RefElement refMethod = refManager.getReference(method);
                                        if (refMethod != null) {
                                            getEntryPointsManager(globalContext).addEntryPoint(refMethod, false);
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
            }
        }, null);
    }
    myProcessedSuspicious = new HashSet<>();
    myPhase = 1;
}
Also used : GroupNames(com.intellij.codeInsight.daemon.GroupNames) PsiClassImplUtil(com.intellij.psi.impl.PsiClassImplUtil) java.util(java.util) JobDescriptor(com.intellij.codeInspection.ex.JobDescriptor) VirtualFile(com.intellij.openapi.vfs.VirtualFile) HashMap(com.intellij.util.containers.HashMap) ToolExtensionPoints(com.intellij.ToolExtensionPoints) InvalidDataException(com.intellij.openapi.util.InvalidDataException) ContainerUtil(com.intellij.util.containers.ContainerUtil) UnusedSymbolLocalInspectionBase(com.intellij.codeInspection.unusedSymbol.UnusedSymbolLocalInspectionBase) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope) Project(com.intellij.openapi.project.Project) com.intellij.codeInspection.reference(com.intellij.codeInspection.reference) Logger(com.intellij.openapi.diagnostic.Logger) RefFilter(com.intellij.codeInspection.util.RefFilter) Extensions(com.intellij.openapi.extensions.Extensions) ProgressManager(com.intellij.openapi.progress.ProgressManager) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) ExtensionPoint(com.intellij.openapi.extensions.ExtensionPoint) AnalysisScope(com.intellij.analysis.AnalysisScope) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) com.intellij.codeInspection(com.intellij.codeInspection) JavaFileType(com.intellij.ide.highlighter.JavaFileType) PsiNonJavaFileReferenceProcessor(com.intellij.psi.search.PsiNonJavaFileReferenceProcessor) TestOnly(org.jetbrains.annotations.TestOnly) Nullable(org.jetbrains.annotations.Nullable) EntryPointsManager(com.intellij.codeInspection.ex.EntryPointsManager) PsiMethodUtil(com.intellij.psi.util.PsiMethodUtil) ApplicationManager(com.intellij.openapi.application.ApplicationManager) GlobalInspectionContextBase(com.intellij.codeInspection.ex.GlobalInspectionContextBase) HighlightUtilBase(com.intellij.codeInsight.daemon.impl.analysis.HighlightUtilBase) com.intellij.psi(com.intellij.psi) WriteExternalException(com.intellij.openapi.util.WriteExternalException) NotNull(org.jetbrains.annotations.NotNull) Element(org.jdom.Element) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiNonJavaFileReferenceProcessor(com.intellij.psi.search.PsiNonJavaFileReferenceProcessor) NotNull(org.jetbrains.annotations.NotNull) PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope)

Aggregations

PsiSearchHelper (com.intellij.psi.search.PsiSearchHelper)16 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)11 SearchScope (com.intellij.psi.search.SearchScope)5 ProgressManager (com.intellij.openapi.progress.ProgressManager)4 NotNull (org.jetbrains.annotations.NotNull)4 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 THashSet (gnu.trove.THashSet)3 AnalysisScope (com.intellij.analysis.AnalysisScope)2 com.intellij.codeInspection (com.intellij.codeInspection)2 GlobalInspectionContextBase (com.intellij.codeInspection.ex.GlobalInspectionContextBase)2 Logger (com.intellij.openapi.diagnostic.Logger)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 ToolExtensionPoints (com.intellij.ToolExtensionPoints)1 GroupNames (com.intellij.codeInsight.daemon.GroupNames)1 HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)1 HighlightUtilBase (com.intellij.codeInsight.daemon.impl.analysis.HighlightUtilBase)1 EntryPointsManager (com.intellij.codeInspection.ex.EntryPointsManager)1 JobDescriptor (com.intellij.codeInspection.ex.JobDescriptor)1 com.intellij.codeInspection.reference (com.intellij.codeInspection.reference)1