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

Collapse All | Expand All

(-)buildnotes_jdt-core.html (+63 lines)
Lines 50-55 Link Here
50
<h2>What's new in this drop</h2>
50
<h2>What's new in this drop</h2>
51
<ul>
51
<ul>
52
<li>
52
<li>
53
Added two new formatter preferences to condense block and javadoc comments.
54
<p>
55
These new preferences are controlled respectively with the options:</p>
56
<code>DefaultCodeFormatterConstants.FORMATTER_COMMENT_CONDENSED_BLOCK_COMMENT</code><br>
57
<code>DefaultCodeFormatterConstants.FORMATTER_COMMENT_CONDENSED_JAVADOC_COMMENT</code>
58
<pre>
59
/**
60
 * FORMATTER / Option to control whether block comments are condensed or not
61
 *     - option id:         "org.eclipse.jdt.core.formatter.comment.condensed_block_comment"
62
 *     - possible values:   { TRUE, FALSE }
63
 *     - default:           FALSE
64
 * 
65
 * @see #TRUE
66
 * @see #FALSE
67
 * @since 3.6
68
 */
69
70
/**
71
 * FORMATTER / Option to control whether javadoc comments are condensed or not
72
 *     - option id:         "org.eclipse.jdt.core.formatter.comment.condensed_javadoc_comment"
73
 *     - possible values:   { TRUE, FALSE }
74
 *     - default:           FALSE
75
 * 
76
 * @see #TRUE
77
 * @see #FALSE
78
 * @since 3.6
79
 */
80
 </pre>
81
<p>For example, when both of these options are used, the following snippet:</p>
82
<pre>
83
public class X {
84
	/*
85
	 * This block comment after formatting will no longer use a new line
86
	 * at the beginning and at the end of the comment...
87
	 */
88
	void foo() {
89
	}
90
	/**
91
	 * This javadoc comment after formatting will no longer use a new line
92
	 * at the beginning and at the end of the comment...
93
	 */
94
	void bar() {
95
	}
96
}
97
</pre>
98
formatted with this preference activated, will produce the following output:
99
<pre>
100
public class X {
101
	/* This block comment after formatting will no longer use a new line at the
102
	 * beginning and at the end of the comment... */
103
	void foo() {
104
	}
105
106
	/** This javadoc comment after formatting will no longer use a new line at
107
	 * the beginning and at the end of the comment... */
108
	void bar() {
109
	}
110
	}
111
}
112
</pre>
113
See bug <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=270209">270209</a> for more details.
114
</li>
115
<li>
53
The <code>CodeFormatter.F_INCLUDE_COMMENT</code> flag now works for all kind
116
The <code>CodeFormatter.F_INCLUDE_COMMENT</code> flag now works for all kind
54
of snippet while using the formatter.<br>
117
of snippet while using the formatter.<br>
55
See bug <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=236406">236406</a> for more details.
118
See bug <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=236406">236406</a> for more details.
(-)formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java (+26 lines)
Lines 605-610 Link Here
605
605
606
	/**
606
	/**
607
	 * <pre>
607
	 * <pre>
608
	 * FORMATTER / Option to control whether block comments are condensed or not
609
	 *     - option id:         "org.eclipse.jdt.core.formatter.comment.condensed_block_comment"
610
	 *     - possible values:   { TRUE, FALSE }
611
	 *     - default:           FALSE
612
	 * </pre>
613
	 * @see #TRUE
614
	 * @see #FALSE
615
	 * @since 3.6
616
	 */
617
	public final static String FORMATTER_COMMENT_CONDENSED_BLOCK_COMMENT = "org.eclipse.jdt.core.formatter.comment.condensed_block_comment"; //$NON-NLS-1$
618
619
	/**
620
	 * <pre>
621
	 * FORMATTER / Option to control whether javadoc comments are condensed or not
622
	 *     - option id:         "org.eclipse.jdt.core.formatter.comment.condensed_javadoc_comment"
623
	 *     - possible values:   { TRUE, FALSE }
624
	 *     - default:           FALSE
625
	 * </pre>
626
	 * @see #TRUE
627
	 * @see #FALSE
628
	 * @since 3.6
629
	 */
630
	public final static String FORMATTER_COMMENT_CONDENSED_JAVADOC_COMMENT = "org.eclipse.jdt.core.formatter.comment.condensed_javadoc_comment"; //$NON-NLS-1$
