Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonah Graham2016-08-11 13:36:34 +0000
committerGerrit Code Review @ Eclipse.org2016-08-12 10:04:57 +0000
commit08b8dca563e1701c09ba5d44a18de4033c72d3b9 (patch)
tree45a155131edb4737a3b19721be9a01695137f853
parentb34d04c0d587d829f4df413de94017739ad582b1 (diff)
downloadorg.eclipse.cdt-08b8dca563e1701c09ba5d44a18de4033c72d3b9.tar.gz
org.eclipse.cdt-08b8dca563e1701c09ba5d44a18de4033c72d3b9.tar.xz
org.eclipse.cdt-08b8dca563e1701c09ba5d44a18de4033c72d3b9.zip
Bug 499553: Fix line comments used as a block on non-indented code
Change-Id: I059ba491e66722b286cfc9366cbfcd7efda535f6 Signed-off-by: Jonah Graham <jonah@kichwacoders.com>
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java13
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterOptions.java8
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java2
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java98
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/CommentsTabPage.java6
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.java1
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.properties1
7 files changed, 128 insertions, 1 deletions
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
@@ -556,6 +556,19 @@ public class DefaultCodeFormatterConstants {
/**
* <pre>
+ * 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
+ * </pre>
+ * @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$
+
+ /**
+ * <pre>
* FORMATTER / Option to control whether comments starting from the beginning of line should stay that way and never be indented.
* - option id: "org.eclipse.cdt.core.formatter.comment.never_indent_line_comments_on_first_column"
* - possible values: { TRUE, FALSE }
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:

Back to the top