private void prepareLineBreaks() { LineBreaksPreparator breaksPreparator = new LineBreaksPreparator(this.tokenManager, this.workingOptions); this.astRoot.accept(breaksPreparator); breaksPreparator.finishUp(); }
private void handleLoopBody(Statement body) { if (body instanceof Block) return; if (body instanceof EmptyStatement && !this.options.put_empty_statement_on_new_line && !(body.getParent() instanceof IfStatement)) return; breakLineBefore(body); adjustEmptyLineAfter(this.tm.lastIndexIn(body, -1), -1); indent(body); }
@Override public boolean visit(SingleMemberAnnotation node) { handleAnnotation(node); return true; }
@Override public boolean visit(AnonymousClassDeclaration node) { if (node.getParent() instanceof EnumConstantDeclaration) { handleBracedCode(node, null, this.options.brace_position_for_enum_constant, this.options.indent_body_declarations_compare_to_enum_constant_header); } else { handleBracedCode(node, null, this.options.brace_position_for_anonymous_type_declaration, this.options.indent_body_declarations_compare_to_type_header); } handleBodyDeclarations(node.bodyDeclarations()); return true; }
@Override public boolean visit(SwitchStatement node) { handleBracedCode(node, node.getExpression(), this.options.brace_position_for_switch, this.options.indent_switchstatements_compare_to_switch); || statement instanceof ContinueStatement || statement instanceof Block; if (isBreaking && !(statement instanceof Block)) adjustEmptyLineAfter(this.tm.lastIndexIn(statement, -1), -1); if (statement instanceof SwitchCase) { if (nonBreakStatementEnd >= 0) { indent(statement); for (Statement statement : statements) { if (statement instanceof BreakStatement) indent(statement); continue; // will add break in visit(Block) if necessary if (this.options.put_empty_statement_on_new_line || !(statement instanceof EmptyStatement)) breakLineBefore(statement); handleParenthesesPositions(lParen, rParen, this.options.parenthesis_positions_in_switch_statement);
@Override public boolean visit(WhileStatement node) { if (!this.options.keep_simple_while_body_on_same_line) handleLoopBody(node.getBody()); int lParen = this.tm.firstIndexIn(node, TokenNameLPAREN); int rParen = this.tm.firstIndexAfter(node.getExpression(), TokenNameRPAREN); handleParenthesesPositions(lParen, rParen, this.options.parenthesis_positions_in_if_while_statement); return true; }
@Override public boolean visit(TypeDeclaration node) { handleBodyDeclarations(node.bodyDeclarations()); if (node.getName().getStartPosition() == -1) return true; // this is a fake type created by parsing in class body mode breakLineBefore(node); handleBracedCode(node, node.getName(), this.options.brace_position_for_type_declaration, this.options.indent_body_declarations_compare_to_type_header); this.declarationModifierVisited = false; return true; }
@Override public boolean visit(ConstructorInvocation node) { int lParen = node.arguments().isEmpty() ? this.tm.lastIndexIn(node, TokenNameLPAREN) : this.tm.firstIndexBefore((ASTNode) node.arguments().get(0), TokenNameLPAREN); int rParen = this.tm.lastIndexIn(node, TokenNameRPAREN); handleParenthesesPositions(lParen, rParen, this.options.parenthesis_positions_in_method_invocation); return true; }
private void handleBracedCode(ASTNode node, ASTNode nodeBeforeOpenBrace, String bracePosition, boolean indentBody) { int openBraceIndex = nodeBeforeOpenBrace == null ? this.tm.firstIndexIn(node, TokenNameLBRACE) : this.tm.firstIndexAfter(nodeBeforeOpenBrace, TokenNameLBRACE); int closeBraceIndex = this.tm.lastIndexIn(node, TokenNameRBRACE); Token openBraceToken = this.tm.get(openBraceIndex); Token closeBraceToken = this.tm.get(closeBraceIndex); handleBracePosition(openBraceToken, closeBraceIndex, bracePosition); openBraceToken.breakAfter(); closeBraceToken.breakBefore(); if (indentBody) { adjustEmptyLineAfter(openBraceIndex, 1); this.tm.get(openBraceIndex + 1).indent(); closeBraceToken.unindent(); } }
@Override public boolean visit(ModuleDeclaration node) { // using settings for type declaration and fields for now, add new settings if necessary breakLineBefore(node); handleBracedCode(node, node.getName(), this.options.brace_position_for_type_declaration, this.options.indent_body_declarations_compare_to_type_header); List<ModuleDirective> statements = node.moduleStatements(); ModuleDirective previous = null; for (ModuleDirective statement : statements) { int blankLines = previous == null ? this.options.blank_lines_before_first_class_body_declaration : previous.getClass().equals(statement.getClass()) ? this.options.blank_lines_before_field : this.options.blank_lines_before_new_chunk; putBlankLinesBefore(statement, blankLines); previous = statement; } this.declarationModifierVisited = false; return true; }
@Override public boolean visit(CatchClause node) { if (this.options.insert_new_line_before_catch_in_try_statement) breakLineBefore(node); int lParen = this.tm.firstIndexIn(node, TokenNameLPAREN); int rParen = this.tm.firstIndexBefore(node.getBody(), TokenNameRPAREN); handleParenthesesPositions(lParen, rParen, this.options.parenthesis_positions_in_catch_clause); return true; }
@Override public boolean visit(MethodDeclaration node) { this.declarationModifierVisited = false; int lParen = this.tm.firstIndexAfter(node.getName(), TokenNameLPAREN); int rParen = node.getBody() == null ? this.tm.lastIndexIn(node, TokenNameRPAREN) : this.tm.firstIndexBefore(node.getBody(), TokenNameRPAREN); handleParenthesesPositions(lParen, rParen, this.options.parenthesis_positions_in_method_declaration); if (node.getBody() == null) return true; if (node.isConstructor()) { handleBracedCode(node.getBody(), null, this.options.brace_position_for_constructor_declaration, this.options.indent_statements_compare_to_body, this.options.insert_new_line_in_empty_method_body); } else { handleBracedCode(node.getBody(), null, this.options.brace_position_for_method_declaration, this.options.indent_statements_compare_to_body, this.options.insert_new_line_in_empty_method_body); Token openBrace = this.tm.firstTokenIn(node.getBody(), TokenNameLBRACE); if (openBrace.getLineBreaksAfter() > 0) // if not, these are empty braces openBrace.putLineBreaksAfter(this.options.blank_lines_at_beginning_of_method_body + 1); } return true; }
@Override public boolean visit(Block node) { List<Statement> statements = node.statements(); for (Statement statement : statements) { if (this.options.put_empty_statement_on_new_line || !(statement instanceof EmptyStatement)) breakLineBefore(statement); } ASTNode parent = node.getParent(); if (parent.getLength() == 0) return true; // this is a fake block created by parsing in statements mode if (parent instanceof MethodDeclaration) return true; // braces have been handled in #visit(MethodDeclaration) String bracePosition = this.options.brace_position_for_block; if (parent instanceof SwitchStatement) { List<Statement> siblings = ((SwitchStatement) parent).statements(); int blockPosition = siblings.indexOf(node); boolean isFirstInCase = blockPosition > 0 && (siblings.get(blockPosition - 1) instanceof SwitchCase); if (isFirstInCase) bracePosition = this.options.brace_position_for_block_in_case; } else if (parent instanceof LambdaExpression) { bracePosition = this.options.brace_position_for_lambda_body; } handleBracedCode(node, null, bracePosition, this.options.indent_statements_compare_to_block); return true; }
@Override public boolean visit(NormalAnnotation node) { handleAnnotation(node); int lParen = this.tm.firstIndexAfter(node.getTypeName(), TokenNameLPAREN); int rParen = this.tm.lastIndexIn(node, TokenNameRPAREN); handleParenthesesPositions(lParen, rParen, this.options.parenthesis_positions_in_annotation); return true; }
@Override public boolean visit(ImportDeclaration node) { breakLineBefore(node); return true; }
@Override public boolean visit(SwitchStatement node) { handleBracedCode(node, node.getExpression(), this.options.brace_position_for_switch, this.options.indent_switchstatements_compare_to_switch, true); || statement instanceof ContinueStatement || statement instanceof Block; if (isBreaking && !(statement instanceof Block)) adjustEmptyLineAfter(this.tm.lastIndexIn(statement, -1), -1); if (statement instanceof SwitchCase) { if (nonBreakStatementEnd >= 0) { indent(statement); for (Statement statement : statements) { if (statement instanceof BreakStatement) indent(statement); continue; // will add break in visit(Block) if necessary if (this.options.put_empty_statement_on_new_line || !(statement instanceof EmptyStatement)) breakLineBefore(statement); handleParenthesesPositions(lParen, rParen, this.options.parenthesis_positions_in_switch_statement);
@Override public boolean visit(ForStatement node) { if (!this.options.keep_simple_for_body_on_same_line) handleLoopBody(node.getBody()); int lParen = this.tm.firstIndexIn(node, TokenNameLPAREN); int rParen = this.tm.firstIndexBefore(node.getBody(), TokenNameRPAREN); handleParenthesesPositions(lParen, rParen, this.options.parenthesis_positions_in_for_statement); return true; }
@Override public boolean visit(AnonymousClassDeclaration node) { if (node.getParent() instanceof EnumConstantDeclaration) { handleBracedCode(node, null, this.options.brace_position_for_enum_constant, this.options.indent_body_declarations_compare_to_enum_constant_header, this.options.insert_new_line_in_empty_enum_constant); } else { handleBracedCode(node, null, this.options.brace_position_for_anonymous_type_declaration, this.options.indent_body_declarations_compare_to_type_header, this.options.insert_new_line_in_empty_anonymous_type_declaration); } handleBodyDeclarations(node.bodyDeclarations()); return true; }
public boolean visit(TypeDeclaration node) { handleBodyDeclarations(node.bodyDeclarations()); if (node.getName().getStartPosition() == -1) return true; // this is a fake type created by parsing in class body mode breakLineBefore(node); handleBracedCode(node, node.getName(), this.options.brace_position_for_type_declaration, this.options.indent_body_declarations_compare_to_type_header, this.options.insert_new_line_in_empty_type_declaration); this.declarationModifierVisited = false; return true; }
@Override public boolean visit(MethodInvocation node) { int lParen = this.tm.firstIndexAfter(node.getName(), TokenNameLPAREN); int rParen = this.tm.lastIndexIn(node, TokenNameRPAREN); handleParenthesesPositions(lParen, rParen, this.options.parenthesis_positions_in_method_invocation); return true; }