Search in sources :

Example 26 with Block

use of com.intellij.formatting.Block in project oxy-template-support-plugin by mutant-industries.

the class OxyTemplateForeignElementWrapper method buildChildren.

@NotNull
public List<Block> buildChildren() {
    final FileViewProvider provider = myNode.getPsi().getContainingFile().getViewProvider();
    PsiElement element = provider.findElementAt(textRange.getStartOffset(), OxyTemplate.INSTANCE);
    assert element != null;
    while (OxyTemplateHelper.checkRangeContainsParent(element, textRange, OxyTemplateTypes.T_TEMPLATE_HTML_CODE)) {
        element = element.getParent();
    }
    ASTNode child = element.getNode();
    final Alignment textAlignment = Alignment.createAlignment();
    ArrayList<Block> result = new ArrayList<>();
    while (child != null) {
        if (!textRange.contains(child.getTextRange())) {
            if (child.getStartOffset() < getTextRange().getEndOffset()) {
                while (child.getFirstChildNode() != null && !textRange.contains((child = child.getFirstChildNode()).getTextRange())) ;
            } else {
                break;
            }
        }
        if (!FormatterUtil.containsWhiteSpacesOnly(child) && child.getTextLength() > 0) {
            child = processChild(result, child, null, chooseAlignment(child, textAlignment), indent);
        }
        child = nextNode(child);
    }
    return result;
}
Also used : Alignment(com.intellij.formatting.Alignment) FileViewProvider(com.intellij.psi.FileViewProvider) ASTNode(com.intellij.lang.ASTNode) ArrayList(java.util.ArrayList) Block(com.intellij.formatting.Block) JSBlock(com.intellij.lang.javascript.formatter.blocks.JSBlock) IndentInheritingBlock(com.intellij.xml.template.formatter.IndentInheritingBlock) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with Block

use of com.intellij.formatting.Block in project ballerina by ballerina-lang.

the class BallerinaBlock method buildChildren.

@Override
protected List<Block> buildChildren() {
    List<Block> blocks = new ArrayList<>();
    ASTNode child = node.getFirstChildNode();
    IElementType parentElementType = node.getElementType();
    while (child != null) {
        IElementType childElementType = child.getElementType();
        if (childElementType != TokenType.WHITE_SPACE) {
            Indent indent = Indent.getNoneIndent();
            if (isInsideADefinitionElement(childElementType)) {
                if (child.getFirstChildNode() != null && child.getLastChildNode() != null) {
                    indent = Indent.getSpaceIndent(4);
                }
            } else if (childElementType == COMMENT_STATEMENT) {
                if (isADefinitionElement(parentElementType) || isACodeBlock(parentElementType)) {
                    indent = Indent.getSpaceIndent(4);
                }
            } else if (childElementType == WORKER_DECLARATION) {
                if (parentElementType == FORK_JOIN_STATEMENT) {
                    indent = Indent.getSpaceIndent(4);
                }
            } else if (childElementType == WORKER_BODY) {
                if (parentElementType == WORKER_DECLARATION) {
                    indent = Indent.getSpaceIndent(4);
                }
            } else if (childElementType == RECORD_KEY_VALUE) {
                indent = Indent.getSpaceIndent(4);
            } else if (childElementType == CODE_BLOCK_BODY || childElementType == NON_EMPTY_CODE_BLOCK_BODY || childElementType == ENUM_FIELD_LIST) {
                indent = Indent.getSpaceIndent(4);
            } else if (childElementType == EXPRESSION_LIST) {
                if (parentElementType == VARIABLE_REFERENCE) {
                    indent = Indent.getSpaceIndent(4);
                }
            } else if (childElementType == FIELD_DEFINITION) {
                if (parentElementType == PRIVATE_STRUCT_BODY) {
                    indent = Indent.getSpaceIndent(4);
                }
            } else if (childElementType == MATCH_PATTERN_CLAUSE) {
                indent = Indent.getSpaceIndent(4);
            } else if (childElementType == OBJECT_BODY) {
                indent = Indent.getSpaceIndent(4);
            } else if (childElementType == OBJECT_FIELD_DEFINITION) {
                indent = Indent.getSpaceIndent(4);
            } else if (childElementType == STREAMING_QUERY_STATEMENT) {
                indent = Indent.getSpaceIndent(4);
            }
            // If the child node text is empty, the IDEA core will throw an exception.
            if (!child.getText().isEmpty()) {
                Block block = new BallerinaBlock(child, Alignment.createAlignment(), indent, null, mySettings, spacingBuilder);
                blocks.add(block);
            }
        }
        child = child.getTreeNext();
    }
    return blocks;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Indent(com.intellij.formatting.Indent) ArrayList(java.util.ArrayList) ASTNode(com.intellij.lang.ASTNode) Block(com.intellij.formatting.Block) AbstractBlock(com.intellij.psi.formatter.common.AbstractBlock)

Aggregations

Block (com.intellij.formatting.Block)27 ArrayList (java.util.ArrayList)14 ASTNode (com.intellij.lang.ASTNode)9 NotNull (org.jetbrains.annotations.NotNull)9 ASTBlock (com.intellij.formatting.ASTBlock)8 Indent (com.intellij.formatting.Indent)6 TextRange (com.intellij.openapi.util.TextRange)6 Alignment (com.intellij.formatting.Alignment)5 Wrap (com.intellij.formatting.Wrap)4 FormattingModel (com.intellij.formatting.FormattingModel)3 FormattingModelBuilder (com.intellij.formatting.FormattingModelBuilder)3 Document (com.intellij.openapi.editor.Document)3 AbstractBlock (com.intellij.psi.formatter.common.AbstractBlock)3 NewLineBlocksIterator (com.intellij.psi.formatter.common.NewLineBlocksIterator)3 IElementType (com.intellij.psi.tree.IElementType)3 Nullable (org.jetbrains.annotations.Nullable)3 PsiComment (com.intellij.psi.PsiComment)2 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)2 AbstractJavaBlock.newJavaBlock (com.intellij.psi.formatter.java.AbstractJavaBlock.newJavaBlock)2 IndentInheritingBlock (com.intellij.xml.template.formatter.IndentInheritingBlock)2