/** * 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); }
private static Block generateGoFileBlock(ASTNode node, CommonCodeStyleSettings settings) { return new GoFileBlock( node, Alignment.createAlignment(), Indent.getAbsoluteNoneIndent(), Wrap.createWrap(WrapType.NONE, false), settings); }
@Nullable @Override public Alignment getAlignment() { PsiElement element = myNode.getPsi(); if (this.alignement.equals(super.getAlignment())) { if (isNode()) { this.alignement = Alignment.createAlignment(); } } return this.alignement; }
/** * Generates blocks for binary expressions * * @return */ private List<Block> generateForBinaryExpr() { final ArrayList<Block> subBlocks = new ArrayList<Block>(); Alignment alignment = mySettings.ALIGN_MULTILINE_BINARY_OPERATION ? Alignment.createAlignment() : null; GrBinaryExpression binary = (GrBinaryExpression) myNode.getPsi(); LOG.assertTrue(binary != null); addBinaryChildrenRecursively( binary, subBlocks, Indent.getContinuationWithoutFirstIndent(), alignment); return subBlocks; }
private List<Block> buildSubBlocks() { final List<Block> blocks = new ArrayList<Block>(); Indent prevIndent = Indent.getNoneIndent(); // if (myNode.getElementType() == CoqTypes.PROOFPHRASE) return // Collections.unmodifiableList(blocks); for (ASTNode child = myNode.getFirstChildNode(); child != null; child = child.getTreeNext()) { if (!shouldCreateBlockFor(child)) continue; Block b = createChildBlock(child, Alignment.createAlignment(), prevIndent); prevIndent = b.getIndent(); blocks.add(b); } return Collections.unmodifiableList(blocks); }
/** * Generates blocks for binary expressions * * @param node * @return */ private static List<Block> generateForBinaryExpr( final ASTNode node, Wrap myWrap, CodeStyleSettings mySettings) { final ArrayList<Block> subBlocks = new ArrayList<Block>(); Alignment alignment = mySettings.ALIGN_MULTILINE_BINARY_OPERATION ? Alignment.createAlignment() : null; MoonBinaryExpression myExpr = (MoonBinaryExpression) node.getPsi(); ASTNode[] children = node.getChildren(null); if (myExpr.getLeftExpression() instanceof MoonBinaryExpression) { addBinaryChildrenRecursively( myExpr.getLeftExpression(), subBlocks, Indent.getContinuationWithoutFirstIndent(), alignment, myWrap, mySettings); } for (ASTNode childNode : children) { if (canBeCorrectBlock(childNode) && !(childNode.getPsi() instanceof MoonBinaryExpression)) { subBlocks.add( new MoonFormattingBlock( childNode, alignment, Indent.getContinuationWithoutFirstIndent(), myWrap, mySettings)); } } if (myExpr.getRightExpression() instanceof MoonBinaryExpression) { addBinaryChildrenRecursively( myExpr.getRightExpression(), subBlocks, Indent.getContinuationWithoutFirstIndent(), alignment, myWrap, mySettings); } return subBlocks; }
@NotNull public ChildAttributes getChildAttributes(int newChildIndex) { return new ChildAttributes(Indent.getNoneIndent(), Alignment.createAlignment()); // return ChildAttributes.DELEGATE_TO_NEXT_CHILD; }
public List<Block> generateSubBlocks() { // For binary expressions PsiElement blockPsi = myNode.getPsi(); if (blockPsi instanceof GrBinaryExpression && !(blockPsi.getParent() instanceof GrBinaryExpression)) { return generateForBinaryExpr(); } // For multiline strings if ((myNode.getElementType() == mSTRING_LITERAL || myNode.getElementType() == mGSTRING_LITERAL) && myBlock.getTextRange().equals(myNode.getTextRange())) { String text = myNode.getText(); if (text.length() > 6) { if (text.substring(0, 3).equals("'''") && text.substring(text.length() - 3).equals("'''") || text.substring(0, 3).equals("\"\"\"") & text.substring(text.length() - 3).equals("\"\"\"")) { return generateForMultiLineString(); } } } if (myNode.getElementType() == mGSTRING_BEGIN && myBlock.getTextRange().equals(myNode.getTextRange())) { String text = myNode.getText(); if (text.length() > 3) { if (text.substring(0, 3).equals("\"\"\"")) { return generateForMultiLineGStringBegin(); } } } // for gstrings if (myNode.getElementType() == GSTRING) { final ArrayList<Block> subBlocks = new ArrayList<Block>(); ASTNode[] children = getGroovyChildren(myNode); for (ASTNode childNode : children) { if (childNode.getTextRange().getLength() > 0) { final Indent indent = GroovyIndentProcessor.getChildIndent(myBlock, childNode); subBlocks.add( new GroovyBlock( childNode, myAlignment, indent, myWrap, mySettings, myGroovySettings, myInnerAlignments)); } } return subBlocks; } // chained properties, calls, indexing, etc if (NESTED.contains(myNode.getElementType()) && blockPsi.getParent() != null && !NESTED.contains(blockPsi.getParent().getNode().getElementType())) { final List<Block> subBlocks = new ArrayList<Block>(); Alignment dotsAlignment = mySettings.ALIGN_MULTILINE_CHAINED_METHODS ? Alignment.createAlignment() : null; addNestedChildren(myNode.getPsi(), subBlocks, dotsAlignment, true); return subBlocks; } // For Parameter lists if (isListLikeClause(blockPsi)) { final ArrayList<Block> subBlocks = new ArrayList<Block>(); List<ASTNode> astNodes = visibleChildren(myNode); final Alignment newAlignment = mustAlign(blockPsi, astNodes) ? Alignment.createAlignment() : null; for (ASTNode childNode : astNodes) { final Indent indent = GroovyIndentProcessor.getChildIndent(myBlock, childNode); subBlocks.add( new GroovyBlock( childNode, isKeyword(childNode) ? null : newAlignment, indent, myWrap, mySettings, myGroovySettings, myInnerAlignments)); } return subBlocks; } boolean classLevel = blockPsi instanceof GrTypeDefinitionBody; if (blockPsi instanceof GrCodeBlock || blockPsi instanceof GroovyFile || classLevel) { List<ASTNode> children = visibleChildren(myNode); calculateAlignments(children, classLevel); final ArrayList<Block> subBlocks = new ArrayList<Block>(); for (ASTNode childNode : children) { final Indent indent = GroovyIndentProcessor.getChildIndent(myBlock, childNode); Alignment alignmentToUse = classLevel ? myAlignment : myInnerAlignments.get(childNode.getPsi()); subBlocks.add( new GroovyBlock( childNode, alignmentToUse, indent, myWrap, mySettings, myGroovySettings, myInnerAlignments)); } return subBlocks; } // For other cases final ArrayList<Block> subBlocks = new ArrayList<Block>(); for (ASTNode childNode : visibleChildren(myNode)) { final Indent indent = GroovyIndentProcessor.getChildIndent(myBlock, childNode); subBlocks.add( new GroovyBlock( childNode, myInnerAlignments.get(childNode.getPsi()), indent, myWrap, mySettings, myGroovySettings, myInnerAlignments)); } return subBlocks; }
private void calculateAlignments(List<ASTNode> children, boolean classLevel) { List<Alignment> currentGroup = null; for (ASTNode child : children) { PsiElement psi = child.getPsi(); if (psi instanceof GrLabeledStatement) { List<LeafPsiElement> table = getSpockTable(((GrLabeledStatement) psi).getStatement()); if (table.isEmpty()) { currentGroup = null; } else { currentGroup = new ArrayList<Alignment>(); for (LeafPsiElement expression : table) { Alignment alignment = Alignment.createAlignment(true); currentGroup.add(alignment); ContainerUtil.putIfNotNull(expression, alignment, myInnerAlignments); } } } else if (currentGroup != null && isTablePart(psi)) { List<LeafPsiElement> table = getSpockTable((GrStatement) psi); for (int i = 0; i < Math.min(table.size(), currentGroup.size()); i++) { myInnerAlignments.put(table.get(i), currentGroup.get(i)); } } else if (psi instanceof GrVariableDeclaration) { if (!classLevel || currentGroup == null || fieldGroupEnded(psi)) { currentGroup = Arrays.asList( Alignment.createAlignment(true), Alignment.createAlignment(true), Alignment.createAlignment(true)); } GrVariable[] variables = ((GrVariableDeclaration) psi).getVariables(); if (variables.length > 0) { Alignment varName = currentGroup.get(1); for (GrVariable variable : variables) { myInnerAlignments.put(variable.getNameIdentifierGroovy(), varName); } if (classLevel && mySettings.ALIGN_GROUP_FIELD_DECLARATIONS) { ContainerUtil.putIfNotNull( ((GrVariableDeclaration) psi).getTypeElementGroovy(), currentGroup.get(0), myInnerAlignments); ASTNode eq = variables[variables.length - 1].getNode().findChildByType(GroovyTokenTypes.mASSIGN); if (eq != null) { myInnerAlignments.put(eq.getPsi(), currentGroup.get(2)); } } } } else if (GeeseUtil.isClosureRBrace(psi) && myGroovySettings.USE_FLYING_GEESE_BRACES) { myInnerAlignments.put(psi, GeeseUtil.calculateRBraceAlignment(psi, myInnerAlignments)); } else { if (psi instanceof PsiComment) { PsiElement prev = psi.getPrevSibling(); if (prev != null && prev.getNode().getElementType() != mNLS || classLevel && !fieldGroupEnded(psi)) { continue; } } currentGroup = null; } } }
public static class Config { private static final Alignment NO_ALIGNMENT = Alignment.createAlignment(); private static final Wrap NO_WRAP = Wrap.createWrap(0, false); private Map<IElementType, Alignment> myAlignments = ContainerUtil.newHashMap(); private Map<IElementType, Indent> myIndents = ContainerUtil.newHashMap(); private Map<IElementType, Wrap> myWraps = ContainerUtil.newHashMap(); private Map<IElementType, Condition<ASTNode>> myNoneAlignmentCondition = ContainerUtil.newHashMap(); private Alignment myDefaultAlignment; private Indent myDefaultIndent; private Wrap myDefaultWrap; public ChildrenBlocksBuilder createBuilder() { return new ChildrenBlocksBuilder(this); } public Config setDefaultAlignment(Alignment alignment) { myDefaultAlignment = alignment; return this; } public Config setDefaultWrap(Wrap wrap) { myDefaultWrap = wrap; return this; } public Config setDefaultIndent(Indent indent) { myDefaultIndent = indent; return this; } public Config setAlignment(@NotNull IElementType elementType, @NotNull Alignment alignment) { myAlignments.put(elementType, alignment); return this; } public Config setNoAlignment(IElementType elementType) { myAlignments.put(elementType, NO_ALIGNMENT); return this; } public Config setNoAlignmentIf( IElementType elementType, Condition<ASTNode> applyAlignCondition) { myNoneAlignmentCondition.put(elementType, applyAlignCondition); return this; } public Config setIndent(IElementType elementType, Indent indent) { myIndents.put(elementType, indent); return this; } private Indent getIndent(IElementType elementType) { Indent indent = myIndents.get(elementType); return indent != null ? indent : myDefaultIndent; } private Alignment getAlignment(ASTNode node) { IElementType elementType = node.getElementType(); Condition<ASTNode> noneAlignmentCondition = myNoneAlignmentCondition.get(elementType); if (noneAlignmentCondition != null && noneAlignmentCondition.value(node)) { return null; } Alignment alignment = myAlignments.get(elementType); if (alignment == null) { return myDefaultAlignment; } return alignment == NO_ALIGNMENT ? null : alignment; } private Wrap getWrap(IElementType elementType) { Wrap wrap = myWraps.get(elementType); if (wrap == NO_WRAP) return null; return wrap != null ? wrap : myDefaultWrap; } public Config setNoWrap(IElementType elementType) { myWraps.put(elementType, NO_WRAP); return this; } }