use of com.intellij.lang.folding.FoldingDescriptor in project intellij-plugins by JetBrains.
the class DartFoldingBuilder method foldMapLiterals.
private static void foldMapLiterals(@NotNull final List<FoldingDescriptor> descriptors, @NotNull final Collection<PsiElement> psiElements) {
for (PsiElement psiElement : psiElements) {
if (psiElement instanceof DartMapLiteralExpression) {
final ASTNode node = psiElement.getNode();
final ASTNode lBrace = node.findChildByType(DartTokenTypes.LBRACE);
final ASTNode rBrace = lBrace == null ? null : node.findChildByType(DartTokenTypes.RBRACE, lBrace);
if (lBrace != null && rBrace != null) {
final String text = node.getText().substring(lBrace.getStartOffset() - node.getStartOffset(), rBrace.getStartOffset() - node.getStartOffset());
if (text.contains("\n")) {
descriptors.add(new FoldingDescriptor(psiElement, TextRange.create(lBrace.getStartOffset(), rBrace.getStartOffset() + rBrace.getTextLength())));
}
}
}
}
}
use of com.intellij.lang.folding.FoldingDescriptor in project intellij-plugins by JetBrains.
the class DartFoldingBuilder method foldFileHeader.
@Nullable
private static TextRange foldFileHeader(@NotNull final List<FoldingDescriptor> descriptors, @NotNull final DartFile dartFile, @NotNull final Document document) {
PsiElement firstComment = dartFile.getFirstChild();
if (firstComment instanceof PsiWhiteSpace)
firstComment = firstComment.getNextSibling();
if (!(firstComment instanceof PsiComment))
return null;
boolean containsCustomRegionMarker = false;
PsiElement nextAfterComments = firstComment;
while (nextAfterComments instanceof PsiComment || nextAfterComments instanceof PsiWhiteSpace) {
containsCustomRegionMarker |= isCustomRegionElement(nextAfterComments);
nextAfterComments = nextAfterComments.getNextSibling();
}
if (nextAfterComments == null)
return null;
if (nextAfterComments instanceof DartLibraryStatement || nextAfterComments instanceof DartPartStatement || nextAfterComments instanceof DartPartOfStatement || nextAfterComments instanceof DartImportOrExportStatement) {
if (nextAfterComments.getPrevSibling() instanceof PsiWhiteSpace)
nextAfterComments = nextAfterComments.getPrevSibling();
if (nextAfterComments.equals(firstComment))
return null;
final TextRange fileHeaderCommentsRange = new UnfairTextRange(firstComment.getTextRange().getStartOffset(), nextAfterComments.getTextRange().getStartOffset());
if (fileHeaderCommentsRange.getLength() > 1 && document.getLineNumber(fileHeaderCommentsRange.getEndOffset()) > document.getLineNumber(fileHeaderCommentsRange.getStartOffset())) {
if (!containsCustomRegionMarker) {
descriptors.add(new FoldingDescriptor(dartFile, fileHeaderCommentsRange));
}
return fileHeaderCommentsRange;
}
}
return null;
}
use of com.intellij.lang.folding.FoldingDescriptor in project intellij-plugins by JetBrains.
the class DartFoldingBuilder method foldConsequentStatements.
private static <T extends PsiElement> void foldConsequentStatements(@NotNull final List<FoldingDescriptor> descriptors, @NotNull final DartFile dartFile, @NotNull final Class<T> aClass) {
final T firstStatement = PsiTreeUtil.getChildOfType(dartFile, aClass);
if (firstStatement == null)
return;
PsiElement lastStatement = firstStatement;
PsiElement nextElement = firstStatement;
while (aClass.isInstance(nextElement) || nextElement instanceof PsiComment || nextElement instanceof PsiWhiteSpace) {
if (aClass.isInstance(nextElement)) {
lastStatement = nextElement;
}
nextElement = nextElement.getNextSibling();
}
if (lastStatement != firstStatement) {
// after "import " or "export " or "part "
final int startOffset = firstStatement.getTextRange().getStartOffset() + firstStatement.getFirstChild().getTextLength() + 1;
final int endOffset = lastStatement.getTextRange().getEndOffset();
final FoldingDescriptor descriptor = new FoldingDescriptor(firstStatement, TextRange.create(startOffset, endOffset));
if (aClass == DartImportOrExportStatement.class) {
// imports are often added/removed automatically, so we enable autoupdate of folded region for foldings even if it's collapsed
descriptor.setCanBeRemovedWhenCollapsed(true);
}
descriptors.add(descriptor);
}
}
use of com.intellij.lang.folding.FoldingDescriptor in project intellij-plugins by JetBrains.
the class DartFoldingBuilder method foldFunctionBody.
private static void foldFunctionBody(@NotNull final List<FoldingDescriptor> descriptors, @NotNull final PsiElement dartComponentOrOperatorDeclaration) {
final DartFunctionBody functionBody = PsiTreeUtil.getChildOfType(dartComponentOrOperatorDeclaration, DartFunctionBody.class);
final IDartBlock block = functionBody == null ? null : functionBody.getBlock();
if (block != null && block.getTextLength() > 2) {
descriptors.add(new FoldingDescriptor(functionBody, block.getTextRange()));
}
}
Aggregations