### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java,v retrieving revision 1.226 diff -u -r1.226 CodeFormatterVisitor.java --- formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java 18 Feb 2010 10:41:32 -0000 1.226 +++ formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java 19 Feb 2010 15:57:01 -0000 @@ -4159,129 +4159,155 @@ return false; } - /* - * Print comments to get proper line number - */ - this.scribe.printComment(); - int line = this.scribe.line; - - this.scribe.printModifiers(methodDeclaration.annotations, this, ICodeFormatterConstants.ANNOTATION_ON_MEMBER); - - if (this.scribe.line > line) { - // annotations introduced new line, but this is not a line wrapping - // see 158267 - line = this.scribe.line; - } - this.scribe.space(); - - TypeParameter[] typeParameters = methodDeclaration.typeParameters; - if (typeParameters != null) { - this.scribe.printNextToken(TerminalTokens.TokenNameLESS, this.preferences.insert_space_before_opening_angle_bracket_in_type_parameters); - if (this.preferences.insert_space_after_opening_angle_bracket_in_type_parameters) { - this.scribe.space(); - } - int length = typeParameters.length; - for (int i = 0; i < length - 1; i++) { - typeParameters[i].traverse(this, methodDeclaration.scope); - this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_type_parameters); - if (this.preferences.insert_space_after_comma_in_type_parameters) { - this.scribe.space(); - } - } - typeParameters[length - 1].traverse(this, methodDeclaration.scope); - if (isClosingGenericToken()) { - this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS, this.preferences.insert_space_before_closing_angle_bracket_in_type_parameters); - } - if (this.preferences.insert_space_after_closing_angle_bracket_in_type_parameters) { - this.scribe.space(); - } - } - - /* - * Print the method return type - */ - final TypeReference returnType = methodDeclaration.returnType; - final MethodScope methodDeclarationScope = methodDeclaration.scope; - - if (returnType != null) { - returnType.traverse(this, methodDeclarationScope); - } /* - * Print the method name + * Print comments to get proper line number */ - this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true); + this.scribe.printComment(); + int line = this.scribe.line; - formatMethodArguments( - methodDeclaration, - this.preferences.insert_space_before_opening_paren_in_method_declaration, - this.preferences.insert_space_between_empty_parens_in_method_declaration, - this.preferences.insert_space_before_closing_paren_in_method_declaration, - this.preferences.insert_space_after_opening_paren_in_method_declaration, - this.preferences.insert_space_before_comma_in_method_declaration_parameters, - this.preferences.insert_space_after_comma_in_method_declaration_parameters, - this.preferences.alignment_for_parameters_in_method_declaration); + // Create alignment + Alignment methodDeclAlignment = this.scribe.createAlignment( + "methodDeclaration",//$NON-NLS-1$ + Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN, + Alignment.R_INNERMOST, + 4, + this.scribe.scanner.currentPosition, + 0 /* no continuation indentation */, + false /* do not adjust */); + this.scribe.enterAlignment(methodDeclAlignment); + boolean ok = false; + do { + try { - /* - * Check for extra dimensions - */ - int extraDimensions = getDimensions(); - if (extraDimensions != 0) { - for (int i = 0; i < extraDimensions; i++) { - this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET); - this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET); - } - } + this.scribe.printModifiers(methodDeclaration.annotations, this, ICodeFormatterConstants.ANNOTATION_ON_MEMBER); + this.scribe.alignFragment(methodDeclAlignment, 0); - formatThrowsClause( - methodDeclaration, - this.preferences.insert_space_before_comma_in_method_declaration_throws, - this.preferences.insert_space_after_comma_in_method_declaration_throws, - this.preferences.alignment_for_throws_clause_in_method_declaration); + if (this.scribe.line > line) { + // annotations introduced new line, but this is not a line wrapping + // see 158267 + line = this.scribe.line; + } + this.scribe.space(); - if (!methodDeclaration.isNative() && !methodDeclaration.isAbstract() && ((methodDeclaration.modifiers & ExtraCompilerModifiers.AccSemicolonBody) == 0)) { - /* - * Method body - */ - String method_declaration_brace = this.preferences.brace_position_for_method_declaration; - formatLeftCurlyBrace(line, method_declaration_brace); - formatOpeningBrace(method_declaration_brace, this.preferences.insert_space_before_opening_brace_in_method_declaration); - final int numberOfBlankLinesAtBeginningOfMethodBody = this.preferences.blank_lines_at_beginning_of_method_body; - if (numberOfBlankLinesAtBeginningOfMethodBody > 0) { - this.scribe.printEmptyLines(numberOfBlankLinesAtBeginningOfMethodBody); - } - final Statement[] statements = methodDeclaration.statements; - if (statements != null) { - this.scribe.printNewLine(); - if (this.preferences.indent_statements_compare_to_body) { - this.scribe.indent(); + TypeParameter[] typeParameters = methodDeclaration.typeParameters; + if (typeParameters != null) { + this.scribe.printNextToken(TerminalTokens.TokenNameLESS, this.preferences.insert_space_before_opening_angle_bracket_in_type_parameters); + if (this.preferences.insert_space_after_opening_angle_bracket_in_type_parameters) { + this.scribe.space(); + } + int length = typeParameters.length; + for (int i = 0; i < length - 1; i++) { + typeParameters[i].traverse(this, methodDeclaration.scope); + this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_type_parameters); + if (this.preferences.insert_space_after_comma_in_type_parameters) { + this.scribe.space(); + } + } + typeParameters[length - 1].traverse(this, methodDeclaration.scope); + if (isClosingGenericToken()) { + this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS, this.preferences.insert_space_before_closing_angle_bracket_in_type_parameters); + } + if (this.preferences.insert_space_after_closing_angle_bracket_in_type_parameters) { + this.scribe.space(); + } } - formatStatements(methodDeclarationScope, statements, true); - this.scribe.printComment(); - if (this.preferences.indent_statements_compare_to_body) { - this.scribe.unIndent(); + this.scribe.alignFragment(methodDeclAlignment, 1); + + /* + * Print the method return type + */ + final TypeReference returnType = methodDeclaration.returnType; + final MethodScope methodDeclarationScope = methodDeclaration.scope; + + if (returnType != null) { + returnType.traverse(this, methodDeclarationScope); } - } else if (this.preferences.insert_new_line_in_empty_method_body) { - this.scribe.printNewLine(); - if (this.preferences.indent_statements_compare_to_body) { - this.scribe.indent(); + this.scribe.alignFragment(methodDeclAlignment, 2); + + /* + * Print the method name + */ + this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true); + this.scribe.alignFragment(methodDeclAlignment, 3); + + formatMethodArguments( + methodDeclaration, + this.preferences.insert_space_before_opening_paren_in_method_declaration, + this.preferences.insert_space_between_empty_parens_in_method_declaration, + this.preferences.insert_space_before_closing_paren_in_method_declaration, + this.preferences.insert_space_after_opening_paren_in_method_declaration, + this.preferences.insert_space_before_comma_in_method_declaration_parameters, + this.preferences.insert_space_after_comma_in_method_declaration_parameters, + this.preferences.alignment_for_parameters_in_method_declaration); + + /* + * Check for extra dimensions + */ + int extraDimensions = getDimensions(); + if (extraDimensions != 0) { + for (int i = 0; i < extraDimensions; i++) { + this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET); + this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET); + } } - this.scribe.printComment(); - if (this.preferences.indent_statements_compare_to_body) { - this.scribe.unIndent(); + + formatThrowsClause( + methodDeclaration, + this.preferences.insert_space_before_comma_in_method_declaration_throws, + this.preferences.insert_space_after_comma_in_method_declaration_throws, + this.preferences.alignment_for_throws_clause_in_method_declaration); + + if (!methodDeclaration.isNative() && !methodDeclaration.isAbstract() && ((methodDeclaration.modifiers & ExtraCompilerModifiers.AccSemicolonBody) == 0)) { + /* + * Method body + */ + String method_declaration_brace = this.preferences.brace_position_for_method_declaration; + formatLeftCurlyBrace(line, method_declaration_brace); + formatOpeningBrace(method_declaration_brace, this.preferences.insert_space_before_opening_brace_in_method_declaration); + final int numberOfBlankLinesAtBeginningOfMethodBody = this.preferences.blank_lines_at_beginning_of_method_body; + if (numberOfBlankLinesAtBeginningOfMethodBody > 0) { + this.scribe.printEmptyLines(numberOfBlankLinesAtBeginningOfMethodBody); + } + final Statement[] statements = methodDeclaration.statements; + if (statements != null) { + this.scribe.printNewLine(); + if (this.preferences.indent_statements_compare_to_body) { + this.scribe.indent(); + } + formatStatements(methodDeclarationScope, statements, true); + this.scribe.printComment(); + if (this.preferences.indent_statements_compare_to_body) { + this.scribe.unIndent(); + } + } else if (this.preferences.insert_new_line_in_empty_method_body) { + this.scribe.printNewLine(); + if (this.preferences.indent_statements_compare_to_body) { + this.scribe.indent(); + } + this.scribe.printComment(); + if (this.preferences.indent_statements_compare_to_body) { + this.scribe.unIndent(); + } + } + this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE); + this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT); + if (method_declaration_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) { + this.scribe.unIndent(); + } + } else { + // no method body + this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); + this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.COMPLEX_TRAILING_COMMENT); } + ok = true; + } catch (AlignmentException e) { + this.scribe.redoAlignment(e); } - this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE); - this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT); - if (method_declaration_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) { - this.scribe.unIndent(); - } - } else { - // no method body - this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); - this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.COMPLEX_TRAILING_COMMENT); - } + } while (!ok); + this.scribe.exitAlignment(methodDeclAlignment, true); return false; } + public boolean visit(NormalAnnotation annotation, BlockScope scope) { this.scribe.printNextToken(TerminalTokens.TokenNameAT); if (this.preferences.insert_space_after_at_in_annotation) {