Search in sources :

Example 36 with FoldingDescriptor

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())));
                }
            }
        }
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement)

Example 37 with FoldingDescriptor

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;
}
Also used : PsiComment(com.intellij.psi.PsiComment) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 38 with FoldingDescriptor

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);
    }
}
Also used : PsiComment(com.intellij.psi.PsiComment) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 39 with FoldingDescriptor

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()));
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor)

Aggregations

FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)39 TextRange (com.intellij.openapi.util.TextRange)24 ASTNode (com.intellij.lang.ASTNode)17 PsiElement (com.intellij.psi.PsiElement)12 IElementType (com.intellij.psi.tree.IElementType)9 NamedFoldingDescriptor (com.intellij.lang.folding.NamedFoldingDescriptor)8 UnfairTextRange (com.intellij.openapi.util.UnfairTextRange)6 PsiComment (com.intellij.psi.PsiComment)5 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)5 FoldingGroup (com.intellij.openapi.editor.FoldingGroup)4 NotNull (org.jetbrains.annotations.NotNull)4 FoldRegion (com.intellij.openapi.editor.FoldRegion)3 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)3 LinkedList (java.util.LinkedList)3 HbBlockWrapper (com.dmarcotte.handlebars.psi.HbBlockWrapper)2 FoldingBuilder (com.intellij.lang.folding.FoldingBuilder)2 ArrayList (java.util.ArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)1 BraceMatcher (com.intellij.codeInsight.highlighting.BraceMatcher)1