ASTNode (@NotNull PsiFile file, int startOffset, int endOffset, @NotNull IElementType elementType) { PsiElement element1 = file.getViewProvider().findElementAt(startOffset, JavaLanguage.INSTANCE); PsiElement element2 = file.getViewProvider().findElementAt(endOffset - 1, JavaLanguage.INSTANCE); if (element1 instanceof PsiWhiteSpace) { startOffset = element1.getTextRange().getEndOffset(); element1 = file.getViewProvider().findElementAt(startOffset, JavaLanguage.INSTANCE); } if (element2 instanceof PsiWhiteSpace) { endOffset = element2.getTextRange().getStartOffset(); element2 = file.getViewProvider().findElementAt(endOffset - 1, JavaLanguage.INSTANCE); } if (element2 == null || element1 == null) return null; final PsiElement commonParent = PsiTreeUtil.findCommonParent(element1, element2); if (commonParent == null) { return null; } ASTNode node = commonParent.getNode(); final ASTNode element = is(node, elementType) ? node : getParentOfType(node, elementType); if (element == null || element.getTextRange().getStartOffset() != startOffset || element.getTextRange().getEndOffset() != endOffset) { return null; } return element; }