### 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 20 Jan 2011 15:30:55 -0000 @@ -1,13 +1,14 @@ /******************************************************************************* - * Copyright (c) 2000, 2010 IBM Corporation and others. + * Copyright (c) 2000, 2011 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * IBM Corporation - initial API and implementation - * Brock Janiczak - Contribution for bug 150741 + * IBM Corporation - initial API and implementation + * Brock Janiczak - Contribution for bug 150741 + * Ray V. (voidstar@gmail.com) - Contribution for bug 282988 *******************************************************************************/ package org.eclipse.jdt.core.formatter; @@ -673,6 +674,19 @@ /** *
+ * FORMATTER / Option to control whether the white space between code and line comments should be preserved or replaced with a single space + * - option id: "org.eclipse.jdt.core.formatter.comment.preserve_white_space_between_code_and_line_comments" + * - possible values: { TRUE, FALSE } + * - default: FALSE + *+ * @see #TRUE + * @see #FALSE + * @since 3.7 + */ + public final static String FORMATTER_COMMENT_PRESERVE_WHITE_SPACE_BETWEEN_CODE_AND_LINE_COMMENT = "org.eclipse.jdt.core.formatter.comment.preserve_white_space_between_code_and_line_comments"; //$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 20 Jan 2011 15:30:55 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2010 IBM Corporation and others. + * Copyright (c) 2000, 2011 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -8,6 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation * Brock Janiczak - Contribution for bug 150741 + * Ray V. (voidstar@gmail.com) - Contribution for bug 282988 *******************************************************************************/ package org.eclipse.jdt.internal.formatter; @@ -115,6 +116,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_white_space_between_code_and_line_comments; public int comment_line_length; public boolean use_tags; @@ -398,6 +400,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_WHITE_SPACE_BETWEEN_CODE_AND_LINE_COMMENT, this.comment_preserve_white_space_between_code_and_line_comments ? 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 +1134,10 @@ if (commentInsertNewLineForParameterOption != null) { this.comment_insert_new_line_for_parameter = JavaCore.INSERT.equals(commentInsertNewLineForParameterOption); } + final Object commentPreserveWhiteSpaceBetweenCodeAndLineCommentsOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_COMMENT_PRESERVE_WHITE_SPACE_BETWEEN_CODE_AND_LINE_COMMENT); + if (commentPreserveWhiteSpaceBetweenCodeAndLineCommentsOption != null) { + this.comment_preserve_white_space_between_code_and_line_comments = DefaultCodeFormatterConstants.TRUE.equals(commentPreserveWhiteSpaceBetweenCodeAndLineCommentsOption); + } final Object commentLineLengthOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH); if (commentLineLengthOption != null) { try { @@ -2132,6 +2139,7 @@ this.comment_new_lines_at_block_boundaries = true; this.comment_new_lines_at_javadoc_boundaries = true; this.comment_line_length = 80; + this.comment_preserve_white_space_between_code_and_line_comments= false; this.continuation_indentation = 2; this.continuation_indentation_for_array_initializer = 2; this.blank_lines_after_imports = 0; @@ -2410,6 +2418,7 @@ this.comment_new_lines_at_block_boundaries = true; this.comment_new_lines_at_javadoc_boundaries = true; this.comment_line_length = 80; + this.comment_preserve_white_space_between_code_and_line_comments= false; this.continuation_indentation = 2; this.continuation_indentation_for_array_initializer = 2; this.blank_lines_after_imports = 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.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 20 Jan 2011 15:30:55 -0000 @@ -7,6 +7,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Ray V. (voidstar@gmail.com) - Contribution for bug 282988 *******************************************************************************/ package org.eclipse.jdt.internal.formatter; @@ -2810,7 +2811,11 @@ // Add pending space if necessary if (this.pendingSpace) { - addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$ + if (this.formatter.preferences.comment_preserve_white_space_between_code_and_line_comments) { + 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 20 Jan 2011 15:30:59 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2010 IBM Corporation and others. + * Copyright (c) 2000, 2011 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,6 +7,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Ray V. (voidstar@gmail.com) - Contribution for bug 282988 *******************************************************************************/ package org.eclipse.jdt.core.tests.formatter; @@ -10429,4 +10430,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_white_space_between_code_and_line_comments = 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 20 Jan 2011 15:30:59 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2010 IBM Corporation and others. + * Copyright (c) 2000, 2011 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -8,6 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation * Brock Janiczak - Contribution for bug 150741 + * Ray V. (voidstar@gmail.com) - Contribution for bug 282988 *******************************************************************************/ package org.eclipse.jdt.core.tests.formatter; @@ -61,8 +62,8 @@ Map formatterOptions; static { -// TESTS_NUMBERS = new int[] { 730 }; -// TESTS_RANGE = new int[] { 730, -1 }; +// TESTS_NUMBERS = new int[] { 736 }; + TESTS_RANGE = new int[] { 734, -1 }; } public static Test suite() { return buildModelTestSuite(FormatterRegressionTests.class); @@ -10981,4 +10982,124 @@ "}" ); } +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=282988 +public void test734() { + this.formatterPrefs = null; + this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_PRESERVE_WHITE_SPACE_BETWEEN_CODE_AND_LINE_COMMENT, 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" + ); +} +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=282988 +public void test735() { + this.formatterPrefs = null; + this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_PRESERVE_WHITE_SPACE_BETWEEN_CODE_AND_LINE_COMMENT, DefaultCodeFormatterConstants.FALSE); + 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" + ); +} +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=282988 +public void test736() { + this.formatterPrefs = null; + this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_PRESERVE_WHITE_SPACE_BETWEEN_CODE_AND_LINE_COMMENT, 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" + ); +} +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=282988 +public void test737() { + this.formatterPrefs = null; + this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_PRESERVE_WHITE_SPACE_BETWEEN_CODE_AND_LINE_COMMENT, DefaultCodeFormatterConstants.FALSE); + 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" + ); +} }