PsiElement (@NotNull PsiElement start, @NotNull PsiElement end) { if (start.getContainingFile() != end.getContainingFile()) { return null; } final PsiElement parent = PsiTreeUtil.findCommonParent(start, end); if (parent == null) { return null; } final TextRange parentRange = parent.getTextRange(); if (parentRange.getStartOffset() == start.getTextRange().getStartOffset() && parentRange.getEndOffset() == end.getTextRange().getEndOffset()) { return parent; } return null; }