diff options
15 files changed, 897 insertions, 20 deletions
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 36ec699cc1..82a6b4dcce 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2018 IBM Corporation and others. + * Copyright (c) 2000, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -7,7 +7,11 @@ * https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 - * + * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation * Brock Janiczak - Contribution for bug 150741 @@ -14837,4 +14841,79 @@ public void testBug541011g() throws JavaModelException { String input = getCompilationUnit("Formatter", "", "test541011", "in.java").getSource(); formatSource(input, getCompilationUnit("Formatter", "", "test541011", "G_out.java").getSource()); } +/** + * https://bugs.eclipse.org/543818 - [12] Formatter Support for Switch Expressions + */ +public void testBug543818a() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_12); + this.formatterPrefs.insert_space_before_comma_in_switch_case_expressions = true; + this.formatterPrefs.insert_space_before_colon_in_case = true; + this.formatterPrefs.indent_switchstatements_compare_to_switch = true; + String input = getCompilationUnit("Formatter", "", "test543818", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test543818", "A_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/543818 - [12] Formatter Support for Switch Expressions + */ +public void testBug543818b() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_12); + this.formatterPrefs.insert_space_after_comma_in_switch_case_expressions = false; + this.formatterPrefs.insert_space_before_closing_paren_in_switch = true; + this.formatterPrefs.indent_switchstatements_compare_to_cases = false; + String input = getCompilationUnit("Formatter", "", "test543818", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test543818", "B_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/543818 - [12] Formatter Support for Switch Expressions + */ +public void testBug543818c() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_12); + this.formatterPrefs.insert_space_before_arrow_in_switch_case = false; + this.formatterPrefs.insert_space_before_opening_paren_in_switch = false; + this.formatterPrefs.indent_breaks_compare_to_cases = false; + String input = getCompilationUnit("Formatter", "", "test543818", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test543818", "C_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/543818 - [12] Formatter Support for Switch Expressions + */ +public void testBug543818d() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_12); + this.formatterPrefs.insert_space_after_arrow_in_switch_case = false; + this.formatterPrefs.insert_space_after_opening_paren_in_switch = true; + this.formatterPrefs.insert_space_before_opening_brace_in_block = false; + this.formatterPrefs.brace_position_for_switch = DefaultCodeFormatterConstants.NEXT_LINE; + String input = getCompilationUnit("Formatter", "", "test543818", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test543818", "D_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/543818 - [12] Formatter Support for Switch Expressions + */ +public void testBug543818e() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_12); + this.formatterPrefs.insert_space_before_arrow_in_switch_default = false; + this.formatterPrefs.insert_space_before_colon_in_default = true; + this.formatterPrefs.parenthesis_positions_in_switch_statement = DefaultCodeFormatterConstants.SEPARATE_LINES; + String input = getCompilationUnit("Formatter", "", "test543818", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test543818", "E_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/543818 - [12] Formatter Support for Switch Expressions + */ +public void testBug543818f() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_12); + this.formatterPrefs.insert_space_after_arrow_in_switch_default = false; + this.formatterPrefs.insert_space_before_opening_brace_in_switch = false; + this.formatterPrefs.insert_space_before_opening_brace_in_block = false; + String input = getCompilationUnit("Formatter", "", "test543818", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test543818", "F_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/543818 - [12] Formatter Support for Switch Expressions + */ +public void testBug543818g() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_12); + String input = getCompilationUnit("Formatter", "", "test543818", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test543818", "G_out.java").getSource()); +} } diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/A_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/A_out.java new file mode 100644 index 0000000000..89ab5633ba --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/A_out.java @@ -0,0 +1,72 @@ + +public class C { + + void foo() { + switch (a + b + c /* + d */ + + e + f) { + case 1 , 2 , 99999 , 5 , 6 : + method1(); + break; + case 999 , 998 , 997 : + case 996 , 995 , // + 994 : + method2(); + default: + method3(); + } + + switch ("a" + "b" + "c" // + + something) { + case "abc1" -> System.out.println("1"); + case "abc2" , + /* ??? */ "abc3" , "abc4" -> System.out.println("234"); + case "abc5" , "abc6" -> { + } + case "abc7" -> { + System.out.println("7"); + return; + } + default -> System.out.println("?"); + } + + int value = switch (ABC) { + case A : + break 1; + case B : + System.out.println("!!!"); + return; + case BB : + ; + case C , D , E , F , G : + case H , I , J : { + System.out.println("@@@"); + break 3454366; + } + default: + break 6; + }; + + Object value2 = switch (a.b.c.d(e.f.g.h())) { + case a -> { + System.out.println("aaaaaaa"); + break ""; + } + case b + c , d.e , f("aaaaaaaaaaaa"// + + // + "bbbbbbbbb"// + ) , (33) , aaa = bbb + ccc , new int[] { 1, 2, aaa } , AAA::BBB -> (Runnable) () -> f(); + // $$$$ + case new Object() { + String toString() { + return ""; + } + } -> ABCD; + case null -> { + return null; + } + case something -> // + null; + default -> throw new RuntimeException("unsupported"); + }; + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/B_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/B_out.java new file mode 100644 index 0000000000..30ba3b5be8 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/B_out.java @@ -0,0 +1,72 @@ + +public class C { + + void foo() { + switch (a + b + c /* + d */ + + e + f ) { + case 1,2,99999,5,6: + method1(); + break; + case 999,998,997: + case 996,995, // + 994: + method2(); + default: + method3(); + } + + switch ("a" + "b" + "c" // + + something ) { + case "abc1" -> System.out.println("1"); + case "abc2", + /* ??? */ "abc3","abc4" -> System.out.println("234"); + case "abc5","abc6" -> { + } + case "abc7" -> { + System.out.println("7"); + return; + } + default -> System.out.println("?"); + } + + int value = switch (ABC ) { + case A: + break 1; + case B: + System.out.println("!!!"); + return; + case BB: + ; + case C,D,E,F,G: + case H,I,J: { + System.out.println("@@@"); + break 3454366; + } + default: + break 6; + }; + + Object value2 = switch (a.b.c.d(e.f.g.h()) ) { + case a -> { + System.out.println("aaaaaaa"); + break ""; + } + case b + c,d.e,f("aaaaaaaaaaaa"// + + // + "bbbbbbbbb"// + ),(33),aaa = bbb + ccc,new int[] { 1, 2, aaa },AAA::BBB -> (Runnable) () -> f(); + // $$$$ + case new Object() { + String toString() { + return ""; + } + } -> ABCD; + case null -> { + return null; + } + case something -> // + null; + default -> throw new RuntimeException("unsupported"); + }; + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/C_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/C_out.java new file mode 100644 index 0000000000..b98e85a4d6 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/C_out.java @@ -0,0 +1,72 @@ + +public class C { + + void foo() { + switch(a + b + c /* + d */ + + e + f) { + case 1, 2, 99999, 5, 6: + method1(); + break; + case 999, 998, 997: + case 996, 995, // + 994: + method2(); + default: + method3(); + } + + switch("a" + "b" + "c" // + + something) { + case "abc1"-> System.out.println("1"); + case "abc2", + /* ??? */ "abc3", "abc4"-> System.out.println("234"); + case "abc5", "abc6"-> { + } + case "abc7"-> { + System.out.println("7"); + return; + } + default -> System.out.println("?"); + } + + int value = switch(ABC) { + case A: + break 1; + case B: + System.out.println("!!!"); + return; + case BB: + ; + case C, D, E, F, G: + case H, I, J: { + System.out.println("@@@"); + break 3454366; + } + default: + break 6; + }; + + Object value2 = switch(a.b.c.d(e.f.g.h())) { + case a-> { + System.out.println("aaaaaaa"); + break ""; + } + case b + c, d.e, f("aaaaaaaaaaaa"// + + // + "bbbbbbbbb"// + ), (33), aaa = bbb + ccc, new int[] { 1, 2, aaa }, AAA::BBB-> (Runnable) () -> f(); + // $$$$ + case new Object() { + String toString() { + return ""; + } + }-> ABCD; + case null-> { + return null; + } + case something-> // + null; + default -> throw new RuntimeException("unsupported"); + }; + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/D_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/D_out.java new file mode 100644 index 0000000000..49e0db739e --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/D_out.java @@ -0,0 +1,76 @@ + +public class C { + + void foo() { + switch ( a + b + c /* + d */ + + e + f) + { + case 1, 2, 99999, 5, 6: + method1(); + break; + case 999, 998, 997: + case 996, 995, // + 994: + method2(); + default: + method3(); + } + + switch ( "a" + "b" + "c" // + + something) + { + case "abc1" ->System.out.println("1"); + case "abc2", + /* ??? */ "abc3", "abc4" ->System.out.println("234"); + case "abc5", "abc6" ->{ + } + case "abc7" ->{ + System.out.println("7"); + return; + } + default -> System.out.println("?"); + } + + int value = switch ( ABC) + { + case A: + break 1; + case B: + System.out.println("!!!"); + return; + case BB: + ; + case C, D, E, F, G: + case H, I, J:{ + System.out.println("@@@"); + break 3454366; + } + default: + break 6; + }; + + Object value2 = switch ( a.b.c.d(e.f.g.h())) + { + case a ->{ + System.out.println("aaaaaaa"); + break ""; + } + case b + c, d.e, f("aaaaaaaaaaaa"// + + // + "bbbbbbbbb"// + ), (33), aaa = bbb + ccc, new int[] { 1, 2, aaa }, AAA::BBB ->(Runnable) () -> f(); + // $$$$ + case new Object() { + String toString() { + return ""; + } + } ->ABCD; + case null ->{ + return null; + } + case something -> // + null; + default -> throw new RuntimeException("unsupported"); + }; + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/E_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/E_out.java new file mode 100644 index 0000000000..d8bbd1a1f4 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/E_out.java @@ -0,0 +1,80 @@ + +public class C { + + void foo() { + switch ( + a + b + c /* + d */ + + e + f + ) { + case 1, 2, 99999, 5, 6: + method1(); + break; + case 999, 998, 997: + case 996, 995, // + 994: + method2(); + default : + method3(); + } + + switch ( + "a" + "b" + "c" // + + something + ) { + case "abc1" -> System.out.println("1"); + case "abc2", + /* ??? */ "abc3", "abc4" -> System.out.println("234"); + case "abc5", "abc6" -> { + } + case "abc7" -> { + System.out.println("7"); + return; + } + default-> System.out.println("?"); + } + + int value = switch ( + ABC + ) { + case A: + break 1; + case B: + System.out.println("!!!"); + return; + case BB: + ; + case C, D, E, F, G: + case H, I, J: { + System.out.println("@@@"); + break 3454366; + } + default : + break 6; + }; + + Object value2 = switch ( + a.b.c.d(e.f.g.h()) + ) { + case a -> { + System.out.println("aaaaaaa"); + break ""; + } + case b + c, d.e, f("aaaaaaaaaaaa"// + + // + "bbbbbbbbb"// + ), (33), aaa = bbb + ccc, new int[] { 1, 2, aaa }, AAA::BBB -> (Runnable) () -> f(); + // $$$$ + case new Object() { + String toString() { + return ""; + } + } -> ABCD; + case null -> { + return null; + } + case something -> // + null; + default-> throw new RuntimeException("unsupported"); + }; + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/F_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/F_out.java new file mode 100644 index 0000000000..6ad72c331f --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/F_out.java @@ -0,0 +1,72 @@ + +public class C { + + void foo() { + switch (a + b + c /* + d */ + + e + f){ + case 1, 2, 99999, 5, 6: + method1(); + break; + case 999, 998, 997: + case 996, 995, // + 994: + method2(); + default: + method3(); + } + + switch ("a" + "b" + "c" // + + something){ + case "abc1" -> System.out.println("1"); + case "abc2", + /* ??? */ "abc3", "abc4" -> System.out.println("234"); + case "abc5", "abc6" -> { + } + case "abc7" -> { + System.out.println("7"); + return; + } + default ->System.out.println("?"); + } + + int value = switch (ABC){ + case A: + break 1; + case B: + System.out.println("!!!"); + return; + case BB: + ; + case C, D, E, F, G: + case H, I, J:{ + System.out.println("@@@"); + break 3454366; + } + default: + break 6; + }; + + Object value2 = switch (a.b.c.d(e.f.g.h())){ + case a -> { + System.out.println("aaaaaaa"); + break ""; + } + case b + c, d.e, f("aaaaaaaaaaaa"// + + // + "bbbbbbbbb"// + ), (33), aaa = bbb + ccc, new int[] { 1, 2, aaa }, AAA::BBB -> (Runnable) () -> f(); + // $$$$ + case new Object() { + String toString() { + return ""; + } + } -> ABCD; + case null -> { + return null; + } + case something -> // + null; + default ->throw new RuntimeException("unsupported"); + }; + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/G_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/G_out.java new file mode 100644 index 0000000000..aa05baad51 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/G_out.java @@ -0,0 +1,72 @@ + +public class C { + + void foo() { + switch (a + b + c /* + d */ + + e + f) { + case 1, 2, 99999, 5, 6: + method1(); + break; + case 999, 998, 997: + case 996, 995, // + 994: + method2(); + default: + method3(); + } + + switch ("a" + "b" + "c" // + + something) { + case "abc1" -> System.out.println("1"); + case "abc2", + /* ??? */ "abc3", "abc4" -> System.out.println("234"); + case "abc5", "abc6" -> { + } + case "abc7" -> { + System.out.println("7"); + return; + } + default -> System.out.println("?"); + } + + int value = switch (ABC) { + case A: + break 1; + case B: + System.out.println("!!!"); + return; + case BB: + ; + case C, D, E, F, G: + case H, I, J: { + System.out.println("@@@"); + break 3454366; + } + default: + break 6; + }; + + Object value2 = switch (a.b.c.d(e.f.g.h())) { + case a -> { + System.out.println("aaaaaaa"); + break ""; + } + case b + c, d.e, f("aaaaaaaaaaaa"// + + // + "bbbbbbbbb"// + ), (33), aaa = bbb + ccc, new int[] { 1, 2, aaa }, AAA::BBB -> (Runnable) () -> f(); + // $$$$ + case new Object() { + String toString() { + return ""; + } + } -> ABCD; + case null -> { + return null; + } + case something -> // + null; + default -> throw new RuntimeException("unsupported"); + }; + } +} diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/in.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/in.java new file mode 100644 index 0000000000..7dfd080e19 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test543818/in.java @@ -0,0 +1,66 @@ + +public class C { + + void foo() { + switch (a + b + c /* + d */ + + e + f) { + case 1,2,99999, 5,6: + method1(); + break; + case 999, 998, 997: + case 996, 995, // + 994: + method2(); + default: + method3(); + } + + switch ("a" + "b" + "c" // + + something) { + case "abc1" -> System.out.println("1"); + case "abc2", + /* ??? */ "abc3", "abc4" -> System.out.println("234"); + case "abc5", "abc6" -> {} + case "abc7" -> { + System.out.println("7"); + return; + } + default-> System.out.println("?"); + } + + int value = switch(ABC) { + case A: + break 1; + case B: + System.out.println("!!!"); + return; + case BB:; + case C, D, E, F, G: + case H, I, J: { + System.out.println("@@@"); + break 3454366; + } + default: break 6; + }; + + Object value2 = switch(a.b.c.d(e.f.g.h())) { + case a -> { + System.out.println("aaaaaaa"); + break ""; + } + case b + c, d.e, f("aaaaaaaaaaaa"// + +// + "bbbbbbbbb"// + ), (33), aaa = bbb + ccc, + new int[] {1, 2, aaa}, AAA::BBB -> (Runnable)()->f(); + // $$$$ + case new Object() { String toString() { return "";}} -> ABCD; + case null -> { + return null; + } + case something -> // + null; + default -> throw new RuntimeException("unsupported"); + }; + } +} 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 e2f4edd280..9a196a59c3 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 @@ -1,4 +1,3 @@ -
/******************************************************************************* * Copyright (c) 2000, 2019 IBM Corporation and others. * @@ -8,6 +7,10 @@ * https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 + * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. * * Contributors: * IBM Corporation - initial API and implementation @@ -2122,6 +2125,28 @@ public class DefaultCodeFormatterConstants { */ public static final String FORMATTER_INSERT_SPACE_AFTER_AND_IN_TYPE_PARAMETER = JavaCore.PLUGIN_ID + ".formatter.insert_space_after_and_in_type_parameter"; //$NON-NLS-1$ /** + * FORMATTER / Option to insert a space after arrow in switch case + * - option id: "org.eclipse.jdt.core.formatter.insert_space_after_arrow_in_switch_case" + * - possible values: { INSERT, DO_NOT_INSERT } + * - default: INSERT + * </pre> + * @see JavaCore#INSERT + * @see JavaCore#DO_NOT_INSERT + * @since 3.17 BETA_JAVA_12 + */ + public static final String FORMATTER_INSERT_SPACE_AFTER_ARROW_IN_SWITCH_CASE = JavaCore.PLUGIN_ID + ".formatter.insert_space_after_arrow_in_switch_case"; //$NON-NLS-1$ + /** + * FORMATTER / Option to insert a space after arrow in switch default + * - option id: "org.eclipse.jdt.core.formatter.insert_space_after_arrow_in_switch_default" + * - possible values: { INSERT, DO_NOT_INSERT } + * - default: INSERT + * </pre> + * @see JavaCore#INSERT + * @see JavaCore#DO_NOT_INSERT + * @since 3.17 BETA_JAVA_12 + */ + public static final String FORMATTER_INSERT_SPACE_AFTER_ARROW_IN_SWITCH_DEFAULT = JavaCore.PLUGIN_ID + ".formatter.insert_space_after_arrow_in_switch_default"; //$NON-NLS-1$ + /** * <pre> * FORMATTER / Option to insert a space after an assignment operator * - option id: "org.eclipse.jdt.core.formatter.insert_space_after_assignment_operator" @@ -2571,6 +2596,18 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_SUPERINTERFACES = JavaCore.PLUGIN_ID + ".formatter.insert_space_after_comma_in_superinterfaces"; //$NON-NLS-1$ /** * <pre> + * FORMATTER / Option to insert a space after the comma in switch case expressions list + * - option id: "org.eclipse.jdt.core.formatter.insert_space_after_comma_in_switch_case_expressions" + * - possible values: { INSERT, DO_NOT_INSERT } + * - default: INSERT + * </pre> + * @see JavaCore#INSERT + * @see JavaCore#DO_NOT_INSERT + * @since 3.17 BETA_JAVA_12 + */ + public static final String FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_SWITCH_CASE_EXPRESSIONS = JavaCore.PLUGIN_ID + ".formatter.insert_space_after_comma_in_switch_case_expressions"; //$NON-NLS-1$ + /** + * <pre> * FORMATTER / Option to insert a space after the comma in explicit type arguments on method/constructor invocations * - option id: "org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_arguments" * - possible values: { INSERT, DO_NOT_INSERT } @@ -2955,6 +2992,28 @@ public class DefaultCodeFormatterConstants { */ public static final String FORMATTER_INSERT_SPACE_BEFORE_AND_IN_TYPE_PARAMETER = JavaCore.PLUGIN_ID + ".formatter.insert_space_before_and_in_type_parameter"; //$NON-NLS-1$ /** + * FORMATTER / Option to insert a space before arrow in switch case + * - option id: "org.eclipse.jdt.core.formatter.insert_space_before_arrow_in_switch_case" + * - possible values: { INSERT, DO_NOT_INSERT } + * - default: INSERT + * </pre> + * @see JavaCore#INSERT + * @see JavaCore#DO_NOT_INSERT + * @since 3.17 BETA_JAVA_12 + */ + public static final String FORMATTER_INSERT_SPACE_BEFORE_ARROW_IN_SWITCH_CASE = JavaCore.PLUGIN_ID + ".formatter.insert_space_before_arrow_in_switch_case"; //$NON-NLS-1$ + /** + * FORMATTER / Option to insert a space before arrow in switch default + * - option id: "org.eclipse.jdt.core.formatter.insert_space_before_arrow_in_switch_default" + * - possible values: { INSERT, DO_NOT_INSERT } + * - default: INSERT + * </pre> + * @see JavaCore#INSERT + * @see JavaCore#DO_NOT_INSERT + * @since 3.17 BETA_JAVA_12 + */ + public static final String FORMATTER_INSERT_SPACE_BEFORE_ARROW_IN_SWITCH_DEFAULT = JavaCore.PLUGIN_ID + ".formatter.insert_space_before_arrow_in_switch_default"; //$NON-NLS-1$ + /** * <pre> * FORMATTER / Option to insert a space before an assignment operator * - option id: "org.eclipse.jdt.core.formatter.insert_space_before_assignment_operator" @@ -3596,6 +3655,18 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_INSERT_SPACE_BEFORE_COMMA_IN_SUPERINTERFACES = JavaCore.PLUGIN_ID + ".formatter.insert_space_before_comma_in_superinterfaces"; //$NON-NLS-1$ /** * <pre> + * FORMATTER / Option to insert a space before the comma in switch case expressions list + * - option id: "org.eclipse.jdt.core.formatter.insert_space_before_comma_in_switch_case_expressions" + * - possible values: { INSERT, DO_NOT_INSERT } + * - default: DO_NOT_INSERT + * </pre> + * @see JavaCore#INSERT + * @see JavaCore#DO_NOT_INSERT + * @since 3.17 BETA_JAVA_12 + */ + public static final String FORMATTER_INSERT_SPACE_BEFORE_COMMA_IN_SWITCH_CASE_EXPRESSIONS = JavaCore.PLUGIN_ID + ".formatter.insert_space_before_comma_in_switch_case_expressions"; //$NON-NLS-1$ + /** + * <pre> * FORMATTER / Option to insert a space before comma in explicit type arguments on method/constructor invocations * - option id: "org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_arguments" * - possible values: { INSERT, DO_NOT_INSERT } 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 0336bb2867..f03ca925af 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2018 IBM Corporation and others. + * Copyright (c) 2000, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation * Jesper Steen Moller - Contributions for @@ -126,13 +130,13 @@ public class DefaultCodeFormatter extends CodeFormatter { this.workingOptions = new DefaultCodeFormatterOptions(options); this.oldCommentFormatOption = getOldCommentFormatOption(options); String compilerSource = options.get(CompilerOptions.OPTION_Source); - this.sourceLevel = compilerSource != null ? compilerSource : CompilerOptions.VERSION_11; + this.sourceLevel = compilerSource != null ? compilerSource : CompilerOptions.VERSION_12; } else { Map<String, String> settings = DefaultCodeFormatterConstants.getJavaConventionsSettings(); this.originalOptions = new DefaultCodeFormatterOptions(settings); this.workingOptions = new DefaultCodeFormatterOptions(settings); this.oldCommentFormatOption = DefaultCodeFormatterConstants.TRUE; - this.sourceLevel = CompilerOptions.VERSION_11; + this.sourceLevel = CompilerOptions.VERSION_12; } if (defaultCodeFormatterOptions != null) { this.originalOptions.set(defaultCodeFormatterOptions.getMap()); @@ -332,7 +336,7 @@ public class DefaultCodeFormatter extends CodeFormatter { } private ASTParser createParser(int kind) { - ASTParser parser = ASTParser.newParser(AST.JLS11); + ASTParser parser = ASTParser.newParser(AST.JLS12); if (kind == K_MODULE_INFO) { parser.setSource(createDummyModuleInfoCompilationUnit()); @@ -344,6 +348,8 @@ public class DefaultCodeFormatter extends CodeFormatter { Map<String, String> parserOptions = JavaCore.getOptions(); parserOptions.put(CompilerOptions.OPTION_Source, this.sourceLevel); parserOptions.put(CompilerOptions.OPTION_DocCommentSupport, CompilerOptions.ENABLED); + parserOptions.put(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); //TODO + parserOptions.put(CompilerOptions.OPTION_ReportPreviewFeatures, CompilerOptions.IGNORE); parser.setCompilerOptions(parserOptions); return parser; } 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 257f5c53fa..4ec3b4ed49 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 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation * Brock Janiczak - Contribution for bug 150741 @@ -272,6 +276,8 @@ public class DefaultCodeFormatterOptions { public boolean keep_simple_getter_setter_on_one_line; public boolean insert_space_after_and_in_type_parameter; + public boolean insert_space_after_arrow_in_switch_case; + public boolean insert_space_after_arrow_in_switch_default; public boolean insert_space_after_assignment_operator; public boolean insert_space_after_at_in_annotation; public boolean insert_space_after_at_in_annotation_type_declaration; @@ -309,6 +315,7 @@ public class DefaultCodeFormatterOptions { public boolean insert_space_after_comma_in_multiple_local_declarations; public boolean insert_space_after_comma_in_parameterized_type_reference; public boolean insert_space_after_comma_in_superinterfaces; + public boolean insert_space_after_comma_in_switch_case_expressions; public boolean insert_space_after_comma_in_type_arguments; public boolean insert_space_after_comma_in_type_parameters; public boolean insert_space_after_ellipsis; @@ -341,6 +348,8 @@ public class DefaultCodeFormatterOptions { public boolean insert_space_after_semicolon_in_try_resources; public boolean insert_space_after_unary_operator; public boolean insert_space_before_and_in_type_parameter; + public boolean insert_space_before_arrow_in_switch_case; + public boolean insert_space_before_arrow_in_switch_default; public boolean insert_space_before_at_in_annotation_type_declaration; public boolean insert_space_before_assignment_operator; public boolean insert_space_before_multiplicative_operator; @@ -393,6 +402,7 @@ public class DefaultCodeFormatterOptions { public boolean insert_space_before_comma_in_multiple_local_declarations; public boolean insert_space_before_comma_in_parameterized_type_reference; public boolean insert_space_before_comma_in_superinterfaces; + public boolean insert_space_before_comma_in_switch_case_expressions; public boolean insert_space_before_comma_in_type_arguments; public boolean insert_space_before_comma_in_type_parameters; public boolean insert_space_before_ellipsis; @@ -640,6 +650,8 @@ public class DefaultCodeFormatterOptions { 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_ARROW_IN_SWITCH_CASE, this.insert_space_after_arrow_in_switch_case? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); + options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_ARROW_IN_SWITCH_DEFAULT, this.insert_space_after_arrow_in_switch_default? 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); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_AT_IN_ANNOTATION, this.insert_space_after_at_in_annotation? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_AT_IN_ANNOTATION_TYPE_DECLARATION, this.insert_space_after_at_in_annotation_type_declaration ? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); @@ -676,6 +688,7 @@ public class DefaultCodeFormatterOptions { options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_MULTIPLE_LOCAL_DECLARATIONS, this.insert_space_after_comma_in_multiple_local_declarations? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_PARAMETERIZED_TYPE_REFERENCE, this.insert_space_after_comma_in_parameterized_type_reference? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_SUPERINTERFACES, this.insert_space_after_comma_in_superinterfaces? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); + options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_SWITCH_CASE_EXPRESSIONS, this.insert_space_after_comma_in_switch_case_expressions ? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_TYPE_ARGUMENTS, this.insert_space_after_comma_in_type_arguments ? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_TYPE_PARAMETERS, this.insert_space_after_comma_in_type_parameters ? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_OPENING_BRACKET_IN_ARRAY_ALLOCATION_EXPRESSION, this.insert_space_after_opening_bracket_in_array_allocation_expression? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); @@ -708,6 +721,8 @@ public class DefaultCodeFormatterOptions { options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_SEMICOLON_IN_TRY_RESOURCES, this.insert_space_after_semicolon_in_try_resources? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_UNARY_OPERATOR, this.insert_space_after_unary_operator? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_AND_IN_TYPE_PARAMETER, this.insert_space_before_and_in_type_parameter ? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); + options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_ARROW_IN_SWITCH_CASE, this.insert_space_before_arrow_in_switch_case ? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); + options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_ARROW_IN_SWITCH_DEFAULT, this.insert_space_before_arrow_in_switch_default ? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_AT_IN_ANNOTATION_TYPE_DECLARATION, this.insert_space_before_at_in_annotation_type_declaration ? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_ASSIGNMENT_OPERATOR, this.insert_space_before_assignment_operator? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_MULTIPLICATIVE_OPERATOR, this.insert_space_before_multiplicative_operator ? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); @@ -759,6 +774,7 @@ public class DefaultCodeFormatterOptions { options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_COMMA_IN_MULTIPLE_FIELD_DECLARATIONS, this.insert_space_before_comma_in_multiple_field_declarations? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_COMMA_IN_MULTIPLE_LOCAL_DECLARATIONS, this.insert_space_before_comma_in_multiple_local_declarations? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_COMMA_IN_SUPERINTERFACES, this.insert_space_before_comma_in_superinterfaces? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); + options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_COMMA_IN_SWITCH_CASE_EXPRESSIONS, this.insert_space_before_comma_in_switch_case_expressions? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_COMMA_IN_TYPE_ARGUMENTS, this.insert_space_before_comma_in_type_arguments ? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_COMMA_IN_TYPE_PARAMETERS, this.insert_space_before_comma_in_type_parameters? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_COMMA_IN_PARAMETERIZED_TYPE_REFERENCE, this.insert_space_before_comma_in_parameterized_type_reference? JavaCore.INSERT : JavaCore.DO_NOT_INSERT); @@ -1635,6 +1651,10 @@ public class DefaultCodeFormatterOptions { if (insertSpaceAfterAndInWildcardOption != null) { this.insert_space_after_and_in_type_parameter = JavaCore.INSERT.equals(insertSpaceAfterAndInWildcardOption); } + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_ARROW_IN_SWITCH_CASE, JavaCore.INSERT, + v -> this.insert_space_after_arrow_in_switch_case = v); + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_ARROW_IN_SWITCH_DEFAULT, JavaCore.INSERT, + v -> this.insert_space_after_arrow_in_switch_default = v); final Object insertSpaceAfterAssignmentOperatorOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_ASSIGNMENT_OPERATOR); if (insertSpaceAfterAssignmentOperatorOption != null) { this.insert_space_after_assignment_operator = JavaCore.INSERT.equals(insertSpaceAfterAssignmentOperatorOption); @@ -1765,6 +1785,8 @@ public class DefaultCodeFormatterOptions { if (insertSpaceAfterCommaInSuperinterfacesOption != null) { this.insert_space_after_comma_in_superinterfaces = JavaCore.INSERT.equals(insertSpaceAfterCommaInSuperinterfacesOption); } + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_SWITCH_CASE_EXPRESSIONS, JavaCore.INSERT, + v -> this.insert_space_after_comma_in_switch_case_expressions = v); final Object insertSpaceAfterCommaInTypeArgumentsOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_TYPE_ARGUMENTS); if (insertSpaceAfterCommaInTypeArgumentsOption != null) { this.insert_space_after_comma_in_type_arguments = JavaCore.INSERT.equals(insertSpaceAfterCommaInTypeArgumentsOption); @@ -1893,6 +1915,10 @@ public class DefaultCodeFormatterOptions { if (insertSpaceBeforeAndInWildcardOption != null) { this.insert_space_before_and_in_type_parameter = JavaCore.INSERT.equals(insertSpaceBeforeAndInWildcardOption); } + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_ARROW_IN_SWITCH_CASE, JavaCore.INSERT, + v -> this.insert_space_before_arrow_in_switch_case = v); + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_ARROW_IN_SWITCH_DEFAULT, JavaCore.INSERT, + v -> this.insert_space_before_arrow_in_switch_default = v); final Object insertSpaceBeforeAtInAnnotationTypeDeclarationOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_AT_IN_ANNOTATION_TYPE_DECLARATION); if (insertSpaceBeforeAtInAnnotationTypeDeclarationOption != null) { this.insert_space_before_at_in_annotation_type_declaration = JavaCore.INSERT.equals(insertSpaceBeforeAtInAnnotationTypeDeclarationOption); @@ -2087,6 +2113,8 @@ public class DefaultCodeFormatterOptions { if (insertSpaceBeforeCommaInSuperinterfacesOption != null) { this.insert_space_before_comma_in_superinterfaces = JavaCore.INSERT.equals(insertSpaceBeforeCommaInSuperinterfacesOption); } + setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_COMMA_IN_SWITCH_CASE_EXPRESSIONS, JavaCore.INSERT, + v -> this.insert_space_before_comma_in_switch_case_expressions = v); final Object insertSpaceBeforeCommaInTypeArgumentsOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_COMMA_IN_TYPE_ARGUMENTS); if (insertSpaceBeforeCommaInTypeArgumentsOption != null) { this.insert_space_before_comma_in_type_arguments = JavaCore.INSERT.equals(insertSpaceBeforeCommaInTypeArgumentsOption); @@ -2908,6 +2936,8 @@ public class DefaultCodeFormatterOptions { 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_arrow_in_switch_case = true; + this.insert_space_after_arrow_in_switch_default = true; this.insert_space_after_assignment_operator = true; this.insert_space_after_at_in_annotation = false; this.insert_space_after_at_in_annotation_type_declaration = false; @@ -2944,6 +2974,7 @@ public class DefaultCodeFormatterOptions { this.insert_space_after_comma_in_multiple_local_declarations = true; this.insert_space_after_comma_in_parameterized_type_reference = true; this.insert_space_after_comma_in_superinterfaces = true; + this.insert_space_after_comma_in_switch_case_expressions = true; this.insert_space_after_comma_in_type_arguments = true; this.insert_space_after_comma_in_type_parameters = true; this.insert_space_after_ellipsis = true; @@ -2976,6 +3007,8 @@ public class DefaultCodeFormatterOptions { this.insert_space_after_semicolon_in_try_resources = true; this.insert_space_after_unary_operator = false; this.insert_space_before_and_in_type_parameter = true; + this.insert_space_before_arrow_in_switch_case = true; + this.insert_space_before_arrow_in_switch_default = true; this.insert_space_before_at_in_annotation_type_declaration = true; this.insert_space_before_assignment_operator = true; this.insert_space_before_multiplicative_operator = true; @@ -3027,6 +3060,7 @@ public class DefaultCodeFormatterOptions { this.insert_space_before_comma_in_multiple_local_declarations = false; this.insert_space_before_comma_in_parameterized_type_reference = false; this.insert_space_before_comma_in_superinterfaces = false; + this.insert_space_before_comma_in_switch_case_expressions = false; this.insert_space_before_comma_in_type_arguments = false; this.insert_space_before_comma_in_type_parameters = false; this.insert_space_before_ellipsis = false; @@ -3257,6 +3291,8 @@ public class DefaultCodeFormatterOptions { 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_arrow_in_switch_case = true; + this.insert_space_after_arrow_in_switch_default = true; this.insert_space_after_assignment_operator = true; this.insert_space_after_at_in_annotation = false; this.insert_space_after_at_in_annotation_type_declaration = false; @@ -3293,6 +3329,7 @@ public class DefaultCodeFormatterOptions { this.insert_space_after_comma_in_multiple_local_declarations = true; this.insert_space_after_comma_in_parameterized_type_reference = true; this.insert_space_after_comma_in_superinterfaces = true; + this.insert_space_after_comma_in_switch_case_expressions = true; this.insert_space_after_comma_in_type_arguments = true; this.insert_space_after_comma_in_type_parameters = true; this.insert_space_after_ellipsis = true; @@ -3325,6 +3362,8 @@ public class DefaultCodeFormatterOptions { this.insert_space_after_semicolon_in_try_resources = true; this.insert_space_after_unary_operator = false; this.insert_space_before_and_in_type_parameter = true; + this.insert_space_before_arrow_in_switch_case = true; + this.insert_space_before_arrow_in_switch_default = true; this.insert_space_before_at_in_annotation_type_declaration = true; this.insert_space_before_assignment_operator = true; this.insert_space_before_multiplicative_operator = true; @@ -3376,6 +3415,7 @@ public class DefaultCodeFormatterOptions { this.insert_space_before_comma_in_multiple_local_declarations = false; this.insert_space_before_comma_in_parameterized_type_reference = false; this.insert_space_before_comma_in_superinterfaces = false; + this.insert_space_before_comma_in_switch_case_expressions = false; this.insert_space_before_comma_in_type_arguments = false; this.insert_space_before_comma_in_type_parameters = false; this.insert_space_before_ellipsis = 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 ae864aa8a9..e5bb06b324 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2014, 2018 Mateusz Matela and others. + * Copyright (c) 2014, 2019 Mateusz Matela and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * Mateusz Matela <mateusz.matela@gmail.com> - [formatter] Formatter does not format Java code correctly, especially when max line width is set - https://bugs.eclipse.org/303519 * Mateusz Matela <mateusz.matela@gmail.com> - [formatter] IndexOutOfBoundsException in TokenManager - https://bugs.eclipse.org/462945 @@ -67,6 +71,7 @@ import org.eclipse.jdt.core.dom.SingleMemberAnnotation; import org.eclipse.jdt.core.dom.SingleVariableDeclaration; import org.eclipse.jdt.core.dom.Statement; import org.eclipse.jdt.core.dom.SwitchCase; +import org.eclipse.jdt.core.dom.SwitchExpression; import org.eclipse.jdt.core.dom.SwitchStatement; import org.eclipse.jdt.core.dom.TryStatement; import org.eclipse.jdt.core.dom.TypeDeclaration; @@ -328,9 +333,64 @@ public class LineBreaksPreparator extends ASTVisitor { } } + boolean arrowMode = statements.stream() + .anyMatch(s -> s instanceof SwitchCase && ((SwitchCase) s).isSwitchLabeledRule()); for (Statement statement : statements) { if (statement instanceof Block) continue; // will add break in visit(Block) if necessary + if (arrowMode && !(statement instanceof SwitchCase)) + continue; + if (this.options.put_empty_statement_on_new_line || !(statement instanceof EmptyStatement)) + breakLineBefore(statement); + } + + return true; + } + + @Override + public boolean visit(SwitchExpression node) { + handleBracedCode(node, node.getExpression(), this.options.brace_position_for_switch, + this.options.indent_switchstatements_compare_to_switch); + + List<Statement> statements = node.statements(); + if (this.options.indent_switchstatements_compare_to_cases) { + int nonBreakStatementEnd = -1; + for (Statement statement : statements) { + boolean isBreaking = statement instanceof BreakStatement || statement instanceof ReturnStatement + || statement instanceof ContinueStatement || statement instanceof Block; + if (isBreaking && !(statement instanceof Block)) + adjustEmptyLineAfter(this.tm.lastIndexIn(statement, -1), -1); + if (statement instanceof SwitchCase) { + if (nonBreakStatementEnd >= 0) { + // indent only comments between previous and current statement + this.tm.get(nonBreakStatementEnd + 1).indent(); + this.tm.firstTokenIn(statement, -1).unindent(); + } + } else if (!(statement instanceof BreakStatement || statement instanceof Block)) { + indent(statement); + } + nonBreakStatementEnd = isBreaking ? -1 : this.tm.lastIndexIn(statement, -1); + } + if (nonBreakStatementEnd >= 0) { + // indent comments between last statement and closing brace + this.tm.get(nonBreakStatementEnd + 1).indent(); + this.tm.lastTokenIn(node, TokenNameRBRACE).unindent(); + } + } + if (this.options.indent_breaks_compare_to_cases) { + for (Statement statement : statements) { + if (statement instanceof BreakStatement) + indent(statement); + } + } + + boolean arrowMode = statements.stream() + .anyMatch(s -> s instanceof SwitchCase && ((SwitchCase) s).isSwitchLabeledRule()); + for (Statement statement : statements) { + if (statement instanceof Block) + continue; // will add break in visit(Block) if necessary + if (arrowMode && !(statement instanceof SwitchCase)) + continue; if (this.options.put_empty_statement_on_new_line || !(statement instanceof EmptyStatement)) breakLineBefore(statement); } 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 ba40de8431..e480257934 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 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * Mateusz Matela <mateusz.matela@gmail.com> - [formatter] Formatter does not format Java code correctly, especially when max line width is set - https://bugs.eclipse.org/303519 * Mateusz Matela <mateusz.matela@gmail.com> - [formatter] IndexOutOfBoundsException in TokenManager - https://bugs.eclipse.org/462945 @@ -38,6 +42,7 @@ import org.eclipse.jdt.core.dom.ArrayType; import org.eclipse.jdt.core.dom.AssertStatement; import org.eclipse.jdt.core.dom.Assignment; import org.eclipse.jdt.core.dom.Block; +import org.eclipse.jdt.core.dom.BreakStatement; import org.eclipse.jdt.core.dom.CastExpression; import org.eclipse.jdt.core.dom.CatchClause; import org.eclipse.jdt.core.dom.ClassInstanceCreation; @@ -86,6 +91,7 @@ import org.eclipse.jdt.core.dom.SuperConstructorInvocation; import org.eclipse.jdt.core.dom.SuperMethodInvocation; import org.eclipse.jdt.core.dom.SuperMethodReference; import org.eclipse.jdt.core.dom.SwitchCase; +import org.eclipse.jdt.core.dom.SwitchExpression; import org.eclipse.jdt.core.dom.SwitchStatement; import org.eclipse.jdt.core.dom.SynchronizedStatement; import org.eclipse.jdt.core.dom.ThrowStatement; @@ -381,26 +387,46 @@ public class SpacePreparator extends ASTVisitor { } @Override + public boolean visit(SwitchExpression node) { + handleToken(node, TokenNameLPAREN, this.options.insert_space_before_opening_paren_in_switch, + this.options.insert_space_after_opening_paren_in_switch); + handleTokenAfter(node.getExpression(), TokenNameRPAREN, + this.options.insert_space_before_closing_paren_in_switch, false); + handleTokenAfter(node.getExpression(), TokenNameLBRACE, + this.options.insert_space_before_opening_brace_in_switch, false); + handleSemicolon(node.statements()); + return true; + } + + @Override public boolean visit(SwitchCase node) { - if (node.isDefault()) { - handleToken(node, TokenNameCOLON, this.options.insert_space_before_colon_in_default, false); + if (node.isSwitchLabeledRule()) { + handleToken(this.tm.lastTokenIn(node, TokenNameARROW), + node.isDefault() ? this.options.insert_space_before_arrow_in_switch_default + : this.options.insert_space_before_arrow_in_switch_case, + node.isDefault() ? this.options.insert_space_after_arrow_in_switch_default + : this.options.insert_space_after_arrow_in_switch_case); } else { + handleToken(this.tm.lastTokenIn(node, TokenNameCOLON), + node.isDefault() ? this.options.insert_space_before_colon_in_default + : this.options.insert_space_before_colon_in_case, + false); + } + if (!node.isDefault()) { handleToken(node, TokenNamecase, false, true); - handleToken(getSwitchExpression(node), TokenNameCOLON, this.options.insert_space_before_colon_in_case, false); + handleCommas(node.expressions(), this.options.insert_space_before_comma_in_switch_case_expressions, + this.options.insert_space_after_comma_in_switch_case_expressions); } return true; } - /** - * - * @param node - * @return expression - * @deprecated - */ - private Expression getSwitchExpression(SwitchCase node) { - return node.getExpression(); + @Override + public boolean visit(BreakStatement node) { + if (node.getExpression() != null && !node.isImplicit()) { + this.tm.firstTokenIn(node, TokenNamebreak).spaceAfter(); + } + return true; } - @Override public boolean visit(DoStatement node) { 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 5c12e80886..9b9d9f603d 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 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * Mateusz Matela <mateusz.matela@gmail.com> - [formatter] Formatter does not format Java code correctly, especially when max line width is set - https://bugs.eclipse.org/303519 * Mateusz Matela <mateusz.matela@gmail.com> - [formatter] follow up bug for comments - https://bugs.eclipse.org/458208 @@ -94,6 +98,7 @@ import org.eclipse.jdt.core.dom.SuperConstructorInvocation; import org.eclipse.jdt.core.dom.SuperFieldAccess; import org.eclipse.jdt.core.dom.SuperMethodInvocation; import org.eclipse.jdt.core.dom.SuperMethodReference; +import org.eclipse.jdt.core.dom.SwitchExpression; import org.eclipse.jdt.core.dom.SwitchStatement; import org.eclipse.jdt.core.dom.ThisExpression; import org.eclipse.jdt.core.dom.TryStatement; @@ -1059,6 +1064,14 @@ public class WrapPreparator extends ASTVisitor { } @Override + public boolean visit(SwitchExpression node) { + int lParen = this.tm.firstIndexIn(node, TokenNameLPAREN); + int rParen = this.tm.firstIndexAfter(node.getExpression(), TokenNameRPAREN); + handleParenthesesPositions(lParen, rParen, this.options.parenthesis_positions_in_switch_statement); + return true; + } + + @Override public boolean visit(DoStatement node) { int lParen = this.tm.firstIndexBefore(node.getExpression(), TokenNameLPAREN); int rParen = this.tm.firstIndexAfter(node.getExpression(), TokenNameRPAREN); |