631
632
	/**
633
	 * <pre>
608
	 * FORMATTER / Option to control whether comments are formatted
634
	 * FORMATTER / Option to control whether comments are formatted
609
	 *     - option id:         "org.eclipse.jdt.core.formatter.comment.format_comments"
635
	 *     - option id:         "org.eclipse.jdt.core.formatter.comment.format_comments"
610
	 *     - possible values:   { TRUE, FALSE }
636
	 *     - possible values:   { TRUE, FALSE }
(-)formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java (+12 lines)
Lines 99-104 Link Here
99
99
100
	public boolean comment_clear_blank_lines_in_javadoc_comment;
100
	public boolean comment_clear_blank_lines_in_javadoc_comment;
101
	public boolean comment_clear_blank_lines_in_block_comment;
101
	public boolean comment_clear_blank_lines_in_block_comment;
102
	public boolean comment_condensed_block_comment;
103
	public boolean comment_condensed_javadoc_comment;
102
	public boolean comment_format_javadoc_comment;
104
	public boolean comment_format_javadoc_comment;
103
	public boolean comment_format_line_comment;
105
	public boolean comment_format_line_comment;
104
	public boolean comment_format_block_comment;
106
	public boolean comment_format_block_comment;
Lines 367-372 Link Here
367
		options.put(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_SWITCH, this.brace_position_for_switch);
369
		options.put(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_SWITCH, this.brace_position_for_switch);
368
		options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_BLOCK_COMMENT, this.comment_clear_blank_lines_in_block_comment ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
370
		options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_BLOCK_COMMENT, this.comment_clear_blank_lines_in_block_comment ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
369
		options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_JAVADOC_COMMENT, this.comment_clear_blank_lines_in_javadoc_comment ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
371
		options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_JAVADOC_COMMENT, this.comment_clear_blank_lines_in_javadoc_comment ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
372
		options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CONDENSED_BLOCK_COMMENT, this.comment_condensed_block_comment ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
373
		options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CONDENSED_JAVADOC_COMMENT, this.comment_condensed_javadoc_comment ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
370
		options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_JAVADOC_COMMENT, this.comment_format_javadoc_comment ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
374
		options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_JAVADOC_COMMENT, this.comment_format_javadoc_comment ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
371
		options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_LINE_COMMENT, this.comment_format_line_comment ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
375
		options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_LINE_COMMENT, this.comment_format_line_comment ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
372
		options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_BLOCK_COMMENT, this.comment_format_block_comment ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
376
		options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_BLOCK_COMMENT, this.comment_format_block_comment ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
Lines 1089-1094 Link Here
1089
				this.comment_line_length = 80;
1093
				this.comment_line_length = 80;
1090
			}
1094
			}
1091
		}
1095
		}
1096
		final Object commentCondensedBlockCommentOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CONDENSED_BLOCK_COMMENT);
1097
		if (commentCondensedBlockCommentOption != null) {
1098
			this.comment_condensed_block_comment = DefaultCodeFormatterConstants.TRUE.equals(commentCondensedBlockCommentOption);
1099
		}
1100
		final Object commentCondensedJavadocCommentOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CONDENSED_JAVADOC_COMMENT);
1101
		if (commentCondensedJavadocCommentOption != null) {
1102
			this.comment_condensed_javadoc_comment = DefaultCodeFormatterConstants.TRUE.equals(commentCondensedJavadocCommentOption);
1103
		}
1092
		final Object indentStatementsCompareToBlockOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INDENT_STATEMENTS_COMPARE_TO_BLOCK);
1104
		final Object indentStatementsCompareToBlockOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INDENT_STATEMENTS_COMPARE_TO_BLOCK);
1093
		if (indentStatementsCompareToBlockOption != null) {
1105
		if (indentStatementsCompareToBlockOption != null) {
1094
			this.indent_statements_compare_to_block = DefaultCodeFormatterConstants.TRUE.equals(indentStatementsCompareToBlockOption);
1106
			this.indent_statements_compare_to_block = DefaultCodeFormatterConstants.TRUE.equals(indentStatementsCompareToBlockOption);
(-)formatter/org/eclipse/jdt/internal/formatter/Scribe.java (-7 / +14 lines)
Lines 1628-1633 Link Here
1628
		boolean firstWord = true;
1628
		boolean firstWord = true;
1629
		boolean clearBlankLines = this.formatter.preferences.comment_clear_blank_lines_in_block_comment;
1629
		boolean clearBlankLines = this.formatter.preferences.comment_clear_blank_lines_in_block_comment;
1630
		boolean joinLines = this.formatter.preferences.join_lines_in_comments;
1630
		boolean joinLines = this.formatter.preferences.join_lines_in_comments;
1631
		boolean condensed = this.formatter.preferences.comment_condensed_block_comment;
1631
		int scannerLine = Util.getLineNumber(this.scanner.currentPosition, this.lineEnds, 0, this.maxLines);
1632
		int scannerLine = Util.getLineNumber(this.scanner.currentPosition, this.lineEnds, 0, this.maxLines);
1632
		int firstLine = scannerLine;
1633
		int firstLine = scannerLine;
1633
		int lineNumber = scannerLine;
1634
		int lineNumber = scannerLine;
Lines 1687-1696 Link Here
1687
							this.column += tokensBuffer.length();
1688
							this.column += tokensBuffer.length();
1688
						}
1689
						}
1689
						// end of comment
1690
						// end of comment
1690
						if (multiLines || hasMultiLines) {
1691
						if (!condensed) {
1691
					    	buffer.append(this.lineSeparator);
1692
							if (multiLines || hasMultiLines) {
1692
					    	this.column = 1;
1693
						    	buffer.append(this.lineSeparator);
1693
					    	printIndentationIfNecessary(buffer);
1694
						    	this.column = 1;
1695
						    	printIndentationIfNecessary(buffer);
1696
							}
1694
						}
1697
						}
1695
						buffer.append(' ');
1698
						buffer.append(' ');
1696
						this.column += BLOCK_FOOTER_LENGTH + 1;
1699
						this.column += BLOCK_FOOTER_LENGTH + 1;
Lines 1736-1742 Link Here
1736
			int max;
1739
			int max;
1737
			lineNumber = Util.getLineNumber(this.scanner.currentPosition, this.lineEnds, scannerLine>1 ? scannerLine-2 : 0, this.maxLines);
1740
			lineNumber = Util.getLineNumber(this.scanner.currentPosition, this.lineEnds, scannerLine>1 ? scannerLine-2 : 0, this.maxLines);
1738
			if (lastTextLine == -1) {
1741
			if (lastTextLine == -1) {
1739
				linesGap = lineNumber - firstLine;
1742
				linesGap = condensed ? 0 : lineNumber - firstLine;
1740
				max = 0;
1743
				max = 0;
1741
			} else {
1744
			} else {
1742
				linesGap = lineNumber - lastTextLine;
1745
				linesGap = lineNumber - lastTextLine;
Lines 1874-1880 Link Here
1874
	}
1877
	}
1875
1878
1876
	private void printBlockCommentHeaderLine(StringBuffer buffer) {
1879
	private void printBlockCommentHeaderLine(StringBuffer buffer) {
1877
	    if (buffer.length() == 0) {
1880
		if (this.formatter.preferences.comment_condensed_block_comment) {
1881
			buffer.insert(0, ' ');
1882
			this.column++;
1883
		}
1884
	    else if (buffer.length() == 0) {
1878
	    	buffer.append(this.lineSeparator);
1885
	    	buffer.append(this.lineSeparator);
1879
	    	this.column = 1;
1886
	    	this.column = 1;
1880
	    	printIndentationIfNecessary(buffer);
1887
	    	printIndentationIfNecessary(buffer);
Lines 3276-3282 Link Here
3276
			printJavadocBlock(previousBlock);
3283
			printJavadocBlock(previousBlock);
3277
3284
3278
			// format the header and footer empty spaces
3285
			// format the header and footer empty spaces
3279
			int newLines = this.line > currentLine || javadoc.isMultiLine() ? 1 : 0;
3286
			int newLines = (!this.formatter.preferences.comment_condensed_javadoc_comment && (this.line > currentLine || javadoc.isMultiLine())) ? 1 : 0;
3280
			printJavadocGapLines(javadoc.textStart, firstBlockStart-1, newLines, this.formatter.preferences.comment_clear_blank_lines_in_javadoc_comment, false, null);
3287
			printJavadocGapLines(javadoc.textStart, firstBlockStart-1, newLines, this.formatter.preferences.comment_clear_blank_lines_in_javadoc_comment, false, null);
3281
			printJavadocGapLines(previousBlock.sourceEnd+1, javadoc.textEnd, newLines, this.formatter.preferences.comment_clear_blank_lines_in_javadoc_comment, true, null);
3288
			printJavadocGapLines(previousBlock.sourceEnd+1, javadoc.textEnd, newLines, this.formatter.preferences.comment_clear_blank_lines_in_javadoc_comment, true, null);
3282
		}
3289
		}
(-)src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java (+127 lines)
Lines 5558-5563 Link Here
5558
}
5558
}
5559
5559
5560
/**
5560
/**
5561
 * @bug 270209: [format] Condensed block comment formatting
5562
 * @test Verify that block and javadoc comments are formatted in condensed
5563
 * 		mode when the corresponding preferences is set
5564
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=270209"
5565
 */
