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 addd2a1d6a0..8be16e9d2a8 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 @@ -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$ + + /** + *
+ * 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 + *+ * @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$ + /** *
* 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$ + /** + *+ * 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 + *+ * @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$ /** ** FORMATTER / Option to insert a new line after template declaration 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 b6f77b62e32..6a21ac31e0e 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 @@ -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; diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java index d6518351cfd..8de7f59fa77 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java @@ -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; } 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 4d9d18be895..2922ba63733 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 @@ -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(); + } } 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 fecf76a9955..0bbea2c0003 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 @@ -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; 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 ffcf7dec9e5..4d6612f2b07 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 @@ -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 diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/IndentationTabPage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/IndentationTabPage.java index dc81a9aef77..0461084582e 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/IndentationTabPage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/IndentationTabPage.java @@ -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); } /* diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/NewLinesTabPage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/NewLinesTabPage.java index 31ea645cd9f..e5033777fa1 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/NewLinesTabPage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/NewLinesTabPage.java @@ -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);