diff options
author | Mateusz Matela | 2018-11-05 22:08:55 +0000 |
---|---|---|
committer | Mateusz Matela | 2018-11-17 18:54:07 +0000 |
commit | 8ff9ccc5ce46767cc19538a21f925e5e217e35d6 (patch) | |
tree | 7be2eb1fae9851c63f40268dc6a9d674f44cea00 | |
parent | 4ca6ea52439bbf09da333dd0c955ea1b885c2b9d (diff) | |
download | eclipse.jdt.core-8ff9ccc5ce46767cc19538a21f925e5e217e35d6.tar.gz eclipse.jdt.core-8ff9ccc5ce46767cc19538a21f925e5e217e35d6.tar.xz eclipse.jdt.core-8ff9ccc5ce46767cc19538a21f925e5e217e35d6.zip |
Bug 205973 - [formatter] Allow to keep simple methods on one line (for
exemple simple getter or setter)
Change-Id: I193360509b5852e7d4e38ab1b417030dc1616b8e
Signed-off-by: Mateusz Matela <mateusz.matela@gmail.com>
24 files changed, 2322 insertions, 203 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java index 459097861e..6523f0e527 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java @@ -5723,7 +5723,7 @@ public void testBug298243() { */ public void testBug298844a() { setFormatLineCommentOnFirstColumn(); - this.formatterPrefs.insert_new_line_in_empty_method_body = false; + this.formatterPrefs.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; String source = "public class X01 {\n" + "public X01() {\n" + @@ -5739,7 +5739,7 @@ public void testBug298844a() { ); } public void testBug298844b() { - this.formatterPrefs.insert_new_line_in_empty_method_body = false; + this.formatterPrefs.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; String source = "public class X02 {\n" + "public void foo() {\n" + @@ -11767,7 +11767,7 @@ public void testBug472413() { * https://bugs.eclipse.org/475793 - [formatter] Incorrect whitespace after lambda block */ public void testBug475793() { - this.formatterPrefs.insert_new_line_in_empty_block = false; + this.formatterPrefs.keep_lambda_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; String source = "public class C {\r\n" + " public void f() {\r\n" + @@ -11786,7 +11786,7 @@ public void testBug475793() { * https://bugs.eclipse.org/475746 - [formatter] insert-space rules sometimes ignored with anonymous subclass or when Annotations present */ public void testBug475746() { - this.formatterPrefs.insert_new_line_in_empty_block = false; + this.formatterPrefs.keep_lambda_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; this.formatterPrefs.insert_space_after_opening_paren_in_method_invocation = true; this.formatterPrefs.insert_space_before_closing_paren_in_method_invocation = true; this.formatterPrefs.insert_space_after_opening_paren_in_method_declaration = true; diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java index bd7bb180a3..f027f9b64a 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java @@ -645,7 +645,7 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { public void test041() { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_type_declaration = false; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test041", "A.java");//$NON-NLS-1$ //$NON-NLS-2$ } @@ -653,7 +653,7 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { public void test042() { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); preferences.number_of_empty_lines_to_preserve = 0; - preferences.insert_new_line_in_empty_type_declaration = false; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; preferences.insert_space_before_opening_brace_in_block = true; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test042", "A.java");//$NON-NLS-1$ //$NON-NLS-2$ @@ -662,7 +662,7 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { public void test043() { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_type_declaration = false; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test043", "A.java");//$NON-NLS-1$ //$NON-NLS-2$ } @@ -1940,10 +1940,10 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); preferences.number_of_empty_lines_to_preserve = 0; preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_anonymous_type_declaration = false; - preferences.insert_new_line_in_empty_type_declaration = false; - preferences.insert_new_line_in_empty_method_body = false; - preferences.insert_new_line_in_empty_block = false; + preferences.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_code_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test173", "A.java", CodeFormatter.K_CLASS_BODY_DECLARATIONS);//$NON-NLS-1$ //$NON-NLS-2$ } @@ -1954,10 +1954,10 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { public void test174() { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_anonymous_type_declaration = false; - preferences.insert_new_line_in_empty_type_declaration = false; - preferences.insert_new_line_in_empty_method_body = false; - preferences.insert_new_line_in_empty_block = false; + preferences.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_code_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test174", "A.java", CodeFormatter.K_CLASS_BODY_DECLARATIONS);//$NON-NLS-1$ //$NON-NLS-2$ } @@ -1969,10 +1969,9 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); preferences.number_of_empty_lines_to_preserve = 0; preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_anonymous_type_declaration = false; - preferences.insert_new_line_in_empty_type_declaration = false; - preferences.insert_new_line_in_empty_method_body = false; - preferences.insert_new_line_in_empty_block = true; + preferences.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test175", "A.java", CodeFormatter.K_CLASS_BODY_DECLARATIONS);//$NON-NLS-1$ //$NON-NLS-2$ } @@ -1983,10 +1982,9 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { public void test176() { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_anonymous_type_declaration = false; - preferences.insert_new_line_in_empty_type_declaration = false; - preferences.insert_new_line_in_empty_method_body = true; - preferences.insert_new_line_in_empty_block = false; + preferences.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test176", "A.java", CodeFormatter.K_CLASS_BODY_DECLARATIONS);//$NON-NLS-1$ //$NON-NLS-2$ } @@ -1997,10 +1995,9 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { public void test177() { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_anonymous_type_declaration = false; - preferences.insert_new_line_in_empty_type_declaration = false; - preferences.insert_new_line_in_empty_method_body = false; - preferences.insert_new_line_in_empty_block = false; + preferences.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test177", "A.java", CodeFormatter.K_COMPILATION_UNIT);//$NON-NLS-1$ //$NON-NLS-2$ } @@ -2011,10 +2008,9 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { public void test178() { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_anonymous_type_declaration = false; - preferences.insert_new_line_in_empty_type_declaration = true; - preferences.insert_new_line_in_empty_method_body = false; - preferences.insert_new_line_in_empty_block = false; + preferences.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + preferences.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test178", "A.java", CodeFormatter.K_COMPILATION_UNIT);//$NON-NLS-1$ //$NON-NLS-2$ } @@ -2025,10 +2021,9 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { public void test179() { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_anonymous_type_declaration = true; - preferences.insert_new_line_in_empty_type_declaration = true; - preferences.insert_new_line_in_empty_method_body = false; - preferences.insert_new_line_in_empty_block = false; + preferences.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + preferences.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test179", "A.java", CodeFormatter.K_COMPILATION_UNIT);//$NON-NLS-1$ //$NON-NLS-2$ } @@ -2039,10 +2034,9 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { public void test180() { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_anonymous_type_declaration = false; - preferences.insert_new_line_in_empty_type_declaration = false; - preferences.insert_new_line_in_empty_method_body = false; - preferences.insert_new_line_in_empty_block = false; + preferences.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test180", "A.java", CodeFormatter.K_COMPILATION_UNIT);//$NON-NLS-1$ //$NON-NLS-2$ } @@ -4293,10 +4287,9 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); preferences.number_of_empty_lines_to_preserve = 0; preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_anonymous_type_declaration = false; - preferences.insert_new_line_in_empty_type_declaration = false; - preferences.insert_new_line_in_empty_method_body = false; - preferences.insert_new_line_in_empty_block = false; + preferences.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test319", "A.java", CodeFormatter.K_CLASS_BODY_DECLARATIONS);//$NON-NLS-1$ //$NON-NLS-2$ } @@ -4305,10 +4298,9 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); preferences.number_of_empty_lines_to_preserve = 0; preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_anonymous_type_declaration = false; - preferences.insert_new_line_in_empty_type_declaration = false; - preferences.insert_new_line_in_empty_method_body = false; - preferences.insert_new_line_in_empty_block = false; + preferences.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test320", "A.java", CodeFormatter.K_CLASS_BODY_DECLARATIONS);//$NON-NLS-1$ //$NON-NLS-2$ } @@ -4317,10 +4309,9 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); preferences.number_of_empty_lines_to_preserve = 0; preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_anonymous_type_declaration = false; - preferences.insert_new_line_in_empty_type_declaration = false; - preferences.insert_new_line_in_empty_method_body = false; - preferences.insert_new_line_in_empty_block = false; + preferences.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test321", "A.java", CodeFormatter.K_CLASS_BODY_DECLARATIONS);//$NON-NLS-1$ //$NON-NLS-2$ } @@ -4329,10 +4320,9 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); preferences.number_of_empty_lines_to_preserve = 0; preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_anonymous_type_declaration = false; - preferences.insert_new_line_in_empty_type_declaration = false; - preferences.insert_new_line_in_empty_method_body = false; - preferences.insert_new_line_in_empty_block = true; + preferences.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test322", "A.java", CodeFormatter.K_CLASS_BODY_DECLARATIONS);//$NON-NLS-1$ //$NON-NLS-2$ } @@ -4341,10 +4331,10 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); preferences.number_of_empty_lines_to_preserve = 0; preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_anonymous_type_declaration = false; - preferences.insert_new_line_in_empty_type_declaration = false; - preferences.insert_new_line_in_empty_method_body = false; - preferences.insert_new_line_in_empty_block = false; + preferences.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_code_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test323", "A.java", CodeFormatter.K_CLASS_BODY_DECLARATIONS);//$NON-NLS-1$ //$NON-NLS-2$ } @@ -4614,7 +4604,7 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { preferences.tab_char = DefaultCodeFormatterOptions.SPACE; preferences.blank_lines_before_method = 1; preferences.blank_lines_before_first_class_body_declaration = 1; - preferences.insert_new_line_in_empty_method_body = false; + preferences.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test347", "A.java", CodeFormatter.K_COMPILATION_UNIT);//$NON-NLS-1$ //$NON-NLS-2$ } @@ -4631,7 +4621,7 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { preferences.tab_char = DefaultCodeFormatterOptions.SPACE; preferences.blank_lines_before_method = 1; preferences.blank_lines_before_first_class_body_declaration = 1; - preferences.insert_new_line_in_empty_method_body = false; + preferences.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); runTest(codeFormatter, "test348", "A.java", CodeFormatter.K_COMPILATION_UNIT);//$NON-NLS-1$ //$NON-NLS-2$ } @@ -7273,9 +7263,9 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { Map options = DefaultCodeFormatterConstants.getJavaConventionsSettings(); DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_type_declaration = true; - preferences.insert_new_line_in_empty_enum_constant = false; - preferences.insert_new_line_in_empty_enum_declaration = false; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + preferences.keep_enum_constant_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_enum_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; preferences.tab_size = 4; Hashtable javaCoreOptions = JavaCore.getOptions(); try { @@ -7303,9 +7293,9 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { Map options = DefaultCodeFormatterConstants.getJavaConventionsSettings(); DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_type_declaration = true; - preferences.insert_new_line_in_empty_enum_constant = false; - preferences.insert_new_line_in_empty_enum_declaration = true; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + preferences.keep_enum_constant_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_enum_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; preferences.tab_size = 4; Hashtable javaCoreOptions = JavaCore.getOptions(); try { @@ -7333,9 +7323,9 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { Map options = DefaultCodeFormatterConstants.getJavaConventionsSettings(); DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_type_declaration = true; - preferences.insert_new_line_in_empty_enum_constant = true; - preferences.insert_new_line_in_empty_enum_declaration = false; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + preferences.keep_enum_constant_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + preferences.keep_enum_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; preferences.tab_size = 4; Hashtable javaCoreOptions = JavaCore.getOptions(); try { @@ -7363,9 +7353,9 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { Map options = DefaultCodeFormatterConstants.getJavaConventionsSettings(); DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); preferences.tab_char = DefaultCodeFormatterOptions.TAB; - preferences.insert_new_line_in_empty_type_declaration = true; - preferences.insert_new_line_in_empty_enum_constant = true; - preferences.insert_new_line_in_empty_enum_declaration = true; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + preferences.keep_enum_constant_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + preferences.keep_enum_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; preferences.tab_size = 4; Hashtable javaCoreOptions = JavaCore.getOptions(); try { @@ -8970,8 +8960,8 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); preferences.indent_body_declarations_compare_to_annotation_declaration_header = false; preferences.indent_body_declarations_compare_to_type_header = true; - preferences.insert_new_line_in_empty_annotation_declaration = false; - preferences.insert_new_line_in_empty_type_declaration = true; + preferences.keep_annotation_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; Hashtable javaCoreOptions = JavaCore.getOptions(); try { Hashtable newJavaCoreOptions = JavaCore.getOptions(); @@ -8996,8 +8986,8 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); preferences.indent_body_declarations_compare_to_annotation_declaration_header = true; preferences.indent_body_declarations_compare_to_type_header = false; - preferences.insert_new_line_in_empty_annotation_declaration = true; - preferences.insert_new_line_in_empty_type_declaration = false; + preferences.keep_annotation_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + preferences.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; Hashtable javaCoreOptions = JavaCore.getOptions(); try { Hashtable newJavaCoreOptions = JavaCore.getOptions(); @@ -14372,4 +14362,199 @@ public void testBug131292f() throws JavaModelException { String input = getCompilationUnit("Formatter", "", "test131292", "in.java").getSource(); formatSource(input, getCompilationUnit("Formatter", "", "test131292", "F_out.java").getSource()); } + +/** + * https://bugs.eclipse.org/205973 - [formatter] Allow to keep simple methods on one line (for exemple simple getter or setter) + */ +public void testBug205973a() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_1_8); + this.formatterPrefs.keep_annotation_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + this.formatterPrefs.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + this.formatterPrefs.keep_if_then_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + this.formatterPrefs.keep_lambda_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + this.formatterPrefs.keep_loop_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + this.formatterPrefs.keep_code_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_enum_constant_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_enum_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_simple_getter_setter_on_one_line = false; + this.formatterPrefs.keep_guardian_clause_on_one_line = false; + String input = getCompilationUnit("Formatter", "", "test205973", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test205973", "A_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/205973 - [formatter] Allow to keep simple methods on one line (for exemple simple getter or setter) + */ +public void testBug205973b() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_1_8); + this.formatterPrefs.keep_annotation_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_if_then_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_lambda_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_SINGLE_ITEM; + this.formatterPrefs.keep_loop_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_SINGLE_ITEM; + this.formatterPrefs.keep_code_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_enum_constant_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + this.formatterPrefs.keep_enum_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + this.formatterPrefs.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_simple_getter_setter_on_one_line = true; + this.formatterPrefs.keep_guardian_clause_on_one_line = false; + String input = getCompilationUnit("Formatter", "", "test205973", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test205973", "B_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/205973 - [formatter] Allow to keep simple methods on one line (for exemple simple getter or setter) + */ +public void testBug205973c() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_1_8); + this.formatterPrefs.keep_annotation_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_SINGLE_ITEM; + this.formatterPrefs.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_SINGLE_ITEM; + this.formatterPrefs.keep_if_then_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_lambda_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_loop_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_code_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + this.formatterPrefs.keep_enum_constant_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_enum_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_SINGLE_ITEM; + this.formatterPrefs.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + this.formatterPrefs.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + this.formatterPrefs.keep_simple_getter_setter_on_one_line = false; + this.formatterPrefs.keep_guardian_clause_on_one_line = true; + String input = getCompilationUnit("Formatter", "", "test205973", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test205973", "C_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/205973 - [formatter] Allow to keep simple methods on one line (for exemple simple getter or setter) + */ +public void testBug205973d() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_1_8); + this.formatterPrefs.keep_annotation_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_if_then_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_lambda_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_SINGLE_ITEM; + this.formatterPrefs.keep_loop_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_code_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + this.formatterPrefs.keep_enum_constant_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_SINGLE_ITEM; + this.formatterPrefs.keep_enum_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_SINGLE_ITEM; + this.formatterPrefs.keep_simple_getter_setter_on_one_line = false; + this.formatterPrefs.keep_guardian_clause_on_one_line = false; + String input = getCompilationUnit("Formatter", "", "test205973", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test205973", "D_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/205973 - [formatter] Allow to keep simple methods on one line (for exemple simple getter or setter) + */ +public void testBug205973e() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_1_8); + this.formatterPrefs.keep_annotation_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_if_then_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + this.formatterPrefs.keep_lambda_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_loop_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_code_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_enum_constant_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_enum_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + this.formatterPrefs.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_simple_getter_setter_on_one_line = true; + this.formatterPrefs.keep_guardian_clause_on_one_line = true; + String input = getCompilationUnit("Formatter", "", "test205973", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test205973", "E_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/205973 - [formatter] Allow to keep simple methods on one line (for exemple simple getter or setter) + */ +public void testBug205973f() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_1_8); + this.formatterPrefs.keep_annotation_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + this.formatterPrefs.keep_if_then_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_lambda_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_loop_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_SINGLE_ITEM; + this.formatterPrefs.keep_code_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_enum_constant_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_enum_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_simple_getter_setter_on_one_line = false; + this.formatterPrefs.keep_guardian_clause_on_one_line = false; + String input = getCompilationUnit("Formatter", "", "test205973", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test205973", "F_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/205973 - [formatter] Allow to keep simple methods on one line (for exemple simple getter or setter) + */ +public void testBug205973g() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_1_8); + this.formatterPrefs.keep_annotation_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + this.formatterPrefs.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_if_then_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_SINGLE_ITEM; + this.formatterPrefs.keep_lambda_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_loop_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_code_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + this.formatterPrefs.keep_enum_constant_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_enum_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_SINGLE_ITEM; + this.formatterPrefs.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_simple_getter_setter_on_one_line = false; + this.formatterPrefs.keep_guardian_clause_on_one_line = false; + String input = getCompilationUnit("Formatter", "", "test205973", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test205973", "G_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/205973 - [formatter] Allow to keep simple methods on one line (for exemple simple getter or setter) + */ +public void testBug205973h() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_1_8); + this.formatterPrefs.keep_annotation_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_PRESERVE; + this.formatterPrefs.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_PRESERVE; + this.formatterPrefs.keep_if_then_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_PRESERVE; + this.formatterPrefs.keep_lambda_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_PRESERVE; + this.formatterPrefs.keep_loop_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_PRESERVE; + this.formatterPrefs.keep_code_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.formatterPrefs.keep_enum_constant_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_PRESERVE; + this.formatterPrefs.keep_enum_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_PRESERVE; + this.formatterPrefs.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_PRESERVE; + this.formatterPrefs.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_PRESERVE; + this.formatterPrefs.keep_simple_getter_setter_on_one_line = false; + this.formatterPrefs.keep_guardian_clause_on_one_line = false; + this.formatterPrefs.align_type_members_on_columns = true; + this.formatterPrefs.align_variable_declarations_on_columns = true; + this.formatterPrefs.align_assignment_statements_on_columns = true; + String input = getCompilationUnit("Formatter", "", "test205973", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test205973", "H_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/205973 - [formatter] Allow to keep simple methods on one line (for exemple simple getter or setter) + */ +public void testBug205973i() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_1_8); + this.formatterPrefs.page_width = 50; + this.formatterPrefs.use_tabs_only_for_leading_indentations = true; + this.formatterPrefs.keep_annotation_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_if_then_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_lambda_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_loop_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_code_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY; + this.formatterPrefs.keep_enum_constant_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_enum_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + this.formatterPrefs.keep_simple_getter_setter_on_one_line = false; + this.formatterPrefs.keep_guardian_clause_on_one_line = false; + String input = getCompilationUnit("Formatter", "", "test205973", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test205973", "I_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/205973 - [formatter] Allow to keep simple methods on one line (for exemple simple getter or setter) + */ +public void testBug205973j() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_1_8); + this.formatterPrefs.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + String input = getCompilationUnit("Formatter", "", "test205973", "J_in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test205973", "J_out.java").getSource()); +} } diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/A_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/A_out.java new file mode 100644 index 0000000000..89aa366a81 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/A_out.java @@ -0,0 +1,160 @@ +class Example { + public void example() { + for (int i = 0; i < 10; i++) {} + int a = 10; + while (a-- > 0) { + System.out.println(a); + } + do { + a += 2; + System.out.println(a); + } while (a < 50); + } +} + +class Example { + public String example(int a) { + if (a < 0) { + throw new IllegalArgumentException(); + } + if (a == 0) { + return null; + } + if (false) {} + if (a % 3 == 0) { + System.out.println("fizz"); + } + if (a % 5 == 0) { + System.out.println("buzz"); + return ""; + } + return Integer.toString(a); + } +} + +class Example { + Runnable emptyLambda = () -> {}; + Runnable emptyLambda2 = () -> {}; + Runnable tinyLambda = () -> { + doSomething(); + }; + Runnable smallLambda = () -> { + doFirstThing(); + doSecondThing(); + }; +} + +class Example { + static { + } + + void foo() { + if (true) { + } else { + } + synchronized (this) { + } + try { + } finally { + } + + labeled: { + } + } +} + +public class Example { + private int something; + + public int getSomething() { + return something; + } + + public void setSomehing(int something) { + this.something = something; + } + + public void doNoting() { + } + + public void doOneThing() { + System.out.println(); + } + + public void doMoreThings() { + something = 4; + doOneThing(); + doOneThing(); + } +} + +public class EmptyClass { +} + +public class TinyClass { + int a; +} + +public class SmallClass { + int a; + String b; +} + +public class AnonymousClasses { + EmptyClass emptyAnonymous = new EmptyClass() {}; + TinyClass tinyAnonymous = new TinyClass() { + String b; + }; + Object o = new SmallClass() { + int a; + + int getA() { + return a; + } + }; +} + +public enum EmptyEnum { +} + +public enum TinyEnum { + A; +} + +public enum SmallEnum { + VALUE(0); + SmallEnum(int val) { + }; +} + +public enum EnumConstants { + EMPTY { + }, + TINY { + int getVal() { + return 2; + } + }, + SMALL { + int val = 3; + + int getVal() { + return 3; + } + }; + int getVal() { + return 1; + } +} + +public @interface EmptyInterface {} + +public @interface TinyInterface { + void run(); +} + +public @interface SmallInteface { + int toA(); + + String toB(); +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/B_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/B_out.java new file mode 100644 index 0000000000..4c112b86ab --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/B_out.java @@ -0,0 +1,145 @@ +class Example { + public void example() { + for (int i = 0; i < 10; i++) {} + int a = 10; + while (a-- > 0) { System.out.println(a); } + do { + a += 2; + System.out.println(a); + } while (a < 50); + } +} + +class Example { + public String example(int a) { + if (a < 0) { + throw new IllegalArgumentException(); + } + if (a == 0) { + return null; + } + if (false) { + } + if (a % 3 == 0) { + System.out.println("fizz"); + } + if (a % 5 == 0) { + System.out.println("buzz"); + return ""; + } + return Integer.toString(a); + } +} + +class Example { + Runnable emptyLambda = () -> {}; + Runnable emptyLambda2 = () -> {}; + Runnable tinyLambda = () -> { doSomething(); }; + Runnable smallLambda = () -> { + doFirstThing(); + doSecondThing(); + }; +} + +class Example { + static { + } + + void foo() { + if (true) { + } else { + } + synchronized (this) { + } + try { + } finally { + } + + labeled: { + } + } +} + +public class Example { + private int something; + + public int getSomething() { return something; } + + public void setSomehing(int something) { this.something = something; } + + public void doNoting() { + } + + public void doOneThing() { + System.out.println(); + } + + public void doMoreThings() { + something = 4; + doOneThing(); + doOneThing(); + } +} + +public class EmptyClass { +} + +public class TinyClass { + int a; +} + +public class SmallClass { + int a; + String b; +} + +public class AnonymousClasses { + EmptyClass emptyAnonymous = new EmptyClass() { + }; + TinyClass tinyAnonymous = new TinyClass() { + String b; + }; + Object o = new SmallClass() { + int a; + + int getA() { return a; } + }; +} + +public enum EmptyEnum {} + +public enum TinyEnum { + A; +} + +public enum SmallEnum { + VALUE(0); + SmallEnum(int val) { + }; +} + +public enum EnumConstants { + EMPTY {}, + TINY { + int getVal() { return 2; } + }, + SMALL { + int val = 3; + + int getVal() { return 3; } + }; + int getVal() { return 1; } +} + +public @interface EmptyInterface { +} + +public @interface TinyInterface { + void run(); +} + +public @interface SmallInteface { + int toA(); + + String toB(); +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/C_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/C_out.java new file mode 100644 index 0000000000..ec05534b58 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/C_out.java @@ -0,0 +1,143 @@ +class Example { + public void example() { + for (int i = 0; i < 10; i++) { + } + int a = 10; + while (a-- > 0) { + System.out.println(a); + } + do { + a += 2; + System.out.println(a); + } while (a < 50); + } +} + +class Example { + public String example(int a) { + if (a < 0) { throw new IllegalArgumentException(); } + if (a == 0) { return null; } + if (false) { + } + if (a % 3 == 0) { + System.out.println("fizz"); + } + if (a % 5 == 0) { + System.out.println("buzz"); + return ""; + } + return Integer.toString(a); + } +} + +class Example { + Runnable emptyLambda = () -> { + }; + Runnable emptyLambda2 = () -> { + }; + Runnable tinyLambda = () -> { + doSomething(); + }; + Runnable smallLambda = () -> { + doFirstThing(); + doSecondThing(); + }; +} + +class Example { + static {} + + void foo() { + if (true) {} else {} + synchronized (this) {} + try {} finally {} + + labeled: {} + } +} + +public class Example { + private int something; + + public int getSomething() { + return something; + } + + public void setSomehing(int something) { + this.something = something; + } + + public void doNoting() {} + + public void doOneThing() { + System.out.println(); + } + + public void doMoreThings() { + something = 4; + doOneThing(); + doOneThing(); + } +} + +public class EmptyClass {} + +public class TinyClass { + int a; +} + +public class SmallClass { + int a; + String b; +} + +public class AnonymousClasses { + EmptyClass emptyAnonymous = new EmptyClass() {}; + TinyClass tinyAnonymous = new TinyClass() { String b; }; + Object o = new SmallClass() { + int a; + + int getA() { + return a; + } + }; +} + +public enum EmptyEnum {} + +public enum TinyEnum { A; } + +public enum SmallEnum { + VALUE(0); + SmallEnum(int val) {}; +} + +public enum EnumConstants { + EMPTY { + }, + TINY { + int getVal() { + return 2; + } + }, + SMALL { + int val = 3; + + int getVal() { + return 3; + } + }; + int getVal() { + return 1; + } +} + +public @interface EmptyInterface {} + +public @interface TinyInterface { void run(); } + +public @interface SmallInteface { + int toA(); + + String toB(); +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/D_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/D_out.java new file mode 100644 index 0000000000..a1e2cca718 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/D_out.java @@ -0,0 +1,148 @@ +class Example { + public void example() { + for (int i = 0; i < 10; i++) { + } + int a = 10; + while (a-- > 0) { + System.out.println(a); + } + do { + a += 2; + System.out.println(a); + } while (a < 50); + } +} + +class Example { + public String example(int a) { + if (a < 0) { + throw new IllegalArgumentException(); + } + if (a == 0) { + return null; + } + if (false) { + } + if (a % 3 == 0) { + System.out.println("fizz"); + } + if (a % 5 == 0) { + System.out.println("buzz"); + return ""; + } + return Integer.toString(a); + } +} + +class Example { + Runnable emptyLambda = () -> {}; + Runnable emptyLambda2 = () -> {}; + Runnable tinyLambda = () -> { doSomething(); }; + Runnable smallLambda = () -> { + doFirstThing(); + doSecondThing(); + }; +} + +class Example { + static {} + + void foo() { + if (true) {} else {} + synchronized (this) {} + try {} finally {} + + labeled: {} + } +} + +public class Example { + private int something; + + public int getSomething() { + return something; + } + + public void setSomehing(int something) { + this.something = something; + } + + public void doNoting() { + } + + public void doOneThing() { + System.out.println(); + } + + public void doMoreThings() { + something = 4; + doOneThing(); + doOneThing(); + } +} + +public class EmptyClass {} + +public class TinyClass { int a; } + +public class SmallClass { + int a; + String b; +} + +public class AnonymousClasses { + EmptyClass emptyAnonymous = new EmptyClass() {}; + TinyClass tinyAnonymous = new TinyClass() { String b; }; + Object o = new SmallClass() { + int a; + + int getA() { + return a; + } + }; +} + +public enum EmptyEnum { +} + +public enum TinyEnum { + A; +} + +public enum SmallEnum { + VALUE(0); + SmallEnum(int val) { + }; +} + +public enum EnumConstants { + EMPTY {}, + TINY { + int getVal() { + return 2; + } + }, + SMALL { + int val = 3; + + int getVal() { + return 3; + } + }; + int getVal() { + return 1; + } +} + +public @interface EmptyInterface { +} + +public @interface TinyInterface { + void run(); +} + +public @interface SmallInteface { + int toA(); + + String toB(); +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/E_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/E_out.java new file mode 100644 index 0000000000..60cf56fc6a --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/E_out.java @@ -0,0 +1,127 @@ +class Example { + public void example() { + for (int i = 0; i < 10; i++) {} + int a = 10; + while (a-- > 0) { System.out.println(a); } + do { a += 2; System.out.println(a); } while (a < 50); + } +} + +class Example { + public String example(int a) { + if (a < 0) { throw new IllegalArgumentException(); } + if (a == 0) { return null; } + if (false) {} + if (a % 3 == 0) { + System.out.println("fizz"); + } + if (a % 5 == 0) { + System.out.println("buzz"); + return ""; + } + return Integer.toString(a); + } +} + +class Example { + Runnable emptyLambda = () -> { + }; + Runnable emptyLambda2 = () -> { + }; + Runnable tinyLambda = () -> { + doSomething(); + }; + Runnable smallLambda = () -> { + doFirstThing(); + doSecondThing(); + }; +} + +class Example { + static { + } + + void foo() { + if (true) { + } else { + } + synchronized (this) { + } + try { + } finally { + } + + labeled: { + } + } +} + +public class Example { + private int something; + + public int getSomething() { return something; } + + public void setSomehing(int something) { this.something = something; } + + public void doNoting() {} + + public void doOneThing() { + System.out.println(); + } + + public void doMoreThings() { + something = 4; + doOneThing(); + doOneThing(); + } +} + +public class EmptyClass { +} + +public class TinyClass { + int a; +} + +public class SmallClass { + int a; + String b; +} + +public class AnonymousClasses { + EmptyClass emptyAnonymous = new EmptyClass() { + }; + TinyClass tinyAnonymous = new TinyClass() { + String b; + }; + Object o = new SmallClass() { + int a; + + int getA() { return a; } + }; +} + +public enum EmptyEnum { +} + +public enum TinyEnum { + A; +} + +public enum SmallEnum { + VALUE(0); + SmallEnum(int val) {}; +} + +public enum EnumConstants { + EMPTY {}, + TINY { int getVal() { return 2; } }, + SMALL { int val = 3; int getVal() { return 3; } }; + int getVal() { return 1; } +} + +public @interface EmptyInterface {} + +public @interface TinyInterface { void run(); } + +public @interface SmallInteface { int toA(); String toB(); } diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/F_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/F_out.java new file mode 100644 index 0000000000..012f0e3bb1 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/F_out.java @@ -0,0 +1,142 @@ +class Example { + public void example() { + for (int i = 0; i < 10; i++) {} + int a = 10; + while (a-- > 0) { System.out.println(a); } + do { + a += 2; + System.out.println(a); + } while (a < 50); + } +} + +class Example { + public String example(int a) { + if (a < 0) { throw new IllegalArgumentException(); } + if (a == 0) { return null; } + if (false) {} + if (a % 3 == 0) { System.out.println("fizz"); } + if (a % 5 == 0) { System.out.println("buzz"); return ""; } + return Integer.toString(a); + } +} + +class Example { + Runnable emptyLambda = () -> { + }; + Runnable emptyLambda2 = () -> { + }; + Runnable tinyLambda = () -> { + doSomething(); + }; + Runnable smallLambda = () -> { + doFirstThing(); + doSecondThing(); + }; +} + +class Example { + static { + } + + void foo() { + if (true) { + } else { + } + synchronized (this) { + } + try { + } finally { + } + + labeled: { + } + } +} + +public class Example { + private int something; + + public int getSomething() { + return something; + } + + public void setSomehing(int something) { + this.something = something; + } + + public void doNoting() { + } + + public void doOneThing() { + System.out.println(); + } + + public void doMoreThings() { + something = 4; + doOneThing(); + doOneThing(); + } +} + +public class EmptyClass {} + +public class TinyClass { int a; } + +public class SmallClass { int a; String b; } + +public class AnonymousClasses { + EmptyClass emptyAnonymous = new EmptyClass() {}; + TinyClass tinyAnonymous = new TinyClass() { + String b; + }; + Object o = new SmallClass() { + int a; + + int getA() { + return a; + } + }; +} + +public enum EmptyEnum {} + +public enum TinyEnum { A; } + +public enum SmallEnum { + VALUE(0); + SmallEnum(int val) { + }; +} + +public enum EnumConstants { + EMPTY {}, + TINY { + int getVal() { + return 2; + } + }, + SMALL { + int val = 3; + + int getVal() { + return 3; + } + }; + int getVal() { + return 1; + } +} + +public @interface EmptyInterface { +} + +public @interface TinyInterface { + void run(); +} + +public @interface SmallInteface { + int toA(); + + String toB(); +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/G_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/G_out.java new file mode 100644 index 0000000000..fc1e78cfd5 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/G_out.java @@ -0,0 +1,121 @@ +class Example { + public void example() { + for (int i = 0; i < 10; i++) { + } + int a = 10; + while (a-- > 0) { + System.out.println(a); + } + do { + a += 2; + System.out.println(a); + } while (a < 50); + } +} + +class Example { + public String example(int a) { + if (a < 0) { throw new IllegalArgumentException(); } + if (a == 0) { return null; } + if (false) {} + if (a % 3 == 0) { System.out.println("fizz"); } + if (a % 5 == 0) { + System.out.println("buzz"); + return ""; + } + return Integer.toString(a); + } +} + +class Example { + Runnable emptyLambda = () -> {}; + Runnable emptyLambda2 = () -> {}; + Runnable tinyLambda = () -> { doSomething(); }; + Runnable smallLambda = () -> { doFirstThing(); doSecondThing(); }; +} + +class Example { + static {} + + void foo() { + if (true) {} else {} + synchronized (this) {} + try {} finally {} + + labeled: {} + } +} + +public class Example { + private int something; + + public int getSomething() { return something; } + + public void setSomehing(int something) { this.something = something; } + + public void doNoting() {} + + public void doOneThing() { System.out.println(); } + + public void doMoreThings() { something = 4; doOneThing(); doOneThing(); } +} + +public class EmptyClass { +} + +public class TinyClass { + int a; +} + +public class SmallClass { + int a; + String b; +} + +public class AnonymousClasses { + EmptyClass emptyAnonymous = new EmptyClass() { + }; + TinyClass tinyAnonymous = new TinyClass() { + String b; + }; + Object o = new SmallClass() { + int a; + + int getA() { return a; } + }; +} + +public enum EmptyEnum {} + +public enum TinyEnum { A; } + +public enum SmallEnum { + VALUE(0); + SmallEnum(int val) {}; +} + +public enum EnumConstants { + EMPTY { + }, + TINY { + int getVal() { return 2; } + }, + SMALL { + int val = 3; + + int getVal() { return 3; } + }; + int getVal() { return 1; } +} + +public @interface EmptyInterface {} + +public @interface TinyInterface { + void run(); +} + +public @interface SmallInteface { + int toA(); + + String toB(); +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/H_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/H_out.java new file mode 100644 index 0000000000..9eba7cd481 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/H_out.java @@ -0,0 +1,109 @@ +class Example { + public void example() { + for (int i = 0; i < 10; i++) { + } + int a = 10; + while (a-- > 0) { System.out.println(a); } + do { + a += 2; + System.out.println(a); + } while (a < 50); + } +} + +class Example { + public String example(int a) { + if (a < 0) { + throw new IllegalArgumentException(); + } + if (a == 0) { return null; } + if (false) {} + if (a % 3 == 0) { + System.out.println("fizz"); + } + if (a % 5 == 0) { System.out.println("buzz"); return ""; } + return Integer.toString(a); + } +} + +class Example { + Runnable emptyLambda = () -> {}; + Runnable emptyLambda2 = () -> { + }; + Runnable tinyLambda = () -> { doSomething(); }; + Runnable smallLambda = () -> { doFirstThing(); doSecondThing(); }; +} + +class Example { + static { + } + + void foo() { + if (true) { + } else { + } + synchronized (this) { + } + try { + } finally { + } + + labeled: { + } + } +} + +public class Example { + private int something; + + public int getSomething() { return something; } + + public void setSomehing(int something) { this.something = something; } + + public void doNoting() {} + + public void doOneThing() { + System.out.println(); + } + + public void doMoreThings() { something = 4; doOneThing(); doOneThing(); } +} + +public class EmptyClass {} + +public class TinyClass { + int a; +} + +public class SmallClass { int a; String b; } + +public class AnonymousClasses { + EmptyClass emptyAnonymous = new EmptyClass() { + }; + TinyClass tinyAnonymous = new TinyClass() { String b; }; + Object o = new SmallClass() { int a; int getA() { return a; } }; +} + +public enum EmptyEnum {} + +public enum TinyEnum { + A; +} + +public enum SmallEnum { VALUE(0); SmallEnum(int val) {}; } + +public enum EnumConstants { + EMPTY { + }, + TINY { int getVal() { return 2; } }, + SMALL { int val = 3; int getVal() { return 3; } }; + int getVal() { return 1; } +} + +public @interface EmptyInterface {} + +public @interface TinyInterface { + void run(); +} + +public @interface SmallInteface { int toA(); String toB(); } diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/I_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/I_out.java new file mode 100644 index 0000000000..82d5839371 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/I_out.java @@ -0,0 +1,125 @@ +class Example { + public void example() { + for (int i = 0; i < 10; i++) {} + int a = 10; + while (a-- > 0) { System.out.println(a); } + do { + a += 2; + System.out.println(a); + } while (a < 50); + } +} + +class Example { + public String example(int a) { + if (a < 0) { + throw new IllegalArgumentException(); + } + if (a == 0) { return null; } + if (false) {} + if (a % 3 == 0) { + System.out.println("fizz"); + } + if (a % 5 == 0) { + System.out.println("buzz"); + return ""; + } + return Integer.toString(a); + } +} + +class Example { + Runnable emptyLambda = () -> {}; + Runnable emptyLambda2 = () -> {}; + Runnable tinyLambda = () -> { + doSomething(); + }; + Runnable smallLambda = () -> { + doFirstThing(); + doSecondThing(); + }; +} + +class Example { + static {} + + void foo() { + if (true) {} else {} + synchronized (this) {} + try {} finally {} + + labeled: {} + } +} + +public class Example { + private int something; + + public int getSomething() { + return something; + } + + public void setSomehing(int something) { + this.something = something; + } + + public void doNoting() {} + + public void doOneThing() { + System.out.println(); + } + + public void doMoreThings() { + something = 4; + doOneThing(); + doOneThing(); + } +} + +public class EmptyClass {} + +public class TinyClass { int a; } + +public class SmallClass { int a; String b; } + +public class AnonymousClasses { + EmptyClass emptyAnonymous = new EmptyClass() {}; + TinyClass tinyAnonymous = new TinyClass() { + String b; + }; + Object o = new SmallClass() { + int a; + + int getA() { return a; } + }; +} + +public enum EmptyEnum {} + +public enum TinyEnum { A; } + +public enum SmallEnum { + VALUE(0); + SmallEnum(int val) {}; +} + +public enum EnumConstants { + EMPTY {}, + TINY { int getVal() { return 2; } }, + SMALL { + int val = 3; + + int getVal() { return 3; } + }; + int getVal() { return 1; } +} + +public @interface EmptyInterface {} + +public @interface TinyInterface { void run(); } + +public @interface SmallInteface { + int toA(); + + String toB(); +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/J_in.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/J_in.java new file mode 100644 index 0000000000..41d96d02d1 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/J_in.java @@ -0,0 +1,30 @@ +class Example { + + void method1() { + int a; + } + + void method2() { + + int a; + + + } + + void method3() { + int a; // + } + + void method4() { + int a; /* */ + } + + void method5() { + /* */ int a; + } + + void method6() { int a; /* */ } /* */ + + void method7() { /* */ int a; } /* */ + +}
\ No newline at end of file diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/J_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/J_out.java new file mode 100644 index 0000000000..2a7ae61fa2 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/J_out.java @@ -0,0 +1,27 @@ +class Example { + + void method1() { int a; } + + void method2() { + + int a; + + } + + void method3() { + int a; // + } + + void method4() { + int a; /* */ + } + + void method5() { + /* */ int a; + } + + void method6() { int a; /* */ } /* */ + + void method7() { /* */ int a; } /* */ + +}
\ No newline at end of file diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/in.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/in.java new file mode 100644 index 0000000000..bde96a4417 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/in.java @@ -0,0 +1,93 @@ +class Example{ + public void example() { + for (int i = 0; i < 10; i++) { + } + int a = 10; + while (a-- > 0) { System.out.println(a); } + do { a += 2; + System.out.println(a); } while(a < 50); + } +} + + +class Example { + public String example(int a) { + if (a < 0) { + throw new IllegalArgumentException(); } + if (a == 0) { return null; } + if (false) {} + if (a % 3 == 0) { + System.out.println("fizz"); } + if (a % 5 == 0) { System.out.println("buzz"); return ""; } + return Integer.toString(a); + } +} + + +class Example { + Runnable emptyLambda = () -> {}; + Runnable emptyLambda2 = () -> { + }; + Runnable tinyLambda = () -> { doSomething(); }; + Runnable smallLambda = () -> { doFirstThing(); doSecondThing(); }; +} + + +class Example { + static { + } + + void foo() { + if (true) {} else {} + synchronized(this) {} + try {} finally {} + + labeled:{} + } +} + + +public class Example { + private int something; + public int getSomething() { return something; } + public void setSomehing(int something) { this.something = something; } + public void doNoting() {} + public void doOneThing() { System.out.println(); + } + public void doMoreThings() { something = 4; doOneThing(); doOneThing(); } +} + + +public class EmptyClass{} +public class TinyClass{ + int a; } +public class SmallClass{ int a; String b; } + + +public class AnonymousClasses { + EmptyClass emptyAnonymous = new EmptyClass() { + }; + TinyClass tinyAnonymous = new TinyClass() { String b; }; + Object o = new SmallClass() { int a; int getA() { return a; } }; +} + + +public enum EmptyEnum {} +public enum TinyEnum{ A; +} +public enum SmallEnum{ VALUE(0); SmallEnum(int val) {}; } + + +public enum EnumConstants { + EMPTY { + }, + TINY { int getVal() { return 2; }}, + SMALL { int val = 3; int getVal() { return 3; }}; + int getVal() { return 1; } +} + + +public @interface EmptyInterface {} +public @interface TinyInterface { + void run(); } +public @interface SmallInteface { int toA(); String toB(); } diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test496/A_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test496/A_out.java index 7b6cead555..19532deebf 100644 --- a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test496/A_out.java +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test496/A_out.java @@ -1,7 +1,8 @@ public class A { void doX() { - if (1 > 0) { return; // + if (1 > 0) { + return; // } return; } diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java index 7716e2a0e4..e663811b81 100644 --- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java @@ -917,6 +917,7 @@ public class DefaultCodeFormatterConstants { * @since 3.1 * @deprecated Use {@link #FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_BLOCK_COMMENT} and {@link #FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_JAVADOC_COMMENT} */ + @Deprecated public final static String FORMATTER_COMMENT_CLEAR_BLANK_LINES = "org.eclipse.jdt.core.formatter.comment.clear_blank_lines"; //$NON-NLS-1$ /** @@ -958,6 +959,7 @@ public class DefaultCodeFormatterConstants { * @deprecated Use multiple settings for each kind of comments. See {@link #FORMATTER_COMMENT_FORMAT_BLOCK_COMMENT}, * {@link #FORMATTER_COMMENT_FORMAT_JAVADOC_COMMENT} and {@link #FORMATTER_COMMENT_FORMAT_LINE_COMMENT}. */ + @Deprecated public final static String FORMATTER_COMMENT_FORMAT = "org.eclipse.jdt.core.formatter.comment.format_comments"; //$NON-NLS-1$ /** @@ -1670,6 +1672,7 @@ public class DefaultCodeFormatterConstants { * {@link #FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_LOCAL_VARIABLE} * {@link #FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_PARAMETER} */ + @Deprecated public static final String FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION = JavaCore.PLUGIN_ID + ".formatter.insert_new_line_after_annotation";//$NON-NLS-1$ /** @@ -1689,6 +1692,7 @@ public class DefaultCodeFormatterConstants { * {@link #FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_PACKAGE} * {@link #FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_TYPE} */ + @Deprecated public static final String FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_MEMBER = JavaCore.PLUGIN_ID + ".formatter.insert_new_line_after_annotation_on_member";//$NON-NLS-1$ /** @@ -1902,7 +1906,9 @@ public class DefaultCodeFormatterConstants { * @see JavaCore#INSERT * @see JavaCore#DO_NOT_INSERT * @since 3.2 + * @deprecated Use {@link #FORMATTER_KEEP_ANNOTATION_DECLARATION_ON_ONE_LINE} instead. */ + @Deprecated public static final String FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANNOTATION_DECLARATION = JavaCore.PLUGIN_ID + ".formatter.insert_new_line_in_empty_annotation_declaration"; //$NON-NLS-1$ /** * <pre> @@ -1914,7 +1920,9 @@ public class DefaultCodeFormatterConstants { * @see JavaCore#INSERT * @see JavaCore#DO_NOT_INSERT * @since 3.0 + * @deprecated Use {@link #FORMATTER_KEEP_ANONYMOUS_TYPE_DECLARATION_ON_ONE_LINE} instead. */ + @Deprecated public static final String FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANONYMOUS_TYPE_DECLARATION = JavaCore.PLUGIN_ID + ".formatter.insert_new_line_in_empty_anonymous_type_declaration"; //$NON-NLS-1$ /** * <pre> @@ -1926,7 +1934,12 @@ public class DefaultCodeFormatterConstants { * @see JavaCore#INSERT * @see JavaCore#DO_NOT_INSERT * @since 3.0 + * @deprecated Use {@link #FORMATTER_KEEP_IF_THEN_BODY_BLOCK_ON_ONE_LINE}, + * {@link #FORMATTER_KEEP_LOOP_BODY_BLOCK_ON_ONE_LINE}, + * {@link #FORMATTER_KEEP_CODE_BLOCK_ON_ONE_LINE}, and + * {@link #FORMATTER_KEEP_LAMBDA_BODY_BLOCK_ON_ONE_LINE} instead. */ + @Deprecated public static final String FORMATTER_INSERT_NEW_LINE_IN_EMPTY_BLOCK = JavaCore.PLUGIN_ID + ".formatter.insert_new_line_in_empty_block"; //$NON-NLS-1$ /** * <pre> @@ -1938,7 +1951,9 @@ public class DefaultCodeFormatterConstants { * @see JavaCore#INSERT * @see JavaCore#DO_NOT_INSERT * @since 3.1 + * @deprecated Use {@link #FORMATTER_KEEP_ENUM_CONSTANT_DECLARATION_ON_ONE_LINE} instead. */ + @Deprecated public static final String FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ENUM_CONSTANT = JavaCore.PLUGIN_ID + ".formatter.insert_new_line_in_empty_enum_constant"; //$NON-NLS-1$ /** * <pre> @@ -1950,7 +1965,9 @@ public class DefaultCodeFormatterConstants { * @see JavaCore#INSERT * @see JavaCore#DO_NOT_INSERT * @since 3.1 + * @deprecated Use {@link #FORMATTER_KEEP_ENUM_DECLARATION_ON_ONE_LINE} instead. */ + @Deprecated public static final String FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ENUM_DECLARATION = JavaCore.PLUGIN_ID + ".formatter.insert_new_line_in_empty_enum_declaration"; //$NON-NLS-1$ /** * <pre> @@ -1962,7 +1979,9 @@ public class DefaultCodeFormatterConstants { * @see JavaCore#INSERT * @see JavaCore#DO_NOT_INSERT * @since 3.0 + * @deprecated Use {@link #FORMATTER_KEEP_METHOD_BODY_ON_ONE_LINE} instead. */ + @Deprecated public static final String FORMATTER_INSERT_NEW_LINE_IN_EMPTY_METHOD_BODY = JavaCore.PLUGIN_ID + ".formatter.insert_new_line_in_empty_method_body"; //$NON-NLS-1$ /** * <pre> @@ -1974,8 +1993,11 @@ public class DefaultCodeFormatterConstants { * @see JavaCore#INSERT * @see JavaCore#DO_NOT_INSERT * @since 3.0 + * @deprecated Use {@link #FORMATTER_KEEP_TYPE_DECLARATION_ON_ONE_LINE} instead. */ + @Deprecated public static final String FORMATTER_INSERT_NEW_LINE_IN_EMPTY_TYPE_DECLARATION = JavaCore.PLUGIN_ID + ".formatter.insert_new_line_in_empty_type_declaration"; //$NON-NLS-1$ + /** * <pre> * FORMATTER / Option to insert a space after and in wilcard @@ -3939,13 +3961,15 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_KEEP_EMPTY_ARRAY_INITIALIZER_ON_ONE_LINE = JavaCore.PLUGIN_ID + ".formatter.keep_empty_array_initializer_on_one_line"; //$NON-NLS-1$ /** * <pre> - * FORMATTER / Option to keep guardian clause on one line + * FORMATTER / Option to keep guardian clause on one line, in addition to the + * #FORMATTER_KEEP_IF_THEN_BODY_BLOCK_ON_ONE_LINE option * - option id: "org.eclipse.jdt.core.formatter.format_guardian_clause_on_one_line" * - possible values: { TRUE, FALSE } * - default: FALSE * </pre> * @see #TRUE * @see #FALSE + * @see #FORMATTER_KEEP_IF_THEN_BODY_BLOCK_ON_ONE_LINE * @since 3.0 */ public static final String FORMATTER_KEEP_GUARDIAN_CLAUSE_ON_ONE_LINE = JavaCore.PLUGIN_ID + ".formatter.format_guardian_clause_on_one_line"; //$NON-NLS-1$ @@ -4013,6 +4037,180 @@ public class DefaultCodeFormatterConstants { /** * <pre> + * FORMATTER / Option to control when a loop body block should be kept on one line + * - option id: "org.eclipse.jdt.core.formatter.keep_loop_body_block_on_one_line" + * - possible values: { ONE_LINE_NEVER, ONE_LINE_IF_EMPTY, ONE_LINE_IF_SINGLE_ITEM, + * ONE_LINE_ALWAYS, ONE_LINE_PRESERVE } + * - default: ONE_LINE_NEVER + * </pre> + * @see #ONE_LINE_NEVER + * @see #ONE_LINE_IF_EMPTY + * @see #ONE_LINE_IF_SINGLE_ITEM + * @see #ONE_LINE_ALWAYS + * @see #ONE_LINE_PRESERVE + * @since 3.16 + */ + public static final String FORMATTER_KEEP_LOOP_BODY_BLOCK_ON_ONE_LINE = JavaCore.PLUGIN_ID + ".formatter.keep_loop_body_block_on_one_line"; //$NON-NLS-1$ + /** + * <pre> + * FORMATTER / Option to control when an if-then statement body block should be kept on one line + * - option id: "org.eclipse.jdt.core.formatter.keep_if_then_body_block_on_one_line" + * - possible values: { ONE_LINE_NEVER, ONE_LINE_IF_EMPTY, ONE_LINE_IF_SINGLE_ITEM, + * ONE_LINE_ALWAYS, ONE_LINE_PRESERVE } + * - default: ONE_LINE_NEVER + * </pre> + * @see #ONE_LINE_NEVER + * @see #ONE_LINE_IF_EMPTY + * @see #ONE_LINE_IF_SINGLE_ITEM + * @see #ONE_LINE_ALWAYS + * @see #ONE_LINE_PRESERVE + * @see #FORMATTER_KEEP_GUARDIAN_CLAUSE_ON_ONE_LINE for a special case + * @since 3.16 + */ + public static final String FORMATTER_KEEP_IF_THEN_BODY_BLOCK_ON_ONE_LINE = JavaCore.PLUGIN_ID + ".formatter.keep_if_then_body_block_on_one_line"; //$NON-NLS-1$ + /** + * <pre> + * FORMATTER / Option to control when a code block other than if-then and loop body should + * be kept on one line + * - option id: "org.eclipse.jdt.core.formatter.keep_code_block_on_one_line" + * - possible values: { ONE_LINE_NEVER, ONE_LINE_IF_EMPTY } + * - default: ONE_LINE_NEVER + * </pre> + * @see #ONE_LINE_NEVER + * @see #ONE_LINE_IF_EMPTY + * @since 3.16 + */ + public static final String FORMATTER_KEEP_CODE_BLOCK_ON_ONE_LINE = JavaCore.PLUGIN_ID + ".formatter.keep_code_block_on_one_line"; //$NON-NLS-1$ + /** + * <pre> + * FORMATTER / Option to control when a method body should be kept on one line + * - option id: "org.eclipse.jdt.core.formatter.keep_method_body_on_one_line" + * - possible values: { ONE_LINE_NEVER, ONE_LINE_IF_EMPTY, ONE_LINE_IF_SINGLE_ITEM, + * ONE_LINE_ALWAYS, ONE_LINE_PRESERVE } + * - default: ONE_LINE_NEVER + * </pre> + * @see #ONE_LINE_NEVER + * @see #ONE_LINE_IF_EMPTY + * @see #ONE_LINE_IF_SINGLE_ITEM + * @see #ONE_LINE_ALWAYS + * @see #ONE_LINE_PRESERVE + * @since 3.16 + */ + public static final String FORMATTER_KEEP_METHOD_BODY_ON_ONE_LINE = JavaCore.PLUGIN_ID + ".formatter.keep_method_body_on_one_line"; //$NON-NLS-1$ + /** + * <pre> + * FORMATTER / Option to control when a lambda body should be kept on one line + * - option id: "org.eclipse.jdt.core.formatter.keep_lambda_body_block_on_one_line" + * - possible values: { ONE_LINE_NEVER, ONE_LINE_IF_EMPTY, ONE_LINE_IF_SINGLE_ITEM, + * ONE_LINE_ALWAYS, ONE_LINE_PRESERVE } + * - default: ONE_LINE_NEVER + * </pre> + * @see #ONE_LINE_NEVER + * @see #ONE_LINE_IF_EMPTY + * @see #ONE_LINE_IF_SINGLE_ITEM + * @see #ONE_LINE_ALWAYS + * @see #ONE_LINE_PRESERVE + * @since 3.16 + */ + public static final String FORMATTER_KEEP_LAMBDA_BODY_BLOCK_ON_ONE_LINE = JavaCore.PLUGIN_ID + ".formatter.keep_lambda_body_block_on_one_line"; //$NON-NLS-1$ + /** + * <pre> + * FORMATTER / Option to always keep simple getters and setters on one line, in addition to the + * #FORMATTER_KEEP_METHOD_BODY_ON_ONE_LINE option + * - option id: "org.eclipse.jdt.core.formatter.keep_simple_getter_setter_on_one_line" + * - possible values: { TRUE, FALSE } + * - default: FALSE + * </pre> + * @see #TRUE + * @see #FALSE + * @see #FORMATTER_KEEP_METHOD_BODY_ON_ONE_LINE + * @since 3.16 + */ + public static final String FORMATTER_KEEP_SIMPLE_GETTER_SETTER_ON_ONE_LINE = JavaCore.PLUGIN_ID + ".formatter.keep_simple_getter_setter_on_one_line"; //$NON-NLS-1$ + /** + * <pre> + * FORMATTER / Option to control when a type declaration should be kept on one line + * - option id: "org.eclipse.jdt.core.formatter.keep_type_declaration_on_one_line" + * - possible values: { ONE_LINE_NEVER, ONE_LINE_IF_EMPTY, ONE_LINE_IF_SINGLE_ITEM, + * ONE_LINE_ALWAYS, ONE_LINE_PRESERVE } + * - default: ONE_LINE_NEVER + * </pre> + * @see #ONE_LINE_NEVER + * @see #ONE_LINE_IF_EMPTY + * @see #ONE_LINE_IF_SINGLE_ITEM + * @see #ONE_LINE_ALWAYS + * @see #ONE_LINE_PRESERVE + * @since 3.16 + * @since 3.0 + */ + public static final String FORMATTER_KEEP_TYPE_DECLARATION_ON_ONE_LINE = JavaCore.PLUGIN_ID + ".formatter.keep_type_declaration_on_one_line"; //$NON-NLS-1$ + /** + * <pre> + * FORMATTER / Option to control when an anonymous type declaration should be kept on one line + * - option id: "org.eclipse.jdt.core.formatter.keep_anonymous_type_declaration_on_one_line" + * - possible values: { ONE_LINE_NEVER, ONE_LINE_IF_EMPTY, ONE_LINE_IF_SINGLE_ITEM, + * ONE_LINE_ALWAYS, ONE_LINE_PRESERVE } + * - default: ONE_LINE_NEVER + * </pre> + * @see #ONE_LINE_NEVER + * @see #ONE_LINE_IF_EMPTY + * @see #ONE_LINE_IF_SINGLE_ITEM + * @see #ONE_LINE_ALWAYS + * @see #ONE_LINE_PRESERVE + * @since 3.16 + */ + public static final String FORMATTER_KEEP_ANONYMOUS_TYPE_DECLARATION_ON_ONE_LINE = JavaCore.PLUGIN_ID + ".formatter.keep_anonymous_type_declaration_on_one_line"; //$NON-NLS-1$ + /** + * <pre> + * FORMATTER / Option to control when an enum constant declaration body should be kept on one line + * - option id: "org.eclipse.jdt.core.formatter.keep_enum_constant_declaration_on_one_line" + * - possible values: { ONE_LINE_NEVER, ONE_LINE_IF_EMPTY, ONE_LINE_IF_SINGLE_ITEM, + * ONE_LINE_ALWAYS, ONE_LINE_PRESERVE } + * - default: ONE_LINE_NEVER + * </pre> + * @see #ONE_LINE_NEVER + * @see #ONE_LINE_IF_EMPTY + * @see #ONE_LINE_IF_SINGLE_ITEM + * @see #ONE_LINE_ALWAYS + * @see #ONE_LINE_PRESERVE + * @since 3.16 + */ + public static final String FORMATTER_KEEP_ENUM_CONSTANT_DECLARATION_ON_ONE_LINE = JavaCore.PLUGIN_ID + ".formatter.keep_enum_constant_declaration_on_one_line"; //$NON-NLS-1$ + /** + * <pre> + * FORMATTER / Option to control when an enum declaration should be kept on one line + * - option id: "org.eclipse.jdt.core.formatter.keep_enum_declaration_on_one_line" + * - possible values: { ONE_LINE_NEVER, ONE_LINE_IF_EMPTY, ONE_LINE_IF_SINGLE_ITEM, + * ONE_LINE_ALWAYS, ONE_LINE_PRESERVE } + * - default: ONE_LINE_NEVER + * </pre> + * @see #ONE_LINE_NEVER + * @see #ONE_LINE_IF_EMPTY + * @see #ONE_LINE_IF_SINGLE_ITEM + * @see #ONE_LINE_ALWAYS + * @see #ONE_LINE_PRESERVE + * @since 3.16 + */ + public static final String FORMATTER_KEEP_ENUM_DECLARATION_ON_ONE_LINE = JavaCore.PLUGIN_ID + ".formatter.keep_enum_declaration_on_one_line"; //$NON-NLS-1$ + /** + * <pre> + * FORMATTER / Option to control when an annotation declaration should be kept on one line + * - option id: "org.eclipse.jdt.core.formatter.keep_annotation_declaration_on_one_line" + * - possible values: { ONE_LINE_NEVER, ONE_LINE_IF_EMPTY, ONE_LINE_IF_SINGLE_ITEM, + * ONE_LINE_ALWAYS, ONE_LINE_PRESERVE } + * - default: ONE_LINE_NEVER + * </pre> + * @see #ONE_LINE_NEVER + * @see #ONE_LINE_IF_EMPTY + * @see #ONE_LINE_IF_SINGLE_ITEM + * @see #ONE_LINE_ALWAYS + * @see #ONE_LINE_PRESERVE + * @since 3.16 + */ + public static final String FORMATTER_KEEP_ANNOTATION_DECLARATION_ON_ONE_LINE = JavaCore.PLUGIN_ID + ".formatter.keep_annotation_declaration_on_one_line"; //$NON-NLS-1$ + + /** + * <pre> * FORMATTER / Option to specify the length of the page. Beyond this length, the formatter will try to split the code * - option id: "org.eclipse.jdt.core.formatter.lineSplit" * - possible values: "<n>", where n is zero or a positive integer @@ -4439,6 +4637,92 @@ public class DefaultCodeFormatterConstants { /** * <pre> + * FORMATTER / Value to never keep braced code on one line. + * </pre> + * @see #FORMATTER_KEEP_LOOP_BODY_BLOCK_ON_ONE_LINE + * @see #FORMATTER_KEEP_IF_THEN_BODY_BLOCK_ON_ONE_LINE + * @see #FORMATTER_KEEP_CODE_BLOCK_ON_ONE_LINE + * @see #FORMATTER_KEEP_METHOD_BODY_ON_ONE_LINE + * @see #FORMATTER_KEEP_LAMBDA_BODY_BLOCK_ON_ONE_LINE + * @see #FORMATTER_KEEP_TYPE_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ANONYMOUS_TYPE_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ENUM_CONSTANT_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ENUM_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ANNOTATION_DECLARATION_ON_ONE_LINE + * @since 3.16 + */ + public static final String ONE_LINE_NEVER = "one_line_never"; //$NON-NLS-1$ + /** + * <pre> + * FORMATTER / Value to keep braced code on one line only if it's empty. + * </pre> + * @see #FORMATTER_KEEP_LOOP_BODY_BLOCK_ON_ONE_LINE + * @see #FORMATTER_KEEP_IF_THEN_BODY_BLOCK_ON_ONE_LINE + * @see #FORMATTER_KEEP_CODE_BLOCK_ON_ONE_LINE + * @see #FORMATTER_KEEP_METHOD_BODY_ON_ONE_LINE + * @see #FORMATTER_KEEP_LAMBDA_BODY_BLOCK_ON_ONE_LINE + * @see #FORMATTER_KEEP_TYPE_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ANONYMOUS_TYPE_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ENUM_CONSTANT_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ENUM_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ANNOTATION_DECLARATION_ON_ONE_LINE + * @since 3.16 + */ + public static final String ONE_LINE_IF_EMPTY = "one_line_if_empty"; //$NON-NLS-1$ + /** + * <pre> + * FORMATTER / Value to keep braced code on one line if it contains at most a single + * item. + * </pre> + * @see #FORMATTER_KEEP_LOOP_BODY_BLOCK_ON_ONE_LINE + * @see #FORMATTER_KEEP_IF_THEN_BODY_BLOCK_ON_ONE_LINE + * @see #FORMATTER_KEEP_METHOD_BODY_ON_ONE_LINE + * @see #FORMATTER_KEEP_LAMBDA_BODY_BLOCK_ON_ONE_LINE + * @see #FORMATTER_KEEP_TYPE_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ANONYMOUS_TYPE_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ENUM_CONSTANT_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ENUM_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ANNOTATION_DECLARATION_ON_ONE_LINE + * @since 3.16 + */ + public static final String ONE_LINE_IF_SINGLE_ITEM = "one_line_if_single_item"; //$NON-NLS-1$ + /** + * <pre> + * FORMATTER / Value to always keep braced code on one line, as long as it doesn't + * exceed the line width limit. + * </pre> + * @see #FORMATTER_KEEP_LOOP_BODY_BLOCK_ON_ONE_LINE + * @see #FORMATTER_KEEP_IF_THEN_BODY_BLOCK_ON_ONE_LINE + * @see #FORMATTER_KEEP_METHOD_BODY_ON_ONE_LINE + * @see #FORMATTER_KEEP_LAMBDA_BODY_BLOCK_ON_ONE_LINE + * @see #FORMATTER_KEEP_TYPE_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ANONYMOUS_TYPE_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ENUM_CONSTANT_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ENUM_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ANNOTATION_DECLARATION_ON_ONE_LINE + * @since 3.16 + */ + public static final String ONE_LINE_ALWAYS = "one_line_always"; //$NON-NLS-1$ + /** + * <pre> + * FORMATTER / Value to keep braced code on one line as long as it doesn't exceed the + * line width limit and it was already in one line in the original source. + * </pre> + * @see #FORMATTER_KEEP_LOOP_BODY_BLOCK_ON_ONE_LINE + * @see #FORMATTER_KEEP_IF_THEN_BODY_BLOCK_ON_ONE_LINE + * @see #FORMATTER_KEEP_METHOD_BODY_ON_ONE_LINE + * @see #FORMATTER_KEEP_LAMBDA_BODY_BLOCK_ON_ONE_LINE + * @see #FORMATTER_KEEP_TYPE_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ANONYMOUS_TYPE_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ENUM_CONSTANT_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ENUM_DECLARATION_ON_ONE_LINE + * @see #FORMATTER_KEEP_ANNOTATION_DECLARATION_ON_ONE_LINE + * @since 3.16 + */ + public static final String ONE_LINE_PRESERVE = "one_line_preserve"; //$NON-NLS-1$ + + /** + * <pre> * FORMATTER / Value to set an option to true. * </pre> * @since 3.0 diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java index 744b097065..c222422a6b 100644 --- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java @@ -414,6 +414,7 @@ public class DefaultCodeFormatter extends CodeFormatter { LineBreaksPreparator breaksPreparator = new LineBreaksPreparator(this.tokenManager, this.workingOptions); this.astRoot.accept(breaksPreparator); breaksPreparator.finishUp(); + this.astRoot.accept(new OneLineEnforcer(this.tokenManager, this.workingOptions)); } private void prepareComments() { diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java index b4ae48f5ad..4f8fce881a 100644 --- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java @@ -19,11 +19,14 @@ *******************************************************************************/ package org.eclipse.jdt.internal.formatter; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.function.Consumer; -import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; import org.eclipse.jdt.internal.compiler.util.Util; /** @@ -246,13 +249,19 @@ public class DefaultCodeFormatterOptions { public boolean insert_new_line_before_else_in_if_statement; public boolean insert_new_line_before_finally_in_try_statement; public boolean insert_new_line_before_while_in_do_statement; - public boolean insert_new_line_in_empty_anonymous_type_declaration; - public boolean insert_new_line_in_empty_block; - public boolean insert_new_line_in_empty_annotation_declaration; - public boolean insert_new_line_in_empty_enum_constant; - public boolean insert_new_line_in_empty_enum_declaration; - public boolean insert_new_line_in_empty_method_body; - public boolean insert_new_line_in_empty_type_declaration; + + public String keep_loop_body_block_on_one_line; + public String keep_if_then_body_block_on_one_line; + public String keep_code_block_on_one_line; + public String keep_lambda_body_block_on_one_line; + public String keep_method_body_on_one_line; + public String keep_type_declaration_on_one_line; + public String keep_anonymous_type_declaration_on_one_line; + public String keep_enum_declaration_on_one_line; + public String keep_enum_constant_declaration_on_one_line; + public String keep_annotation_declaration_on_one_line; + public boolean keep_simple_getter_setter_on_one_line; + public boolean insert_space_after_and_in_type_parameter; public boolean insert_space_after_assignment_operator; public boolean insert_space_after_at_in_annotation; @@ -443,6 +452,13 @@ public class DefaultCodeFormatterOptions { public int initial_indentation_level; public String line_separator; + private final static List<String> KEEP_ON_ONE_LINE_VALUES = Arrays.asList( + DefaultCodeFormatterConstants.ONE_LINE_NEVER, + DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY, + DefaultCodeFormatterConstants.ONE_LINE_IF_SINGLE_ITEM, + DefaultCodeFormatterConstants.ONE_LINE_ALWAYS, + DefaultCodeFormatterConstants.ONE_LINE_PRESERVE); + private DefaultCodeFormatterOptions() { // cannot be instantiated } @@ -576,13 +592,17 @@ public class DefaultCodeFormatterOptions { options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_ELSE_IN_IF_STATEMENT, this.insert_new_line_before_else_in_if_statement? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_FINALLY_IN_TRY_STATEMENT, this.insert_new_line_before_finally_in_try_statement? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_WHILE_IN_DO_STATEMENT, this.insert_new_line_before_while_in_do_statement? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); - options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANONYMOUS_TYPE_DECLARATION, this.insert_new_line_in_empty_anonymous_type_declaration? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); - options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_BLOCK, this.insert_new_line_in_empty_block? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); - options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANNOTATION_DECLARATION, this.insert_new_line_in_empty_annotation_declaration ? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); - options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ENUM_CONSTANT, this.insert_new_line_in_empty_enum_constant? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); - options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ENUM_DECLARATION, this.insert_new_line_in_empty_enum_declaration? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); - options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_METHOD_BODY, this.insert_new_line_in_empty_method_body? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); - options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_TYPE_DECLARATION, this.insert_new_line_in_empty_type_declaration? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); + options.put(DefaultCodeFormatterConstants.FORMATTER_KEEP_ANNOTATION_DECLARATION_ON_ONE_LINE, this.keep_annotation_declaration_on_one_line); + options.put(DefaultCodeFormatterConstants.FORMATTER_KEEP_ANONYMOUS_TYPE_DECLARATION_ON_ONE_LINE, this.keep_anonymous_type_declaration_on_one_line); + options.put(DefaultCodeFormatterConstants.FORMATTER_KEEP_IF_THEN_BODY_BLOCK_ON_ONE_LINE, this.keep_if_then_body_block_on_one_line); + options.put(DefaultCodeFormatterConstants.FORMATTER_KEEP_LAMBDA_BODY_BLOCK_ON_ONE_LINE, this.keep_lambda_body_block_on_one_line); + options.put(DefaultCodeFormatterConstants.FORMATTER_KEEP_LOOP_BODY_BLOCK_ON_ONE_LINE, this.keep_loop_body_block_on_one_line); + options.put(DefaultCodeFormatterConstants.FORMATTER_KEEP_CODE_BLOCK_ON_ONE_LINE, this.keep_code_block_on_one_line); + options.put(DefaultCodeFormatterConstants.FORMATTER_KEEP_ENUM_CONSTANT_DECLARATION_ON_ONE_LINE, this.keep_enum_constant_declaration_on_one_line); + options.put(DefaultCodeFormatterConstants.FORMATTER_KEEP_ENUM_DECLARATION_ON_ONE_LINE, this.keep_enum_declaration_on_one_line); + options.put(DefaultCodeFormatterConstants.FORMATTER_KEEP_METHOD_BODY_ON_ONE_LINE, this.keep_method_body_on_one_line); + options.put(DefaultCodeFormatterConstants.FORMATTER_KEEP_TYPE_DECLARATION_ON_ONE_LINE, this.keep_type_declaration_on_one_line); + options.put(DefaultCodeFormatterConstants.FORMATTER_KEEP_SIMPLE_GETTER_SETTER_ON_ONE_LINE, this.keep_simple_getter_setter_on_one_line? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_LABEL, this.insert_new_line_after_label? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_AND_IN_TYPE_PARAMETER, this.insert_space_after_and_in_type_parameter? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_ASSIGNMENT_OPERATOR, this.insert_space_after_assignment_operator? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); @@ -1521,34 +1541,31 @@ public class DefaultCodeFormatterOptions { if (insertNewLineBeforeWhileInDoStatementOption != null) { this.insert_new_line_before_while_in_do_statement = JavaCore.INSERT.equals(insertNewLineBeforeWhileInDoStatementOption); } - final Object insertNewLineInEmptyAnonymousTypeDeclarationOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANONYMOUS_TYPE_DECLARATION); - if (insertNewLineInEmptyAnonymousTypeDeclarationOption != null) { - this.insert_new_line_in_empty_anonymous_type_declaration = JavaCore.INSERT.equals(insertNewLineInEmptyAnonymousTypeDeclarationOption); - } - final Object insertNewLineInEmptyBlockOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_BLOCK); - if (insertNewLineInEmptyBlockOption != null) { - this.insert_new_line_in_empty_block = JavaCore.INSERT.equals(insertNewLineInEmptyBlockOption); - } - final Object insertNewLineInEmptyAnnotationDeclarationOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANNOTATION_DECLARATION); - if (insertNewLineInEmptyAnnotationDeclarationOption != null) { - this.insert_new_line_in_empty_annotation_declaration = JavaCore.INSERT.equals(insertNewLineInEmptyAnnotationDeclarationOption); - } - final Object insertNewLineInEmptyEnumConstantOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ENUM_CONSTANT); - if (insertNewLineInEmptyEnumConstantOption != null) { - this.insert_new_line_in_empty_enum_constant = JavaCore.INSERT.equals(insertNewLineInEmptyEnumConstantOption); - } - final Object insertNewLineInEmptyEnumDeclarationOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ENUM_DECLARATION); - if (insertNewLineInEmptyEnumDeclarationOption != null) { - this.insert_new_line_in_empty_enum_declaration = JavaCore.INSERT.equals(insertNewLineInEmptyEnumDeclarationOption); - } - final Object insertNewLineInEmptyMethodBodyOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_METHOD_BODY); - if (insertNewLineInEmptyMethodBodyOption != null) { - this.insert_new_line_in_empty_method_body = JavaCore.INSERT.equals(insertNewLineInEmptyMethodBodyOption); - } - final Object insertNewLineInEmptyTypeDeclarationOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_TYPE_DECLARATION); - if (insertNewLineInEmptyTypeDeclarationOption != null) { - this.insert_new_line_in_empty_type_declaration = JavaCore.INSERT.equals(insertNewLineInEmptyTypeDeclarationOption); - } + + setString(settings, DefaultCodeFormatterConstants.FORMATTER_KEEP_ANNOTATION_DECLARATION_ON_ONE_LINE, KEEP_ON_ONE_LINE_VALUES, + v -> this.keep_annotation_declaration_on_one_line = v); + setString(settings, DefaultCodeFormatterConstants.FORMATTER_KEEP_ANONYMOUS_TYPE_DECLARATION_ON_ONE_LINE, KEEP_ON_ONE_LINE_VALUES, + v -> this.keep_anonymous_type_declaration_on_one_line = v); + setString(settings, DefaultCodeFormatterConstants.FORMATTER_KEEP_IF_THEN_BODY_BLOCK_ON_ONE_LINE, KEEP_ON_ONE_LINE_VALUES, + v -> this.keep_if_then_body_block_on_one_line = v); + setString(settings, DefaultCodeFormatterConstants.FORMATTER_KEEP_LOOP_BODY_BLOCK_ON_ONE_LINE, KEEP_ON_ONE_LINE_VALUES, + v -> this.keep_loop_body_block_on_one_line = v); + setString(settings, DefaultCodeFormatterConstants.FORMATTER_KEEP_LAMBDA_BODY_BLOCK_ON_ONE_LINE, KEEP_ON_ONE_LINE_VALUES, + v -> this.keep_lambda_body_block_on_one_line = v); + setString(settings, DefaultCodeFormatterConstants.FORMATTER_KEEP_CODE_BLOCK_ON_ONE_LINE, + Arrays.asList(DefaultCodeFormatterConstants.ONE_LINE_NEVER, DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY), + v -> this.keep_code_block_on_one_line = v); + setString(settings, DefaultCodeFormatterConstants.FORMATTER_KEEP_ENUM_CONSTANT_DECLARATION_ON_ONE_LINE, KEEP_ON_ONE_LINE_VALUES, + v -> this.keep_enum_constant_declaration_on_one_line = v); + setString(settings, DefaultCodeFormatterConstants.FORMATTER_KEEP_ENUM_DECLARATION_ON_ONE_LINE, KEEP_ON_ONE_LINE_VALUES, + v -> this.keep_enum_declaration_on_one_line = v); + setString(settings, DefaultCodeFormatterConstants.FORMATTER_KEEP_METHOD_BODY_ON_ONE_LINE, KEEP_ON_ONE_LINE_VALUES, + v -> this.keep_method_body_on_one_line = v); + setString(settings, DefaultCodeFormatterConstants.FORMATTER_KEEP_TYPE_DECLARATION_ON_ONE_LINE, KEEP_ON_ONE_LINE_VALUES, + v -> this.keep_type_declaration_on_one_line = v); + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_KEEP_SIMPLE_GETTER_SETTER_ON_ONE_LINE, DefaultCodeFormatterConstants.TRUE, + v -> this.keep_simple_getter_setter_on_one_line = v); + final Object insertNewLineAfterLabelOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_LABEL); if (insertNewLineAfterLabelOption != null) { this.insert_new_line_after_label = JavaCore.INSERT.equals(insertNewLineAfterLabelOption); @@ -2380,6 +2397,21 @@ public class DefaultCodeFormatterOptions { return defaultValue; } + private void setString(Map<String, String> settings, String key, List<String> allowedValues, Consumer<String> setter) { + Object value = settings.get(key); + if (value != null) { + if (!allowedValues.contains(value)) + throw new IllegalArgumentException("Unrecognized value for setting " + key + ": " + value); //$NON-NLS-1$ //$NON-NLS-2$ + setter.accept((String) value); + } + } + + private void setBoolean(Map<String, String> settings, String key, String trueValue, Consumer<Boolean> setter) { + Object value = settings.get(key); + if (value != null) + setter.accept(trueValue.equals(value)); + } + /** * This method is used to handle deprecated preferences which might be replaced by * one or more preferences. @@ -2476,6 +2508,51 @@ public class DefaultCodeFormatterOptions { this.insert_new_line_after_annotation_on_local_variable = JavaCore.INSERT.equals(insertNewLineAfterAnnotationOnLocalVariableOption); } } + + // insert new line between empty braces -> keep braced code on one line + HashMap<Boolean, String> insertToOneLine = new HashMap<>(); + insertToOneLine.put(true, DefaultCodeFormatterConstants.ONE_LINE_NEVER); + insertToOneLine.put(false, DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY); + if (settings.get(DefaultCodeFormatterConstants.FORMATTER_KEEP_ANNOTATION_DECLARATION_ON_ONE_LINE) == null) { + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANNOTATION_DECLARATION, JavaCore.INSERT, + v -> this.keep_annotation_declaration_on_one_line = insertToOneLine.get(v)); + } + if (settings.get(DefaultCodeFormatterConstants.FORMATTER_KEEP_ANONYMOUS_TYPE_DECLARATION_ON_ONE_LINE) == null) { + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANONYMOUS_TYPE_DECLARATION, JavaCore.INSERT, + v -> this.keep_anonymous_type_declaration_on_one_line = insertToOneLine.get(v)); + } + if (settings.get(DefaultCodeFormatterConstants.FORMATTER_KEEP_IF_THEN_BODY_BLOCK_ON_ONE_LINE) == null) { + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_BLOCK, JavaCore.INSERT, + v -> this.keep_if_then_body_block_on_one_line = insertToOneLine.get(v)); + } + if (settings.get(DefaultCodeFormatterConstants.FORMATTER_KEEP_LOOP_BODY_BLOCK_ON_ONE_LINE) == null) { + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_BLOCK, JavaCore.INSERT, + v -> this.keep_loop_body_block_on_one_line = insertToOneLine.get(v)); + } + if (settings.get(DefaultCodeFormatterConstants.FORMATTER_KEEP_LAMBDA_BODY_BLOCK_ON_ONE_LINE) == null) { + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_BLOCK, JavaCore.INSERT, + v -> this.keep_lambda_body_block_on_one_line = insertToOneLine.get(v)); + } + if (settings.get(DefaultCodeFormatterConstants.FORMATTER_KEEP_CODE_BLOCK_ON_ONE_LINE) == null) { + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_BLOCK, JavaCore.INSERT, + v -> this.keep_code_block_on_one_line = insertToOneLine.get(v)); + } + if (settings.get(DefaultCodeFormatterConstants.FORMATTER_KEEP_ENUM_CONSTANT_DECLARATION_ON_ONE_LINE) == null) { + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ENUM_CONSTANT, JavaCore.INSERT, + v -> this.keep_enum_constant_declaration_on_one_line = insertToOneLine.get(v)); + } + if (settings.get(DefaultCodeFormatterConstants.FORMATTER_KEEP_ENUM_DECLARATION_ON_ONE_LINE) == null) { + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ENUM_DECLARATION, JavaCore.INSERT, + v -> this.keep_enum_declaration_on_one_line = insertToOneLine.get(v)); + } + if (settings.get(DefaultCodeFormatterConstants.FORMATTER_KEEP_METHOD_BODY_ON_ONE_LINE) == null) { + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_METHOD_BODY, JavaCore.INSERT, + v -> this.keep_method_body_on_one_line = insertToOneLine.get(v)); + } + if (settings.get(DefaultCodeFormatterConstants.FORMATTER_KEEP_TYPE_DECLARATION_ON_ONE_LINE) == null) { + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_TYPE_DECLARATION, JavaCore.INSERT, + v -> this.keep_type_declaration_on_one_line = insertToOneLine.get(v)); + } } public void setDefaultSettings() { @@ -2596,13 +2673,17 @@ public class DefaultCodeFormatterOptions { this.insert_new_line_before_else_in_if_statement = false; this.insert_new_line_before_finally_in_try_statement = false; this.insert_new_line_before_while_in_do_statement = false; - this.insert_new_line_in_empty_anonymous_type_declaration = true; - this.insert_new_line_in_empty_block = true; - this.insert_new_line_in_empty_annotation_declaration = true; - this.insert_new_line_in_empty_enum_constant = true; - 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; + this.keep_annotation_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_if_then_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_lambda_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_loop_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_code_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_enum_constant_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_enum_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_simple_getter_setter_on_one_line = false; this.insert_space_after_and_in_type_parameter = true; this.insert_space_after_assignment_operator = true; this.insert_space_after_at_in_annotation = false; @@ -2916,13 +2997,16 @@ public class DefaultCodeFormatterOptions { this.insert_new_line_before_else_in_if_statement = false; this.insert_new_line_before_finally_in_try_statement = false; this.insert_new_line_before_while_in_do_statement = false; - this.insert_new_line_in_empty_anonymous_type_declaration = true; - this.insert_new_line_in_empty_block = true; - this.insert_new_line_in_empty_annotation_declaration = true; - this.insert_new_line_in_empty_enum_constant = true; - 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; + this.keep_annotation_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_anonymous_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_if_then_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_lambda_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_loop_body_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_code_block_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_enum_constant_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_enum_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_method_body_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; + this.keep_type_declaration_on_one_line = DefaultCodeFormatterConstants.ONE_LINE_NEVER; this.insert_space_after_and_in_type_parameter = true; this.insert_space_after_assignment_operator = true; this.insert_space_after_at_in_annotation = false; diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/LineBreaksPreparator.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/LineBreaksPreparator.java index b0b16b86ff..9dc434e889 100644 --- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/LineBreaksPreparator.java +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/LineBreaksPreparator.java @@ -150,8 +150,7 @@ public class LineBreaksPreparator extends ASTVisitor { breakLineBefore(node); handleBracedCode(node, node.getName(), this.options.brace_position_for_type_declaration, - this.options.indent_body_declarations_compare_to_type_header, - this.options.insert_new_line_in_empty_type_declaration); + this.options.indent_body_declarations_compare_to_type_header); this.declarationModifierVisited = false; return true; @@ -203,8 +202,7 @@ public class LineBreaksPreparator extends ASTVisitor { @Override public boolean visit(EnumDeclaration node) { handleBracedCode(node, node.getName(), this.options.brace_position_for_enum_declaration, - this.options.indent_body_declarations_compare_to_enum_declaration_header, - this.options.insert_new_line_in_empty_enum_declaration); + this.options.indent_body_declarations_compare_to_enum_declaration_header); handleBodyDeclarations(node.bodyDeclarations()); List<EnumConstantDeclaration> enumConstants = node.enumConstants(); @@ -236,8 +234,7 @@ public class LineBreaksPreparator extends ASTVisitor { @Override public boolean visit(AnnotationTypeDeclaration node) { handleBracedCode(node, node.getName(), this.options.brace_position_for_annotation_type_declaration, - this.options.indent_body_declarations_compare_to_annotation_declaration_header, - this.options.insert_new_line_in_empty_annotation_declaration); + this.options.indent_body_declarations_compare_to_annotation_declaration_header); handleBodyDeclarations(node.bodyDeclarations()); if (node.getModifiers() == 0) @@ -251,12 +248,10 @@ public class LineBreaksPreparator extends ASTVisitor { public boolean visit(AnonymousClassDeclaration node) { if (node.getParent() instanceof EnumConstantDeclaration) { handleBracedCode(node, null, this.options.brace_position_for_enum_constant, - this.options.indent_body_declarations_compare_to_enum_constant_header, - this.options.insert_new_line_in_empty_enum_constant); + this.options.indent_body_declarations_compare_to_enum_constant_header); } else { handleBracedCode(node, null, this.options.brace_position_for_anonymous_type_declaration, - this.options.indent_body_declarations_compare_to_type_header, - this.options.insert_new_line_in_empty_anonymous_type_declaration); + this.options.indent_body_declarations_compare_to_type_header); } handleBodyDeclarations(node.bodyDeclarations()); return true; @@ -276,9 +271,7 @@ public class LineBreaksPreparator extends ASTVisitor { String bracePosition = node.isConstructor() ? this.options.brace_position_for_constructor_declaration : this.options.brace_position_for_method_declaration; - handleBracedCode(node.getBody(), null, bracePosition, - this.options.indent_statements_compare_to_body, - this.options.insert_new_line_in_empty_method_body); + handleBracedCode(node.getBody(), null, bracePosition, this.options.indent_statements_compare_to_body); Token openBrace = this.tm.firstTokenIn(node.getBody(), TokenNameLBRACE); if (openBrace.getLineBreaksAfter() > 0) // if not, these are empty braces openBrace.putLineBreaksAfter(this.options.blank_lines_at_beginning_of_method_body + 1); @@ -287,18 +280,14 @@ public class LineBreaksPreparator extends ASTVisitor { @Override public boolean visit(Block node) { - if (this.options.keep_guardian_clause_on_one_line && this.tm.isGuardClause(node)) - return true; - List<Statement> statements = node.statements(); for (Statement statement : statements) { if (this.options.put_empty_statement_on_new_line || !(statement instanceof EmptyStatement)) breakLineBefore(statement); } - if (node.getParent().getLength() == 0) - return true; // this is a fake block created by parsing in statements mode - ASTNode parent = node.getParent(); + if (parent.getLength() == 0) + return true; // this is a fake block created by parsing in statements mode if (parent instanceof MethodDeclaration) return true; // braces have been handled in #visit(MethodDeclaration) @@ -312,8 +301,7 @@ public class LineBreaksPreparator extends ASTVisitor { } else if (parent instanceof LambdaExpression) { bracePosition = this.options.brace_position_for_lambda_body; } - handleBracedCode(node, null, bracePosition, this.options.indent_statements_compare_to_block, - this.options.insert_new_line_in_empty_block); + handleBracedCode(node, null, bracePosition, this.options.indent_statements_compare_to_block); return true; } @@ -321,7 +309,7 @@ public class LineBreaksPreparator extends ASTVisitor { @Override public boolean visit(SwitchStatement node) { handleBracedCode(node, node.getExpression(), this.options.brace_position_for_switch, - this.options.indent_switchstatements_compare_to_switch, true); + this.options.indent_switchstatements_compare_to_switch); List<Statement> statements = node.statements(); if (this.options.indent_switchstatements_compare_to_cases) { @@ -690,8 +678,7 @@ public class LineBreaksPreparator extends ASTVisitor { // using settings for type declaration and fields for now, add new settings if necessary breakLineBefore(node); handleBracedCode(node, node.getName(), this.options.brace_position_for_type_declaration, - this.options.indent_body_declarations_compare_to_type_header, - this.options.insert_new_line_in_empty_type_declaration); + this.options.indent_body_declarations_compare_to_type_header); List<ModuleDirective> statements = node.moduleStatements(); ModuleDirective previous = null; @@ -711,8 +698,7 @@ public class LineBreaksPreparator extends ASTVisitor { this.tm.firstTokenIn(node, -1).breakBefore(); } - private void handleBracedCode(ASTNode node, ASTNode nodeBeforeOpenBrace, String bracePosition, boolean indentBody, - boolean newLineInEmpty) { + private void handleBracedCode(ASTNode node, ASTNode nodeBeforeOpenBrace, String bracePosition, boolean indentBody) { int openBraceIndex = nodeBeforeOpenBrace == null ? this.tm.firstIndexIn(node, TokenNameLBRACE) : this.tm.firstIndexAfter(nodeBeforeOpenBrace, TokenNameLBRACE); @@ -721,18 +707,9 @@ public class LineBreaksPreparator extends ASTVisitor { Token closeBraceToken = this.tm.get(closeBraceIndex); handleBracePosition(openBraceToken, closeBraceIndex, bracePosition); - boolean isEmpty = true; - for (int i = openBraceIndex + 1; i < closeBraceIndex; i++) { - if (!this.tm.get(i).isComment()) { - isEmpty = false; - break; - } - } + openBraceToken.breakAfter(); + closeBraceToken.breakBefore(); - if (!isEmpty || newLineInEmpty) { - openBraceToken.breakAfter(); - closeBraceToken.breakBefore(); - } if (indentBody) { adjustEmptyLineAfter(openBraceIndex, 1); this.tm.get(openBraceIndex + 1).indent(); diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/OneLineEnforcer.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/OneLineEnforcer.java new file mode 100644 index 0000000000..9d9763fe05 --- /dev/null +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/OneLineEnforcer.java @@ -0,0 +1,217 @@ +/******************************************************************************* + * Copyright (c) 2014, 2018 Mateusz Matela and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Mateusz Matela <mateusz.matela@gmail.com> - Initial API and implementation + * + *******************************************************************************/ +package org.eclipse.jdt.internal.formatter; + +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameLBRACE; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameRBRACE; +import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNamewhile; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.ASTVisitor; +import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration; +import org.eclipse.jdt.core.dom.AnonymousClassDeclaration; +import org.eclipse.jdt.core.dom.Assignment; +import org.eclipse.jdt.core.dom.Block; +import org.eclipse.jdt.core.dom.DoStatement; +import org.eclipse.jdt.core.dom.EnhancedForStatement; +import org.eclipse.jdt.core.dom.EnumConstantDeclaration; +import org.eclipse.jdt.core.dom.EnumDeclaration; +import org.eclipse.jdt.core.dom.ExpressionStatement; +import org.eclipse.jdt.core.dom.ForStatement; +import org.eclipse.jdt.core.dom.IfStatement; +import org.eclipse.jdt.core.dom.LambdaExpression; +import org.eclipse.jdt.core.dom.MethodDeclaration; +import org.eclipse.jdt.core.dom.ModuleDeclaration; +import org.eclipse.jdt.core.dom.PrimitiveType; +import org.eclipse.jdt.core.dom.ReturnStatement; +import org.eclipse.jdt.core.dom.Statement; +import org.eclipse.jdt.core.dom.ThrowStatement; +import org.eclipse.jdt.core.dom.Type; +import org.eclipse.jdt.core.dom.TypeDeclaration; +import org.eclipse.jdt.core.dom.WhileStatement; +import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; + +/** Implementation of the "Keep braced code on one line" feature. */ +public class OneLineEnforcer extends ASTVisitor { + private final TokenManager tm; + private final DefaultCodeFormatterOptions options; + + public OneLineEnforcer(TokenManager tokenManager, DefaultCodeFormatterOptions options) { + this.tm = tokenManager; + this.options = options; + } + + @Override + public void endVisit(TypeDeclaration node) { + if (node.getParent().getLength() == 0) + return; // this is a fake block created by parsing in statements mode + tryKeepOnOneLine(node, node.getName(), node.bodyDeclarations(), this.options.keep_type_declaration_on_one_line); + } + + @Override + public void endVisit(EnumDeclaration node) { + List<ASTNode> items = new ArrayList<>(); + items.addAll(node.bodyDeclarations()); + items.addAll(node.enumConstants()); + tryKeepOnOneLine(node, node.getName(), items, this.options.keep_enum_declaration_on_one_line); + } + + @Override + public void endVisit(AnnotationTypeDeclaration node) { + tryKeepOnOneLine(node, node.getName(), node.bodyDeclarations(), + this.options.keep_annotation_declaration_on_one_line); + } + + @Override + public void endVisit(AnonymousClassDeclaration node) { + if (node.getParent() instanceof EnumConstantDeclaration) { + tryKeepOnOneLine(node, null, node.bodyDeclarations(), + this.options.keep_enum_constant_declaration_on_one_line); + } else { + tryKeepOnOneLine(node, null, node.bodyDeclarations(), + this.options.keep_anonymous_type_declaration_on_one_line); + } + } + + @Override + public void endVisit(Block node) { + ASTNode parent = node.getParent(); + List<Statement> statements = node.statements(); + if (parent.getLength() == 0) + return; // this is a fake block created by parsing in statements mode + String oneLineOption; + if (parent instanceof MethodDeclaration) { + oneLineOption = this.options.keep_method_body_on_one_line; + if (this.options.keep_simple_getter_setter_on_one_line) { + MethodDeclaration method = (MethodDeclaration) parent; + String name = method.getName().getIdentifier(); + Type returnType = method.getReturnType2(); + boolean returnsVoid = returnType instanceof PrimitiveType + && ((PrimitiveType) returnType).getPrimitiveTypeCode() == PrimitiveType.VOID; + boolean isGetter = name.matches("(is|get)\\p{Lu}.*") //$NON-NLS-1$ + && !method.isConstructor() && !returnsVoid && method.parameters().isEmpty() + && statements.size() == 1 && statements.get(0) instanceof ReturnStatement; + boolean isSetter = name.matches("set\\p{Lu}.*") //$NON-NLS-1$ + && !method.isConstructor() && returnsVoid && method.parameters().size() == 1 + && statements.size() == 1 && statements.get(0) instanceof ExpressionStatement + && ((ExpressionStatement) statements.get(0)).getExpression() instanceof Assignment; + if (isGetter || isSetter) + oneLineOption = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + } + } else if (parent instanceof IfStatement && ((IfStatement) parent).getElseStatement() == null) { + oneLineOption = this.options.keep_if_then_body_block_on_one_line; + if (this.options.keep_guardian_clause_on_one_line) { + boolean isGuardian = statements.size() == 1 && (statements.get(0) instanceof ReturnStatement + || statements.get(0) instanceof ThrowStatement); + // guard clause cannot start with a comment: https://bugs.eclipse.org/58565 + int openBraceIndex = this.tm.firstIndexIn(node, TokenNameLBRACE); + isGuardian = isGuardian && !this.tm.get(openBraceIndex + 1).isComment(); + if (isGuardian) + oneLineOption = DefaultCodeFormatterConstants.ONE_LINE_ALWAYS; + } + } else if (parent instanceof LambdaExpression) { + oneLineOption = this.options.keep_lambda_body_block_on_one_line; + } else if (parent instanceof ForStatement || parent instanceof EnhancedForStatement + || parent instanceof WhileStatement) { + oneLineOption = this.options.keep_loop_body_block_on_one_line; + } else if (parent instanceof DoStatement) { + oneLineOption = this.options.keep_loop_body_block_on_one_line; + int openBraceIndex = this.tm.firstIndexIn(node, TokenNameLBRACE); + int closeBraceIndex = this.tm.lastIndexIn(node, TokenNameRBRACE); + Token whileToken = this.tm.firstTokenAfter(node, TokenNamewhile); + int lastIndex = whileToken.getLineBreaksBefore() == 0 ? this.tm.lastIndexIn(parent, -1) : closeBraceIndex; + tryKeepOnOneLine(openBraceIndex, closeBraceIndex, lastIndex, statements, oneLineOption); + return; + } else { + oneLineOption = this.options.keep_code_block_on_one_line; + } + tryKeepOnOneLine(node, null, statements, oneLineOption); + } + + @Override + public void endVisit(ModuleDeclaration node) { + tryKeepOnOneLine(node, node.getName(), node.moduleStatements(), this.options.keep_type_declaration_on_one_line); + } + + private void tryKeepOnOneLine(ASTNode node, ASTNode nodeBeforeOpenBrace, List<? extends ASTNode> items, + String oneLineOption) { + int openBraceIndex = nodeBeforeOpenBrace == null ? this.tm.firstIndexIn(node, TokenNameLBRACE) + : this.tm.firstIndexAfter(nodeBeforeOpenBrace, TokenNameLBRACE); + int closeBraceIndex = this.tm.lastIndexIn(node, TokenNameRBRACE); + tryKeepOnOneLine(openBraceIndex, closeBraceIndex, closeBraceIndex, items, oneLineOption); + } + + private void tryKeepOnOneLine(int openBraceIndex, int closeBraceIndex, int lastIndex, List<? extends ASTNode> items, + String oneLineOption) { + if (DefaultCodeFormatterConstants.ONE_LINE_NEVER.equals(oneLineOption)) + return; + if (DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY.equals(oneLineOption) && !items.isEmpty()) + return; + if (DefaultCodeFormatterConstants.ONE_LINE_IF_SINGLE_ITEM.equals(oneLineOption) && items.size() > 1) + return; + if (DefaultCodeFormatterConstants.ONE_LINE_PRESERVE.equals(oneLineOption) + && this.tm.countLineBreaksBetween(this.tm.get(openBraceIndex), this.tm.get(lastIndex)) > 0) + return; + + Set<Integer> breakIndexes = items.stream().map(n -> this.tm.firstIndexIn(n, -1)).collect(Collectors.toSet()); + breakIndexes.add(openBraceIndex + 1); + breakIndexes.add(closeBraceIndex); + Token prev = this.tm.get(openBraceIndex); + int startPos = this.tm.getPositionInLine(openBraceIndex); + int pos = startPos + this.tm.getLength(prev, startPos); + for (int i = openBraceIndex + 1; i <= lastIndex; i++) { + Token token = this.tm.get(i); + int preexistingBreaks = this.tm.countLineBreaksBetween(prev, token); + if (this.options.number_of_empty_lines_to_preserve > 0 && preexistingBreaks > 1) + return; // blank line will be preserved + boolean isSpace = prev.isSpaceAfter() || token.isSpaceBefore(); + if (prev.isComment() || token.isComment()) { + if (preexistingBreaks > 0) + return; // line break around a comment will be preserved + char charBefore = this.tm.charAt(token.originalStart - 1); + isSpace = isSpace || charBefore == ' ' || charBefore == '\t'; + } + if (prev.getLineBreaksAfter() > 0 || token.getLineBreaksBefore() > 0) { + if (!breakIndexes.contains(i)) + return; // extra line break within an item, can't remove it + isSpace = isSpace || !(i == closeBraceIndex && i == openBraceIndex + 1); + } + if (isSpace) + pos++; + pos += this.tm.getLength(token, pos); + prev = token; + } + if (!items.isEmpty()) { + if (items.get(0).getParent().getParent() instanceof LambdaExpression) + pos -= startPos; // lambda body could be put in a wrapped line, so only check its own width + if (pos > this.options.page_width) + return; // line width limit exceeded + } + + for (Integer i : breakIndexes) { + prev = this.tm.get(i - 1); + prev.clearLineBreaksAfter(); + Token token = this.tm.get(i); + token.clearLineBreaksBefore(); + if (!items.isEmpty()) + token.spaceBefore(); + } + } +} diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/SpacePreparator.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/SpacePreparator.java index 5c933ff8c5..96353d1bfa 100644 --- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/SpacePreparator.java +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/SpacePreparator.java @@ -571,11 +571,6 @@ public class SpacePreparator extends ASTVisitor { Statement thenStatement = node.getThenStatement(); handleTokenBefore(thenStatement, TokenNameRPAREN, this.options.insert_space_before_closing_paren_in_if, false); - if (thenStatement instanceof Block && this.tm.isGuardClause((Block) thenStatement)) { - handleToken(thenStatement, TokenNameLBRACE, false, true); - this.tm.lastTokenIn(node, TokenNameRBRACE).spaceBefore(); - } - handleLoopBody(thenStatement); handleSemicolon(thenStatement); return true; diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/TokenManager.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/TokenManager.java index 5783e12ba9..4e42073ab8 100644 --- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/TokenManager.java +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/TokenManager.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2014, 2016 Mateusz Matela and others. + * Copyright (c) 2014, 2018 Mateusz Matela and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -15,7 +15,6 @@ package org.eclipse.jdt.internal.formatter; import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameCOMMENT_BLOCK; import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameCOMMENT_JAVADOC; -import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameLBRACE; import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameNotAToken; import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNameStringLiteral; @@ -27,10 +26,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.eclipse.jdt.core.dom.ASTNode; -import org.eclipse.jdt.core.dom.Block; -import org.eclipse.jdt.core.dom.IfStatement; -import org.eclipse.jdt.core.dom.ReturnStatement; -import org.eclipse.jdt.core.dom.ThrowStatement; import org.eclipse.jdt.internal.formatter.Token.WrapMode; import org.eclipse.jdt.internal.formatter.linewrap.CommentWrapExecutor; @@ -177,21 +172,6 @@ public class TokenManager implements Iterable<Token> { return this.tokens.iterator(); } - public boolean isGuardClause(Block node) { - if (node.statements().size() != 1) - return false; - ASTNode parent = node.getParent(); - if (!(parent instanceof IfStatement) || ((IfStatement) parent).getElseStatement() != null) - return false; - Object statement = node.statements().get(0); - if (!(statement instanceof ReturnStatement) && !(statement instanceof ThrowStatement)) - return false; - // guard clause cannot start with a comment - // https://bugs.eclipse.org/bugs/show_bug.cgi?id=58565 - int openBraceIndex = firstIndexIn(node, TokenNameLBRACE); - return !get(openBraceIndex + 1).isComment(); - } - public int firstIndexIn(ASTNode node, int tokenType) { int index = findIndex(node.getStartPosition(), tokenType, true); assert tokenInside(node, index); diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/Aligner.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/Aligner.java index f71cf15e3f..011565e0a0 100644 --- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/Aligner.java +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/Aligner.java @@ -93,7 +93,7 @@ public class Aligner { } public void handleAlign(List<BodyDeclaration> bodyDeclarations) { - if (!this.options.align_type_members_on_columns) + if (!this.options.align_type_members_on_columns || areKeptOnOneLine(bodyDeclarations)) return; List<List<FieldDeclaration>> fieldGroups = toAlignGroups(bodyDeclarations, n -> optionalCast(n, FieldDeclaration.class)); @@ -110,12 +110,18 @@ public class Aligner { public void handleAlign(Block block) { List<Statement> statements = block.statements(); + if (areKeptOnOneLine(statements)) + return; if (this.options.align_variable_declarations_on_columns) alignDeclarations(statements); if (this.options.align_assignment_statements_on_columns) alignAssignmentStatements(statements); } + private boolean areKeptOnOneLine(List<? extends ASTNode> nodes) { + return nodes.stream().allMatch(n -> this.tm.firstTokenIn(n, -1).getLineBreaksBefore() == 0); + } + private void alignDeclarations(List<Statement> statements) { List<List<VariableDeclarationStatement>> variableGroups = toAlignGroups(statements, n -> optionalCast(n, VariableDeclarationStatement.class)); diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java index ca9a2c6e04..cf7ba63479 100644 --- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java @@ -186,10 +186,10 @@ public class WrapPreparator extends ASTVisitor { * temporary values used when calling {@link #handleWrap(int)} to avoid ArrayList initialization and long lists of * parameters */ - private List<Integer> wrapIndexes = new ArrayList<Integer>(); + private List<Integer> wrapIndexes = new ArrayList<>(); /** Indexes for wraps that shouldn't happen but should be indented if cannot be removed */ - private List<Integer> secondaryWrapIndexes = new ArrayList<Integer>(); - private List<Float> wrapPenalties = new ArrayList<Float>(); + private List<Integer> secondaryWrapIndexes = new ArrayList<>(); + private List<Float> wrapPenalties = new ArrayList<>(); private int wrapParentIndex = -1; private int wrapGroupEnd = -1; @@ -773,6 +773,23 @@ public class WrapPreparator extends ASTVisitor { public boolean visit(LambdaExpression node) { if (node.getBody() instanceof Block) { forceContinuousWrapping(node.getBody(), this.tm.firstIndexIn(node, -1)); + + List<Statement> statements = ((Block) node.getBody()).statements(); + if (!statements.isEmpty()) { + int openBraceIndex = this.tm.firstIndexBefore(statements.get(0), TokenNameLBRACE); + int closeBraceIndex = this.tm.firstIndexAfter(statements.get(statements.size() - 1), TokenNameRBRACE); + boolean areKeptOnOneLine = statements.stream() + .allMatch(n -> this.tm.firstTokenIn(n, -1).getLineBreaksBefore() == 0); + if (areKeptOnOneLine) { + for (Statement statement : statements) + this.wrapIndexes.add(this.tm.firstIndexIn(statement, -1)); + this.wrapParentIndex = openBraceIndex; + this.wrapGroupEnd = closeBraceIndex; + handleWrap(Alignment.M_ONE_PER_LINE_SPLIT, node); + this.tm.get(closeBraceIndex).setWrapPolicy(new WrapPolicy(WrapMode.TOP_PRIORITY, openBraceIndex, + closeBraceIndex, 0, this.currentDepth, 1, false, false)); + } + } } if (node.hasParentheses()) { List<VariableDeclaration> parameters = node.parameters(); @@ -1036,6 +1053,8 @@ public class WrapPreparator extends ASTVisitor { } else if (parentNode instanceof DoStatement) { extraIndent = 0; this.wrapParentIndex = this.tm.firstIndexIn(parentNode, -1); // only if !indoentOnColumn + } else if (parentNode instanceof LambdaExpression) { + extraIndent = 1; } else if ((wrappingOption & Alignment.M_INDENT_BY_ONE) != 0) { extraIndent = 1; } else if (parentNode instanceof ArrayInitializer) { |