Bug 540811 - [formatter] Allow to keep simple methods on one line - UI
Change-Id: I339879955964f934458f696cbd7c3f3c6e459952
Signed-off-by: Mateusz Matela <mateusz.matela@gmail.com>
diff --git a/org.eclipse.jdt.ui/preview/formatter.java b/org.eclipse.jdt.ui/preview/formatter.java
index c42fd9c..d3ce991 100644
--- a/org.eclipse.jdt.ui/preview/formatter.java
+++ b/org.eclipse.jdt.ui/preview/formatter.java
@@ -594,10 +594,10 @@
{
@SuppressWarnings("unused") final @Positive int k;
}
- void foo() { ;; do {} while (false); for (;;) {} }
+ void foo() { ;; }
void empty(@SuppressWarnings("unused") final int i) { }}
-enum MyEnum { @Deprecated UNDEFINED(0) { }}enum EmptyEnum { }@interface EmptyAnnotation {
-}//--PREVIEW--END--section-newlines
+enum MyEnum { @Deprecated UNDEFINED(0) { }}
+//--PREVIEW--END--section-newlines
//--PREVIEW--START--section-newlines-controlstatements
class Example {
@@ -606,7 +606,6 @@
try { } catch (Exception e) { } finally { }
}
void foo2() {
- if (true) { return; }
if (true) { return; } else if (false) { return; } else { return; }
}
void foo(int state) {
@@ -625,6 +624,112 @@
//--PREVIEW--END--section-newlines-controlstatements-simpleloops
//--PREVIEW--END--section-newlines-controlstatements
+//--PREVIEW--START--org.eclipse.jdt.core.formatter.keep_loop_body_block_on_one_line
+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);
+ }
+}
+//--PREVIEW--END--org.eclipse.jdt.core.formatter.keep_loop_body_block_on_one_line
+
+//--PREVIEW--START--org.eclipse.jdt.core.formatter.keep_if_then_body_block_on_one_line
+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);
+ }
+}
+//--PREVIEW--END--org.eclipse.jdt.core.formatter.keep_if_then_body_block_on_one_line
+
+//--PREVIEW--START--org.eclipse.jdt.core.formatter.keep_lambda_body_block_on_one_line
+class Example {
+ Runnable emptyLambda = () -> {};
+ Runnable emptyLambda2 = () -> {
+ };
+ Runnable tinyLambda = () -> { doSomething(); };
+ Runnable smallLambda = () -> { doFirstThing(); doSecondThing(); };
+}
+//--PREVIEW--END--org.eclipse.jdt.core.formatter.keep_lambda_body_block_on_one_line
+
+//--PREVIEW--START--org.eclipse.jdt.core.formatter.keep_code_block_on_one_line
+class Example {
+ static {
+ }
+
+ void foo() {
+ if (true) {} else {}
+ synchronized(this) {}
+ try {} finally {}
+
+ labeled:{}
+ }
+}
+//--PREVIEW--END--org.eclipse.jdt.core.formatter.keep_code_block_on_one_line
+
+//--PREVIEW--START--org.eclipse.jdt.core.formatter.keep_method_body_on_one_line
+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(); }
+}
+//--PREVIEW--END--org.eclipse.jdt.core.formatter.keep_method_body_on_one_line
+
+
+//--PREVIEW--START--org.eclipse.jdt.core.formatter.keep_type_declaration_on_one_line
+public class EmptyClass{}
+public class TinyClass{
+ int a; }
+public class SmallClass{ int a; String b; }
+//--PREVIEW--END--org.eclipse.jdt.core.formatter.keep_type_declaration_on_one_line
+
+//--PREVIEW--START--org.eclipse.jdt.core.formatter.keep_anonymous_type_declaration_on_one_line
+public class AnonymousClasses {
+ EmptyClass emptyAnonymous = new EmptyClass() {
+ };
+ TinyClass tinyAnonymous = new TinyClass() { String b; };
+ Object o = new SmallClass() { int a; int getA() { return a; } };
+}
+//--PREVIEW--END--org.eclipse.jdt.core.formatter.keep_anonymous_type_declaration_on_one_line
+
+//--PREVIEW--START--org.eclipse.jdt.core.formatter.keep_enum_declaration_on_one_line
+public enum EmptyEnum {}
+public enum TinyEnum{ A;
+}
+public enum SmallEnum{ VALUE(0); SmallEnum(int val) {}; }
+//--PREVIEW--END--org.eclipse.jdt.core.formatter.keep_enum_declaration_on_one_line
+
+//--PREVIEW--START--org.eclipse.jdt.core.formatter.keep_enum_constant_declaration_on_one_line
+public enum EnumConstants {
+ EMPTY {
+ },
+ TINY { int getVal() { return 2; }},
+ SMALL { int val = 3; int getVal() { return 3; }};
+ int getVal() { return 1; }
+}
+//--PREVIEW--END--org.eclipse.jdt.core.formatter.keep_enum_constant_declaration_on_one_line
+
+//--PREVIEW--START--org.eclipse.jdt.core.formatter.keep_annotation_declaration_on_one_line
+public @interface EmptyInterface {}
+public @interface TinyInterface {
+ void run(); }
+public @interface SmallInteface { int toA(); String toB(); }
+//--PREVIEW--END--org.eclipse.jdt.core.formatter.keep_annotation_declaration_on_one_line
+
}
class LINE_WRAPING {
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterMessages.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterMessages.java
index 86855de..afafc74 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterMessages.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterMessages.java
@@ -183,13 +183,6 @@
public static String FormatterModifyDialog_newLines_pref_before_else_statements;
public static String FormatterModifyDialog_newLines_pref_before_finally_statements;
public static String FormatterModifyDialog_newLines_pref_before_while_in_do_statements;
- public static String FormatterModifyDialog_newLines_pref_empty_annotation_decl;
- public static String FormatterModifyDialog_newLines_pref_empty_anonymous_class_body;
- public static String FormatterModifyDialog_newLines_pref_empty_block;
- public static String FormatterModifyDialog_newLines_pref_empty_class_body;
- public static String FormatterModifyDialog_newLines_pref_empty_enum_constant;
- public static String FormatterModifyDialog_newLines_pref_empty_enum_declaration;
- public static String FormatterModifyDialog_newLines_pref_empty_method_body;
public static String FormatterModifyDialog_newLines_pref_empty_statement;
public static String FormatterModifyDialog_newLines_pref_end_of_file;
public static String FormatterModifyDialog_newLines_pref_enum_constants;
@@ -202,6 +195,17 @@
public static String FormatterModifyDialog_newLines_pref_keep_simple_if_on_one_line;
public static String FormatterModifyDialog_newLines_pref_keep_simple_while_body_on_one_line;
public static String FormatterModifyDialog_newLines_pref_keep_then_on_same_line;
+ public static String FormatterModifyDialog_newLines_pref_keep_annotation_declaration_on_one_line;
+ public static String FormatterModifyDialog_newLines_pref_keep_anonymous_type_declaration_on_one_line;
+ public static String FormatterModifyDialog_newLines_pref_keep_code_block_on_one_line;
+ public static String FormatterModifyDialog_newLines_pref_keep_enum_constant_declaration_on_one_line;
+ public static String FormatterModifyDialog_newLines_pref_keep_enum_declaration_on_one_line;
+ public static String FormatterModifyDialog_newLines_pref_keep_if_then_body_block_on_one_line;
+ public static String FormatterModifyDialog_newLines_pref_keep_lambda_body_block_on_one_line;
+ public static String FormatterModifyDialog_newLines_pref_keep_loop_body_block_on_one_line;
+ public static String FormatterModifyDialog_newLines_pref_keep_method_body_on_one_line;
+ public static String FormatterModifyDialog_newLines_pref_keep_simple_getter_setter_on_one_line;
+ public static String FormatterModifyDialog_newLines_pref_keep_type_declaration_on_one_line;
public static String FormatterModifyDialog_newLines_pref_local_variables;
public static String FormatterModifyDialog_newLines_pref_methods;
public static String FormatterModifyDialog_newLines_pref_packages;
@@ -209,11 +213,16 @@
public static String FormatterModifyDialog_newLines_pref_type_annotations;
public static String FormatterModifyDialog_newLines_pref_types;
public static String FormatterModifyDialog_newLines_tree_after_annotations;
- public static String FormatterModifyDialog_newLines_tree_between_empty_braces;
public static String FormatterModifyDialog_newLines_tree_control_statements;
public static String FormatterModifyDialog_newLines_tree_if_else;
+ public static String FormatterModifyDialog_newLines_tree_keep_braced_code_on_one_line;
public static String FormatterModifyDialog_newLines_tree_new_lines;
public static String FormatterModifyDialog_newLines_tree_simple_loops;
+ public static String FormatterModifyDialog_newLines_val_one_line_always;
+ public static String FormatterModifyDialog_newLines_val_one_line_if_empty;
+ public static String FormatterModifyDialog_newLines_val_one_line_if_single_item;
+ public static String FormatterModifyDialog_newLines_val_one_line_never;
+ public static String FormatterModifyDialog_newLines_val_one_line_preserve;
public static String FormatterModifyDialog_offOn_description;
public static String FormatterModifyDialog_offOn_error_endsWithWhitespace;
public static String FormatterModifyDialog_offOn_error_startsWithWhitespace;
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterMessages.properties b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterMessages.properties
index f4917c0..af35cec 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterMessages.properties
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterMessages.properties
@@ -348,32 +348,36 @@
FormatterModifyDialog_newLines_tree_new_lines=New Lines
FormatterModifyDialog_newLines_tree_after_annotations=After annotations
-FormatterModifyDialog_newLines_tree_between_empty_braces=Between empty braces
FormatterModifyDialog_newLines_tree_control_statements=In control statements
+FormatterModifyDialog_newLines_tree_if_else='if else'
+FormatterModifyDialog_newLines_tree_simple_loops=Simple loops
+FormatterModifyDialog_newLines_tree_keep_braced_code_on_one_line=Keep braced code on one line
FormatterModifyDialog_newLines_pref_before_else_statements=Before 'else' in an 'if' statement
FormatterModifyDialog_newLines_pref_before_catch_statements=Before 'catch' in a 'try' statement
FormatterModifyDialog_newLines_pref_before_finally_statements=Before 'finally' in a 'try' statement
FormatterModifyDialog_newLines_pref_before_while_in_do_statements=Before 'while' in a 'do' statement
-FormatterModifyDialog_newLines_tree_if_else='if else'
-FormatterModifyDialog_newLines_tree_simple_loops=Simple loops
FormatterModifyDialog_newLines_pref_keep_then_on_same_line=Keep 'then' statement on same line
-FormatterModifyDialog_newLines_pref_keep_simple_do_while_body_on_one_line=Keep simple 'do-while' loop body on same line
+FormatterModifyDialog_newLines_pref_keep_simple_do_while_body_on_one_line=Keep simple 'do while' loop body on same line
FormatterModifyDialog_newLines_pref_keep_simple_for_body_on_one_line=Keep simple 'for' loop body on same line
FormatterModifyDialog_newLines_pref_keep_simple_if_on_one_line=Keep simple 'if' on one line
FormatterModifyDialog_newLines_pref_keep_simple_while_body_on_one_line=Keep simple 'while' loop body on same line
FormatterModifyDialog_newLines_pref_keep_else_on_same_line=Keep 'else' statement on same line
FormatterModifyDialog_newLines_pref_keep_else_if_on_one_line=Keep 'else if' on one line
FormatterModifyDialog_newLines_pref_keep_guardian_clause_on_one_line=Keep 'return' or 'throw' clause on one line
+FormatterModifyDialog_newLines_pref_keep_annotation_declaration_on_one_line=Annotation declaration
+FormatterModifyDialog_newLines_pref_keep_anonymous_type_declaration_on_one_line=Anonymous class declaration
+FormatterModifyDialog_newLines_pref_keep_code_block_on_one_line=Other code blocks in statements
+FormatterModifyDialog_newLines_pref_keep_enum_constant_declaration_on_one_line=Enum constant declaration
+FormatterModifyDialog_newLines_pref_keep_enum_declaration_on_one_line=Enum declaration
+FormatterModifyDialog_newLines_pref_keep_if_then_body_block_on_one_line='if then' statement body
+FormatterModifyDialog_newLines_pref_keep_lambda_body_block_on_one_line=Lambda body
+FormatterModifyDialog_newLines_pref_keep_loop_body_block_on_one_line=Loop body ('for', 'while', 'do while')
+FormatterModifyDialog_newLines_pref_keep_method_body_on_one_line=Method declaration
+FormatterModifyDialog_newLines_pref_keep_simple_getter_setter_on_one_line=Keep simple getters and setters on one line
+FormatterModifyDialog_newLines_pref_keep_type_declaration_on_one_line=Class declaration
-FormatterModifyDialog_newLines_pref_empty_class_body=In empty class body
-FormatterModifyDialog_newLines_pref_empty_annotation_decl=In empty annotation body
-FormatterModifyDialog_newLines_pref_empty_anonymous_class_body=In empty anonymous class body
-FormatterModifyDialog_newLines_pref_empty_enum_declaration=In empty enum declaration
-FormatterModifyDialog_newLines_pref_empty_enum_constant=In empty enum constant body
-FormatterModifyDialog_newLines_pref_empty_method_body=In empty method body
-FormatterModifyDialog_newLines_pref_empty_block=In empty block
FormatterModifyDialog_newLines_pref_end_of_file=At end of file
FormatterModifyDialog_newLines_pref_after_labels=After labels
FormatterModifyDialog_newLines_pref_empty_statement=Before empty statement
@@ -389,6 +393,11 @@
FormatterModifyDialog_newLines_pref_enum_constants=On enum constants
FormatterModifyDialog_newLines_pref_fields=On fields
FormatterModifyDialog_newLines_pref_methods=On methods
+FormatterModifyDialog_newLines_val_one_line_always=If fits in width limit
+FormatterModifyDialog_newLines_val_one_line_if_empty=If empty
+FormatterModifyDialog_newLines_val_one_line_if_single_item=If at most one item
+FormatterModifyDialog_newLines_val_one_line_never=Never
+FormatterModifyDialog_newLines_val_one_line_preserve=Preserve state
FormatterModifyDialog_offOn_description=Off/On tags can be used in any comments to turn the formatter off and on in a source file.\n- At the beginning of each file, the formatter is enabled.\n- Each time the formatter sees an off tag, it disables formatting for that comment and the source after it.\n- Each time the formatter sees an on tag, it enables formatting for the source after that comment.\n
FormatterModifyDialog_offOn_pref_enable=Enable Off/On tags
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterModifyDialog.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterModifyDialog.java
index bec1385..6b5f73a 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterModifyDialog.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterModifyDialog.java
@@ -66,6 +66,7 @@
import org.eclipse.jdt.internal.ui.preferences.FilteredPreferenceTree;
import org.eclipse.jdt.internal.ui.preferences.FilteredPreferenceTree.PreferenceTreeNode;
import org.eclipse.jdt.internal.ui.preferences.PreferenceHighlight;
+import org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialog.ProfilePreferenceTree.SectionBuilder;
import org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialog.ProfilePreferenceTree.SimpleTreeBuilder;
import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile;
import org.eclipse.jdt.internal.ui.util.SWTUtil;
@@ -1107,14 +1108,14 @@
private void createNewLinesTree() {
Consumer<Section> modAll= s -> CheckboxPreference.addModifyAll(s, fImages);
fTree.builder(FormatterMessages.FormatterModifyDialog_newLines_tree_new_lines, "section-newlines") //$NON-NLS-1$
- .node(fTree.builder(FormatterMessages.FormatterModifyDialog_newLines_tree_between_empty_braces, "-declarations", modAll) //$NON-NLS-1$
- .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_empty_block, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_BLOCK)
- .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_empty_class_body, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_TYPE_DECLARATION)
- .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_empty_anonymous_class_body, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANONYMOUS_TYPE_DECLARATION)
- .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_empty_method_body, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_METHOD_BODY)
- .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_empty_enum_declaration, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ENUM_DECLARATION)
- .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_empty_enum_constant, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ENUM_CONSTANT)
- .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_empty_annotation_decl, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANNOTATION_DECLARATION))
+ .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_empty_statement, DefaultCodeFormatterConstants.FORMATTER_PUT_EMPTY_STATEMENT_ON_NEW_LINE)
+ .gap()
+ .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_after_opening_brace_of_array_initializer,
+ DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_OPENING_BRACE_IN_ARRAY_INITIALIZER)
+ .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_before_closing_brace_of_array_initializer,
+ DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_CLOSING_BRACE_IN_ARRAY_INITIALIZER)
+ .gap()
+ .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_end_of_file, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AT_END_OF_FILE_IF_MISSING)
.node(fTree.builder(FormatterMessages.FormatterModifyDialog_newLines_tree_control_statements, "-controlstatements", modAll) //$NON-NLS-1$
.pref(FormatterMessages.FormatterModifyDialog_newLines_pref_before_else_statements, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_ELSE_IN_IF_STATEMENT)
.pref(FormatterMessages.FormatterModifyDialog_newLines_pref_before_catch_statements, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_CATCH_IN_TRY_STATEMENT)
@@ -1128,8 +1129,7 @@
pref.addDependant(child, valueAcceptor(DefaultCodeFormatterConstants.FALSE));
})
.pref(FormatterMessages.FormatterModifyDialog_newLines_pref_keep_else_on_same_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_ELSE_STATEMENT_ON_SAME_LINE)
- .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_keep_else_if_on_one_line, DefaultCodeFormatterConstants.FORMATTER_COMPACT_ELSE_IF)
- .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_keep_guardian_clause_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_GUARDIAN_CLAUSE_ON_ONE_LINE))
+ .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_keep_else_if_on_one_line, DefaultCodeFormatterConstants.FORMATTER_COMPACT_ELSE_IF))
.node(fTree.builder(FormatterMessages.FormatterModifyDialog_newLines_tree_simple_loops, "-simpleloops", modAll) //$NON-NLS-1$
.pref(FormatterMessages.FormatterModifyDialog_newLines_pref_keep_simple_for_body_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_SIMPLE_FOR_BODY_ON_SAME_LINE)
.pref(FormatterMessages.FormatterModifyDialog_newLines_pref_keep_simple_while_body_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_SIMPLE_WHILE_BODY_ON_SAME_LINE)
@@ -1143,13 +1143,7 @@
.pref(FormatterMessages.FormatterModifyDialog_newLines_pref_local_variables, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_LOCAL_VARIABLE)
.pref(FormatterMessages.FormatterModifyDialog_newLines_pref_paramters, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_PARAMETER)
.pref(FormatterMessages.FormatterModifyDialog_newLines_pref_type_annotations, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_TYPE_ANNOTATION))
- .gap()
- .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_empty_statement, DefaultCodeFormatterConstants.FORMATTER_PUT_EMPTY_STATEMENT_ON_NEW_LINE)
- .gap()
- .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_after_opening_brace_of_array_initializer, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_OPENING_BRACE_IN_ARRAY_INITIALIZER)
- .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_before_closing_brace_of_array_initializer, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_CLOSING_BRACE_IN_ARRAY_INITIALIZER)
- .gap()
- .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_end_of_file, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AT_END_OF_FILE_IF_MISSING)
+ .node(createKeepOnOneLineSection())
.build(null, (parent, label, key) -> {
String[] values= CheckboxPreference.DO_NOT_INSERT_INSERT;
if (parent.getKey().endsWith("-ifelse") || parent.getKey().endsWith("-simpleloops") //$NON-NLS-1$ //$NON-NLS-2$
@@ -1160,6 +1154,60 @@
});
}
+ private SimpleTreeBuilder<?> createKeepOnOneLineSection() {
+ String[] oneLineOptions= {
+ DefaultCodeFormatterConstants.ONE_LINE_NEVER,
+ DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY,
+ DefaultCodeFormatterConstants.ONE_LINE_IF_SINGLE_ITEM,
+ DefaultCodeFormatterConstants.ONE_LINE_ALWAYS,
+ DefaultCodeFormatterConstants.ONE_LINE_PRESERVE,
+ };
+ String[] oneLineLabels= {
+ FormatterMessages.FormatterModifyDialog_newLines_val_one_line_never,
+ FormatterMessages.FormatterModifyDialog_newLines_val_one_line_if_empty,
+ FormatterMessages.FormatterModifyDialog_newLines_val_one_line_if_single_item,
+ FormatterMessages.FormatterModifyDialog_newLines_val_one_line_always,
+ FormatterMessages.FormatterModifyDialog_newLines_val_one_line_preserve,
+ };
+ PreferenceBuilder prefBuilder= (parent, label, key) -> {
+ String[] values= oneLineOptions;
+ String[] items= oneLineLabels;
+ if (DefaultCodeFormatterConstants.FORMATTER_KEEP_CODE_BLOCK_ON_ONE_LINE.equals(key)) {
+ values= Arrays.copyOf(values, 2);
+ items= Arrays.copyOf(items, 2);
+ }
+ return fTree.addComboPref(parent, label, key, values, items);
+ };
+ SectionBuilder sectionBuilder= fTree
+ .builder(FormatterMessages.FormatterModifyDialog_newLines_tree_keep_braced_code_on_one_line, "-keepononeline", s -> ComboPreference.addModifyAll(s, fImages)) //$NON-NLS-1$
+ .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_keep_loop_body_block_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_LOOP_BODY_BLOCK_ON_ONE_LINE)
+ .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_keep_if_then_body_block_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_IF_THEN_BODY_BLOCK_ON_ONE_LINE, pref -> {
+ CheckboxPreference guardianPref= fTree.addCheckbox(pref, FormatterMessages.FormatterModifyDialog_newLines_pref_keep_guardian_clause_on_one_line,
+ DefaultCodeFormatterConstants.FORMATTER_KEEP_GUARDIAN_CLAUSE_ON_ONE_LINE, CheckboxPreference.FALSE_TRUE);
+ pref.addDependant(guardianPref, valueAcceptor(oneLineOptions[0], oneLineOptions[1], oneLineOptions[4]));
+ })
+ .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_keep_lambda_body_block_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_LAMBDA_BODY_BLOCK_ON_ONE_LINE)
+ .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_keep_code_block_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_CODE_BLOCK_ON_ONE_LINE)
+ .gap()
+ .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_keep_method_body_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_METHOD_BODY_ON_ONE_LINE, pref -> {
+ CheckboxPreference getterSetterPref= fTree.addCheckbox(pref, FormatterMessages.FormatterModifyDialog_newLines_pref_keep_simple_getter_setter_on_one_line,
+ DefaultCodeFormatterConstants.FORMATTER_KEEP_SIMPLE_GETTER_SETTER_ON_ONE_LINE, CheckboxPreference.FALSE_TRUE);
+ pref.addDependant(getterSetterPref, valueAcceptor(oneLineOptions[0], oneLineOptions[1], oneLineOptions[4]));
+ })
+ .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_keep_type_declaration_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_TYPE_DECLARATION_ON_ONE_LINE)
+ .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_keep_anonymous_type_declaration_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_ANONYMOUS_TYPE_DECLARATION_ON_ONE_LINE)
+ .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_keep_enum_declaration_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_ENUM_DECLARATION_ON_ONE_LINE)
+ .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_keep_enum_constant_declaration_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_ENUM_CONSTANT_DECLARATION_ON_ONE_LINE)
+ .pref(FormatterMessages.FormatterModifyDialog_newLines_pref_keep_annotation_declaration_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_ANNOTATION_DECLARATION_ON_ONE_LINE);
+
+ return fTree.new SimpleTreeBuilder<PreferenceTreeNode<?>>(null, null, null) {
+ @Override
+ protected PreferenceTreeNode<?> build(Section parent, PreferenceBuilder ignored) {
+ return sectionBuilder.build(parent, prefBuilder);
+ }
+ };
+ }
+
private void createLineWrapTree() {
final Section globalSection= fTree.addSection(null, FormatterMessages.FormatterModifyDialog_lineWrap_tree_line_wrapping, "section-linewrap"); //$NON-NLS-1$
fTree.addNumberPref(globalSection,
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/ProfileVersioner.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/ProfileVersioner.java
index b520017..a089b1d 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/ProfileVersioner.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/ProfileVersioner.java
@@ -44,7 +44,9 @@
private static final int VERSION_13= 13; // https://bugs.eclipse.org/514019
private static final int VERSION_14= 14; // https://bugs.eclipse.org/128653, https://bugs.eclipse.org/531826
- private static final int CURRENT_VERSION= VERSION_14;
+ private static final int VERSION_15= 15; // https://bugs.eclipse.org/205973
+
+ private static final int CURRENT_VERSION= VERSION_15;
@Override
public int getFirstVersion() {
@@ -109,6 +111,9 @@
case VERSION_13 :
version13to14(oldSettings);
//$FALL-THROUGH$
+ case VERSION_14:
+ version14to15(oldSettings);
+ //$FALL-THROUGH$
default:
for (final Iterator<String> iter= oldSettings.keySet().iterator(); iter.hasNext(); ) {
final String key= iter.next();
@@ -580,8 +585,8 @@
private static void version9to10(Map<String, String> oldSettings) {
checkAndReplace(oldSettings,
- DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_TYPE_DECLARATION,
- DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANNOTATION_DECLARATION);
+ FORMATTER_INSERT_NEW_LINE_IN_EMPTY_TYPE_DECLARATION,
+ FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANNOTATION_DECLARATION);
checkAndReplace(oldSettings,
DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_TYPE_HEADER,
DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ANNOTATION_DECLARATION_HEADER);
@@ -632,6 +637,26 @@
oldSettings.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_ALIGN_TAGS_DESCREIPTIONS_GROUPED, DefaultCodeFormatterConstants.FALSE);
}
+ private static void version14to15(Map<String, String> oldSettings) {
+ String[][] transitions= {
+ { DefaultCodeFormatterConstants.FORMATTER_KEEP_ANNOTATION_DECLARATION_ON_ONE_LINE, FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANNOTATION_DECLARATION },
+ { DefaultCodeFormatterConstants.FORMATTER_KEEP_ANONYMOUS_TYPE_DECLARATION_ON_ONE_LINE, FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANONYMOUS_TYPE_DECLARATION },
+ { DefaultCodeFormatterConstants.FORMATTER_KEEP_IF_THEN_BODY_BLOCK_ON_ONE_LINE, FORMATTER_INSERT_NEW_LINE_IN_EMPTY_BLOCK },
+ { DefaultCodeFormatterConstants.FORMATTER_KEEP_LOOP_BODY_BLOCK_ON_ONE_LINE, FORMATTER_INSERT_NEW_LINE_IN_EMPTY_BLOCK },
+ { DefaultCodeFormatterConstants.FORMATTER_KEEP_LAMBDA_BODY_BLOCK_ON_ONE_LINE, FORMATTER_INSERT_NEW_LINE_IN_EMPTY_BLOCK },
+ { DefaultCodeFormatterConstants.FORMATTER_KEEP_CODE_BLOCK_ON_ONE_LINE, FORMATTER_INSERT_NEW_LINE_IN_EMPTY_BLOCK },
+ { DefaultCodeFormatterConstants.FORMATTER_KEEP_ENUM_CONSTANT_DECLARATION_ON_ONE_LINE, FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ENUM_CONSTANT },
+ { DefaultCodeFormatterConstants.FORMATTER_KEEP_ENUM_DECLARATION_ON_ONE_LINE, FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ENUM_DECLARATION },
+ { DefaultCodeFormatterConstants.FORMATTER_KEEP_METHOD_BODY_ON_ONE_LINE, FORMATTER_INSERT_NEW_LINE_IN_EMPTY_METHOD_BODY },
+ { DefaultCodeFormatterConstants.FORMATTER_KEEP_TYPE_DECLARATION_ON_ONE_LINE, FORMATTER_INSERT_NEW_LINE_IN_EMPTY_TYPE_DECLARATION },
+ };
+ for (String[] transition : transitions) {
+ String value= oldSettings.get(transition[1]);
+ if (JavaCore.DO_NOT_INSERT.equals(value))
+ oldSettings.put(transition[0], DefaultCodeFormatterConstants.ONE_LINE_IF_EMPTY);
+ }
+ }
+
/* old format constant values */
private static final String FORMATTER_METHOD_DECLARATION_ARGUMENTS_ALIGNMENT = JavaCore.PLUGIN_ID + ".formatter.method_declaration_arguments_alignment"; //$NON-NLS-1$
@@ -772,4 +797,55 @@
@Deprecated
private static final String FORMATTER_COMMENT_FORMATHTML= PreferenceConstants.FORMATTER_COMMENT_FORMATHTML;
+ /**
+ * @deprecated As of 3.16 replaced by
+ * {@link DefaultCodeFormatterConstants#FORMATTER_KEEP_ANNOTATION_DECLARATION_ON_ONE_LINE}
+ */
+ @Deprecated
+ private static final String FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANNOTATION_DECLARATION= DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANNOTATION_DECLARATION;
+
+ /**
+ * @deprecated As of 3.16 replaced by
+ * {@link DefaultCodeFormatterConstants#FORMATTER_KEEP_ANONYMOUS_TYPE_DECLARATION_ON_ONE_LINE}
+ */
+ @Deprecated
+ private static final String FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANONYMOUS_TYPE_DECLARATION= DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ANONYMOUS_TYPE_DECLARATION;
+
+ /**
+ * @deprecated As of 3.16 replaced by
+ * {@link DefaultCodeFormatterConstants#FORMATTER_KEEP_IF_THEN_BODY_BLOCK_ON_ONE_LINE},
+ * {@link DefaultCodeFormatterConstants#FORMATTER_KEEP_LOOP_BODY_BLOCK_ON_ONE_LINE},
+ * {@link DefaultCodeFormatterConstants#FORMATTER_KEEP_LAMBDA_BODY_BLOCK_ON_ONE_LINE},
+ * and {@link DefaultCodeFormatterConstants#FORMATTER_KEEP_CODE_BLOCK_ON_ONE_LINE}
+ */
+ @Deprecated
+ private static final String FORMATTER_INSERT_NEW_LINE_IN_EMPTY_BLOCK= DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_BLOCK;
+
+ /**
+ * @deprecated As of 3.16 replaced by
+ * {@link DefaultCodeFormatterConstants#FORMATTER_KEEP_ENUM_CONSTANT_DECLARATION_ON_ONE_LINE}
+ */
+ @Deprecated
+ private static final String FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ENUM_CONSTANT= DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ENUM_CONSTANT;
+
+ /**
+ * @deprecated As of 3.16 replaced by
+ * {@link DefaultCodeFormatterConstants#FORMATTER_KEEP_ENUM_DECLARATION_ON_ONE_LINE}
+ */
+ @Deprecated
+ private static final String FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ENUM_DECLARATION= DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_ENUM_DECLARATION;
+
+ /**
+ * @deprecated As of 3.16 replaced by
+ * {@link DefaultCodeFormatterConstants#FORMATTER_KEEP_METHOD_BODY_ON_ONE_LINE}
+ */
+ @Deprecated
+ private static final String FORMATTER_INSERT_NEW_LINE_IN_EMPTY_METHOD_BODY= DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_METHOD_BODY;
+
+ /**
+ * @deprecated As of 3.16 replaced by
+ * {@link DefaultCodeFormatterConstants#FORMATTER_KEEP_TYPE_DECLARATION_ON_ONE_LINE}
+ */
+ @Deprecated
+ private static final String FORMATTER_INSERT_NEW_LINE_IN_EMPTY_TYPE_DECLARATION= DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_IN_EMPTY_TYPE_DECLARATION;
}