PsiElement (@NotNull final PsiElement element) { final IElementType elementType = element.getNode().getElementType(); PsiElement nextSibling = element.getNextSibling(); PsiElement result = element; while (nextSibling != null && (nextSibling.getNode().getElementType().equals(elementType) || (atWhitespaceToken(nextSibling) && StringUtil.countChars(nextSibling.getText(), '\n') <= 1))) { String text = nextSibling.getText(); final String prefix = getPrefix(element); final String postfix = getPostfix(element); text = StringUtil.trimStart(text.trim(), prefix.trim()); text = StringUtil.trimEnd(text, postfix); if (nextSibling.getNode().getElementType().equals(elementType) && StringUtil.isEmptyOrSpaces(text)) { break; } if (nextSibling.getNode().getElementType().equals(elementType)) { result = nextSibling; } nextSibling = nextSibling.getNextSibling(); } return result; }