/** * There is a possible case that 'implements' section is incomplete (e.g. ends with comma). We may * want to align lbrace to the comma then. * * @param alignment block alignment * @param baseNode base AST node * @return alignment strategy to use for the given node */ private static AlignmentStrategy getAlignmentStrategy( Alignment alignment, ASTNode baseNode, @NotNull CommonCodeStyleSettings settings) { if (baseNode.getElementType() != JavaElementType.CLASS || !settings.ALIGN_MULTILINE_EXTENDS_LIST) { return AlignmentStrategy.wrap(alignment); } for (ASTNode node = baseNode.getLastChildNode(); node != null; node = FormatterUtil.getPreviousNonWhitespaceSibling(node)) { if (node.getElementType() != JavaElementType.IMPLEMENTS_LIST) { continue; } ASTNode lastChildNode = node.getLastChildNode(); if (lastChildNode != null && lastChildNode.getElementType() == TokenType.ERROR_ELEMENT) { Alignment alignmentToUse = alignment; if (alignment == null) { alignmentToUse = Alignment.createAlignment(); } return AlignmentStrategy.wrap( alignmentToUse, false, JavaTokenType.LBRACE, JavaElementType.JAVA_CODE_REFERENCE, node.getElementType()); } break; } return AlignmentStrategy.wrap(alignment); }
@NotNull public Block create( @NotNull final List<ASTNode> subNodes, final Wrap wrap, @Nullable final Alignment alignment) { final ArrayList<Block> subBlocks = new ArrayList<Block>(); final ASTNode firstNode = subNodes.get(0); if (firstNode.getElementType() == JavaTokenType.DOT) { AlignmentStrategy strategy = AlignmentStrategy.getNullStrategy(); Block block = createJavaBlock( firstNode, mySettings, myJavaSettings, Indent.getNoneIndent(), null, strategy); subBlocks.add(block); subNodes.remove(0); if (!subNodes.isEmpty()) { subBlocks.add(create(subNodes, wrap, null)); } return new SyntheticCodeBlock( subBlocks, alignment, mySettings, myJavaSettings, Indent.getContinuationIndent(myIndentSettings.USE_RELATIVE_INDENTS), wrap); } return new SyntheticCodeBlock( createJavaBlocks(subNodes), alignment, mySettings, myJavaSettings, Indent.getContinuationWithoutFirstIndent(myIndentSettings.USE_RELATIVE_INDENTS), null); }
@NotNull private List<Block> createJavaBlocks(@NotNull final List<ASTNode> subNodes) { final ArrayList<Block> result = new ArrayList<Block>(); for (ASTNode node : subNodes) { Indent indent = Indent.getContinuationWithoutFirstIndent(myIndentSettings.USE_RELATIVE_INDENTS); result.add( createJavaBlock( node, mySettings, myJavaSettings, indent, null, AlignmentStrategy.getNullStrategy())); } return result; }
@Nullable private ASTNode processCaseAndStatementAfter( final ArrayList<Block> result, ASTNode child, final Alignment childAlignment, final Wrap childWrap, final Indent indent) { final ArrayList<Block> localResult = new ArrayList<Block>(); processChild( localResult, child, AlignmentStrategy.getNullStrategy(), null, Indent.getNoneIndent()); child = child.getTreeNext(); Indent childIndent = Indent.getNormalIndent(); while (child != null) { if (child.getElementType() == JavaElementType.SWITCH_LABEL_STATEMENT || isRBrace(child)) { result.add(createCaseSectionBlock(localResult, childAlignment, indent, childWrap)); return child.getTreePrev(); } if (!FormatterUtil.containsWhiteSpacesOnly(child)) { if (child.getElementType() == JavaElementType.BLOCK_STATEMENT) { childIndent = Indent.getNoneIndent(); } boolean breakOrReturn = isBreakOrReturn(child); processChild(localResult, child, AlignmentStrategy.getNullStrategy(), null, childIndent); if (breakOrReturn) { result.add(createCaseSectionBlock(localResult, childAlignment, indent, childWrap)); return child; } } child = child.getTreeNext(); } result.add(createCaseSectionBlock(localResult, childAlignment, indent, childWrap)); return null; }
@Override protected List<Block> buildChildren() { final ArrayList<Block> result = new ArrayList<Block>(); ASTNode child = getNode().getFirstChildNode(); while (child != null) { IElementType childType = child.getElementType(); if (childType != TokenType.WHITE_SPACE && !FormatterUtil.containsWhiteSpacesOnly(child) && !child.getText().trim().isEmpty()) { result.add( createBlock( child, calcIndent(child), null, AlignmentStrategy.getNullStrategy(), settings, spacingBuilder)); } child = child.getTreeNext(); } return result; }