Search in sources :

Example 1 with HbBlockWrapper

use of com.dmarcotte.handlebars.psi.HbBlockWrapper in project intellij-plugins by JetBrains.

the class HbFoldingBuilder method appendDescriptors.

private void appendDescriptors(PsiElement psi, List<FoldingDescriptor> descriptors, Document document) {
    if (isSingleLine(psi, document)) {
        return;
    }
    if (HbTokenTypes.COMMENT == psi.getNode().getElementType()) {
        ASTNode commentNode = psi.getNode();
        String commentText = commentNode.getText();
        // tags before we allow folding
        if (commentText.length() > 6 && commentText.substring(0, 3).equals("{{!") && commentText.substring(commentText.length() - 2, commentText.length()).equals("}}")) {
            TextRange range = new TextRange(commentNode.getTextRange().getStartOffset() + 3, commentNode.getTextRange().getEndOffset() - 2);
            descriptors.add(new FoldingDescriptor(commentNode, range));
        }
    }
    if (psi instanceof HbBlockWrapper) {
        PsiElement endOpenBlockStache = getOpenBlockCloseStacheElement(psi.getFirstChild());
        PsiElement endCloseBlockStache = getCloseBlockCloseStacheElement(psi.getLastChild());
        // if we've got a well formed block with the open and close elems we need, define a region to fold
        if (endOpenBlockStache != null && endCloseBlockStache != null) {
            int endOfFirstOpenStacheLine = document.getLineEndOffset(document.getLineNumber(psi.getTextRange().getStartOffset()));
            // we set the start of the text we'll fold to be just before the close braces of the open stache,
            //     or, if the open stache spans multiple lines, to the end of the first line
            int foldingRangeStartOffset = Math.min(endOpenBlockStache.getTextRange().getStartOffset(), endOfFirstOpenStacheLine);
            // we set the end of the text we'll fold to be just before the final close braces in this block
            int foldingRangeEndOffset = endCloseBlockStache.getTextRange().getStartOffset();
            TextRange range = new TextRange(foldingRangeStartOffset, foldingRangeEndOffset);
            descriptors.add(new FoldingDescriptor(psi, range));
        }
    }
    PsiElement child = psi.getFirstChild();
    while (child != null) {
        appendDescriptors(child, descriptors, document);
        child = child.getNextSibling();
    }
}
Also used : HbBlockWrapper(com.dmarcotte.handlebars.psi.HbBlockWrapper) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

Example 2 with HbBlockWrapper

use of com.dmarcotte.handlebars.psi.HbBlockWrapper in project idea-handlebars by dmarcotte.

the class HbFoldingBuilder method appendDescriptors.

private void appendDescriptors(PsiElement psi, List<FoldingDescriptor> descriptors, Document document) {
    if (isSingleLine(psi, document)) {
        return;
    }
    if (HbTokenTypes.COMMENT == psi.getNode().getElementType()) {
        ASTNode commentNode = psi.getNode();
        String commentText = commentNode.getText();
        // tags before we allow folding
        if (commentText.length() > 6 && commentText.substring(0, 3).equals("{{!") && commentText.substring(commentText.length() - 2, commentText.length()).equals("}}")) {
            TextRange range = new TextRange(commentNode.getTextRange().getStartOffset() + 3, commentNode.getTextRange().getEndOffset() - 2);
            descriptors.add(new FoldingDescriptor(commentNode, range));
        }
    }
    if (psi instanceof HbBlockWrapper) {
        PsiElement endOpenBlockStache = getOpenBlockCloseStacheElement(psi.getFirstChild());
        PsiElement endCloseBlockStache = getCloseBlockCloseStacheElement(psi.getLastChild());
        // if we've got a well formed block with the open and close elems we need, define a region to fold
        if (endOpenBlockStache != null && endCloseBlockStache != null) {
            int endOfFirstOpenStacheLine = document.getLineEndOffset(document.getLineNumber(psi.getTextRange().getStartOffset()));
            // we set the start of the text we'll fold to be just before the close braces of the open stache,
            // or, if the open stache spans multiple lines, to the end of the first line
            int foldingRangeStartOffset = Math.min(endOpenBlockStache.getTextRange().getStartOffset(), endOfFirstOpenStacheLine);
            // we set the end of the text we'll fold to be just before the final close braces in this block
            int foldingRangeEndOffset = endCloseBlockStache.getTextRange().getStartOffset();
            TextRange range = new TextRange(foldingRangeStartOffset, foldingRangeEndOffset);
            descriptors.add(new FoldingDescriptor(psi, range));
        }
    }
    PsiElement child = psi.getFirstChild();
    while (child != null) {
        appendDescriptors(child, descriptors, document);
        child = child.getNextSibling();
    }
}
Also used : HbBlockWrapper(com.dmarcotte.handlebars.psi.HbBlockWrapper) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

Aggregations

HbBlockWrapper (com.dmarcotte.handlebars.psi.HbBlockWrapper)2 ASTNode (com.intellij.lang.ASTNode)2 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiElement (com.intellij.psi.PsiElement)2