Search in sources :

Example 1 with PyReferenceOwner

use of com.jetbrains.python.psi.PyReferenceOwner in project intellij-community by JetBrains.

the class PyGotoDeclarationHandler method getGotoDeclarationTarget.

@Nullable
@Override
public PsiElement getGotoDeclarationTarget(@Nullable final PsiElement sourceElement, final Editor editor) {
    if (sourceElement == null) {
        return null;
    }
    final PyResolveContext context = PyResolveContext.noImplicits().withTypeEvalContext(TypeEvalContext.userInitiated(sourceElement.getProject(), sourceElement.getContainingFile()));
    PyReferenceOwner referenceOwner = null;
    final PsiElement parent = sourceElement.getParent();
    if (sourceElement instanceof PyReferenceOwner) {
        referenceOwner = (PyReferenceOwner) sourceElement;
    } else if (parent instanceof PyReferenceOwner) {
        //Reference expression may be parent of IDENTIFIER
        referenceOwner = (PyReferenceOwner) parent;
    }
    if (referenceOwner != null) {
        return referenceOwner.getReference(context).resolve();
    }
    // If element is not ref owner, it still may have provided references, lets find some
    final PsiElement element = findProvidedReferenceAndResolve(sourceElement);
    if (element != null) {
        return element;
    }
    if (parent != null) {
        return findProvidedReferenceAndResolve(parent);
    }
    return null;
}
Also used : PyReferenceOwner(com.jetbrains.python.psi.PyReferenceOwner) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PyReferenceOwner

use of com.jetbrains.python.psi.PyReferenceOwner in project intellij-community by JetBrains.

the class PyTargetElementEvaluator method getElementByReference.

@Nullable
@Override
public PsiElement getElementByReference(@NotNull PsiReference ref, int flags) {
    if ((flags & TargetElementUtil.ELEMENT_NAME_ACCEPTED) == 0) {
        return null;
    }
    final PsiElement element = ref.getElement();
    PsiElement result = ref.resolve();
    Set<PsiElement> visited = new HashSet<>();
    visited.add(result);
    while (result instanceof PyReferenceOwner && (result instanceof PyReferenceExpression || result instanceof PyTargetExpression)) {
        PsiElement nextResult = ((PyReferenceOwner) result).getReference(PyResolveContext.noImplicits()).resolve();
        if (nextResult != null && !visited.contains(nextResult) && PsiTreeUtil.getParentOfType(element, ScopeOwner.class) == PsiTreeUtil.getParentOfType(result, ScopeOwner.class) && (nextResult instanceof PyReferenceExpression || nextResult instanceof PyTargetExpression || nextResult instanceof PyParameter)) {
            visited.add(nextResult);
            result = nextResult;
        } else {
            break;
        }
    }
    return result;
}
Also used : PyReferenceOwner(com.jetbrains.python.psi.PyReferenceOwner) PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) PyReferenceExpression(com.jetbrains.python.psi.PyReferenceExpression) PyParameter(com.jetbrains.python.psi.PyParameter) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiElement (com.intellij.psi.PsiElement)2 PyReferenceOwner (com.jetbrains.python.psi.PyReferenceOwner)2 Nullable (org.jetbrains.annotations.Nullable)2 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)1 PyParameter (com.jetbrains.python.psi.PyParameter)1 PyReferenceExpression (com.jetbrains.python.psi.PyReferenceExpression)1 PyTargetExpression (com.jetbrains.python.psi.PyTargetExpression)1 PyResolveContext (com.jetbrains.python.psi.resolve.PyResolveContext)1 HashSet (java.util.HashSet)1