Search in sources :

Example 46 with LocalSearchScope

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

the class RenameJavaClassProcessor method findSubmemberHidesMemberCollisions.

public static void findSubmemberHidesMemberCollisions(final PsiClass aClass, final String newName, final List<UsageInfo> result) {
    if (aClass.getParent() instanceof PsiClass) {
        PsiClass parent = (PsiClass) aClass.getParent();
        Collection<PsiClass> inheritors = ClassInheritorsSearch.search(parent).findAll();
        for (PsiClass inheritor : inheritors) {
            if (newName.equals(inheritor.getName())) {
                final ClassCollisionsDetector classCollisionsDetector = new ClassCollisionsDetector(aClass);
                for (PsiReference reference : ReferencesSearch.search(inheritor, new LocalSearchScope(inheritor))) {
                    classCollisionsDetector.addClassCollisions(reference.getElement(), newName, result);
                }
            }
            PsiClass[] inners = inheritor.getInnerClasses();
            for (PsiClass inner : inners) {
                if (newName.equals(inner.getName())) {
                    result.add(new SubmemberHidesMemberUsageInfo(inner, aClass));
                }
            }
        }
    } else if (aClass instanceof PsiTypeParameter) {
        final PsiTypeParameterListOwner owner = ((PsiTypeParameter) aClass).getOwner();
        if (owner instanceof PsiClass) {
            final PsiClass[] supers = ((PsiClass) owner).getSupers();
            for (PsiClass superClass : supers) {
                if (newName.equals(superClass.getName())) {
                    final ClassCollisionsDetector classCollisionsDetector = new ClassCollisionsDetector(aClass);
                    for (PsiReference reference : ReferencesSearch.search(superClass, new LocalSearchScope(superClass))) {
                        classCollisionsDetector.addClassCollisions(reference.getElement(), newName, result);
                    }
                }
                PsiClass[] inners = superClass.getInnerClasses();
                for (final PsiClass inner : inners) {
                    if (newName.equals(inner.getName())) {
                        ReferencesSearch.search(inner).forEach(reference -> {
                            PsiElement refElement = reference.getElement();
                            if (refElement instanceof PsiReferenceExpression && ((PsiReferenceExpression) refElement).isQualified())
                                return true;
                            MemberHidesOuterMemberUsageInfo info = new MemberHidesOuterMemberUsageInfo(refElement, aClass);
                            result.add(info);
                            return true;
                        });
                    }
                }
            }
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) java.util(java.util) RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) RefactoringUIUtil(com.intellij.refactoring.util.RefactoringUIUtil) ChangeContextUtil(com.intellij.codeInsight.ChangeContextUtil) NonNls(org.jetbrains.annotations.NonNls) UsageInfo(com.intellij.usageView.UsageInfo) ArrayUtilRt(com.intellij.util.ArrayUtilRt) RefactoringBundle(com.intellij.refactoring.RefactoringBundle) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Comparing(com.intellij.openapi.util.Comparing) Project(com.intellij.openapi.project.Project) Logger(com.intellij.openapi.diagnostic.Logger) JavaRefactoringSettings(com.intellij.refactoring.JavaRefactoringSettings) MultiMap(com.intellij.util.containers.MultiMap) HelpID(com.intellij.refactoring.HelpID) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) LightElement(com.intellij.psi.impl.light.LightElement) Nullable(org.jetbrains.annotations.Nullable) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) ClassUtil(com.intellij.psi.util.ClassUtil) RefactoringUtil(com.intellij.refactoring.util.RefactoringUtil) Processor(com.intellij.util.Processor) Pair(com.intellij.openapi.util.Pair) com.intellij.psi(com.intellij.psi) ClassInheritorsSearch(com.intellij.psi.search.searches.ClassInheritorsSearch) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull)

Example 47 with LocalSearchScope

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

the class NonPhysicalReferenceSearcher method processQuery.

