diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java index 00778a40f40..d47a4a2c3ea 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java @@ -554,6 +554,19 @@ public class DefaultCodeFormatterConstants { */ public final static String FORMATTER_COMMENT_PRESERVE_WHITE_SPACE_BETWEEN_CODE_AND_LINE_COMMENT = CCorePlugin.PLUGIN_ID + ".formatter.comment.preserve_white_space_between_code_and_line_comments"; //$NON-NLS-1$ + /** + *
+ * FORMATTER / Option to control whether line comments on subsequent lines on unindented code should be treated as block comment + * - option id: "org.eclipse.cdt.core.formatter.comment.line_up_line_comment_in_blocks_on_first_column" + * - possible values: { TRUE, FALSE } + * - default: FALSE + *+ * @see #TRUE + * @see #FALSE + * @since 6.1 + */ + public final static String FORMATTER_COMMENT_LINE_UP_LINE_COMMENT_IN_BLOCKS_ON_FIRST_COLUMN = CCorePlugin.PLUGIN_ID + ".formatter.comment.line_up_line_comment_in_blocks_on_first_column"; //$NON-NLS-1$ + /** *
* FORMATTER / Option to control whether comments starting from the beginning of line should stay that way and never be indented. diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterOptions.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterOptions.java index 08266d3f276..830de249fcd 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterOptions.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterOptions.java @@ -106,6 +106,8 @@ public class DefaultCodeFormatterOptions { // public int comment_line_length; public int comment_min_distance_between_code_and_line_comment; public boolean comment_preserve_white_space_between_code_and_line_comment; + /** @since 6.1 */ + public boolean comment_line_up_line_comment_in_blocks_on_first_column; public boolean never_indent_line_comments_on_first_column; public int continuation_indentation; @@ -320,6 +322,7 @@ public class DefaultCodeFormatterOptions { // options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, Integer.toString(this.comment_line_length)); options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_MIN_DISTANCE_BETWEEN_CODE_AND_LINE_COMMENT, Integer.toString(this.comment_min_distance_between_code_and_line_comment)); options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_PRESERVE_WHITE_SPACE_BETWEEN_CODE_AND_LINE_COMMENT, this.comment_preserve_white_space_between_code_and_line_comment ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); + options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_UP_LINE_COMMENT_IN_BLOCKS_ON_FIRST_COLUMN, this.comment_line_up_line_comment_in_blocks_on_first_column ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN, this.never_indent_line_comments_on_first_column ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_CONTINUATION_INDENTATION, Integer.toString(this.continuation_indentation)); options.put(DefaultCodeFormatterConstants.FORMATTER_CONTINUATION_INDENTATION_FOR_INITIALIZER_LIST, Integer.toString(this.continuation_indentation_for_initializer_list)); @@ -854,6 +857,10 @@ public class DefaultCodeFormatterOptions { if (commentPreserveWhiteSpaceBetweenCodeAndLineCommentOption != null) { this.comment_preserve_white_space_between_code_and_line_comment = DefaultCodeFormatterConstants.TRUE.equals(commentPreserveWhiteSpaceBetweenCodeAndLineCommentOption); } + final Object commentLineUpLineCommentInBlocksOnFirstColumn = settings.get(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_UP_LINE_COMMENT_IN_BLOCKS_ON_FIRST_COLUMN); + if (commentLineUpLineCommentInBlocksOnFirstColumn != null) { + this.comment_line_up_line_comment_in_blocks_on_first_column = DefaultCodeFormatterConstants.TRUE.equals(commentLineUpLineCommentInBlocksOnFirstColumn); + } final Object neverIndentLineCommentsOnFirstColumn = settings.get(DefaultCodeFormatterConstants.FORMATTER_COMMENT_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN); if (neverIndentLineCommentsOnFirstColumn != null) { this.never_indent_line_comments_on_first_column = DefaultCodeFormatterConstants.TRUE.equals(neverIndentLineCommentsOnFirstColumn); @@ -1537,6 +1544,7 @@ public class DefaultCodeFormatterOptions { this.brace_position_for_type_declaration = DefaultCodeFormatterConstants.END_OF_LINE; this.comment_min_distance_between_code_and_line_comment = 1; this.comment_preserve_white_space_between_code_and_line_comment = true; + this.comment_line_up_line_comment_in_blocks_on_first_column = false; this.never_indent_line_comments_on_first_column = true; // this.comment_clear_blank_lines = false; // this.comment_format = true; diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java index 32f9234b67e..595906b3758 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java @@ -1318,7 +1318,7 @@ public class Scribe { // Print comment line indentation int commentIndentationLevel; boolean onFirstColumn = isOnFirstColumn(start); - if (indentationLevel == 0) { + if (!preferences.comment_line_up_line_comment_in_blocks_on_first_column && indentationLevel == 0) { commentIndentationLevel = column - 1; } else { if (onFirstColumn && preferences.never_indent_line_comments_on_first_column) { diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java index 57cd47c5477..a4636d1d369 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java @@ -3032,4 +3032,102 @@ public class CodeFormatterTest extends BaseUITestCase { String expected = before; assertFormatterResult(before, expected); } + + + //int abcde = 100; // line 1 of comment + // // line 2 of comment + + //int abcde = 100; // line 1 of comment + // // line 2 of comment + public void testLineCommentAsBlocks1a() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_UP_LINE_COMMENT_IN_BLOCKS_ON_FIRST_COLUMN, + DefaultCodeFormatterConstants.TRUE); + assertFormatterResult(); + } + + //int abcde = 100; // line 1 of comment + // // line 2 of comment + + //int abcde = 100; // line 1 of comment + //// line 2 of comment + public void testLineCommentAsBlocks1b() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_UP_LINE_COMMENT_IN_BLOCKS_ON_FIRST_COLUMN, + DefaultCodeFormatterConstants.FALSE); + assertFormatterResult(); + } + + //void x() { + // int abcde = 100; // line 1 of comment + // // line 2 of comment + //} + + //void x() { + // int abcde = 100; // line 1 of comment + // // line 2 of comment + //} + public void testLineCommentAsBlocks2() throws Exception { + assertFormatterResult(); + } + + // // line 1 of comment + //// line 2 of comment + + //// line 1 of comment + //// line 2 of comment + public void testLineCommentAsBlocks3() throws Exception { + assertFormatterResult(); + } + + //// line 1 of comment + // // line 2 of comment + + //// line 1 of comment + //// line 2 of comment + public void testLineCommentAsBlocks4() throws Exception { + assertFormatterResult(); + } + + // // line 1 of comment + // // line 2 of comment + + //// line 1 of comment + //// line 2 of comment + public void testLineCommentAsBlocks5() throws Exception { + assertFormatterResult(); + } + + //// line 1 of comment + //// line 2 of comment + + //// line 1 of comment + //// line 2 of comment + public void testLineCommentAsBlocks6() throws Exception { + assertFormatterResult(); + } + + //int abcde = 100; // line 1 of comment + // // line 2 of comment + // + //// line 1 of another comment block, delimited by blank line + + //int abcde = 100; // line 1 of comment + // // line 2 of comment + // + //// line 1 of another comment block, delimited by blank line + public void testLineCommentAsBlocks7() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_UP_LINE_COMMENT_IN_BLOCKS_ON_FIRST_COLUMN, + DefaultCodeFormatterConstants.TRUE); + assertFormatterResult(); + } + + //int main(void) { // line 1 of comment + // // line 2 of comment + //} + + //int main(void) { // line 1 of comment + // // line 2 of comment + //} + public void testLineCommentAsBlocks8() throws Exception { + assertFormatterResult(); + } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/CommentsTabPage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/CommentsTabPage.java index 1a7edbf81e7..fb93ad71ffe 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/CommentsTabPage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/CommentsTabPage.java @@ -72,8 +72,13 @@ public class CommentsTabPage extends FormatterTabPage { private final String PREVIEW= createPreviewHeader(FormatterMessages.CommentsTabPage_preview_header) + + "int gVariable = 100; \t\t// line 1 of comment\n" + //$NON-NLS-1$ + // needs as many tabs as indent size, consider case when tab indent size is 1 + "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t// line 2 of comment\n" + //$NON-NLS-1$ + "\n" + //$NON-NLS-1$ "void lineComments() {\n" + //$NON-NLS-1$ "\tprintf(\"%d\\n\", 1234); \t\t// Integer number\n" + //$NON-NLS-1$ + "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t// More here\n" + //$NON-NLS-1$ "\tprintf(\"%.5g\\n\", 12.34);\t\t// Floating point number\n" + //$NON-NLS-1$ "}\n"; //$NON-NLS-1$ @@ -96,6 +101,7 @@ public class CommentsTabPage extends FormatterTabPage { // final CheckboxPreference singleLineCommentsOnFirstColumn= createPrefFalseTrue(lineCommentGroup, numColumns, FormatterMessages.CommentsTabPage_format_line_comments_on_first_column, DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_LINE_COMMENT_STARTING_ON_FIRST_COLUMN, false); // ((GridData) singleLineCommentsOnFirstColumn.getControl().getLayoutData()).horizontalIndent= indent; createPrefFalseTrue(lineCommentGroup, numColumns, FormatterMessages.CommentsTabPage_preserve_white_space_before_line_comment, DefaultCodeFormatterConstants.FORMATTER_COMMENT_PRESERVE_WHITE_SPACE_BETWEEN_CODE_AND_LINE_COMMENT, false); + createPrefFalseTrue(lineCommentGroup, numColumns, FormatterMessages.CommentsTabPage_line_up_line_comment_in_blocks_on_first_column, DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_UP_LINE_COMMENT_IN_BLOCKS_ON_FIRST_COLUMN, false); createNumberPref(lineCommentGroup, numColumns, FormatterMessages.CommentsTabPage_line_width, DefaultCodeFormatterConstants.FORMATTER_COMMENT_MIN_DISTANCE_BETWEEN_CODE_AND_LINE_COMMENT, 0, 9999); // final CheckboxPreference singleLineComments= createPrefFalseTrue(lineCommentGroup, numColumns, FormatterMessages.CommentsTabPage_enable_line_comment_formatting, DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_LINE_COMMENT, false); // createPrefFalseTrue(lineCommentGroup, numColumns, FormatterMessages.CommentsTabPage_never_indent_line_comments_on_first_column, DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN, false); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.java index f03873769dc..2f079920610 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.java @@ -317,6 +317,7 @@ final class FormatterMessages extends NLS { public static String CommentsTabPage_preview_header; public static String CommentsTabPage_group1_title; public static String CommentsTabPage_preserve_white_space_before_line_comment; + public static String CommentsTabPage_line_up_line_comment_in_blocks_on_first_column; public static String CommentsTabPage_line_width; public static String CustomCodeFormatterBlock_formatter_name; public static String CustomCodeFormatterBlock_default_formatter; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.properties index 779d7ef9fe5..62303b9a144 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.properties @@ -368,6 +368,7 @@ CustomCodeFormatterBlock_contributed_formatter_warning=Contributed formatters ma CommentsTabPage_preview_header=Comments CommentsTabPage_group1_title=Line comments CommentsTabPage_preserve_white_space_before_line_comment=Preserve white space between code and line comments if possible +CommentsTabPage_line_up_line_comment_in_blocks_on_first_column=Treat indented line comments as block of comments on unindented code CommentsTabPage_line_width=Minimum distance between code and line comments: