View | Details | Raw Unified | Return to bug 305371
Collapse All | Expand All

(-)formatter/org/eclipse/jdt/internal/formatter/Scribe.java (-16 / +13 lines)
Lines 2217-2231 Link Here
2217
							// then it becomes not a good idea to change the trailing flag
2217
							// then it becomes not a good idea to change the trailing flag
2218
							if (trailing == BASIC_TRAILING_COMMENT && hasLineComment) {
2218
							if (trailing == BASIC_TRAILING_COMMENT && hasLineComment) {
2219
								int currentCommentIndentation = getCurrentIndentation(whiteSpaces, 0);
2219
								int currentCommentIndentation = getCurrentIndentation(whiteSpaces, 0);
2220
								int lastCommentIndentation = this.lastLineComment.currentIndentation;
2220
								int relativeIndentation = currentCommentIndentation - this.lastLineComment.currentIndentation;
2221
								if (this.tabLength > 0) {
2221
								if (this.tabLength == 0) {
2222
									if ((currentCommentIndentation % this.tabLength) == 0) {
2222
									canChangeTrailing = relativeIndentation == 0;
2223
										lastCommentIndentation = (lastCommentIndentation / this.tabLength) * this.tabLength;
2223
								} else {
2224
									} else {
2224
									canChangeTrailing = relativeIndentation > -this.tabLength;
2225
										currentCommentIndentation = ((currentCommentIndentation / this.tabLength) + 1) * this.tabLength;
2226
									}
2227
								}
2225
								}
2228
								canChangeTrailing = currentCommentIndentation >= lastCommentIndentation;
2229
							}
2226
							}
2230
							// if the trailing can be change, then look at the following tokens
2227
							// if the trailing can be change, then look at the following tokens
2231
							if (canChangeTrailing) {
2228
							if (canChangeTrailing) {
Lines 2494-2508 Link Here
2494
					int currentCommentIndentation = getCurrentIndentation(this.lastLineComment.leadingSpaces, 0);
2491
					int currentCommentIndentation = getCurrentIndentation(this.lastLineComment.leadingSpaces, 0);
2495
					// Keep the current comment indentation when over the previous contiguous line comment
2492
					// Keep the current comment indentation when over the previous contiguous line comment
2496
					// and the previous comment has not been reindented
2493
					// and the previous comment has not been reindented
2497
					int lastCommentIndentation = this.lastLineComment.currentIndentation;
2494
					int relativeIndentation = currentCommentIndentation - this.lastLineComment.currentIndentation;
2498
					if (this.tabLength > 0) {
2495
					boolean similarCommentsIndentation = false;
2499
						if ((currentCommentIndentation % this.tabLength) == 0) {
2496
					if (this.tabLength == 0) {
2500
							lastCommentIndentation = (lastCommentIndentation / this.tabLength) * this.tabLength;
2497
						similarCommentsIndentation = relativeIndentation == 0;
2501
						} else {
2498
					} else if (relativeIndentation > -this.tabLength) {
2502
							currentCommentIndentation = ((currentCommentIndentation / this.tabLength) + 1) * this.tabLength;
2499
						similarCommentsIndentation = this.formatter.preferences.comment_format_line_comment_starting_on_first_column ||
2503
						}
2500
							(currentCommentIndentation != 0 && this.lastLineComment.currentIndentation != 0);
2504
					}
2501
					}
2505
					if (currentCommentIndentation >= lastCommentIndentation && this.lastLineComment.indentation != this.indentationLevel) {
2502
					if (similarCommentsIndentation && this.lastLineComment.indentation != this.indentationLevel) {
2506
						int currentIndentationLevel = this.indentationLevel;
2503
						int currentIndentationLevel = this.indentationLevel;
2507
						this.indentationLevel = this.lastLineComment.indentation ;
2504
						this.indentationLevel = this.lastLineComment.indentation ;
2508
						printIndentationIfNecessary();
2505
						printIndentationIfNecessary();
(-)src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java (+58 lines)
Lines 6281-6284 Link Here
6281
	    "}\n");
6281
	    "}\n");
6282
}
6282
}
6283
6283
6284
/**
6285
 * @bug 305371: [formatter] Unexpected indentation of line comment
6286
 * @test Verify that comments with too different indentation are not considered as contiguous
6287
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=305371"
6288
 */
6289
public void testBug305371() {
6290
	this.formatterPrefs = null;
6291
	this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_LINE_COMMENT_STARTING_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.FALSE);
6292
	String source = 
6293
		"class X01 {\n" + 
6294
		"//  unformatted    comment    !\n" + 
6295
		"        //  formatted    comment    !\n" + 
6296
	    "}\n";
6297
	formatSource(source,
6298
		"class X01 {\n" + 
6299
		"//  unformatted    comment    !\n" + 
6300
		"	// formatted comment !\n" + 
6301
	    "}\n");
6302
}
6303
public void testBug305371b() {
6304
	this.formatterPrefs = null;
6305
	this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_LINE_COMMENT_STARTING_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.FALSE);
6306
	String source = 
6307
		"class X02 {\n" + 
6308
		"        //  formatted    comment    !\n" + 
6309
		"//  unformatted    comment    !\n" + 
6310
	    "}\n";
6311
	formatSource(source,
6312
		"class X02 {\n" + 
6313
		"	// formatted comment !\n" + 
6314
		"//  unformatted    comment    !\n" + 
6315
	    "}\n");
6316
}
6317
public void testBug305371c() {
6318
	String source = 
6319
		"class X03 {\n" + 
6320
		"        //  formatted    comment    1\n" + 
6321
		"    //  formatted    comment    2\n" + 
6322
	    "}\n";
6323
	formatSource(source,
6324
		"class X03 {\n" + 
6325
		"	// formatted comment 1\n" + 
6326
		"	// formatted comment 2\n" + 
6327
	    "}\n");
6328
}
6329
public void testBug305371d() {
6330
	String source = 
6331
		"class X04 {\n" + 
6332
		"    //  formatted    comment    1\n" + 
6333
		"        //  formatted    comment    2\n" + 
6334
	    "}\n";
6335
	formatSource(source,
6336
		"class X04 {\n" + 
6337
		"	// formatted comment 1\n" + 
6338
		"	// formatted comment 2\n" + 
6339
	    "}\n");
6340
}
6341
6284
}
6342
}

Return to bug 305371