Search in sources :

Example 1 with CallHierarchyNodeDescriptor

use of com.intellij.ide.hierarchy.call.CallHierarchyNodeDescriptor in project intellij-community by JetBrains.

the class GrCallReferenceProcessor method process.

@Override
public boolean process(@NotNull PsiReference reference, @NotNull JavaCallHierarchyData data) {
    PsiClass originalClass = data.getOriginalClass();
    PsiMethod method = data.getMethod();
    Set<PsiMethod> methodsToFind = data.getMethodsToFind();
    PsiMethod methodToFind = data.getMethodToFind();
    PsiClassType originalType = data.getOriginalType();
    Map<PsiMember, NodeDescriptor> methodToDescriptorMap = data.getResultMap();
    Project project = data.getProject();
    if (reference instanceof GrReferenceExpression) {
        final GrExpression qualifier = ((GrReferenceExpression) reference).getQualifierExpression();
        if (org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isSuperReference(qualifier)) {
            // filter super.foo() call inside foo() and similar cases (bug 8411)
            assert qualifier != null;
            final PsiClass superClass = PsiUtil.resolveClassInType(qualifier.getType());
            if (originalClass == null || originalClass.isInheritor(superClass, true)) {
                return true;
            }
        }
        if (qualifier != null && !methodToFind.hasModifierProperty(PsiModifier.STATIC)) {
            final PsiType qualifierType = qualifier.getType();
            if (qualifierType instanceof PsiClassType && !TypeConversionUtil.isAssignable(qualifierType, originalType) && methodToFind != method) {
                final PsiClass psiClass = ((PsiClassType) qualifierType).resolve();
                if (psiClass != null) {
                    final PsiMethod callee = psiClass.findMethodBySignature(methodToFind, true);
                    if (callee != null && !methodsToFind.contains(callee)) {
                        // skip sibling methods
                        return true;
                    }
                }
            }
        }
    } else {
        if (!(reference instanceof PsiElement)) {
            return true;
        }
        final PsiElement parent = ((PsiElement) reference).getParent();
        if (parent instanceof PsiNewExpression) {
            if (((PsiNewExpression) parent).getClassReference() != reference) {
                return true;
            }
        } else if (parent instanceof GrAnonymousClassDefinition) {
            if (((GrAnonymousClassDefinition) parent).getBaseClassReferenceGroovy() != reference) {
                return true;
            }
        } else {
            return true;
        }
    }
    final PsiElement element = reference.getElement();
    final PsiMember key = CallHierarchyNodeDescriptor.getEnclosingElement(element);
    synchronized (methodToDescriptorMap) {
        CallHierarchyNodeDescriptor d = (CallHierarchyNodeDescriptor) methodToDescriptorMap.get(key);
        if (d == null) {
            d = new CallHierarchyNodeDescriptor(project, (CallHierarchyNodeDescriptor) data.getNodeDescriptor(), element, false, true);
            methodToDescriptorMap.put(key, d);
        } else if (!d.hasReference(reference)) {
            d.incrementUsageCount();
        }
        d.addReference(reference);
    }
    return true;
}
Also used : GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) CallHierarchyNodeDescriptor(com.intellij.ide.hierarchy.call.CallHierarchyNodeDescriptor) NodeDescriptor(com.intellij.ide.util.treeView.NodeDescriptor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Project(com.intellij.openapi.project.Project) CallHierarchyNodeDescriptor(com.intellij.ide.hierarchy.call.CallHierarchyNodeDescriptor)

Example 2 with CallHierarchyNodeDescriptor

use of com.intellij.ide.hierarchy.call.CallHierarchyNodeDescriptor in project kotlin by JetBrains.

the class KotlinCallTreeStructure method createNodeDescriptor.

@Nullable
private static HierarchyNodeDescriptor createNodeDescriptor(Project project, PsiElement element, HierarchyNodeDescriptor parent, boolean navigateToReference, boolean wrapAsLightElements) {
    PsiElement nodeElement = element;
    if (wrapAsLightElements && element instanceof KtElement) {
        nodeElement = CollectionsKt.firstOrNull(LightClassUtilsKt.toLightElements((KtElement) element));
    }
    if (nodeElement == null)
        return null;
    boolean root = (parent == null);
    return nodeElement instanceof KtElement ? new KotlinCallHierarchyNodeDescriptor(project, parent, nodeElement, root, navigateToReference) : new CallHierarchyNodeDescriptor(project, parent, nodeElement, root, navigateToReference);
}
Also used : PsiElement(com.intellij.psi.PsiElement) CallHierarchyNodeDescriptor(com.intellij.ide.hierarchy.call.CallHierarchyNodeDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with CallHierarchyNodeDescriptor

use of com.intellij.ide.hierarchy.call.CallHierarchyNodeDescriptor in project kotlin by JetBrains.

the class KotlinCallTreeStructure method addNodeDescriptorForElement.

protected static void addNodeDescriptorForElement(PsiReference reference, PsiElement element, Map<PsiElement, HierarchyNodeDescriptor> declarationToDescriptorMap, HierarchyNodeDescriptor descriptor, boolean wrapAsLightElements) {
    HierarchyNodeDescriptor d = declarationToDescriptorMap.get(element);
    if (d == null) {
        d = createNodeDescriptor(element.getProject(), element, descriptor, true, wrapAsLightElements);
        if (d == null)
            return;
        declarationToDescriptorMap.put(element, d);
    } else if (d instanceof CallHierarchyNodeDescriptor) {
        ((CallHierarchyNodeDescriptor) d).incrementUsageCount();
    }
    if (d instanceof CallHierarchyNodeDescriptor) {
        ((CallHierarchyNodeDescriptor) d).addReference(reference);
    } else if (d instanceof KotlinCallHierarchyNodeDescriptor) {
        ((KotlinCallHierarchyNodeDescriptor) d).addReference(reference);
    }
}
Also used : HierarchyNodeDescriptor(com.intellij.ide.hierarchy.HierarchyNodeDescriptor) CallHierarchyNodeDescriptor(com.intellij.ide.hierarchy.call.CallHierarchyNodeDescriptor) CallHierarchyNodeDescriptor(com.intellij.ide.hierarchy.call.CallHierarchyNodeDescriptor)

Example 4 with CallHierarchyNodeDescriptor

use of com.intellij.ide.hierarchy.call.CallHierarchyNodeDescriptor in project kotlin by JetBrains.

the class KotlinCalleeMethodsTreeStructure method buildChildren.

@NotNull
@Override
protected Object[] buildChildren(@NotNull HierarchyNodeDescriptor descriptor) {
    PsiElement targetElement = getTargetElement(descriptor);
    // Kotlin class constructor invoked from Java code
    if (targetElement instanceof PsiMethod) {
        PsiMethod psiMethod = (PsiMethod) targetElement;
        if (psiMethod.isConstructor()) {
            PsiClass psiClass = psiMethod.getContainingClass();
            PsiElement navigationElement = psiClass != null ? psiClass.getNavigationElement() : null;
            if (navigationElement instanceof KtClass) {
                return buildChildrenByKotlinTarget(descriptor, (KtElement) navigationElement);
            }
        }
    }
    // Kotlin function or property invoked from Java code
    if (targetElement instanceof KtLightMethod) {
        return buildChildrenByKotlinTarget(descriptor, ((KtLightMethod) targetElement).getKotlinOrigin());
    }
    if (targetElement instanceof KtElement) {
        return buildChildrenByKotlinTarget(descriptor, (KtElement) targetElement);
    }
    CallHierarchyNodeDescriptor javaDescriptor = descriptor instanceof CallHierarchyNodeDescriptor ? (CallHierarchyNodeDescriptor) descriptor : ((KotlinCallHierarchyNodeDescriptor) descriptor).getJavaDelegate();
    return javaTreeStructure.getChildElements(javaDescriptor);
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) KtLightMethod(org.jetbrains.kotlin.asJava.elements.KtLightMethod) PsiElement(com.intellij.psi.PsiElement) CallHierarchyNodeDescriptor(com.intellij.ide.hierarchy.call.CallHierarchyNodeDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

CallHierarchyNodeDescriptor (com.intellij.ide.hierarchy.call.CallHierarchyNodeDescriptor)4 PsiElement (com.intellij.psi.PsiElement)2 HierarchyNodeDescriptor (com.intellij.ide.hierarchy.HierarchyNodeDescriptor)1 NodeDescriptor (com.intellij.ide.util.treeView.NodeDescriptor)1 Project (com.intellij.openapi.project.Project)1 PsiClass (com.intellij.psi.PsiClass)1 PsiMethod (com.intellij.psi.PsiMethod)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 KtLightMethod (org.jetbrains.kotlin.asJava.elements.KtLightMethod)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1 GrAnonymousClassDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition)1