TextRange (@Nullable XmlToken tagName, IElementType tagStart) { if (tagName == null) { return null; } PsiElement s = tagName.getPrevSibling(); while (s != null && s.getNode().getElementType() != tagStart) { s = s.getPrevSibling(); } PsiElement f = tagName.getNextSibling(); while (f != null && !(f.getNode().getElementType() == XmlTokenType.XML_TAG_END || f.getNode().getElementType() == XmlTokenType.XML_EMPTY_ELEMENT_END)) { f = f.getNextSibling(); } if (s != null && f != null) { return new TextRange(s.getTextRange().getStartOffset(), f.getTextRange().getEndOffset()); } return null; }