1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

Bug 268404 - Added options for label formatting

Change-Id: I192ede1c17d2b1929dcb6fb33158eb7628707598
Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
This commit is contained in:
Marco Stornelli 2019-05-26 10:26:32 +02:00
parent dc62eb218d
commit a009b41021
8 changed files with 163 additions and 1 deletions

View file

@ -776,6 +776,21 @@ public class DefaultCodeFormatterConstants {
*/
public static final String FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_LINKAGE = CCorePlugin.PLUGIN_ID
+ ".formatter.indent_body_declarations_compare_to_linkage"; //$NON-NLS-1$
/**
* <pre>
* FORMATTER / Option to indent labels compare to statements where it is defined
* - option id: "org.eclipse.cdt.core.formatter.indent_label_compare_to_statements"
* - possible values: { TRUE, FALSE }
* - default: TRUE
* </pre>
* @see #TRUE
* @see #FALSE
* @since 6.8
*/
public static final String FORMATTER_INDENT_LABEL_COMPARE_TO_STATEMENTS = CCorePlugin.PLUGIN_ID
+ ".formatter.indent_label_compare_to_statements"; //$NON-NLS-1$
/**
* <pre>
* FORMATTER / Option to indent breaks compare to cases
@ -883,6 +898,19 @@ public class DefaultCodeFormatterConstants {
*/
public static final String FORMATTER_INSERT_NEW_LINE_AFTER_OPENING_BRACE_IN_INITIALIZER_LIST = CCorePlugin.PLUGIN_ID
+ ".formatter.insert_new_line_after_opening_brace_in_array_initializer";//$NON-NLS-1$
/**
* <pre>
* FORMATTER / Option to insert a new line after a label
* - option id: "org.eclipse.cdt.core.formatter.insert_new_line_after_label"
* - possible values: { INSERT, DO_NOT_INSERT }
* - default: DO_NOT_INSERT
* </pre>
* @see CCorePlugin#INSERT
* @see CCorePlugin#DO_NOT_INSERT
* @since 6.8
*/
public static final String FORMATTER_INSERT_NEW_LINE_AFTER_LABEL = CCorePlugin.PLUGIN_ID
+ ".formatter.insert_new_line_after_label";//$NON-NLS-1$
/**
* <pre>
* FORMATTER / Option to insert a new line after template declaration

View file

@ -139,6 +139,10 @@ public class DefaultCodeFormatterOptions {
public boolean indent_empty_lines;
public boolean indent_switchstatements_compare_to_cases;
public boolean indent_switchstatements_compare_to_switch;
/**
* @since 6.8
*/
public boolean indent_label_compare_to_statements;
public int indentation_size;
public boolean insert_new_line_after_opening_brace_in_initializer_list;
@ -155,6 +159,10 @@ public class DefaultCodeFormatterOptions {
public boolean insert_new_line_before_while_in_do_statement;
public boolean insert_new_line_before_identifier_in_function_declaration;
public boolean insert_new_line_in_empty_block;
/**
* @since 6.8
*/
public boolean insert_new_line_after_label;
// public boolean insert_new_line_in_empty_method_body;
// public boolean insert_new_line_in_empty_type_declaration;
public boolean insert_space_after_assignment_operator;
@ -442,6 +450,9 @@ public class DefaultCodeFormatterOptions {
options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_LINKAGE,
this.indent_body_declarations_compare_to_linkage ? DefaultCodeFormatterConstants.TRUE
: DefaultCodeFormatterConstants.FALSE);
options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_LABEL_COMPARE_TO_STATEMENTS,
this.indent_label_compare_to_statements ? DefaultCodeFormatterConstants.TRUE
: DefaultCodeFormatterConstants.FALSE);
options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BREAKS_COMPARE_TO_CASES,
this.indent_breaks_compare_to_cases ? DefaultCodeFormatterConstants.TRUE
: DefaultCodeFormatterConstants.FALSE);
@ -457,6 +468,8 @@ public class DefaultCodeFormatterOptions {
options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_OPENING_BRACE_IN_INITIALIZER_LIST,
this.insert_new_line_after_opening_brace_in_initializer_list ? CCorePlugin.INSERT
: CCorePlugin.DO_NOT_INSERT);
options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_LABEL,
this.insert_new_line_after_label ? CCorePlugin.INSERT : CCorePlugin.DO_NOT_INSERT);
options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_TEMPLATE_DECLARATION,
this.insert_new_line_after_template_declaration ? CCorePlugin.INSERT : CCorePlugin.DO_NOT_INSERT);
options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AT_END_OF_FILE_IF_MISSING,
@ -1321,6 +1334,12 @@ public class DefaultCodeFormatterOptions {
this.indent_body_declarations_compare_to_linkage = DefaultCodeFormatterConstants.TRUE
.equals(indentBodyDeclarationsCompareToLinkageOption);
}
final Object indentLabelCompareToStatementsOptions = settings
.get(DefaultCodeFormatterConstants.FORMATTER_INDENT_LABEL_COMPARE_TO_STATEMENTS);
if (indentLabelCompareToStatementsOptions != null) {
this.indent_label_compare_to_statements = DefaultCodeFormatterConstants.TRUE
.equals(indentLabelCompareToStatementsOptions);
}
final Object indentBreaksCompareToCasesOption = settings
.get(DefaultCodeFormatterConstants.FORMATTER_INDENT_BREAKS_COMPARE_TO_CASES);
if (indentBreaksCompareToCasesOption != null) {
@ -1359,6 +1378,11 @@ public class DefaultCodeFormatterOptions {
this.insert_new_line_after_opening_brace_in_initializer_list = CCorePlugin.INSERT
.equals(insertNewLineAfterOpeningBraceInInitializerListOption);
}
final Object insertNewLineAfterLabelOption = settings
.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_LABEL);
if (insertNewLineAfterLabelOption != null) {
this.insert_new_line_after_label = CCorePlugin.INSERT.equals(insertNewLineAfterLabelOption);
}
final Object insertNewLineAfterTemplateDeclarationOption = settings
.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_TEMPLATE_DECLARATION);
if (insertNewLineAfterOpeningBraceInInitializerListOption != null) {
@ -2253,6 +2277,7 @@ public class DefaultCodeFormatterOptions {
this.indent_statements_compare_to_body = true;
this.indent_body_declarations_compare_to_namespace_header = false;
this.indent_body_declarations_compare_to_linkage = false;
this.indent_label_compare_to_statements = true;
// this.indent_body_declarations_compare_to_enum_declaration_header = true;
this.indent_body_declarations_compare_to_access_specifier = true;
this.indent_breaks_compare_to_cases = true;
@ -2268,6 +2293,7 @@ public class DefaultCodeFormatterOptions {
this.insert_new_line_before_while_in_do_statement = false;
this.insert_new_line_before_identifier_in_function_declaration = false;
this.insert_new_line_in_empty_block = true;
this.insert_new_line_after_label = false;
// this.insert_new_line_in_empty_method_body = true;
// this.insert_new_line_in_empty_type_declaration = true;
this.insert_space_after_assignment_operator = true;
@ -2453,6 +2479,7 @@ public class DefaultCodeFormatterOptions {
this.indent_statements_compare_to_body = true;
this.indent_body_declarations_compare_to_namespace_header = false;
this.indent_body_declarations_compare_to_linkage = false;
this.indent_label_compare_to_statements = true;
// this.indent_body_declarations_compare_to_enum_declaration_header = true;
this.indent_breaks_compare_to_cases = true;
this.indent_empty_lines = false;
@ -2465,6 +2492,7 @@ public class DefaultCodeFormatterOptions {
this.insert_new_line_before_closing_brace_in_initializer_list = false;
this.insert_new_line_before_else_in_if_statement = true;
this.insert_new_line_in_empty_block = true;
this.insert_new_line_after_label = false;
// this.insert_new_line_in_empty_enum_declaration = true;
// this.insert_new_line_in_empty_method_body = true;
// this.insert_new_line_in_empty_type_declaration = true;
@ -2518,6 +2546,7 @@ public class DefaultCodeFormatterOptions {
this.indent_statements_compare_to_body = true;
this.indent_body_declarations_compare_to_namespace_header = true;
this.indent_body_declarations_compare_to_linkage = true;
this.indent_label_compare_to_statements = false;
// this.indent_body_declarations_compare_to_enum_declaration_header = true;
this.indent_declaration_compare_to_template_header = true;
this.indent_breaks_compare_to_cases = true;
@ -2537,6 +2566,7 @@ public class DefaultCodeFormatterOptions {
this.insert_new_line_before_identifier_in_function_declaration = true;
this.insert_new_line_before_while_in_do_statement = true;
this.insert_new_line_in_empty_block = true;
this.insert_new_line_after_label = false;
// this.insert_new_line_in_empty_enum_declaration = false;
// this.insert_new_line_in_empty_method_body = false;
// this.insert_new_line_in_empty_type_declaration = false;
@ -2596,6 +2626,7 @@ public class DefaultCodeFormatterOptions {
this.indent_statements_compare_to_body = false;
this.indent_body_declarations_compare_to_namespace_header = false;
this.indent_body_declarations_compare_to_linkage = false;
this.indent_label_compare_to_statements = true;
this.indent_breaks_compare_to_cases = true;
this.indent_empty_lines = false;
this.indent_switchstatements_compare_to_cases = true;
@ -2609,6 +2640,7 @@ public class DefaultCodeFormatterOptions {
this.insert_new_line_before_else_in_if_statement = true;
this.insert_new_line_before_while_in_do_statement = true;
this.insert_new_line_in_empty_block = true;
this.insert_new_line_after_label = false;
// this.insert_new_line_in_empty_enum_declaration = true;
// this.insert_new_line_in_empty_method_body = true;
// this.insert_new_line_in_empty_type_declaration = true;

View file

@ -3912,13 +3912,20 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
}
private int visit(IASTLabelStatement node) {
// TLETODO [formatter] label indentation
int indentationLevel = scribe.indentationLevel;
if (!preferences.indent_label_compare_to_statements) {
scribe.indentationLevel = 0;
}
formatLeadingAttributes(node);
node.getName().accept(this);
scribe.printNextToken(Token.tCOLON, preferences.insert_space_before_colon_in_labeled_statement);
if (preferences.insert_space_after_colon_in_labeled_statement) {
scribe.space();
}
if (preferences.insert_new_line_after_label) {
scribe.startNewLine();
}
scribe.indentationLevel = indentationLevel;
node.getNestedStatement().accept(this);
return PROCESS_SKIP;
}

View file

@ -4242,4 +4242,86 @@ public class CodeFormatterTest extends BaseUITestCase {
public void testSwitchNoParen_Bug353022() throws Exception {
assertFormatterResult();
}
//int main(int argc, char **argv) {
// goto foo;
// foo: for (int i = 0; i < 100; ++i) {
// goto foo;
// }
//}
//int main(int argc, char **argv) {
// goto foo;
// foo:
// for (int i = 0; i < 100; ++i) {
// goto foo;
// }
//}
public void testLabelIndentAndNewLine_Bug268404() throws Exception {
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_LABEL_COMPARE_TO_STATEMENTS,
DefaultCodeFormatterConstants.TRUE);
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_LABEL, CCorePlugin.INSERT);
assertFormatterResult();
}
//int main(int argc, char **argv) {
// goto foo;
// foo: for (int i = 0; i < 100; ++i) {
// goto foo;
// }
//}
//int main(int argc, char **argv) {
// goto foo;
//foo: for (int i = 0; i < 100; ++i) {
// goto foo;
// }
//}
public void testLabelNoIndentNoNewLine_Bug268404() throws Exception {
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_LABEL_COMPARE_TO_STATEMENTS,
DefaultCodeFormatterConstants.FALSE);
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_LABEL, CCorePlugin.DO_NOT_INSERT);
assertFormatterResult();
}
//int main(int argc, char **argv) {
// goto foo;
// foo: for (int i = 0; i < 100; ++i) {
// goto foo;
// }
//}
//int main(int argc, char **argv) {
// goto foo;
// foo: for (int i = 0; i < 100; ++i) {
// goto foo;
// }
//}
public void testLabelIndentNoNewLine_Bug268404() throws Exception {
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_LABEL_COMPARE_TO_STATEMENTS,
DefaultCodeFormatterConstants.TRUE);
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_LABEL, CCorePlugin.DO_NOT_INSERT);
assertFormatterResult();
}
//int main(int argc, char **argv) {
// goto foo;
// foo: for (int i = 0; i < 100; ++i) {
// goto foo;
// }
//}
//int main(int argc, char **argv) {
// goto foo;
//foo:
// for (int i = 0; i < 100; ++i) {
// goto foo;
// }
//}
public void testLabelNoIndentNewLine_Bug268404() throws Exception {
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_LABEL_COMPARE_TO_STATEMENTS,
DefaultCodeFormatterConstants.FALSE);
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_LABEL, CCorePlugin.INSERT);
assertFormatterResult();
}
}