public void processQuery(@NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        return;
    }
    final SearchScope scope = queryParameters.getScopeDeterminedByUser();
    final PsiElement element = queryParameters.getElementToSearch();
    final PsiFile containingFile = element.getContainingFile();
    if (!(scope instanceof GlobalSearchScope) && !isApplicableTo(containingFile)) {
        return;
    }
    final LocalSearchScope currentScope;
    if (scope instanceof LocalSearchScope) {
        if (queryParameters.isIgnoreAccessScope()) {
            return;
        }
        currentScope = (LocalSearchScope) scope;
    } else {
        currentScope = null;
    }
    Project project = element.getProject();
    if (!project.isInitialized()) {
        // skip default and other projects that look weird
        return;
    }
    final PsiManager psiManager = PsiManager.getInstance(project);
    for (VirtualFile virtualFile : FileEditorManager.getInstance(project).getOpenFiles()) {
        if (virtualFile.getFileType().isBinary()) {
            continue;
        }
        PsiFile file = psiManager.findFile(virtualFile);
        if (isApplicableTo(file)) {
            final LocalSearchScope fileScope = new LocalSearchScope(file);
            final LocalSearchScope searchScope = currentScope == null ? fileScope : fileScope.intersectWith(currentScope);
            ReferencesSearch.searchOptimized(element, searchScope, true, queryParameters.getOptimizer(), consumer);
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 48 with LocalSearchScope

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

the class FunctionHelper method hasVarReference.

static boolean hasVarReference(PsiElement expressionOrCodeBlock, String name, StreamToLoopReplacementContext context) {
    PsiLambdaExpression lambda = (PsiLambdaExpression) context.createExpression(name + "->" + expressionOrCodeBlock.getText());
    PsiParameter var = lambda.getParameterList().getParameters()[0];
    PsiElement body = lambda.getBody();
    LOG.assertTrue(body != null);
    return ReferencesSearch.search(var, new LocalSearchScope(body)).findFirst() != null;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 49 with LocalSearchScope

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

the class JavaFindUsagesHandler method findReferencesToHighlight.

@NotNull
@Override
public Collection<PsiReference> findReferencesToHighlight(@NotNull final PsiElement target, @NotNull final SearchScope searchScope) {
    if (target instanceof PsiMethod) {
        final PsiMethod[] superMethods = ((PsiMethod) target).findDeepestSuperMethods();
        if (superMethods.length == 0) {
            return MethodReferencesSearch.search((PsiMethod) target, searchScope, true).findAll();
        }
        final Collection<PsiReference> result = new ArrayList<>();
        GlobalSearchScope resolveScope = null;
        if (searchScope instanceof LocalSearchScope) {
            final PsiElement[] scopeElements = ((LocalSearchScope) searchScope).getScope();
            resolveScope = GlobalSearchScope.union(ContainerUtil.map2Array(scopeElements, GlobalSearchScope.class, PsiElement::getResolveScope));
        }
        for (PsiMethod superMethod : superMethods) {
            if (resolveScope != null) {
                superMethod = PsiSuperMethodUtil.correctMethodByScope(superMethod, resolveScope);
            }
            result.addAll(MethodReferencesSearch.search(superMethod, searchScope, true).findAll());
        }
        return result;
    }
    return super.findReferencesToHighlight(target, searchScope);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 50 with LocalSearchScope

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

the class ConvertToInstanceMethodProcessor method findUsages.

@NotNull
protected UsageInfo[] findUsages() {
    LOG.assertTrue(myTargetParameter.getDeclarationScope() == myMethod);
    final Project project = myMethod.getProject();
    final PsiReference[] methodReferences = ReferencesSearch.search(myMethod, GlobalSearchScope.projectScope(project), false).toArray(PsiReference.EMPTY_ARRAY);
    List<UsageInfo> result = new ArrayList<>();
    for (final PsiReference ref : methodReferences) {
        final PsiElement element = ref.getElement();
        if (element instanceof PsiReferenceExpression) {
            if (element.getParent() instanceof PsiMethodCallExpression) {
                result.add(new MethodCallUsageInfo((PsiMethodCallExpression) element.getParent()));
            }
        } else if (element instanceof PsiDocTagValue) {
            //TODO:!!!
            result.add(new JavaDocUsageInfo(ref));
        }
    }
    for (final PsiReference ref : ReferencesSearch.search(myTargetParameter, new LocalSearchScope(myMethod), false)) {
        final PsiElement element = ref.getElement();
        if (element instanceof PsiReferenceExpression || element instanceof PsiDocParamRef) {
            result.add(new ParameterUsageInfo(ref));
        }
    }
    if (myTargetClass.isInterface()) {
        PsiClass[] implementingClasses = RefactoringHierarchyUtil.findImplementingClasses(myTargetClass);
        for (final PsiClass implementingClass : implementingClasses) {
            result.add(new ImplementingClassUsageInfo(implementingClass));
        }
    }
    return result.toArray(new UsageInfo[result.size()]);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) Project(com.intellij.openapi.project.Project) PsiDocParamRef(com.intellij.psi.impl.source.javadoc.PsiDocParamRef) UsageInfo(com.intellij.usageView.UsageInfo) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

LocalSearchScope (com.intellij.psi.search.LocalSearchScope)113 SearchScope (com.intellij.psi.search.SearchScope)31 NotNull (org.jetbrains.annotations.NotNull)22 PsiElement (com.intellij.psi.PsiElement)19 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)19 Project (com.intellij.openapi.project.Project)18 Nullable (org.jetbrains.annotations.Nullable)13 ArrayList (java.util.ArrayList)12 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 UsageInfo (com.intellij.usageView.UsageInfo)11 TextRange (com.intellij.openapi.util.TextRange)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9 com.intellij.psi (com.intellij.psi)8 PsiReference (com.intellij.psi.PsiReference)8 PsiFile (com.intellij.psi.PsiFile)7 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)7 AnalysisScope (com.intellij.analysis.AnalysisScope)6 Processor (com.intellij.util.Processor)6 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)5 Ref (com.intellij.openapi.util.Ref)4