Search in sources :

Example 86 with SearchScope

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

the class JavaFxMethodSearcher method searchMethod.

private static void searchMethod(@NotNull PsiMethod psiMethod, @NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
    final Project project = PsiUtilCore.getProjectInReadAction(psiMethod);
    final SearchScope scope = ReadAction.compute(queryParameters::getEffectiveSearchScope);
    if (scope instanceof LocalSearchScope) {
        final VirtualFile[] vFiles = ((LocalSearchScope) scope).getVirtualFiles();
        for (VirtualFile vFile : vFiles) {
            if (JavaFxFileTypeFactory.isFxml(vFile)) {
                final PsiFile psiFile = PsiManager.getInstance(project).findFile(vFile);
                if (psiFile != null) {
                    final Boolean goOn = ReadAction.compute(() -> searchMethodInFile(psiMethod, psiFile, consumer));
                    if (!goOn)
                        break;
                }
            }
        }
    } else if (scope instanceof GlobalSearchScope) {
        final String propertyName = ReadAction.compute(() -> PropertyUtil.getPropertyName(psiMethod.getName()));
        if (propertyName == null)
            return;
        final String className = ReadAction.compute(() -> {
            final PsiClass psiClass = psiMethod.getContainingClass();
            return psiClass != null ? psiClass.getName() : null;
        });
        if (className == null)
            return;
        final GlobalSearchScope fxmlScope = new JavaFxScopeEnlarger.GlobalFxmlSearchScope((GlobalSearchScope) scope);
        final VirtualFile[] filteredFiles = ReadAction.compute(() -> CacheManager.SERVICE.getInstance(project).getVirtualFilesWithWord(className, UsageSearchContext.IN_PLAIN_TEXT, fxmlScope, true));
        if (ArrayUtil.isEmpty(filteredFiles))
            return;
        final GlobalSearchScope filteredScope = GlobalSearchScope.filesScope(project, ContainerUtil.newHashSet(filteredFiles));
        ApplicationManager.getApplication().runReadAction((Runnable) () -> CacheManager.SERVICE.getInstance(project).processFilesWithWord(file -> searchMethodInFile(psiMethod, file, consumer), propertyName, UsageSearchContext.IN_PLAIN_TEXT, filteredScope, true));
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) JavaFxFileTypeFactory(org.jetbrains.plugins.javaFX.fxml.JavaFxFileTypeFactory) ArrayUtil(com.intellij.util.ArrayUtil) JavaFxPropertyElement(org.jetbrains.plugins.javaFX.refactoring.JavaFxPropertyElement) XmlAttribute(com.intellij.psi.xml.XmlAttribute) VirtualFile(com.intellij.openapi.vfs.VirtualFile) QueryExecutor(com.intellij.util.QueryExecutor) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) ContainerUtil(com.intellij.util.containers.ContainerUtil) ReadAction(com.intellij.openapi.application.ReadAction) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) UsageSearchContext(com.intellij.psi.search.UsageSearchContext) PropertyUtil(com.intellij.psi.util.PropertyUtil) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) CacheManager(com.intellij.psi.impl.cache.CacheManager) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Project(com.intellij.openapi.project.Project) XmlElement(com.intellij.psi.xml.XmlElement) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) 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 87 with SearchScope

use of com.intellij.psi.search.SearchScope in project kotlin by JetBrains.

the class KtNamedDeclarationStub method getUseScope.

