1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 499553: Fix line comments used as a block on non-indented code

Change-Id: I059ba491e66722b286cfc9366cbfcd7efda535f6
Signed-off-by: Jonah Graham <jonah@kichwacoders.com>
This commit is contained in:
Jonah Graham 2016-08-11 14:36:34 +01:00 committed by Gerrit Code Review @ Eclipse.org
parent b34d04c0d5
commit 08b8dca563
7 changed files with 128 additions and 1 deletions

View file

@ -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$
/**
* <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.

View file

@ -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;

View file

@ -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) {

View file

@ -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();
}
}

View file

@ -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);

View file

@ -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;

View file

@ -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: