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;
}
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;
}
Aggregations