PsiElement (@NotNull PsiElement element, int startOffset, int endOffset) { PsiElement anchor = AbstractFileViewProvider.findElementAt(element, startOffset); // finds child in this tree only, unlike PsiElement.findElementAt() if (anchor == null && startOffset == element.getTextLength()) { anchor = PsiTreeUtil.getDeepestLast(element); } if (anchor == null) return null; PsiElement result = findParent(startOffset, endOffset, anchor); if (endOffset == startOffset) { while ((result == null || result.getTextRange().getStartOffset() != startOffset) && anchor.getTextRange().getStartOffset() == endOffset) { anchor = PsiTreeUtil.prevLeaf(anchor, false); if (anchor == null) break; result = findParent(startOffset, endOffset, anchor); } } return result; }