Search in sources :

Example 1 with Wrap

use of com.intellij.formatting.Wrap in project buck by facebook.

the class BuckBlock method buildSubBlock.

private BuckBlock buildSubBlock(ASTNode childNode) {
    Indent indent = Indent.getNoneIndent();
    Alignment alignment = null;
    Wrap wrap = null;
    if (hasElementType(myNode, BUCK_CONTAINERS)) {
        if (hasElementType(childNode, BuckTypes.COMMA)) {
            wrap = Wrap.createWrap(WrapType.NONE, true);
        } else if (!hasElementType(childNode, BUCK_ALL_BRACES)) {
            assert myChildWrap != null;
            wrap = myChildWrap;
            indent = Indent.getNormalIndent();
        }
    }
    return new BuckBlock(childNode, mySettings, alignment, indent, wrap);
}
Also used : Indent(com.intellij.formatting.Indent) Alignment(com.intellij.formatting.Alignment) Wrap(com.intellij.formatting.Wrap)

Example 2 with Wrap

use of com.intellij.formatting.Wrap in project intellij-community by JetBrains.

the class JavaChildBlockWrapFactory method create.

/**
   * Creates {@link Wrap wrap} to be used with the children blocks of the the given block.
   *
   * @param block                   target block which sub-blocks should use wrap created by the current method
   * @param settings                code formatting settings to consider during wrap construction
   * @param reservedWrapsProvider   reserved {@code 'element type -> wrap instance'} mappings provider. <b>Note:</b> this
   *                                argument is considered to be a part of legacy heritage and is intended to be removed as
   *                                soon as formatting code refactoring is done
   * @return                        wrap to use for the sub-blocks of the given block
   */