5566
public void testBug270209_Block01() throws JavaModelException {
5567
	this.formatterPrefs.comment_condensed_block_comment = true;
5568
	String source = 
5569
		"public interface X01 {\n" + 
5570
		"\n" + 
5571
		"/* Instead of like this.  I use these a lot and\n" + 
5572
		" * this can take up a lot of space. */\n" + 
5573
		"void foo();\n" + 
5574
		"}\n";
5575
	formatSource(source,
5576
		"public interface X01 {\n" + 
5577
		"\n" + 
5578
		"	/* Instead of like this. I use these a lot and this can take up a lot of\n" + 
5579
		"	 * space. */\n" + 
5580
		"	void foo();\n" + 
5581
		"}\n"
5582
	);
5583
}
5584
public void testBug270209_Block02() throws JavaModelException {
5585
	this.formatterPrefs.comment_condensed_block_comment = true;
5586
	String source = 
5587
		"public interface X02 {\n" + 
5588
		"\n" + 
5589
		"/*\n" + 
5590
		" * Instead of like this.  I use these a lot and\n" + 
5591
		" * this can take up a lot of space.\n" + 
5592
		" */\n" + 
5593
		"void foo();\n" + 
5594
		"}\n";
5595
	formatSource(source,
5596
		"public interface X02 {\n" + 
5597
		"\n" + 
5598
		"	/* Instead of like this. I use these a lot and this can take up a lot of\n" + 
5599
		"	 * space. */\n" + 
5600
		"	void foo();\n" + 
5601
		"}\n"
5602
	);
5603
}
5604
public void testBug270209_Block03() throws JavaModelException {
5605
	this.formatterPrefs.comment_condensed_block_comment = true;
5606
	String source = 
5607
		"public interface X03 {\n" + 
5608
		"\n" + 
5609
		"/*\n" + 
5610
		" * \n" + 
5611
		" * Instead of like this.  I use these a lot and\n" + 
5612
		" * this can take up a lot of space.\n" + 
5613
		" * \n" + 
5614
		" */\n" + 
5615
		"void foo();\n" + 
5616
		"}\n";
5617
	formatSource(source,
5618
		"public interface X03 {\n" + 
5619
		"\n" + 
5620
		"	/* Instead of like this. I use these a lot and this can take up a lot of\n" + 
5621
		"	 * space. */\n" + 
5622
		"	void foo();\n" + 
5623
		"}\n"
5624
	);
5625
}
5626
public void testBug270209_Javadoc01() throws JavaModelException {
5627
	this.formatterPrefs.comment_condensed_javadoc_comment = true;
5628
	String source = 
5629
		"public interface X01 {\n" + 
5630
		"\n" + 
5631
		"/** Instead of like this.  I use these a lot and\n" + 
5632
		" * this can take up a lot of space. */\n" + 
5633
		"void foo();\n" + 
5634
		"}\n";
5635
	formatSource(source,
5636
		"public interface X01 {\n" + 
5637
		"\n" + 
5638
		"	/** Instead of like this. I use these a lot and this can take up a lot of\n" + 
5639
		"	 * space. */\n" + 
5640
		"	void foo();\n" + 
5641
		"}\n"
5642
	);
5643
}
5644
public void testBug270209_Javadoc02() throws JavaModelException {
5645
	this.formatterPrefs.comment_condensed_javadoc_comment = true;
5646
	String source = 
5647
		"public interface X02 {\n" + 
5648
		"\n" + 
5649
		"/**\n" + 
5650
		" * Instead of like this.  I use these a lot and\n" + 
5651
		" * this can take up a lot of space.\n" + 
5652
		" */\n" + 
5653
		"void foo();\n" + 
5654
		"}\n";
5655
	formatSource(source,
5656
		"public interface X02 {\n" + 
5657
		"\n" + 
5658
		"	/** Instead of like this. I use these a lot and this can take up a lot of\n" + 
5659
		"	 * space. */\n" + 
5660
		"	void foo();\n" + 
5661
		"}\n"
5662
	);
5663
}
5664
public void testBug270209_Javadoc03() throws JavaModelException {
5665
	this.formatterPrefs.comment_condensed_javadoc_comment = true;
5666
	String source = 
5667
		"public interface X03 {\n" + 
5668
		"\n" + 
5669
		"/**\n" + 
5670
		" * \n" + 
5671
		" * Instead of like this.  I use these a lot and\n" + 
5672
		" * this can take up a lot of space.\n" + 
5673
		" * \n" + 
5674
		" */\n" + 
5675
		"void foo();\n" + 
5676
		"}\n";
5677
	formatSource(source,
5678
		"public interface X03 {\n" + 
5679
		"\n" + 
5680
		"	/** Instead of like this. I use these a lot and this can take up a lot of\n" + 
5681
		"	 * space. */\n" + 
5682
		"	void foo();\n" + 
5683
		"}\n"
5684
	);
5685
}
5686
5687
/**
5561
 * @bug 273619: [formatter] Formatting repeats *} in javadoc
5688
 * @bug 273619: [formatter] Formatting repeats *} in javadoc
5562
 * @test Ensure that *} is not repeated while formatting
5689
 * @test Ensure that *} is not repeated while formatting
5563
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=273619"
5690
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=273619"

Return to bug 270209