PsiElement (@Nullable final PsiElement sourceElement, final Editor editor) { if (sourceElement == null) { return null; } final PyResolveContext context = PyResolveContext.defaultContext(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) { referenceOwner = (PyReferenceOwner)parent; //Reference expression may be parent of IDENTIFIER } if (referenceOwner != null) { PsiElement resolved = PyResolveUtil.resolveDeclaration(referenceOwner.getReference(context), context); if (resolved != null && resolved.getContainingFile() instanceof PyiFile) { return ObjectUtils.chooseNotNull(PyiUtil.getOriginalElement((PyElement)resolved), resolved); } return resolved != referenceOwner ? resolved : null; } // 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; }