PsiElement child = psiElement.getFirstChild();
while (child != null) {
if (child instanceof PsiComment) {
String text = child.getText();
if (text.startsWith("/*") && text.indexOf('\n') > -1) {
FoldingDescriptor foldingDescriptor = new FoldingDescriptor(
child.getNode(),
child.getTextRange());
foldingDescriptors.add(foldingDescriptor);
}
}
if (child instanceof BasePsiElement) {
BasePsiElement basePsiElement = (BasePsiElement) child;
ElementType elementType = basePsiElement.getElementType();
int blockEndOffset = basePsiElement.getTextOffset() + basePsiElement.getTextLength();
boolean folded = false;
if (elementType.is(ElementTypeAttribute.FOLDABLE_BLOCK)) {
BasePsiElement subjectPsiElement = basePsiElement.lookupFirstPsiElement(ElementTypeAttribute.SUBJECT);
if (subjectPsiElement == null) {
PsiElement firstChild = basePsiElement.getFirstChild();
if (firstChild instanceof TokenPsiElement) {
subjectPsiElement = (BasePsiElement) firstChild;
}
}
if (subjectPsiElement != null && subjectPsiElement.getParent() == basePsiElement) {
int subjectEndOffset = subjectPsiElement.getTextOffset() + subjectPsiElement.getTextLength();
int subjectLineNumber = document.getLineNumber(subjectEndOffset);
int blockEndOffsetLineNumber = document.getLineNumber(blockEndOffset);
if (subjectLineNumber < blockEndOffsetLineNumber) {
TextRange textRange = new TextRange(subjectEndOffset, blockEndOffset);
FoldingDescriptor foldingDescriptor = new FoldingDescriptor(basePsiElement.getNode(), textRange);
foldingDescriptors.add(foldingDescriptor);
nestingIndex++;
folded = true;
}
}
}
if (!folded && elementType.is(ElementTypeAttribute.STATEMENT)) {
if (basePsiElement.containsLineBreaks()) {
TextRange textRange = null;
BasePsiElement firstPsiElement = basePsiElement.lookupFirstLeafPsiElement();
int firstElementEndOffset = firstPsiElement.getTextOffset() + firstPsiElement.getTextLength();
int firstElementLineNumber = document.getLineNumber(firstElementEndOffset);
BasePsiElement subjectPsiElement = basePsiElement.lookupFirstPsiElement(ElementTypeAttribute.SUBJECT);
if (subjectPsiElement != null && subjectPsiElement.getParent() == basePsiElement) {
int subjectEndOffset = subjectPsiElement.getTextOffset() + subjectPsiElement.getTextLength();
int subjectLineNumber = document.getLineNumber(subjectEndOffset);
if (subjectLineNumber == firstElementLineNumber) {
textRange = new TextRange(subjectEndOffset, blockEndOffset);
}
}
if (textRange == null) {
textRange = new TextRange(firstElementEndOffset, blockEndOffset);
}
FoldingDescriptor foldingDescriptor = new FoldingDescriptor(basePsiElement.getNode(), textRange);
foldingDescriptors.add(foldingDescriptor);
nestingIndex++;
}
}