Search in sources :

Example 16 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.

the class GroovyFoldingBuilder method processImports.

private static void processImports(final List<FoldingDescriptor> descriptors, GrImportStatement[] imports) {
    if (imports.length < 2)
        return;
    PsiElement first = imports[0];
    while (first != null) {
        PsiElement marker = first;
        PsiElement next = first.getNextSibling();
        while (next instanceof GrImportStatement || next instanceof LeafPsiElement) {
            if (next instanceof GrImportStatement)
                marker = next;
            next = next.getNextSibling();
        }
        if (marker != first) {
            int start = first.getTextRange().getStartOffset();
            int end = marker.getTextRange().getEndOffset();
            int tail = "import ".length();
            if (start + tail < end && !JavaFoldingBuilderBase.hasErrorElementsNearby(first.getContainingFile(), start, end)) {
                FoldingDescriptor descriptor = new FoldingDescriptor(first.getNode(), new TextRange(start + tail, end));
                // 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);
            }
        }
        while (!(next instanceof GrImportStatement) && next != null) next = next.getNextSibling();
        first = next;
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) NamedFoldingDescriptor(com.intellij.lang.folding.NamedFoldingDescriptor) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) TextRange(com.intellij.openapi.util.TextRange) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 17 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.

the class GroovyFoldingBuilder method collapseBlock.

private static void collapseBlock(List<FoldingDescriptor> descriptors, PsiElement psi) {
    if (psi instanceof GrCodeBlock) {
        final int lineFeedCount = StringUtil.countChars(psi.getText(), '\n');
        if (lineFeedCount <= 2) {
            final PsiElement lbrace = ((GrCodeBlock) psi).getLBrace();
            final PsiElement rbrace = ((GrCodeBlock) psi).getRBrace();
            if (lbrace != null && rbrace != null) {
                final PsiElement next = lbrace.getNextSibling();
                final PsiElement prev = rbrace.getPrevSibling();
                if (next != null && PsiImplUtil.isWhiteSpaceOrNls(next) && prev != null && PsiImplUtil.isWhiteSpaceOrNls(prev)) {
                    final FoldingGroup group = FoldingGroup.newGroup("block_group");
                    descriptors.add(new NamedFoldingDescriptor(psi, lbrace.getTextRange().getStartOffset(), next.getTextRange().getEndOffset(), group, "{"));
                    descriptors.add(new NamedFoldingDescriptor(psi, prev.getTextRange().getStartOffset(), rbrace.getTextRange().getEndOffset(), group, "}"));
                    return;
                }
            }
        }
    }
    descriptors.add(new FoldingDescriptor(psi, psi.getTextRange()));
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) NamedFoldingDescriptor(com.intellij.lang.folding.NamedFoldingDescriptor) FoldingGroup(com.intellij.openapi.editor.FoldingGroup) NamedFoldingDescriptor(com.intellij.lang.folding.NamedFoldingDescriptor) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 18 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.

the class GroovyFoldingBuilder method appendDescriptors.

private void appendDescriptors(PsiElement element, List<FoldingDescriptor> descriptors, Set<PsiElement> usedComments) {
    ASTNode node = element.getNode();
    if (node == null)
        return;
    IElementType type = node.getElementType();
    if (TokenSets.BLOCK_SET.contains(type) && !isSingleHighLevelClassBody(element) || type == GroovyElementTypes.CLOSABLE_BLOCK) {
        if (isMultiline(element)) {
            collapseBlock(descriptors, element);
        }
    }
    // comments
    if ((type.equals(GroovyTokenTypes.mML_COMMENT) || type.equals(GroovyDocElementTypes.GROOVY_DOC_COMMENT)) && isMultiline(element) && isWellEndedComment(element)) {
        descriptors.add(new FoldingDescriptor(node, node.getTextRange()));
    }
    if (type.equals(GroovyTokenTypes.mSL_COMMENT) && !usedComments.contains(element)) {
        boolean containsCustomRegionMarker = isCustomRegionElement(element);
        usedComments.add(element);
        PsiElement end = null;
        for (PsiElement current = element.getNextSibling(); current != null; current = current.getNextSibling()) {
            if (PsiImplUtil.isWhiteSpaceOrNls(current))
                continue;
            IElementType elementType = current.getNode().getElementType();
            if (elementType == GroovyTokenTypes.mSL_COMMENT) {
                end = current;
                usedComments.add(current);
                containsCustomRegionMarker |= isCustomRegionElement(current);
                continue;
            }
            break;
        }
        if (end != null && !containsCustomRegionMarker) {
            final TextRange range = new TextRange(element.getTextRange().getStartOffset(), end.getTextRange().getEndOffset());
            descriptors.add(new FoldingDescriptor(element, range));
        }
    }
    //multiline strings
    addFoldingForStrings(descriptors, node);
    Set<PsiElement> newUsedComments = new HashSet<>();
    for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        appendDescriptors(child, descriptors, newUsedComments);
    }
    if (element instanceof GroovyFile) {
        processImports(descriptors, ((GroovyFile) element).getImportStatements());
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) NamedFoldingDescriptor(com.intellij.lang.folding.NamedFoldingDescriptor) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) HashSet(com.intellij.util.containers.hash.HashSet)

