Search in sources :

Example 91 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 92 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 93 with SearchScope

use of com.intellij.psi.search.SearchScope in project oxy-template-support-plugin by mutant-industries.

the class GlobalVariableReferenceSearch method processQuery.

@Override
public void processQuery(@NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
    if (!(queryParameters.getElementToSearch() instanceof GlobalVariableDefinition)) {
        return;
    }
    final GlobalVariableDefinition target = (GlobalVariableDefinition) queryParameters.getElementToSearch();
    SearchScope scope = restrictScopeToOxyTemplates(queryParameters.getEffectiveSearchScope());
    queryParameters.getOptimizer().searchWord(target.getName(), scope, UsageSearchContext.IN_CODE, true, target.getExpression(), new RequestResultProcessor() {

        @Override
        public boolean processTextOccurrence(@NotNull PsiElement element, int offsetInElement, @NotNull Processor<PsiReference> consumer) {
            PsiReference reference;
            if (element instanceof JSReferenceExpression && (reference = element.getReference()) != null) {
                PsiElement resolveResult = reference.resolve();
                if (resolveResult instanceof GlobalVariableDefinition && target.getExpression().isEquivalentTo(((GlobalVariableDefinition) resolveResult).getExpression())) {
                    return consumer.process(reference);
                }
            }
            return true;
        }
    });
}
Also used : JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) GlobalVariableDefinition(ool.intellij.plugin.psi.reference.innerjs.globals.GlobalVariableDefinition) SearchScope(com.intellij.psi.search.SearchScope) PsiReference(com.intellij.psi.PsiReference) RequestResultProcessor(com.intellij.psi.search.RequestResultProcessor) PsiElement(com.intellij.psi.PsiElement)

Example 94 with SearchScope

use of com.intellij.psi.search.SearchScope in project oxy-template-support-plugin by mutant-industries.

the class JavaMacroReferenceSearch method processQuery.

@Override
public void processQuery(@NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
    final PsiElement target = queryParameters.getElementToSearch();
    PsiClass psiClass;
    if (!(target instanceof PsiClass) || !OxyTemplateIndexUtil.isMacro(psiClass = (PsiClass) target)) {
        return;
    }
    SearchScope scope = restrictScopeToOxyTemplates(queryParameters.getEffectiveSearchScope());
    assert psiClass.getName() != null;
    final String query = StringUtil.decapitalize(psiClass.getName().replaceFirst(MacroIndex.JAVA_MACRO_SUFFIX + "$", ""));
    queryParameters.getOptimizer().searchWord(query, scope, UsageSearchContext.IN_CODE, true, target);
}
Also used : PsiClass(com.intellij.psi.PsiClass) SearchScope(com.intellij.psi.search.SearchScope) PsiElement(com.intellij.psi.PsiElement)

Example 95 with SearchScope

use of com.intellij.psi.search.SearchScope in project oxy-template-support-plugin by mutant-industries.

the class JavaGetterReferenceSearch method doSearch.

static void doSearch(@NotNull final PsiMethod method, @NotNull final SearchRequestCollector optimizer, SearchScope effectiveSearchScope) {
    if (!method.getModifierList().hasModifierProperty(PsiModifier.PUBLIC)) {
        return;
    }
    String methodName = method.getName();
    if (!methodName.matches("((^is)|(^get))[A-Z].*")) {
        return;
    }
    String query = StringUtils.decapitalize(methodName.replaceFirst("(^is)|(^get)", ""));
    SearchScope scope = OxyTemplateReferenceSearch.restrictScopeToOxyTemplates(effectiveSearchScope);
    optimizer.searchWord(query, scope, UsageSearchContext.IN_CODE, true, method, new RequestResultProcessor() {

        @Override
        public boolean processTextOccurrence(@NotNull PsiElement element, int offsetInElement, @NotNull Processor<PsiReference> consumer) {
            PsiElement reference;
            return !((element instanceof JSReferenceExpression && (reference = ((JSReferenceExpression) element).resolve()) != null) && reference.isEquivalentTo(method)) || consumer.process((JSReferenceExpression) element);
        }
    });
}
Also used : JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) SearchScope(com.intellij.psi.search.SearchScope) PsiReference(com.intellij.psi.PsiReference) RequestResultProcessor(com.intellij.psi.search.RequestResultProcessor) PsiElement(com.intellij.psi.PsiElement)

Aggregations

SearchScope (com.intellij.psi.search.SearchScope)98 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)42 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)38 NotNull (org.jetbrains.annotations.NotNull)23 PsiElement (com.intellij.psi.PsiElement)22 Project (com.intellij.openapi.project.Project)21 VirtualFile (com.intellij.openapi.vfs.VirtualFile)18 PsiClass (com.intellij.psi.PsiClass)9 Processor (com.intellij.util.Processor)8 com.intellij.psi (com.intellij.psi)7 PsiMethod (com.intellij.psi.PsiMethod)7 PsiReference (com.intellij.psi.PsiReference)6 HashMap (com.intellij.util.containers.HashMap)6 Nullable (org.jetbrains.annotations.Nullable)6 ClassInheritorsSearch (com.intellij.psi.search.searches.ClassInheritorsSearch)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