Search in sources :

Example 31 with FoldingDescriptor

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

the class PythonFoldingBuilder method foldLongStrings.

private static void foldLongStrings(ASTNode node, List<FoldingDescriptor> descriptors) {
    //don't want to fold docstrings like """\n string \n """
    boolean shouldFoldDocString = getDocStringOwnerType(node) != null && StringUtil.countNewLines(node.getChars()) > 1;
    boolean shouldFoldString = getDocStringOwnerType(node) == null && StringUtil.countNewLines(node.getChars()) > 0;
    if (shouldFoldDocString || shouldFoldString) {
        descriptors.add(new FoldingDescriptor(node, node.getTextRange()));
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor)

Example 32 with FoldingDescriptor

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

the class PythonFoldingBuilder method foldStatementList.

private static void foldStatementList(ASTNode node, List<FoldingDescriptor> descriptors) {
    IElementType elType = node.getTreeParent().getElementType();
    if (elType == PyElementTypes.FUNCTION_DECLARATION || elType == PyElementTypes.CLASS_DECLARATION || ifFoldBlocks(node, elType)) {
        ASTNode colon = node.getTreeParent().findChildByType(PyTokenTypes.COLON);
        if (colon != null && colon.getStartOffset() + 1 < node.getTextRange().getEndOffset() - 1) {
            final CharSequence chars = node.getChars();
            int nodeStart = node.getTextRange().getStartOffset();
            int endOffset = node.getTextRange().getEndOffset();
            while (endOffset > colon.getStartOffset() + 2 && endOffset > nodeStart && Character.isWhitespace(chars.charAt(endOffset - nodeStart - 1))) {
                endOffset--;
            }
            descriptors.add(new FoldingDescriptor(node, new TextRange(colon.getStartOffset() + 1, endOffset)));
        } else {
            TextRange range = node.getTextRange();
            if (range.getStartOffset() < range.getEndOffset() - 1) {
                // only for ranges at least 1 char wide
                descriptors.add(new FoldingDescriptor(node, range));
            }
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange)

Example 33 with FoldingDescriptor

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

the class ResourceFoldingBuilder method buildFoldRegions.

@Override
@NotNull
public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement element, @NotNull Document document, boolean quick) {
    if (!(element instanceof PsiJavaFile || element instanceof XmlFile) || quick && !UNIT_TEST_MODE || !isFoldingEnabled()) {
        return FoldingDescriptor.EMPTY;
    }
    final List<FoldingDescriptor> result = new ArrayList<FoldingDescriptor>();
    if (element instanceof PsiJavaFile) {
        final PsiJavaFile file = (PsiJavaFile) element;
        file.accept(new JavaRecursiveElementWalkingVisitor() {

            @Override
            public void visitReferenceExpression(PsiReferenceExpression expression) {
                InlinedResource inlinedResource = findJavaExpressionReference(expression);
                if (inlinedResource != NONE) {
                    result.add(inlinedResource.getDescriptor());
                }
                super.visitReferenceExpression(expression);
            }
        });
    } else {
        final XmlFile file = (XmlFile) element;
        file.accept(new XmlRecursiveElementVisitor() {

            @Override
            public void visitXmlAttributeValue(XmlAttributeValue value) {
                InlinedResource inlinedResource = findXmlValueReference(value);
                if (inlinedResource != NONE) {
                    FoldingDescriptor descriptor = inlinedResource.getDescriptor();
                    if (descriptor != null) {
                        result.add(descriptor);
                    }
                }
                super.visitXmlAttributeValue(value);
            }
        });
    }
    return result.toArray(new FoldingDescriptor[result.size()]);
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) XmlFile(com.intellij.psi.xml.XmlFile) ArrayList(java.util.ArrayList) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) NotNull(org.jetbrains.annotations.NotNull)

Example 34 with FoldingDescriptor

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

the class GherkinFoldingBuilder method appendDescriptors.

private void appendDescriptors(ASTNode node, List<FoldingDescriptor> descriptors) {
    if (BLOCKS_TO_FOLD.contains(node.getElementType()) && node.getTextRange().getLength() >= 2) {
        descriptors.add(new FoldingDescriptor(node, node.getTextRange()));
    }
    ASTNode child = node.getFirstChildNode();
    while (child != null) {
        appendDescriptors(child, descriptors);
        child = child.getTreeNext();
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) ASTNode(com.intellij.lang.ASTNode)

Example 35 with FoldingDescriptor

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

the class CfmlFoldingBuilder method buildFoldRegions.

@NotNull
public FoldingDescriptor[] buildFoldRegions(@NotNull ASTNode node, @NotNull Document document) {
    final PsiElement element = node.getPsi();
    if (element instanceof CfmlFile) {
        final CfmlFile file = (CfmlFile) element;
        final PsiElement[] children = file.getChildren();
        Collection<FoldingDescriptor> result = new LinkedList<>();
        for (PsiElement child : children) {
            if (child != null && (child instanceof CfmlCompositeElement || child instanceof PsiComment)) {
                List<FoldingDescriptor> descriptors = new ArrayList<>();
                addFoldingDescriptors(descriptors, child, document);
                result.addAll(descriptors);
            }
        }
        return result.toArray(FoldingDescriptor.EMPTY);
    }
    return FoldingDescriptor.EMPTY;
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) PsiComment(com.intellij.psi.PsiComment) CfmlFile(com.intellij.coldFusion.model.files.CfmlFile) ArrayList(java.util.ArrayList) CfmlCompositeElement(com.intellij.coldFusion.model.psi.CfmlCompositeElement) PsiElement(com.intellij.psi.PsiElement) LinkedList(java.util.LinkedList) NotNull(org.jetbrains.annotations.NotNull)

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