Example 19 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project android by JetBrains.

the class ResourceFoldingBuilder method createdInlinedResource.

private static InlinedResource createdInlinedResource(@NotNull ResourceType type, @NotNull String name, @NotNull PsiElement foldElement) {
    // Not part of a call: just fold the R.string reference itself
    LocalResourceRepository appResources = getAppResources(foldElement);
    if (appResources != null && appResources.hasResourceItem(type, name)) {
        ASTNode node = foldElement.getNode();
        if (node != null) {
            TextRange textRange = foldElement.getTextRange();
            HashSet<Object> dependencies = new HashSet<Object>();
            dependencies.add(foldElement);
            FoldingDescriptor descriptor = new FoldingDescriptor(node, textRange, null, dependencies);
            InlinedResource inlinedResource = new InlinedResource(type, name, appResources, descriptor, foldElement);
            dependencies.add(inlinedResource);
            return inlinedResource;
        }
    }
    return NONE;
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) HashSet(java.util.HashSet)

Example 20 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project intellij-plugins by JetBrains.

the class CfmlFoldingBuilder method addFoldingDescriptors.

private static void addFoldingDescriptors(final List<FoldingDescriptor> descriptors, final PsiElement tag, @NotNull Document document) {
    TextRange elementRange = tag.getTextRange();
    final int start = elementRange.getStartOffset();
    final int end = elementRange.getEndOffset();
    if (start + 1 < end) {
        TextRange range = null;
        ASTNode astNode = tag.getNode();
        IElementType astType = astNode.getElementType();
        if (tag instanceof CfmlTag) {
            //if (tag instanceof CfmlTagFunctionImpl || tag instanceof CfmlTagComponentImpl || tag instanceof CfmlTagScriptImpl) {
            range = buildRangeForBraces(range, astNode, CfmlTokenTypes.R_ANGLEBRACKET, CfmlTokenTypes.LSLASH_ANGLEBRACKET);
        //}
        } else if (astType == CfmlElementTypes.FUNCTIONBODY || astType == CfmlElementTypes.BLOCK_OF_STATEMENTS) {
            range = buildRange(range, start, end);
        } else if (astType == CfmlElementTypes.SWITCHEXPRESSION) {
            ASTNode lparen = astNode.findChildByType(CfscriptTokenTypes.L_CURLYBRACKET);
            ASTNode rparen = astNode.findChildByType(CfscriptTokenTypes.R_CURLYBRACKET);
            if (lparen != null && rparen != null) {
                range = buildRange(range, lparen.getStartOffset(), rparen.getTextRange().getEndOffset());
            }
        } else if (tag instanceof PsiComment) {
            boolean isColdFusionComment = astNode.getElementType() == CfmlTokenTypes.COMMENT;
            int endIndex = astNode.getText().lastIndexOf(isColdFusionComment ? "--->" : "*/");
            if (endIndex != -1) {
                String commentText = astNode.getText().substring(0, endIndex);
                if (commentText.contains("\n")) {
                    int startOffset = tag.getTextRange().getStartOffset();
                    range = buildRange(range, startOffset + (isColdFusionComment ? "<!---" : "/*").length(), startOffset + commentText.length());
                }
            }
        }
        if (range != null) {
            descriptors.add(new FoldingDescriptor(astNode, range));
        }
        // TODO: insert condition
        addFoldingDescriptorsFromChildren(descriptors, tag, document);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiComment(com.intellij.psi.PsiComment) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) CfmlTag(com.intellij.coldFusion.model.psi.CfmlTag)

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