View | Details | Raw Unified | Return to bug 284789 | Differences between
and this patch

Collapse All | Expand All

(-)formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java (-110 / +136 lines)
Lines 4159-4287 Link Here
4159
			return false;
4159
			return false;
4160
		}
4160
		}
4161
4161
4162
        /*
4163
         * Print comments to get proper line number
4164
         */
4165
        this.scribe.printComment();
4166
        int line = this.scribe.line;
4167
4168
        this.scribe.printModifiers(methodDeclaration.annotations, this, ICodeFormatterConstants.ANNOTATION_ON_MEMBER);
4169
4170
		if (this.scribe.line > line) {
4171
        	// annotations introduced new line, but this is not a line wrapping
4172
			// see 158267
4173
			line = this.scribe.line;
4174
		}
4175
		this.scribe.space();
4176
4177
		TypeParameter[] typeParameters = methodDeclaration.typeParameters;
4178
		if (typeParameters != null) {
4179
			this.scribe.printNextToken(TerminalTokens.TokenNameLESS, this.preferences.insert_space_before_opening_angle_bracket_in_type_parameters);
4180
			if (this.preferences.insert_space_after_opening_angle_bracket_in_type_parameters) {
4181
				this.scribe.space();
4182
			}
4183
			int length = typeParameters.length;
4184
			for (int i = 0; i < length - 1; i++) {
4185
				typeParameters[i].traverse(this, methodDeclaration.scope);
4186
				this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_type_parameters);
4187
				if (this.preferences.insert_space_after_comma_in_type_parameters) {
4188
					this.scribe.space();
4189
				}
4190
			}
4191
			typeParameters[length - 1].traverse(this, methodDeclaration.scope);
4192
			if (isClosingGenericToken()) {
4193
				this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS, this.preferences.insert_space_before_closing_angle_bracket_in_type_parameters);
4194
			}
4195
			if (this.preferences.insert_space_after_closing_angle_bracket_in_type_parameters) {
4196
				this.scribe.space();
4197
			}
4198
		}
4199
4200
		/*
4201
		 * Print the method return type
4202
		 */
4203
		final TypeReference returnType = methodDeclaration.returnType;
4204
		final MethodScope methodDeclarationScope = methodDeclaration.scope;
4205
4206
		if (returnType != null) {
4207
			returnType.traverse(this, methodDeclarationScope);
4208
		}
4209
		/*
4162
		/*
4210
		 * Print the method name
4163
		 * Print comments to get proper line number
4211
		 */
4164
		 */
4212
		this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true);
4165
		this.scribe.printComment();
4166
		int line = this.scribe.line;
4213
4167
4214
		formatMethodArguments(
4168
		// Create alignment
4215
			methodDeclaration,
4169
		Alignment methodDeclAlignment = this.scribe.createAlignment(
4216
			this.preferences.insert_space_before_opening_paren_in_method_declaration,
4170
				"methodDeclaration",//$NON-NLS-1$
4217
			this.preferences.insert_space_between_empty_parens_in_method_declaration,
4171
				Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN,
4218
			this.preferences.insert_space_before_closing_paren_in_method_declaration,
4172
				Alignment.R_INNERMOST,
4219
			this.preferences.insert_space_after_opening_paren_in_method_declaration,
4173
				4,
4220
			this.preferences.insert_space_before_comma_in_method_declaration_parameters,
4174
				this.scribe.scanner.currentPosition,
4221
			this.preferences.insert_space_after_comma_in_method_declaration_parameters,
4175
				0 /* no continuation indentation */,
4222
			this.preferences.alignment_for_parameters_in_method_declaration);
4176
				false /* do not adjust */);
4177
		this.scribe.enterAlignment(methodDeclAlignment);
4178
		boolean ok = false;