@NotNull
@Override
public SearchScope getUseScope() {
    KtElement enclosingBlock = KtPsiUtil.getEnclosingElementForLocalDeclaration(this, false);
    if (enclosingBlock != null) {
        PsiElement enclosingParent = enclosingBlock.getParent();
        if (enclosingParent instanceof KtContainerNode) {
            enclosingParent = enclosingParent.getParent();
        }
        if (enclosingBlock instanceof KtBlockExpression && enclosingParent instanceof KtDoWhileExpression) {
            KtExpression condition = ((KtDoWhileExpression) enclosingParent).getCondition();
            if (condition != null)
                return new LocalSearchScope(new PsiElement[] { enclosingBlock, condition });
        }
        return new LocalSearchScope(enclosingBlock);
    }
    if (hasModifier(KtTokens.PRIVATE_KEYWORD)) {
        KtElement containingClass = PsiTreeUtil.getParentOfType(this, KtClassOrObject.class);
        if (containingClass instanceof KtObjectDeclaration && ((KtObjectDeclaration) containingClass).isCompanion()) {
            KtElement companionObjectClass = PsiTreeUtil.getParentOfType(containingClass, KtClassOrObject.class);
            if (companionObjectClass != null) {
                containingClass = companionObjectClass;
            }
        }
        if (containingClass != null) {
            return new LocalSearchScope(containingClass);
        }
    }
    SearchScope scope = super.getUseScope();
    KtClassOrObject classOrObject = KtPsiUtilKt.getContainingClassOrObject(this);
    if (classOrObject != null) {
        scope = scope.intersectWith(classOrObject.getUseScope());
    }
    return scope;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 88 with SearchScope

use of com.intellij.psi.search.SearchScope in project kotlin by JetBrains.

the class KotlinCallerMethodsTreeStructure method buildChildren.

@NotNull
@Override
protected Object[] buildChildren(@NotNull HierarchyNodeDescriptor descriptor) {
    final PsiElement element = getTargetElement(descriptor);
    KtElement codeBlockForLocalDeclaration = getEnclosingElementForLocalDeclaration(element);
    if (codeBlockForLocalDeclaration != null) {
        BindingContext bindingContext = ResolutionUtils.analyze((KtElement) element, BodyResolveMode.FULL);
        final Map<PsiReference, PsiElement> referencesToElements = new HashMap<PsiReference, PsiElement>();
        codeBlockForLocalDeclaration.accept(new CalleeReferenceVisitorBase(bindingContext, true) {

            @Override
            protected void processDeclaration(KtSimpleNameExpression reference, PsiElement declaration) {
                if (!declaration.equals(element))
                    return;
                //noinspection unchecked
                PsiElement container = PsiTreeUtil.getParentOfType(reference, KtNamedFunction.class, KtPropertyAccessor.class, KtClassOrObject.class);
                if (container instanceof KtPropertyAccessor) {
                    container = PsiTreeUtil.getParentOfType(container, KtProperty.class);
                }
                if (container != null) {
                    referencesToElements.put(ReferenceUtilKt.getMainReference(reference), container);
                }
            }
        });
        return collectNodeDescriptors(descriptor, referencesToElements, null);
    }
    SearchScope searchScope = getSearchScope(scopeType, basePsiClass);
    Map<PsiElement, HierarchyNodeDescriptor> methodToDescriptorMap = Maps.newHashMap();
    Object[] javaCallers = null;
    if (element instanceof PsiMethod) {
        javaCallers = javaTreeStructure.getChildElements(getJavaNodeDescriptor(descriptor));
        processPsiMethodCallers(Collections.singleton((PsiMethod) element), descriptor, methodToDescriptorMap, searchScope, true);
    }
    if (element instanceof KtNamedFunction || element instanceof KtSecondaryConstructor) {
        Collection<PsiMethod> lightMethods = LightClassUtil.INSTANCE.getLightClassMethods((KtFunction) element);
        processPsiMethodCallers(lightMethods, descriptor, methodToDescriptorMap, searchScope, false);
    }
    if (element instanceof KtProperty) {
        LightClassUtil.PropertyAccessorsPsiMethods propertyMethods = LightClassUtil.INSTANCE.getLightClassPropertyMethods((KtProperty) element);
        processPsiMethodCallers(propertyMethods, descriptor, methodToDescriptorMap, searchScope, false);
    }
    if (element instanceof KtClassOrObject) {
        KtPrimaryConstructor constructor = ((KtClassOrObject) element).getPrimaryConstructor();
        if (constructor != null) {
            PsiMethod lightMethod = LightClassUtil.INSTANCE.getLightClassMethod(constructor);
            processPsiMethodCallers(Collections.singleton(lightMethod), descriptor, methodToDescriptorMap, searchScope, false);
        } else {
            processKtClassOrObjectCallers((KtClassOrObject) element, descriptor, methodToDescriptorMap, searchScope);
        }
    }
    Object[] callers = methodToDescriptorMap.values().toArray(new Object[methodToDescriptorMap.size()]);
    return (javaCallers != null) ? ArrayUtil.mergeArrays(javaCallers, callers) : callers;
}
Also used : HashMap(com.intellij.util.containers.HashMap) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) SearchScope(com.intellij.psi.search.SearchScope) LightClassUtil(org.jetbrains.kotlin.asJava.LightClassUtil) HierarchyNodeDescriptor(com.intellij.ide.hierarchy.HierarchyNodeDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 89 with SearchScope

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

the class PyTargetExpressionImpl method getUseScope.

@NotNull
@Override
public SearchScope getUseScope() {
    if (isQualified()) {
        return super.getUseScope();
    }
    final ScopeOwner owner = ScopeUtil.getScopeOwner(this);
    if (owner != null) {
        final Scope scope = ControlFlowCache.getScope(owner);
        if (scope.isGlobal(getName())) {
            return GlobalSearchScope.projectScope(getProject());
        }
        if (scope.isNonlocal(getName())) {
            return new LocalSearchScope(getContainingFile());
        }
    }
    // find highest level function containing our var
    PyElement container = this;
    while (true) {
        PyElement parentContainer = PsiTreeUtil.getParentOfType(container, PyFunction.class, PyClass.class);
        if (parentContainer instanceof PyClass) {
            if (isQualified()) {
                return super.getUseScope();
            }
            break;
        }
        if (parentContainer == null) {
            break;
        }
        container = parentContainer;
    }
    if (container instanceof PyFunction) {
        return new LocalSearchScope(container);
    }
    return super.getUseScope();
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) NotNull(org.jetbrains.annotations.NotNull)

Example 90 with SearchScope

use of com.intellij.psi.search.SearchScope in project intellij-plugins by JetBrains.

the class CucumberUtil method findPossibleGherkinElementUsages.

/**
   * Passes to {@link com.intellij.psi.search.TextOccurenceProcessor} all elements in gherkin files that <em>may</em> have reference to
   * provided argument. I.e: calling this function for string literal "(.+)foo" would find step "Given I am foo".
   * To extract search text, {@link #getTheBiggestWordToSearchByIndex(String)} is used.
   *
   * @param stepDefinitionElement step defining element to search refs for.
   * @param regexp                regexp step should match
   * @param processor             each text occurence would be reported here
   * @param effectiveSearchScope  search scope
   * @return whether reference was found and passed to processor
   * @see #findGherkinReferencesToElement(com.intellij.psi.PsiElement, String, com.intellij.util.Processor, com.intellij.psi.search.SearchScope)
   */
public static boolean findPossibleGherkinElementUsages(@NotNull final PsiElement stepDefinitionElement, @NotNull final String regexp, @NotNull final TextOccurenceProcessor processor, @NotNull final SearchScope effectiveSearchScope) {
    final String word = getTheBiggestWordToSearchByIndex(regexp);
    if (StringUtil.isEmptyOrSpaces(word)) {
        return true;
    }
    final SearchScope searchScope = CucumberStepSearchUtil.restrictScopeToGherkinFiles(() -> effectiveSearchScope);
    final short context = (short) (UsageSearchContext.IN_STRINGS | UsageSearchContext.IN_CODE);
    final PsiSearchHelper instance = SERVICE.getInstance(stepDefinitionElement.getProject());
    return instance.processElementsWithWord(processor, searchScope, word, context, true);
}
Also used : PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) SearchScope(com.intellij.psi.search.SearchScope)

Aggregations

SearchScope (com.intellij.psi.search.SearchScope)90 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)39 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)36 Project (com.intellij.openapi.project.Project)21 NotNull (org.jetbrains.annotations.NotNull)21 VirtualFile (com.intellij.openapi.vfs.VirtualFile)18 PsiElement (com.intellij.psi.PsiElement)16 PsiClass (com.intellij.psi.PsiClass)8 Processor (com.intellij.util.Processor)8 com.intellij.psi (com.intellij.psi)6 PsiMethod (com.intellij.psi.PsiMethod)6 HashMap (com.intellij.util.containers.HashMap)6 ClassInheritorsSearch (com.intellij.psi.search.searches.ClassInheritorsSearch)5 Nullable (org.jetbrains.annotations.Nullable)5 AnalysisScope (com.intellij.analysis.AnalysisScope)4 ProgressManager (com.intellij.openapi.progress.ProgressManager)4 TextRange (com.intellij.openapi.util.TextRange)4 PsiFile (com.intellij.psi.PsiFile)4 PsiSearchHelper (com.intellij.psi.search.PsiSearchHelper)4 QueryExecutor (com.intellij.util.QueryExecutor)4