Search in sources :

Example 1 with FoldingDescriptor

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

the class PropertyFoldingBuilder method checkLiteral.

private static void checkLiteral(PsiLiteralExpression expression, List<FoldingDescriptor> result) {
    if (isI18nProperty(expression)) {
        final IProperty property = getI18nProperty(expression);
        final HashSet<Object> set = new HashSet<>();
        set.add(property != null ? property : PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT);
        final String msg = formatI18nProperty(expression, property);
        final PsiElement parent = expression.getParent();
        if (!msg.equals(expression.getText()) && parent instanceof PsiExpressionList && ((PsiExpressionList) parent).getExpressions()[0] == expression) {
            final PsiExpressionList expressions = (PsiExpressionList) parent;
            final int count = JavaI18nUtil.getPropertyValueParamsMaxCount(expression);
            final PsiExpression[] args = expressions.getExpressions();
            if (args.length == 1 + count && parent.getParent() instanceof PsiMethodCallExpression) {
                boolean ok = true;
                for (int i = 1; i < count + 1; i++) {
                    Object value = JavaConstantExpressionEvaluator.computeConstantExpression(args[i], false);
                    if (value == null) {
                        if (!(args[i] instanceof PsiReferenceExpression)) {
                            ok = false;
                            break;
                        }
                    }
                }
                if (ok) {
                    result.add(new FoldingDescriptor(ObjectUtils.assertNotNull(parent.getParent().getNode()), parent.getParent().getTextRange(), null, set));
                    return;
                }
            }
        }
        result.add(new FoldingDescriptor(ObjectUtils.assertNotNull(expression.getNode()), expression.getTextRange(), null, set));
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) IProperty(com.intellij.lang.properties.IProperty)

Example 2 with FoldingDescriptor

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

the class PythonFoldingBuilder method foldCollectionLiteral.

private static void foldCollectionLiteral(ASTNode node, List<FoldingDescriptor> descriptors) {
    if (StringUtil.countNewLines(node.getChars()) > 0) {
        TextRange range = node.getTextRange();
        int delta = node.getElementType() == PyElementTypes.TUPLE_EXPRESSION ? 0 : 1;
        descriptors.add(new FoldingDescriptor(node, TextRange.create(range.getStartOffset() + delta, range.getEndOffset() - delta)));
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) TextRange(com.intellij.openapi.util.TextRange)

Example 3 with FoldingDescriptor

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

the class PythonFoldingBuilder method appendDescriptors.

private static void appendDescriptors(ASTNode node, List<FoldingDescriptor> descriptors) {
    IElementType elementType = node.getElementType();
    if (elementType instanceof PyFileElementType) {
        final List<PyImportStatementBase> imports = ((PyFile) node.getPsi()).getImportBlock();
        if (imports.size() > 1) {
            final PyImportStatementBase firstImport = imports.get(0);
            final PyImportStatementBase lastImport = imports.get(imports.size() - 1);
            descriptors.add(new FoldingDescriptor(firstImport, new TextRange(firstImport.getTextRange().getStartOffset(), lastImport.getTextRange().getEndOffset())));
        }
    } else if (elementType == PyElementTypes.STATEMENT_LIST) {
        foldStatementList(node, descriptors);
    } else if (elementType == PyElementTypes.STRING_LITERAL_EXPRESSION) {
        foldLongStrings(node, descriptors);
    } else if (FOLDABLE_COLLECTIONS_LITERALS.contains(elementType)) {
        foldCollectionLiteral(node, descriptors);
    } else if (elementType == PyTokenTypes.END_OF_LINE_COMMENT) {
        foldSequentialComments(node, descriptors);
    }
    ASTNode child = node.getFirstChildNode();
    while (child != null) {
        appendDescriptors(child, descriptors);
        child = child.getTreeNext();
    }
}
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 4 with FoldingDescriptor

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

the class PythonFoldingBuilder method foldSequentialComments.

private static void foldSequentialComments(ASTNode node, List<FoldingDescriptor> descriptors) {
    //do not start folded comments from custom region
    if (isCustomRegionElement(node.getPsi())) {
        return;
    }
    //need to skip previous comments in sequence
    ASTNode curNode = node.getTreePrev();
    while (curNode != null) {
        if (curNode.getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) {
            if (isCustomRegionElement(curNode.getPsi())) {
                break;
            }
            return;
        }
        curNode = curNode.getPsi() instanceof PsiWhiteSpace ? curNode.getTreePrev() : null;
    }
    //fold sequence comments in one block
    curNode = node.getTreeNext();
    ASTNode lastCommentNode = node;
    while (curNode != null) {
        if (curNode.getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) {
            //do not end folded comments with custom region
            if (isCustomRegionElement(curNode.getPsi())) {
                break;
            }
            lastCommentNode = curNode;
            curNode = curNode.getTreeNext();
            continue;
        }
        curNode = curNode.getPsi() instanceof PsiWhiteSpace ? curNode.getTreeNext() : null;
    }
    if (lastCommentNode != node) {
        descriptors.add(new FoldingDescriptor(node, TextRange.create(node.getStartOffset(), lastCommentNode.getTextRange().getEndOffset())));
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) ASTNode(com.intellij.lang.ASTNode) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 5 with FoldingDescriptor

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

the class RncFoldingBuilder method process.

private static void process(@Nullable ASTNode node, Document document, ArrayList<FoldingDescriptor> regions) {
    if (node == null) {
        return;
    }
    final ASTNode[] braces = node.getChildren(RncTokenTypes.BRACES);
    if (braces.length == 2) {
        final ASTNode lbrace = braces[0];
        final ASTNode rbrace = braces[1];
        if (shouldFold(lbrace, rbrace, document)) {
            final TextRange range = new TextRange(lbrace.getStartOffset(), rbrace.getTextRange().getEndOffset());
            regions.add(new FoldingDescriptor(lbrace, range));
        }
    } else if (isAnnotation(node.getElementType())) {
        if (isOnDifferentLine(node.getFirstChildNode(), node.getLastChildNode(), document)) {
            regions.add(new FoldingDescriptor(node, node.getTextRange()));
        }
    }
    node = node.getFirstChildNode();
    while (node != null) {
        node = checkNodeAndSiblings(node, RncTokenTypes.DOC_TOKENS, regions, document);
        node = checkNodeAndSiblings(node, RncTokenTypes.COMMENTS, regions, document);
        process(node, document, regions);
        if (node != null) {
            node = node.getTreeNext();
        }
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange)

Aggregations

FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)51 TextRange (com.intellij.openapi.util.TextRange)31 ASTNode (com.intellij.lang.ASTNode)20 PsiElement (com.intellij.psi.PsiElement)16 IElementType (com.intellij.psi.tree.IElementType)12 NamedFoldingDescriptor (com.intellij.lang.folding.NamedFoldingDescriptor)8 PsiComment (com.intellij.psi.PsiComment)7 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)7 FoldingGroup (com.intellij.openapi.editor.FoldingGroup)6 UnfairTextRange (com.intellij.openapi.util.UnfairTextRange)6 NotNull (org.jetbrains.annotations.NotNull)6 ArrayList (java.util.ArrayList)5 LinkedList (java.util.LinkedList)4 Nullable (org.jetbrains.annotations.Nullable)4 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)3 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)3 FoldRegion (com.intellij.openapi.editor.FoldRegion)3 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)3 Test (org.junit.Test)3 HbBlockWrapper (com.dmarcotte.handlebars.psi.HbBlockWrapper)2