### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core 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.119 diff -u -r1.119 DefaultCodeFormatterConstants.java --- formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java 3 Sep 2010 16:07:38 -0000 1.119 +++ formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java 17 Jan 2011 07:40:21 -0000 @@ -673,6 +673,19 @@ /** *
+ * FORMATTER / Option to control whether comments appearing at the end of lines should preserve their indentation or have that indentation replaced with a single space + * - option id: "org.eclipse.jdt.core.formatter.comment.preserve_trailing_line_comment_indentation" + * - possible values: { TRUE, FALSE } + * - default: TRUE + *+ * @see #TRUE + * @see #FALSE + * @since 3.7 + */ + public final static String FORMATTER_COMMENT_PRESERVE_TRAILING_LINE_COMMENT_INDENTATION = "org.eclipse.jdt.core.formatter.comment.preserve_trailing_line_comment_indentation"; //$NON-NLS-1$ + + /** + *
* FORMATTER / Option to control whether multiple lines comments are formatted * - option id: "org.eclipse.jdt.core.formatter.comment.format_block_comments" * - possible values: { TRUE, FALSE } 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.109 diff -u -r1.109 DefaultCodeFormatterOptions.java --- formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java 3 Sep 2010 15:44:26 -0000 1.109 +++ formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java 17 Jan 2011 07:40:23 -0000 @@ -115,6 +115,7 @@ public boolean comment_indent_root_tags; public boolean comment_insert_empty_line_before_root_tags; public boolean comment_insert_new_line_for_parameter; + public boolean comment_preserve_trailing_line_comment_indentation; public int comment_line_length; public boolean use_tags; @@ -398,6 +399,7 @@ options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_INDENT_ROOT_TAGS, this.comment_indent_root_tags ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_INSERT_EMPTY_LINE_BEFORE_ROOT_TAGS, this.comment_insert_empty_line_before_root_tags ? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_INSERT_NEW_LINE_FOR_PARAMETER, this.comment_insert_new_line_for_parameter ? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); + options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_PRESERVE_TRAILING_LINE_COMMENT_INDENTATION, this.comment_preserve_trailing_line_comment_indentation ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, Integer.toString(this.comment_line_length)); options.put(DefaultCodeFormatterConstants.FORMATTER_CONTINUATION_INDENTATION, Integer.toString(this.continuation_indentation)); options.put(DefaultCodeFormatterConstants.FORMATTER_CONTINUATION_INDENTATION_FOR_ARRAY_INITIALIZER, Integer.toString(this.continuation_indentation_for_array_initializer)); @@ -1131,6 +1133,10 @@ if (commentInsertNewLineForParameterOption != null) { this.comment_insert_new_line_for_parameter = JavaCore.INSERT.equals(commentInsertNewLineForParameterOption); } + final Object commentPreserveTrailingLineCommentIndentationOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_COMMENT_PRESERVE_TRAILING_LINE_COMMENT_INDENTATION); + if (commentPreserveTrailingLineCommentIndentationOption != null) { + this.comment_preserve_trailing_line_comment_indentation = DefaultCodeFormatterConstants.TRUE.equals(commentPreserveTrailingLineCommentIndentationOption); + } final Object commentLineLengthOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH); if (commentLineLengthOption != null) { try { 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.226 diff -u -r1.226 Scribe.java --- formatter/org/eclipse/jdt/internal/formatter/Scribe.java 5 Jan 2011 15:04:04 -0000 1.226 +++ formatter/org/eclipse/jdt/internal/formatter/Scribe.java 17 Jan 2011 07:40:25 -0000 @@ -2810,7 +2810,11 @@ // Add pending space if necessary if (this.pendingSpace) { - addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$ + if (this.formatter.preferences.comment_preserve_trailing_line_comment_indentation) { + addInsertEdit(currentTokenStartPosition, new String(this.lastLineComment.leadingSpaces)); + } else { + addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$ + } } this.needSpace = false; this.pendingSpace = false; #P org.eclipse.jdt.core.tests.model Index: src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java,v retrieving revision 1.43 diff -u -r1.43 FormatterBugsTests.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java 27 Dec 2010 15:11:44 -0000 1.43 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java 17 Jan 2011 07:40:29 -0000 @@ -10429,4 +10429,36 @@ "}\n" ); } + +/** + * @bug 282988: [formatter] Option to align single-line comments in a column + * @test Ensure that with line comment formatting turned off comment alignment doesn't change + * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=282988" + */ +public void testBug282988() throws Exception { + this.formatterPrefs.comment_preserve_trailing_line_comment_indentation = true; + String source = + "package test;\n" + + "\n" + + "public class FormatterError {\n" + + " public void storeSomething(java.nio.ByteBuffer buffer) throws Exception {\n" + + " buffer.clear();\n" + + " buffer.putLong(0); // backlink to previous version of this object\n" + + " buffer.putInt(1); // version identifier\n" + + " buffer.flip(); // prepare to write\n" + + " }\n" + + "}\n"; + formatSource(source, + "package test;\n" + + "\n" + + "public class FormatterError {\n" + + " public void storeSomething(java.nio.ByteBuffer buffer) throws Exception {\n" + + " buffer.clear();\n" + + " buffer.putLong(0); // backlink to previous version of this object\n" + + " buffer.putInt(1); // version identifier\n" + + " buffer.flip(); // prepare to write\n" + + " }\n" + + "}\n" + ); +} } Index: src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java,v retrieving revision 1.262 diff -u -r1.262 FormatterRegressionTests.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java 9 Nov 2010 14:24:24 -0000 1.262 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java 17 Jan 2011 07:40:36 -0000 @@ -10981,4 +10981,33 @@ "}" ); } +public void test734() { + this.formatterPrefs = null; + this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_PRESERVE_TRAILING_LINE_COMMENT_INDENTATION, DefaultCodeFormatterConstants.TRUE); + String source = + "package p;\n" + + "\n" + + "public class Comment {\n" + + " public static void main(String[] args) {\n" + + " // internal indentation\n" + + " int i = 1; // tabs\n" + + " int j = 2; // spaces\n" + + " int k = 3; // mixed tabs and spaces\n" + + " System.out.print(i); /* does not affect block comments */\n" + + " }\n" + + "}\n"; + formatSource(source, + "package p;\n" + + "\n" + + "public class Comment {\n" + + " public static void main(String[] args) {\n" + + " // internal indentation\n" + + " int i = 1; // tabs\n" + + " int j = 2; // spaces\n" + + " int k = 3; // mixed tabs and spaces\n" + + " System.out.print(i); /* does not affect block comments */\n" + + " }\n" + + "}\n" + ); +} }