PsiElement (List<? extends PsiElement> occurrences, Editor editor) { if (occurrences.isEmpty()) { return null; } int offset = editor.getCaretModel().getOffset(); for (PsiElement occurrence : occurrences) { if (occurrence != null && occurrence.getTextRange().contains(offset)) { return occurrence; } } int line = editor.getDocument().getLineNumber(offset); for (PsiElement occurrence : occurrences) { PyPsiUtils.assertValid(occurrence); if (occurrence.isValid() && editor.getDocument().getLineNumber(occurrence.getTextRange().getStartOffset()) == line) { return occurrence; } } for (PsiElement occurrence : occurrences) { PyPsiUtils.assertValid(occurrence); return occurrence; } return null; }