private void handleToken(Token token, boolean spaceBefore, boolean spaceAfter) { if (spaceBefore) token.spaceBefore(); if (spaceAfter) token.spaceAfter(); }
private void handleSemicolon(ASTNode node) { if (this.options.insert_space_before_semicolon) { Token lastToken = this.tm.lastTokenIn(node, -1); if (lastToken.tokenType == TokenNameSEMICOLON) lastToken.spaceBefore(); } }
private void handleToken(Token token, boolean spaceBefore, boolean spaceAfter) { if (spaceBefore) token.spaceBefore(); if (spaceAfter) token.spaceAfter(); }
private void handleSemicolon(ASTNode node) { if (this.options.insert_space_before_semicolon) { Token lastToken = this.tm.lastTokenIn(node, -1); if (lastToken.tokenType == TokenNameSEMICOLON) lastToken.spaceBefore(); } }
private void handleSemicolon(ASTNode node) { if (this.options.insert_space_before_semicolon) { Token lastToken = this.tm.lastTokenIn(node, -1); if (lastToken.tokenType == TokenNameSEMICOLON) lastToken.spaceBefore(); } }
private void handleToken(Token token, boolean spaceBefore, boolean spaceAfter) { if (spaceBefore) token.spaceBefore(); if (spaceAfter) token.spaceAfter(); }
@Override public void endVisit(SingleVariableDeclaration node) { // this must be endVisit in case a space added by a visit on a child node needs to be cleared if (node.isVarargs()) { handleTokenBefore(node.getName(), TokenNameELLIPSIS, this.options.insert_space_before_ellipsis, this.options.insert_space_after_ellipsis); List<Annotation> varargsAnnotations = node.varargsAnnotations(); if (!varargsAnnotations.isEmpty()) { this.tm.firstTokenIn(varargsAnnotations.get(0), TokenNameAT).spaceBefore(); this.tm.lastTokenIn(varargsAnnotations.get(varargsAnnotations.size() - 1), -1).clearSpaceAfter(); } } else { handleToken(node.getName(), TokenNameIdentifier, true, false); } }
@Override public void endVisit(SingleVariableDeclaration node) { // this must be endVisit in case a space added by a visit on a child node needs to be cleared if (node.isVarargs()) { handleTokenBefore(node.getName(), TokenNameELLIPSIS, this.options.insert_space_before_ellipsis, this.options.insert_space_after_ellipsis); List<Annotation> varargsAnnotations = node.varargsAnnotations(); if (!varargsAnnotations.isEmpty()) { this.tm.firstTokenIn(varargsAnnotations.get(0), TokenNameAT).spaceBefore(); this.tm.lastTokenIn(varargsAnnotations.get(varargsAnnotations.size() - 1), -1).clearSpaceAfter(); } } else { handleToken(node.getName(), TokenNameIdentifier, true, false); } }
private void handleLoopBody(Statement loopBody) { /* space before body statement may be needed if it will stay on the same line */ int firstTokenIndex = this.tm.firstIndexIn(loopBody, -1); if (!(loopBody instanceof Block) && !(loopBody instanceof EmptyStatement) && !this.tm.get(firstTokenIndex - 1).isComment()) { this.tm.get(firstTokenIndex).spaceBefore(); } }
@Override public void endVisit(SingleVariableDeclaration node) { // this must be endVisit in case a space added by a visit on a child node needs to be cleared if (node.isVarargs()) { handleTokenBefore(node.getName(), TokenNameELLIPSIS, this.options.insert_space_before_ellipsis, this.options.insert_space_after_ellipsis); List<Annotation> varargsAnnotations = node.varargsAnnotations(); if (!varargsAnnotations.isEmpty()) { this.tm.firstTokenIn(varargsAnnotations.get(0), TokenNameAT).spaceBefore(); this.tm.lastTokenIn(varargsAnnotations.get(varargsAnnotations.size() - 1), -1).clearSpaceAfter(); } } else { handleToken(node.getName(), TokenNameIdentifier, true, false); } }
private void handleInvocation(ASTNode invocationNode, ASTNode nodeBeforeOpeningParen, ASTNode nodeAfterClosingParen) { if (handleEmptyParens(nodeBeforeOpeningParen, this.options.insert_space_between_empty_parens_in_method_invocation)) { handleToken(nodeBeforeOpeningParen, TokenNameLPAREN, this.options.insert_space_before_opening_paren_in_method_invocation, false); } else { handleToken(nodeBeforeOpeningParen, TokenNameLPAREN, this.options.insert_space_before_opening_paren_in_method_invocation, this.options.insert_space_after_opening_paren_in_method_invocation); if (this.options.insert_space_before_closing_paren_in_method_invocation) { Token closingParen = nodeAfterClosingParen == null ? this.tm.lastTokenIn(invocationNode, TokenNameRPAREN) : this.tm.firstTokenBefore(nodeAfterClosingParen, TokenNameRPAREN); closingParen.spaceBefore(); } } }
@Override public boolean visit(VariableDeclarationExpression node) { ASTNode parent = node.getParent(); if (parent instanceof ForStatement) { handleCommas(node.fragments(), this.options.insert_space_before_comma_in_for_inits, this.options.insert_space_after_comma_in_for_inits); } else if (parent instanceof ExpressionStatement) { handleCommas(node.fragments(), this.options.insert_space_before_comma_in_multiple_local_declarations, this.options.insert_space_after_comma_in_multiple_local_declarations); } this.tm.firstTokenAfter(node.getType(), -1).spaceBefore(); return true; }
@Override public boolean visit(VariableDeclarationExpression node) { ASTNode parent = node.getParent(); if (parent instanceof ForStatement) { handleCommas(node.fragments(), this.options.insert_space_before_comma_in_for_inits, this.options.insert_space_after_comma_in_for_inits); } else if (parent instanceof ExpressionStatement) { handleCommas(node.fragments(), this.options.insert_space_before_comma_in_multiple_local_declarations, this.options.insert_space_after_comma_in_multiple_local_declarations); } this.tm.firstTokenAfter(node.getType(), -1).spaceBefore(); return true; }
@Override public boolean visit(VariableDeclarationExpression node) { ASTNode parent = node.getParent(); if (parent instanceof ForStatement) { handleCommas(node.fragments(), this.options.insert_space_before_comma_in_for_inits, this.options.insert_space_after_comma_in_for_inits); } else if (parent instanceof ExpressionStatement) { handleCommas(node.fragments(), this.options.insert_space_before_comma_in_multiple_local_declarations, this.options.insert_space_after_comma_in_multiple_local_declarations); } this.tm.firstTokenAfter(node.getType(), -1).spaceBefore(); return true; }
private void handleAnnotation(Annotation node, boolean handleParenthesis) { handleToken(node, TokenNameAT, false, this.options.insert_space_after_at_in_annotation); if (handleParenthesis) { handleToken(node, TokenNameLPAREN, this.options.insert_space_before_opening_paren_in_annotation, this.options.insert_space_after_opening_paren_in_annotation); if (this.options.insert_space_before_closing_paren_in_annotation) this.tm.lastTokenIn(node, TokenNameRPAREN).spaceBefore(); } ASTNode parent = node.getParent(); boolean skipSpaceAfter = parent instanceof Annotation || parent instanceof MemberValuePair || (parent instanceof AnnotationTypeMemberDeclaration && ((AnnotationTypeMemberDeclaration) parent).getDefault() == node) || parent instanceof ArrayInitializer; if (!skipSpaceAfter) this.tm.lastTokenIn(node, -1).spaceAfter(); }
private void handleAnnotation(Annotation node, boolean handleParenthesis) { handleToken(node, TokenNameAT, false, this.options.insert_space_after_at_in_annotation); if (handleParenthesis) { handleToken(node, TokenNameLPAREN, this.options.insert_space_before_opening_paren_in_annotation, this.options.insert_space_after_opening_paren_in_annotation); if (this.options.insert_space_before_closing_paren_in_annotation) this.tm.lastTokenIn(node, TokenNameRPAREN).spaceBefore(); } ASTNode parent = node.getParent(); boolean skipSpaceAfter = parent instanceof Annotation || parent instanceof MemberValuePair || (parent instanceof AnnotationTypeMemberDeclaration && ((AnnotationTypeMemberDeclaration) parent).getDefault() == node) || parent instanceof ArrayInitializer; if (!skipSpaceAfter) this.tm.lastTokenIn(node, -1).spaceAfter(); }
private void splitToken(Token token, int tokenIndex, int splitPosition) { assert splitPosition > token.originalStart && splitPosition <= token.originalEnd; Token part1 = new Token(token.originalStart, splitPosition - 1, token.tokenType); Token part2 = new Token(splitPosition, token.originalEnd, token.tokenType); if (token.isSpaceBefore()) part1.spaceBefore(); part1.putLineBreaksBefore(token.getLineBreaksBefore()); if (token.isSpaceAfter()) part2.spaceAfter(); part2.putLineBreaksAfter(token.getLineBreaksAfter()); part1.setIndent(token.getIndent()); part2.setIndent(token.getIndent()); part1.setWrapPolicy(token.getWrapPolicy()); this.commentStructure.set(tokenIndex, part1); this.commentStructure.add(tokenIndex + 1, part2); }
private void splitToken(Token token, int tokenIndex, int splitPosition) { assert splitPosition > token.originalStart && splitPosition <= token.originalEnd; Token part1 = new Token(token.originalStart, splitPosition - 1, token.tokenType); Token part2 = new Token(splitPosition, token.originalEnd, token.tokenType); if (token.isSpaceBefore()) part1.spaceBefore(); part1.putLineBreaksBefore(token.getLineBreaksBefore()); if (token.isSpaceAfter()) part2.spaceAfter(); part2.putLineBreaksAfter(token.getLineBreaksAfter()); part1.setIndent(token.getIndent()); part2.setIndent(token.getIndent()); part1.setWrapPolicy(token.getWrapPolicy()); this.commentStructure.set(tokenIndex, part1); this.commentStructure.add(tokenIndex + 1, part2); }
private void splitToken(Token token, int tokenIndex, int splitPosition) { assert splitPosition > token.originalStart && splitPosition <= token.originalEnd; Token part1 = new Token(token.originalStart, splitPosition - 1, token.tokenType); Token part2 = new Token(splitPosition, token.originalEnd, token.tokenType); if (token.isSpaceBefore()) part1.spaceBefore(); part1.putLineBreaksBefore(token.getLineBreaksBefore()); if (token.isSpaceAfter()) part2.spaceAfter(); part2.putLineBreaksAfter(token.getLineBreaksAfter()); part1.setIndent(token.getIndent()); part2.setIndent(token.getIndent()); part1.setWrapPolicy(token.getWrapPolicy()); this.commentStructure.set(tokenIndex, part1); this.commentStructure.add(tokenIndex + 1, part2); }
@Override public boolean visit(IfStatement node) { handleToken(node, TokenNameLPAREN, this.options.insert_space_before_opening_paren_in_if, this.options.insert_space_after_opening_paren_in_if); Statement thenStatement = node.getThenStatement(); int closingParenIndex = this.tm.firstIndexBefore(thenStatement, TokenNameRPAREN); handleToken(this.tm.get(closingParenIndex), this.options.insert_space_before_closing_paren_in_if, /* space before then statement may be needed if it will stay on the same line */ !(thenStatement instanceof Block) && !this.tm.get(closingParenIndex + 1).isComment()); if (thenStatement instanceof Block && this.tm.isGuardClause((Block) thenStatement)) { handleToken(thenStatement, TokenNameLBRACE, false, true); this.tm.lastTokenIn(node, TokenNameRBRACE).spaceBefore(); } handleSemicolon(thenStatement); return true; }