View file

@ -381,6 +381,7 @@ final class FormatterMessages extends NLS {
public static String IndentationTabPage_switch_group_option_indent_break_statements;
public static String IndentationTabPage_namespace_group_option_indent_declarations_within_namespace;
public static String IndentationTabPage_linkage_group_option_indent_declarations_within_linkage;
public static String IndentationTabPage_indent_labels;
public static String IndentationTabPage_indent_empty_lines;
public static String IndentationTabPage_use_tabs_only_for_leading_indentations;
public static String ModifyDialog_dialog_title;
@ -405,6 +406,7 @@ final class FormatterMessages extends NLS {
public static String NewLinesTabPage_newlines_group_option_after_colon_in_constructor_initializer_list;
public static String NewLinesTabPage_newlines_emtpy_statement_on_new_line;
public static String NewLinesTabPage_newlines_before_identifier_in_function_declaration;
public static String NewLinesTabPage_newlines_after_label;
// public static String NewLinesTabPage_newlines_group_option_empty_class_body;
// public static String NewLinesTabPage_newlines_group_option_empty_method_body;
// public static String NewLinesTabPage_newlines_group_option_empty_block;

View file

@ -437,6 +437,7 @@ IndentationTabPage_block_group_option_indent_statements_compare_to_block=Stateme
IndentationTabPage_namespace_group_option_indent_declarations_within_namespace=Declarations within '&namespace' definition
IndentationTabPage_linkage_group_option_indent_declarations_within_linkage=Declarations within '&linkage' definition
IndentationTabPage_indent_empty_lines=Empty lines
IndentationTabPage_indent_labels=Labels
IndentationTabPage_switch_group_option_indent_statements_within_switch_body=Statements wit&hin 'switch' body
IndentationTabPage_switch_group_option_indent_statements_within_case_body=Statements within 'c&ase' body
@ -477,6 +478,7 @@ NewLinesTabPage_newlines_group_option_before_colon_in_constructor_initializer_li
NewLinesTabPage_newlines_group_option_after_colon_in_constructor_initializer_list=after colon in constructor initializer list
NewLinesTabPage_newlines_emtpy_statement_on_new_line=before empty statement
NewLinesTabPage_newlines_before_identifier_in_function_declaration=before identifier in function declaration
NewLinesTabPage_newlines_after_label=after label
#NewLinesTabPage_newlines_group_option_empty_class_body=in empty &class body
#NewLinesTabPage_newlines_group_option_empty_method_body=in empt&y method body
#NewLinesTabPage_newlines_group_option_empty_block=in empty &block

View file

@ -73,6 +73,8 @@ public class IndentationTabPage extends FormatterTabPage {
"break;" + //$NON-NLS-1$
"}" + //$NON-NLS-1$
"}" + //$NON-NLS-1$
"label: for (int i = 0; i < 10; i++)" + //$NON-NLS-1$
"goto label;" + //$NON-NLS-1$
"}" + //$NON-NLS-1$
"} // end namespace FOO\n\n" + //$NON-NLS-1$
"extern \"C\" {\n" + //$NON-NLS-1$
@ -170,6 +172,9 @@ public class IndentationTabPage extends FormatterTabPage {
createCheckboxPref(classGroup, numColumns, FormatterMessages.IndentationTabPage_indent_empty_lines,
DefaultCodeFormatterConstants.FORMATTER_INDENT_EMPTY_LINES, FALSE_TRUE);
createCheckboxPref(classGroup, numColumns, FormatterMessages.IndentationTabPage_indent_labels,
DefaultCodeFormatterConstants.FORMATTER_INDENT_LABEL_COMPARE_TO_STATEMENTS, FALSE_TRUE);
}
/*

View file

@ -26,6 +26,8 @@ public class NewLinesTabPage extends FormatterTabPage {
+ "class Point {" + //$NON-NLS-1$
"public:" + //$NON-NLS-1$
"Point(double x, double y) : x(x), y(y) {" + //$NON-NLS-1$
"label: for (int i = 0; i < 10; i++)" + //$NON-NLS-1$
"goto label;" + //$NON-NLS-1$
"}\n\n" + //$NON-NLS-1$
"private:" + //$NON-NLS-1$
"double x;" + //$NON-NLS-1$
@ -59,6 +61,8 @@ public class NewLinesTabPage extends FormatterTabPage {
FormatterMessages.NewLinesTabPage_newlines_before_identifier_in_function_declaration,
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_IDENTIFIER_IN_FUNCTION_DECLARATION,
DO_NOT_INSERT_INSERT);
createPref(newlinesGroup, numColumns, FormatterMessages.NewLinesTabPage_newlines_after_label,
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_LABEL, DO_NOT_INSERT_INSERT);
// createPref(newlinesGroup, numColumns, FormatterMessages.NewLinesTabPage_newlines_group_option_empty_class_body, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_TYPE_DECLARATION, DO_NOT_INSERT_INSERT);
// createPref(newlinesGroup, numColumns, FormatterMessages.NewLinesTabPage_newlines_group_option_empty_anonymous_class_body, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANONYMOUS_TYPE_DECLARATION, DO_NOT_INSERT_INSERT);
// createPref(newlinesGroup, numColumns, FormatterMessages.NewLinesTabPage_newlines_group_option_empty_method_body, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_METHOD_BODY, DO_NOT_INSERT_INSERT);