### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: buildnotes_jdt-core.html =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/buildnotes_jdt-core.html,v retrieving revision 1.7335 diff -u -r1.7335 buildnotes_jdt-core.html --- buildnotes_jdt-core.html 25 Feb 2010 15:27:16 -0000 1.7335 +++ buildnotes_jdt-core.html 26 Feb 2010 15:54:21 -0000 @@ -103,6 +103,54 @@ String[] getBoundsSignatures() throws JavaModelException; +
  • +Added new formatter preferences to enable or disable the formatting of block and +line comments that start on the first column.
    +Note that disable the formatting of the comments also disable their indentation +otherwise the formatter could not produce stable outputs... +

    +The default is to format these kinds of comments to have a backward compatible +behavior. +

    +These new preferences are controlled with the options:

    +DefaultCodeFormatterConstants.FORMATTER_NEVER_FORMAT_BLOCK_COMMENTS_ON_FIRST_COLUMN
    +DefaultCodeFormatterConstants.FORMATTER_NEVER_FORMAT_LINE_COMMENTS_ON_FIRST_COLUMN +
    +/**
    + * FORMATTER / Option to format block comments that start on the first column
    + *     - option id:         "org.eclipse.jdt.core.formatter.formatter.never_format_block_comments_on_first_column"
    + *     - possible values:   { TRUE, FALSE }
    + *     - default:           FALSE
    + * 
    + * Note that this option is ignored if the formatter is created with the mode {@link ToolFactory#M_FORMAT_NEW}.
    + * @see #TRUE
    + * @see #FALSE
    + * @see ToolFactory#createCodeFormatter(Map, int)
    + * @since 3.6
    + */
    +
    +/**
    + * FORMATTER / Option to format line comments that start on the first column
    + *     - option id:         "org.eclipse.jdt.core.formatter.formatter.never_format_line_comments_on_first_column"
    + *     - possible values:   { TRUE, FALSE }
    + *     - default:           FALSE
    + * 
    + * Note that this option is ignored if the formatter is created with the mode {@link ToolFactory#M_FORMAT_NEW}.
    + * @see #TRUE
    + * @see #FALSE
    + * @see ToolFactory#createCodeFormatter(Map, int)
    + * @since 3.6
    + */
    +

    For example, the following snippet:

    +
    +public class X01 {
    +//    int	a  =   1;
    +/*    int	b  =   2; */
    +}
    +
    +will be untouched by the formatter if both options are activated. +See bug 251133 for more details. +
  • Problem Reports Fixed

    Index: formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java,v retrieving revision 1.105 diff -u -r1.105 DefaultCodeFormatterConstants.java --- formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java 23 Feb 2010 15:04:49 -0000 1.105 +++ formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java 26 Feb 2010 15:54:23 -0000 @@ -3138,12 +3138,41 @@ public static final String FORMATTER_LINE_SPLIT = JavaCore.PLUGIN_ID + ".formatter.lineSplit"; //$NON-NLS-1$ /** *
    +	 * FORMATTER / Option to format block comments that start on the first column
    +	 *     - option id:         "org.eclipse.jdt.core.formatter.formatter.never_format_block_comments_on_first_column"
    +	 *     - possible values:   { TRUE, FALSE }
    +	 *     - default:           FALSE
    +	 * 
    + * Note that this option is ignored if the formatter is created with the mode {@link ToolFactory#M_FORMAT_NEW}. + * @see #TRUE + * @see #FALSE + * @see ToolFactory#createCodeFormatter(Map, int) + * @since 3.6 + */ + public static final String FORMATTER_NEVER_FORMAT_BLOCK_COMMENTS_ON_FIRST_COLUMN = JavaCore.PLUGIN_ID + ".formatter.never_format_block_comments_on_first_column"; //$NON-NLS-1$ + /** + *
    +	 * FORMATTER / Option to format line comments that start on the first column
    +	 *     - option id:         "org.eclipse.jdt.core.formatter.formatter.never_format_line_comments_on_first_column"
    +	 *     - possible values:   { TRUE, FALSE }
    +	 *     - default:           FALSE
    +	 * 
    + * Note that this option is ignored if the formatter is created with the mode {@link ToolFactory#M_FORMAT_NEW}. + * @see #TRUE + * @see #FALSE + * @see ToolFactory#createCodeFormatter(Map, int) + * @since 3.6 + */ + public static final String FORMATTER_NEVER_FORMAT_LINE_COMMENTS_ON_FIRST_COLUMN = JavaCore.PLUGIN_ID + ".formatter.never_format_line_comments_on_first_column"; //$NON-NLS-1$ + /** + *
     	 * FORMATTER / Option to indent block comments that start on the first column
     	 *     - option id:         "org.eclipse.jdt.core.formatter.formatter.never_indent_block_comments_on_first_column"
     	 *     - possible values:   { TRUE, FALSE }
     	 *     - default:           FALSE
     	 * 
    - * Note that this option is ignored if the formatter is created with the mode {@link ToolFactory#M_FORMAT_NEW}. + * Note that this option is ignored if the formatter is created with the mode {@link ToolFactory#M_FORMAT_NEW} + * or the {@link #FORMATTER_NEVER_FORMAT_BLOCK_COMMENTS_ON_FIRST_COLUMN} is set to {@value #TRUE}. * @see #TRUE * @see #FALSE * @see ToolFactory#createCodeFormatter(Map, int) @@ -3157,7 +3186,8 @@ * - possible values: { TRUE, FALSE } * - default: FALSE * - * Note that this option is ignored if the formatter is created with the mode {@link ToolFactory#M_FORMAT_NEW}. + * Note that this option is ignored if the formatter is created with the mode {@link ToolFactory#M_FORMAT_NEW} + * or the {@link #FORMATTER_NEVER_FORMAT_LINE_COMMENTS_ON_FIRST_COLUMN} is set to {@value #TRUE}. * @see #TRUE * @see #FALSE * @see ToolFactory#createCodeFormatter(Map, int) Index: formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java,v retrieving revision 1.101 diff -u -r1.101 DefaultCodeFormatterOptions.java --- formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java 23 Feb 2010 15:04:52 -0000 1.101 +++ formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java 26 Feb 2010 15:54:25 -0000 @@ -305,6 +305,8 @@ public boolean keep_empty_array_initializer_on_one_line; public boolean keep_simple_if_on_one_line; public boolean keep_then_statement_on_same_line; + public boolean never_format_block_comments_on_first_column; + public boolean never_format_line_comments_on_first_column; public boolean never_indent_block_comments_on_first_column; public boolean never_indent_line_comments_on_first_column; public int number_of_empty_lines_to_preserve; @@ -589,6 +591,8 @@ options.put(DefaultCodeFormatterConstants.FORMATTER_KEEP_EMPTY_ARRAY_INITIALIZER_ON_ONE_LINE, this.keep_empty_array_initializer_on_one_line ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_KEEP_SIMPLE_IF_ON_ONE_LINE, this.keep_simple_if_on_one_line ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_KEEP_THEN_STATEMENT_ON_SAME_LINE, this.keep_then_statement_on_same_line ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); + options.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_FORMAT_BLOCK_COMMENTS_ON_FIRST_COLUMN, this.never_format_block_comments_on_first_column ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); + options.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_FORMAT_LINE_COMMENTS_ON_FIRST_COLUMN, this.never_format_line_comments_on_first_column ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_BLOCK_COMMENTS_ON_FIRST_COLUMN, this.never_indent_block_comments_on_first_column ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN, this.never_indent_line_comments_on_first_column ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE, Integer.toString(this.number_of_empty_lines_to_preserve)); @@ -1875,6 +1879,14 @@ if (keepThenStatementOnSameLineOption != null) { this.keep_then_statement_on_same_line = DefaultCodeFormatterConstants.TRUE.equals(keepThenStatementOnSameLineOption); } + final Object neverFormatBlockCommentOnFirstColumnOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_NEVER_FORMAT_BLOCK_COMMENTS_ON_FIRST_COLUMN); + if (neverFormatBlockCommentOnFirstColumnOption != null) { + this.never_format_block_comments_on_first_column = DefaultCodeFormatterConstants.TRUE.equals(neverFormatBlockCommentOnFirstColumnOption); + } + final Object neverFormatLineCommentOnFirstColumnOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_NEVER_FORMAT_LINE_COMMENTS_ON_FIRST_COLUMN); + if (neverFormatLineCommentOnFirstColumnOption != null) { + this.never_format_line_comments_on_first_column = DefaultCodeFormatterConstants.TRUE.equals(neverFormatLineCommentOnFirstColumnOption); + } final Object neverIndentBlockCommentOnFirstColumnOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_BLOCK_COMMENTS_ON_FIRST_COLUMN); if (neverIndentBlockCommentOnFirstColumnOption != null) { this.never_indent_block_comments_on_first_column = DefaultCodeFormatterConstants.TRUE.equals(neverIndentBlockCommentOnFirstColumnOption); @@ -2241,6 +2253,8 @@ this.keep_empty_array_initializer_on_one_line = false; this.keep_simple_if_on_one_line = false; this.keep_then_statement_on_same_line = false; + this.never_format_block_comments_on_first_column = false; + this.never_format_line_comments_on_first_column = false; this.never_indent_block_comments_on_first_column = false; this.never_indent_line_comments_on_first_column = false; this.number_of_empty_lines_to_preserve = 1; @@ -2511,6 +2525,8 @@ this.keep_empty_array_initializer_on_one_line = false; this.keep_simple_if_on_one_line = false; this.keep_then_statement_on_same_line = false; + this.never_format_block_comments_on_first_column = false; + this.never_format_line_comments_on_first_column = false; this.never_indent_block_comments_on_first_column = false; this.never_indent_line_comments_on_first_column = false; this.number_of_empty_lines_to_preserve = 1; Index: formatter/org/eclipse/jdt/internal/formatter/Scribe.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Scribe.java,v retrieving revision 1.194 diff -u -r1.194 Scribe.java --- formatter/org/eclipse/jdt/internal/formatter/Scribe.java 23 Feb 2010 11:35:53 -0000 1.194 +++ formatter/org/eclipse/jdt/internal/formatter/Scribe.java 26 Feb 2010 15:54:27 -0000 @@ -1417,9 +1417,11 @@ boolean indentComment = false; if (this.indentationLevel != 0) { - if (isJavadoc - || !this.formatter.preferences.never_indent_block_comments_on_first_column - || !onFirstColumn) { + if (isJavadoc || + !(onFirstColumn && + (this.formatter.preferences.never_format_block_comments_on_first_column + || this.formatter.preferences.never_indent_block_comments_on_first_column) + )) { indentComment = true; printIndentationIfNecessary(); } @@ -1431,7 +1433,7 @@ this.pendingSpace = false; int commentColumn = this.column; - if (includesBlockComments) { + if (includesBlockComments && !(onFirstColumn && this.formatter.preferences.never_format_block_comments_on_first_column)) { if (printBlockComment(currentTokenStartPosition, currentTokenEndPosition)) { return; } @@ -2406,11 +2408,14 @@ // Print comment line indentation int commentIndentationLevel; + boolean onFirstColumn = isOnFirstColumn(start); if (this.indentationLevel == 0) { commentIndentationLevel = this.column - 1; } else { - if (this.formatter.preferences.never_indent_line_comments_on_first_column && - isOnFirstColumn(start)) { + if (onFirstColumn && + (this.formatter.preferences.never_format_line_comments_on_first_column || + this.formatter.preferences.never_indent_line_comments_on_first_column) + ) { commentIndentationLevel = this.column - 1; } else { // Indentation may be specific for contiguous comment @@ -2469,7 +2474,7 @@ this.pendingSpace = false; int previousStart = currentTokenStartPosition; - if (!isNlsTag && includesLineComments) { + if (!isNlsTag && includesLineComments && !(onFirstColumn && this.formatter.preferences.never_format_line_comments_on_first_column)) { printLineComment(currentTokenStartPosition, currentTokenEndPosition-1); } else { // do nothing!? Index: model/org/eclipse/jdt/core/ToolFactory.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/core/ToolFactory.java,v retrieving revision 1.79 diff -u -r1.79 ToolFactory.java --- model/org/eclipse/jdt/core/ToolFactory.java 27 Jun 2008 16:04:01 -0000 1.79 +++ model/org/eclipse/jdt/core/ToolFactory.java 26 Feb 2010 15:54:27 -0000 @@ -163,6 +163,8 @@ Map currentOptions = new HashMap(options); if (mode == M_FORMAT_NEW) { // disable the option for not indenting comments starting on first column + currentOptions.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_FORMAT_BLOCK_COMMENTS_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.FALSE); + currentOptions.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_FORMAT_LINE_COMMENTS_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.FALSE); currentOptions.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_BLOCK_COMMENTS_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.FALSE); currentOptions.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.FALSE); } #P org.eclipse.jdt.core.tests.model Index: src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java,v retrieving revision 1.55 diff -u -r1.55 FormatterCommentsBugsTest.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java 23 Feb 2010 11:35:45 -0000 1.55 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java 26 Feb 2010 15:54:32 -0000 @@ -3621,6 +3621,104 @@ } /** + * @bug 251133: [formatter] Save actions fails to remove excess new lines when set to "format edited lines" + * @test Ensure that empty lines/spaces selection is well formatted + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=251133" + */ +public void testBug251133_Line01() throws JavaModelException { + String source = + "public class X01 {\n" + + "// int a = 1;\n" + + "// int b = 2;\n" + + "}"; + formatSource(source, + "public class X01 {\n" + + " // int a = 1;\n" + + " // int b = 2;\n" + + "}" + ); +} +public void testBug251133_Line02() throws JavaModelException { + this.formatterPrefs.never_indent_line_comments_on_first_column = true; + String source = + "public class X01 {\n" + + "// int a = 1;\n" + + "// int b = 2;\n" + + "}"; + formatSource(source, + "public class X01 {\n" + + "// int a = 1;\n" + + "// int b = 2;\n" + + "}" + ); +} +public void testBug251133_Line03() throws JavaModelException { + this.formatterPrefs.never_format_line_comments_on_first_column = true; + String source = + "public class X01 {\n" + + "// int a = 1;\n" + + "// int b = 2;\n" + + "}"; + formatSource(source); +} +public void testBug251133_Line04() throws JavaModelException { + this.formatterPrefs.never_indent_line_comments_on_first_column = false; + this.formatterPrefs.never_format_line_comments_on_first_column = true; + String source = + "public class X01 {\n" + + "// int a = 1;\n" + + "// int b = 2;\n" + + "}"; + formatSource(source); +} +public void testBug251133_Block01() throws JavaModelException { + String source = + "public class X01 {\n" + + "/* int a = 1; */\n" + + "/* int b = 2; */\n" + + "}"; + formatSource(source, + "public class X01 {\n" + + " /* int a = 1; */\n" + + " /* int b = 2; */\n" + + "}" + ); +} +public void testBug251133_Block02() throws JavaModelException { + this.formatterPrefs.never_indent_block_comments_on_first_column = true; + String source = + "public class X01 {\n" + + "/* int a = 1; */\n" + + "/* int b = 2; */\n" + + "}"; + formatSource(source, + "public class X01 {\n" + + "/* int a = 1; */\n" + + "/* int b = 2; */\n" + + "}" + ); +} +public void testBug251133_Block03() throws JavaModelException { + this.formatterPrefs.never_format_block_comments_on_first_column = true; + String source = + "public class X01 {\n" + + "/* int a = 1; */\n" + + "/* int b = 2; */\n" + + "}"; + formatSource(source); +} +public void testBug251133_Block04() throws JavaModelException { + this.formatterPrefs.never_indent_block_comments_on_first_column = false; + this.formatterPrefs.never_format_block_comments_on_first_column = true; + String source = + "public class X01 {\n" + + "/* int a = 1; */\n" + + "/* int b = 2; */\n" + + "}"; + formatSource(source); +} + +/** * @bug 256799: [formatter] Formatter wrongly adds space to //$FALL-THROUGH$ * @test Ensure that the comment formatter preserve $FALL-THROUGH$ tag leading spaces * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=256799"