public void processComment(Token commentToken) { assert commentToken.isComment(); if (commentToken.tokenType == TokenNameCOMMENT_LINE) { handleSingleLineComment(commentToken, this.tm.indexOf(commentToken)); } else { handleMultiLineComment(commentToken, this.tm.indexOf(commentToken)); } }
public boolean isGuardClause(Block node) { if (node.statements().size() != 1) return false; ASTNode parent = node.getParent(); if (!(parent instanceof IfStatement) || ((IfStatement) parent).getElseStatement() != null) return false; Object statement = node.statements().get(0); if (!(statement instanceof ReturnStatement) && !(statement instanceof ThrowStatement)) return false; // guard clause cannot start with a comment // https://bugs.eclipse.org/bugs/show_bug.cgi?id=58565 int openBraceIndex = firstIndexIn(node, TokenNameLBRACE); return !get(openBraceIndex + 1).isComment(); }
public boolean isGuardClause(Block node) { if (node.statements().size() != 1) return false; ASTNode parent = node.getParent(); if (!(parent instanceof IfStatement) || ((IfStatement) parent).getElseStatement() != null) return false; Object statement = node.statements().get(0); if (!(statement instanceof ReturnStatement) && !(statement instanceof ThrowStatement)) return false; // guard clause cannot start with a comment // https://bugs.eclipse.org/bugs/show_bug.cgi?id=58565 int openBraceIndex = firstIndexIn(node, TokenNameLBRACE); return !get(openBraceIndex + 1).isComment(); }
public void processComment(Token commentToken) { assert commentToken.isComment(); if (commentToken.tokenType == TokenNameCOMMENT_LINE) { handleSingleLineComment(commentToken, this.tm.indexOf(commentToken)); } else { handleMultiLineComment(commentToken, this.tm.indexOf(commentToken)); } }
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(); } }
public void processComment(Token commentToken) { assert commentToken.isComment(); if (commentToken.tokenType == TokenNameCOMMENT_LINE) { handleSingleLineComment(commentToken, this.tm.indexOf(commentToken)); } else { handleMultiLineComment(commentToken, this.tm.indexOf(commentToken)); } }
@Override public boolean visit(UnionType node) { List<Type> types = node.types(); if (types.isEmpty()) return true; if (this.options.wrap_before_or_operator_multicatch) { for (Type type : types) { if (this.wrapIndexes.isEmpty()) { this.wrapIndexes.add(this.tm.firstIndexIn(type, -1)); } else { this.wrapIndexes.add(this.tm.firstIndexBefore(type, TokenNameOR)); this.secondaryWrapIndexes.add(this.tm.firstIndexIn(type, -1)); } } this.wrapParentIndex = this.tm.firstIndexBefore(node, -1); while (this.tm.get(this.wrapParentIndex).isComment()) this.wrapParentIndex--; this.wrapGroupEnd = this.tm.lastIndexIn(types.get(types.size() - 1), -1); handleWrap(this.options.alignment_for_union_type_in_multicatch); } else { prepareElementsList(types, TokenNameOR, TokenNameLPAREN); handleWrap(this.options.alignment_for_union_type_in_multicatch); } return true; }
@Override public boolean visit(Assignment node) { int rightSideIndex = this.tm.firstIndexIn(node.getRightHandSide(), -1); if (this.tm.get(rightSideIndex).getLineBreaksBefore() > 0) return true; // must be an array initializer in new line because of brace_position_for_array_initializer int operatorIndex = this.tm.firstIndexBefore(node.getRightHandSide(), -1); while (this.tm.get(operatorIndex).isComment()) operatorIndex--; assert node.getOperator().toString().equals(this.tm.toString(operatorIndex)); this.wrapIndexes.add(this.options.wrap_before_assignment_operator ? operatorIndex : rightSideIndex); this.secondaryWrapIndexes.add(this.options.wrap_before_assignment_operator ? rightSideIndex : operatorIndex); this.wrapParentIndex = operatorIndex - 1; this.wrapGroupEnd = this.tm.lastIndexIn(node.getRightHandSide(), -1); handleWrap(this.options.alignment_for_assignment); return true; }
@Override public boolean visit(Assignment node) { int rightSideIndex = this.tm.firstIndexIn(node.getRightHandSide(), -1); if (this.tm.get(rightSideIndex).getLineBreaksBefore() > 0) return true; // must be an array initializer in new line because of brace_position_for_array_initializer int operatorIndex = this.tm.firstIndexBefore(node.getRightHandSide(), -1); while (this.tm.get(operatorIndex).isComment()) operatorIndex--; assert node.getOperator().toString().equals(this.tm.toString(operatorIndex)); this.wrapIndexes.add(this.options.wrap_before_assignment_operator ? operatorIndex : rightSideIndex); this.secondaryWrapIndexes.add(this.options.wrap_before_assignment_operator ? rightSideIndex : operatorIndex); this.wrapParentIndex = operatorIndex - 1; this.wrapGroupEnd = this.tm.lastIndexIn(node.getRightHandSide(), -1); handleWrap(this.options.alignment_for_assignment); return true; }
@Override public boolean visit(Assignment node) { int rightSideIndex = this.tm.firstIndexIn(node.getRightHandSide(), -1); if (this.tm.get(rightSideIndex).getLineBreaksBefore() > 0) return true; // must be an array initializer in new line because of brace_position_for_array_initializer int operatorIndex = this.tm.firstIndexBefore(node.getRightHandSide(), -1); while (this.tm.get(operatorIndex).isComment()) operatorIndex--; assert node.getOperator().toString().equals(this.tm.toString(operatorIndex)); this.wrapIndexes.add(this.options.wrap_before_assignment_operator ? operatorIndex : rightSideIndex); this.secondaryWrapIndexes.add(this.options.wrap_before_assignment_operator ? rightSideIndex : operatorIndex); this.wrapParentIndex = operatorIndex - 1; this.wrapGroupEnd = this.tm.lastIndexIn(node.getRightHandSide(), -1); handleWrap(this.options.alignment_for_assignment); return true; }
private void indent(ASTNode node) { int startIndex = this.tm.firstIndexIn(node, -1); while (startIndex > 0 && this.tm.get(startIndex - 1).isComment()) startIndex--; this.tm.get(startIndex).indent(); int lastIndex = this.tm.lastIndexIn(node, -1); if (lastIndex + 1 < this.tm.size()) this.tm.get(lastIndex + 1).unindent(); }
private void indent(ASTNode node) { int startIndex = this.tm.firstIndexIn(node, -1); while (startIndex > 0 && this.tm.get(startIndex - 1).isComment()) startIndex--; this.tm.get(startIndex).indent(); int lastIndex = this.tm.lastIndexIn(node, -1); if (lastIndex + 1 < this.tm.size()) this.tm.get(lastIndex + 1).unindent(); }
private void indent(ASTNode node) { int startIndex = this.tm.firstIndexIn(node, -1); while (startIndex > 0 && this.tm.get(startIndex - 1).isComment()) startIndex--; this.tm.get(startIndex).indent(); int lastIndex = this.tm.lastIndexIn(node, -1); if (lastIndex + 1 < this.tm.size()) this.tm.get(lastIndex + 1).unindent(); }
@Override public boolean visit(EnumConstantDeclaration node) { this.declarationModifierVisited = false; int lParen = this.tm.firstIndexAfter(node.getName(), -1); while (this.tm.get(lParen).isComment()) lParen++; if (this.tm.get(lParen).tokenType == TokenNameLPAREN) { int rParen = node.getAnonymousClassDeclaration() == null ? this.tm.lastIndexIn(node, TokenNameRPAREN) : this.tm.firstIndexBefore(node.getAnonymousClassDeclaration(), TokenNameRPAREN); handleParenthesesPositions(lParen, rParen, this.options.parenthesis_positions_in_enum_constant_declaration); } return true; }
@Override public boolean visit(EnumConstantDeclaration node) { this.declarationModifierVisited = false; int lParen = this.tm.firstIndexAfter(node.getName(), -1); while (this.tm.get(lParen).isComment()) lParen++; if (this.tm.get(lParen).tokenType == TokenNameLPAREN) { int rParen = node.getAnonymousClassDeclaration() == null ? this.tm.lastIndexIn(node, TokenNameRPAREN) : this.tm.firstIndexBefore(node.getAnonymousClassDeclaration(), TokenNameRPAREN); handleParenthesesPositions(lParen, rParen, this.options.parenthesis_positions_in_enum_constant_declaration); } return true; }
@Override public boolean visit(EnumConstantDeclaration node) { this.declarationModifierVisited = false; int lParen = this.tm.firstIndexAfter(node.getName(), -1); while (this.tm.get(lParen).isComment()) lParen++; if (this.tm.get(lParen).tokenType == TokenNameLPAREN) { int rParen = node.getAnonymousClassDeclaration() == null ? this.tm.lastIndexIn(node, TokenNameRPAREN) : this.tm.firstIndexBefore(node.getAnonymousClassDeclaration(), TokenNameRPAREN); handleParenthesesPositions(lParen, rParen, this.options.parenthesis_positions_in_enum_constant_declaration); } return true; }
private void setTokenWrapPolicy(int index, WrapPolicy policy, boolean wrapPreceedingComments) { if (wrapPreceedingComments) { for (int i = index - 1; i >= 0; i--) { Token previous = this.tm.get(i); if (!previous.isComment()) break; if (previous.getLineBreaksAfter() == 0 && i == index - 1) index = i; if (previous.getLineBreaksBefore() > 0) previous.setWrapPolicy(policy); } } Token token = this.tm.get(index); if (token.getWrapPolicy() != WrapPolicy.DISABLE_WRAP) token.setWrapPolicy(policy); if (this.options.join_wrapped_lines && token.tokenType == TokenNameCOMMENT_BLOCK) { // allow wrap preparator to decide if this comment should be wrapped token.clearLineBreaksBefore(); } }
int getWrapIndent(Token token) { WrapPolicy policy = token.getWrapPolicy(); if (policy == null) return token.getIndent(); if (policy == WrapPolicy.FORCE_FIRST_COLUMN) return 0; Token wrapParent = this.tm.get(policy.wrapParentIndex); int wrapIndent = wrapParent.getIndent(); if (policy.indentOnColumn) { wrapIndent = this.tm.getPositionInLine(policy.wrapParentIndex); wrapIndent += this.tm.getLength(wrapParent, wrapIndent); Token next = this.tm.get(policy.wrapParentIndex + 1); if (wrapParent.isSpaceAfter() || (next.isSpaceBefore() && !next.isComment())) wrapIndent++; } wrapIndent += policy.extraIndent; return this.tm.toIndent(wrapIndent, true); } }
@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; }
@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; }