PyFunction (@NotNull PsiElement elementAt) { final PyFunction parentFunction = PsiTreeUtil.getParentOfType(elementAt, PyFunction.class); if (parentFunction != null) { final ASTNode nameNode = parentFunction.getNameNode(); if (nameNode != null) { final PsiElement prev = elementAt.getContainingFile().findElementAt(elementAt.getTextOffset()-1); if (nameNode.getPsi() == elementAt || nameNode.getPsi() == prev) { return parentFunction; } } } return null; }