4179
		do {
4180
			try {
4223
4181
4224
		/*
4182
				this.scribe.printModifiers(methodDeclaration.annotations, this, ICodeFormatterConstants.ANNOTATION_ON_MEMBER);
4225
		 * Check for extra dimensions
4183
				this.scribe.alignFragment(methodDeclAlignment, 0);
4226
		 */
4227
		int extraDimensions = getDimensions();
4228
		if (extraDimensions != 0) {
4229
			 for (int i = 0; i < extraDimensions; i++) {
4230
			 	this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
4231
			 	this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
4232
			 }
4233
		}
4234
4184
4235
		formatThrowsClause(
4185
				if (this.scribe.line > line) {
4236
			methodDeclaration,
4186
					// annotations introduced new line, but this is not a line wrapping
4237
			this.preferences.insert_space_before_comma_in_method_declaration_throws,
4187
					// see 158267
4238
			this.preferences.insert_space_after_comma_in_method_declaration_throws,
4188
					line = this.scribe.line;
4239
			this.preferences.alignment_for_throws_clause_in_method_declaration);
4189
				}
4190
				this.scribe.space();
4240
4191
4241
		if (!methodDeclaration.isNative() && !methodDeclaration.isAbstract() && ((methodDeclaration.modifiers & ExtraCompilerModifiers.AccSemicolonBody) == 0)) {
4192
				TypeParameter[] typeParameters = methodDeclaration.typeParameters;
4242
			/*
4193
				if (typeParameters != null) {
4243
			 * Method body
4194
					this.scribe.printNextToken(TerminalTokens.TokenNameLESS, this.preferences.insert_space_before_opening_angle_bracket_in_type_parameters);
4244
			 */
4195
					if (this.preferences.insert_space_after_opening_angle_bracket_in_type_parameters) {
4245
			String method_declaration_brace = this.preferences.brace_position_for_method_declaration;
4196
						this.scribe.space();
4246
			formatLeftCurlyBrace(line, method_declaration_brace);
4197
					}
4247
			formatOpeningBrace(method_declaration_brace, this.preferences.insert_space_before_opening_brace_in_method_declaration);
4198
					int length = typeParameters.length;
4248
			final int numberOfBlankLinesAtBeginningOfMethodBody = this.preferences.blank_lines_at_beginning_of_method_body;
4199
					for (int i = 0; i < length - 1; i++) {
4249
			if (numberOfBlankLinesAtBeginningOfMethodBody > 0) {
4200
						typeParameters[i].traverse(this, methodDeclaration.scope);
4250
				this.scribe.printEmptyLines(numberOfBlankLinesAtBeginningOfMethodBody);
4201
						this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_type_parameters);
4251
			}
4202
						if (this.preferences.insert_space_after_comma_in_type_parameters) {
4252
			final Statement[] statements = methodDeclaration.statements;
4203
							this.scribe.space();
4253
			if (statements != null) {
4204
						}
4254
				this.scribe.printNewLine();
4205
					}
4255
				if (this.preferences.indent_statements_compare_to_body) {
4206
					typeParameters[length - 1].traverse(this, methodDeclaration.scope);
4256
					this.scribe.indent();
4207
					if (isClosingGenericToken()) {
4208
						this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS, this.preferences.insert_space_before_closing_angle_bracket_in_type_parameters);
4209
					}
4210
					if (this.preferences.insert_space_after_closing_angle_bracket_in_type_parameters) {
4211
						this.scribe.space();
4212
					}
4257
				}
4213
				}
4258
				formatStatements(methodDeclarationScope, statements, true);
4214
				this.scribe.alignFragment(methodDeclAlignment, 1);
4259
				this.scribe.printComment();
4215
4260
				if (this.preferences.indent_statements_compare_to_body) {
4216
				/*
4261
					this.scribe.unIndent();
4217
				 * Print the method return type
4218
				 */
4219
				final TypeReference returnType = methodDeclaration.returnType;
4220
				final MethodScope methodDeclarationScope = methodDeclaration.scope;
4221
		
4222
				if (returnType != null) {
4223
					returnType.traverse(this, methodDeclarationScope);
4262
				}
4224
				}
4263
			} else if (this.preferences.insert_new_line_in_empty_method_body) {
4225
				this.scribe.alignFragment(methodDeclAlignment, 2);
4264
				this.scribe.printNewLine();
4226
4265
				if (this.preferences.indent_statements_compare_to_body) {
4227
				/*
4266
					this.scribe.indent();
4228
				 * Print the method name
4229
				 */
4230
				this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true);
4231
				this.scribe.alignFragment(methodDeclAlignment, 3);
4232
4233
				formatMethodArguments(
4234
					methodDeclaration,
4235
					this.preferences.insert_space_before_opening_paren_in_method_declaration,
4236
					this.preferences.insert_space_between_empty_parens_in_method_declaration,
4237
					this.preferences.insert_space_before_closing_paren_in_method_declaration,
4238
					this.preferences.insert_space_after_opening_paren_in_method_declaration,
4239
					this.preferences.insert_space_before_comma_in_method_declaration_parameters,
4240
					this.preferences.insert_space_after_comma_in_method_declaration_parameters,
4241
					this.preferences.alignment_for_parameters_in_method_declaration);
4242
4243
				/*
4244
				 * Check for extra dimensions
4245
				 */
4246
				int extraDimensions = getDimensions();
4247
				if (extraDimensions != 0) {
4248
					 for (int i = 0; i < extraDimensions; i++) {
4249
					 	this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
4250
					 	this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
4251
					 }
4267
				}
4252
				}