@Nullable
public Wrap create(ASTBlock block, CommonCodeStyleSettings settings, ReservedWrapsProvider reservedWrapsProvider) {
    ASTNode node = block.getNode();
    Wrap wrap = block.getWrap();
    final IElementType nodeType = node.getElementType();
    if (nodeType == JavaElementType.EXTENDS_LIST || nodeType == JavaElementType.IMPLEMENTS_LIST) {
        return Wrap.createWrap(settings.EXTENDS_LIST_WRAP, false);
    } else if (node instanceof PsiPolyadicExpression) {
        Wrap actualWrap = wrap != null ? wrap : reservedWrapsProvider.getReservedWrap(JavaElementType.BINARY_EXPRESSION);
        if (actualWrap == null) {
            return Wrap.createWrap(settings.BINARY_OPERATION_WRAP, false);
        } else {
            if (JavaFormatterUtil.areSamePriorityBinaryExpressions(node, node.getTreeParent())) {
                return actualWrap;
            } else {
                return Wrap.createChildWrap(actualWrap, WrapType.byLegacyRepresentation(settings.BINARY_OPERATION_WRAP), false);
            }
        }
    } else if (nodeType == JavaElementType.CONDITIONAL_EXPRESSION) {
        return Wrap.createWrap(settings.TERNARY_OPERATION_WRAP, false);
    } else if (nodeType == JavaElementType.ASSERT_STATEMENT) {
        return Wrap.createWrap(settings.ASSERT_STATEMENT_WRAP, false);
    } else if (nodeType == JavaElementType.FOR_STATEMENT) {
        return Wrap.createWrap(settings.FOR_STATEMENT_WRAP, false);
    } else if (nodeType == JavaElementType.THROWS_LIST) {
        return Wrap.createWrap(settings.THROWS_LIST_WRAP, true);
    } else if (nodeType == JavaElementType.CODE_BLOCK) {
        if (settings.KEEP_SIMPLE_METHODS_IN_ONE_LINE && node.getPsi().getParent() instanceof PsiMethod && !node.textContains('\n')) {
            return null;
        }
        return Wrap.createWrap(WrapType.NORMAL, false);
    } else if (JavaFormatterUtil.isAssignment(node)) {
        return Wrap.createWrap(settings.ASSIGNMENT_WRAP, true);
    } else {
        return null;
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Wrap(com.intellij.formatting.Wrap) PsiPolyadicExpression(com.intellij.psi.PsiPolyadicExpression) PsiMethod(com.intellij.psi.PsiMethod) ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with Wrap

use of com.intellij.formatting.Wrap in project intellij-community by JetBrains.

the class ExtendsListBlock method buildChildren.

@Override
protected List<Block> buildChildren() {
    final ArrayList<Block> result = new ArrayList<>();
    ArrayList<Block> elementsExceptKeyword = new ArrayList<>();
    myChildAlignment = createChildAlignment();
    myChildIndent = Indent.getContinuationIndent(myIndentSettings.USE_RELATIVE_INDENTS);
    myUseChildAttributes = true;
    Wrap childWrap = createChildWrap();
    ASTNode child = myNode.getFirstChildNode();
    Alignment alignment = alignList() ? Alignment.createAlignment() : null;
    while (child != null) {
        if (!FormatterUtil.containsWhiteSpacesOnly(child) && child.getTextLength() > 0) {
            IElementType elementType = child.getElementType();
            if (ElementType.KEYWORD_BIT_SET.contains(elementType)) {
                if (!elementsExceptKeyword.isEmpty()) {
                    result.add(new SyntheticCodeBlock(elementsExceptKeyword, null, mySettings, myJavaSettings, Indent.getNoneIndent(), null));
                    elementsExceptKeyword = new ArrayList<>();
                }
                Indent indent = mySettings.ALIGN_THROWS_KEYWORD && elementType == JavaTokenType.THROWS_KEYWORD ? Indent.getNoneIndent() : myChildIndent;
                result.add(createJavaBlock(child, mySettings, myJavaSettings, indent, arrangeChildWrap(child, childWrap), alignment));
            } else {
                Alignment candidate = myAlignmentStrategy.getAlignment(elementType);
                if (candidate != null) {
                    alignment = myChildAlignment = candidate;
                }
                processChild(elementsExceptKeyword, child, myChildAlignment, childWrap, myChildIndent);
            }
        }
        child = child.getTreeNext();
    }
    if (!elementsExceptKeyword.isEmpty()) {
        result.add(new SyntheticCodeBlock(elementsExceptKeyword, alignment, mySettings, myJavaSettings, Indent.getNoneIndent(), null));
    }
    return result;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Alignment(com.intellij.formatting.Alignment) Indent(com.intellij.formatting.Indent) Wrap(com.intellij.formatting.Wrap) ArrayList(java.util.ArrayList) ASTNode(com.intellij.lang.ASTNode) Block(com.intellij.formatting.Block)

Example 4 with Wrap

use of com.intellij.formatting.Wrap in project intellij-community by JetBrains.

the class ArrayInitializerBlocksBuilder method buildBlocks.

public List<Block> buildBlocks() {
    Wrap wrap = Wrap.createWrap(getWrapType(mySettings.ARRAY_INITIALIZER_WRAP), false);
    Alignment alignment = mySettings.ALIGN_MULTILINE_ARRAY_INITIALIZER_EXPRESSION ? Alignment.createAlignment() : null;
    ChildrenBlocksBuilder.Config config = new ChildrenBlocksBuilder.Config().setDefaultIndent(Indent.getContinuationWithoutFirstIndent()).setIndent(JavaTokenType.RBRACE, Indent.getNoneIndent()).setIndent(JavaTokenType.LBRACE, Indent.getNoneIndent()).setDefaultWrap(wrap).setNoWrap(JavaTokenType.COMMA).setNoWrap(JavaTokenType.RBRACE).setNoWrap(JavaTokenType.LBRACE).setDefaultAlignment(alignment).setNoAlignment(JavaTokenType.COMMA).setNoAlignment(JavaTokenType.LBRACE).setNoAlignmentIf(JavaTokenType.RBRACE, node -> {
        PsiElement prev = PsiTreeUtil.skipSiblingsBackward(node.getPsi(), PsiWhiteSpace.class);
        if (prev == null)
            return false;
        return prev.getNode().getElementType() != JavaTokenType.COMMA;
    });
    return config.createBuilder().buildNodeChildBlocks(myNode, myBlockFactory);
}
Also used : Alignment(com.intellij.formatting.Alignment) Wrap(com.intellij.formatting.Wrap) PsiElement(com.intellij.psi.PsiElement)

Example 5 with Wrap

use of com.intellij.formatting.Wrap in project intellij-community by JetBrains.

the class ChainedCallChunk method buildBlocksFrom.

private List<Block> buildBlocksFrom(List<ASTNode> nodes) {
    List<ChainedCallChunk> methodCall = splitMethodCallOnChunksByDots(nodes);
    Wrap wrap = null;
    Alignment chainedCallsAlignment = null;
    List<Block> blocks = new ArrayList<>();
    for (int i = 0; i < methodCall.size(); i++) {
        ChainedCallChunk currentCallChunk = methodCall.get(i);
        if (isMethodCall(currentCallChunk) || isComment(currentCallChunk)) {
            if (wrap == null) {
                wrap = createCallChunkWrap(i, methodCall);
            }
            if (chainedCallsAlignment == null) {
                chainedCallsAlignment = createCallChunkAlignment(i, methodCall);
            }
        } else {
            wrap = null;
            chainedCallsAlignment = null;
        }
        CallChunkBlockBuilder builder = new CallChunkBlockBuilder(mySettings, myJavaSettings);
        blocks.add(builder.create(currentCallChunk.nodes, wrap, chainedCallsAlignment));
    }
    return blocks;
}
Also used : Alignment(com.intellij.formatting.Alignment) Wrap(com.intellij.formatting.Wrap) ArrayList(java.util.ArrayList) Block(com.intellij.formatting.Block)

Aggregations

Wrap (com.intellij.formatting.Wrap)9 Alignment (com.intellij.formatting.Alignment)6 ASTNode (com.intellij.lang.ASTNode)5 IElementType (com.intellij.psi.tree.IElementType)5 Block (com.intellij.formatting.Block)3 Indent (com.intellij.formatting.Indent)3 PsiElement (com.intellij.psi.PsiElement)2 ArrayList (java.util.ArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 PsiMethod (com.intellij.psi.PsiMethod)1 PsiNameValuePair (com.intellij.psi.PsiNameValuePair)1 PsiPolyadicExpression (com.intellij.psi.PsiPolyadicExpression)1 CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)1