4268
				this.scribe.printComment();
4253
4269
				if (this.preferences.indent_statements_compare_to_body) {
4254
				formatThrowsClause(
4270
					this.scribe.unIndent();
4255
					methodDeclaration,
4256
					this.preferences.insert_space_before_comma_in_method_declaration_throws,
4257
					this.preferences.insert_space_after_comma_in_method_declaration_throws,
4258
					this.preferences.alignment_for_throws_clause_in_method_declaration);
4259
4260
				if (!methodDeclaration.isNative() && !methodDeclaration.isAbstract() && ((methodDeclaration.modifiers & ExtraCompilerModifiers.AccSemicolonBody) == 0)) {
4261
					/*
4262
					 * Method body
4263
					 */
4264
					String method_declaration_brace = this.preferences.brace_position_for_method_declaration;
4265
					formatLeftCurlyBrace(line, method_declaration_brace);
4266
					formatOpeningBrace(method_declaration_brace, this.preferences.insert_space_before_opening_brace_in_method_declaration);
4267
					final int numberOfBlankLinesAtBeginningOfMethodBody = this.preferences.blank_lines_at_beginning_of_method_body;
4268
					if (numberOfBlankLinesAtBeginningOfMethodBody > 0) {
4269
						this.scribe.printEmptyLines(numberOfBlankLinesAtBeginningOfMethodBody);
4270
					}
4271
					final Statement[] statements = methodDeclaration.statements;
4272
					if (statements != null) {
4273
						this.scribe.printNewLine();
4274
						if (this.preferences.indent_statements_compare_to_body) {
4275
							this.scribe.indent();
4276
						}
4277
						formatStatements(methodDeclarationScope, statements, true);
4278
						this.scribe.printComment();
4279
						if (this.preferences.indent_statements_compare_to_body) {
4280
							this.scribe.unIndent();
4281
						}
4282
					} else if (this.preferences.insert_new_line_in_empty_method_body) {
4283
						this.scribe.printNewLine();
4284
						if (this.preferences.indent_statements_compare_to_body) {
4285
							this.scribe.indent();
4286
						}
4287
						this.scribe.printComment();
4288
						if (this.preferences.indent_statements_compare_to_body) {
4289
							this.scribe.unIndent();
4290
						}
4291
					}
4292
					this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE);
4293
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
4294
					if (method_declaration_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) {
4295
						this.scribe.unIndent();
4296
					}
4297
				} else {
4298
					// no method body
4299
					this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
4300
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.COMPLEX_TRAILING_COMMENT);
4271
				}
4301
				}
4302
				ok = true;
4303
			} catch (AlignmentException e) {
4304
				this.scribe.redoAlignment(e);
4272
			}
4305
			}
4273
			this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE);
4306
		} while (!ok);
4274
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
4307
		this.scribe.exitAlignment(methodDeclAlignment, true);
4275
			if (method_declaration_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) {
4276
				this.scribe.unIndent();
4277
			}
4278
		} else {
4279
			// no method body
4280
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
4281
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.COMPLEX_TRAILING_COMMENT);
4282
		}
4283
		return false;
4308
		return false;
4284
	}
4309
	}
4310
4285
	public boolean visit(NormalAnnotation annotation, BlockScope scope) {
4311
	public boolean visit(NormalAnnotation annotation, BlockScope scope) {
4286
		this.scribe.printNextToken(TerminalTokens.TokenNameAT);
4312
		this.scribe.printNextToken(TerminalTokens.TokenNameAT);
4287
		if (this.preferences.insert_space_after_at_in_annotation) {
4313
		if (this.preferences.insert_space_after_at_in_annotation) {

Return to bug 284789