diff options
author | Mateusz Matela | 2019-08-02 21:18:28 +0000 |
---|---|---|
committer | Mateusz Matela | 2019-08-02 22:58:28 +0000 |
commit | d5ab917ba37be34f167c95a66058be3ba7693473 (patch) | |
tree | 35937e352bab914b3d84186bf15da8d0a0314dfc | |
parent | b44080f7925870eb072aaf500f89e1049c6427e9 (diff) | |
download | eclipse.jdt.core-d5ab917ba37be34f167c95a66058be3ba7693473.tar.gz eclipse.jdt.core-d5ab917ba37be34f167c95a66058be3ba7693473.tar.xz eclipse.jdt.core-d5ab917ba37be34f167c95a66058be3ba7693473.zip |
Bug 522089 - [formatter] Provide support for blank line before end ofI20190803-1800
method
Change-Id: I259fed5bffc5fc000172342525310cf9eb276c27
7 files changed, 267 insertions, 1 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 c8ebc8728b..2f7925c726 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 @@ -15074,4 +15074,24 @@ public void testBug169131d() throws JavaModelException { "}", CodeFormatter.K_MODULE_INFO | CodeFormatter.F_INCLUDE_COMMENTS); } +/** + * https://bugs.eclipse.org/522089 - [formatter] Provide support for blank line before end of method + */ +public void testBug522089a() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_1_8); + this.formatterPrefs.number_of_empty_lines_to_preserve = 3; + this.formatterPrefs.blank_lines_at_end_of_method_body = 2; + String input = getCompilationUnit("Formatter", "", "test522089", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test522089", "A_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/522089 - [formatter] Provide support for blank line before end of method + */ +public void testBug522089b() throws JavaModelException { + setComplianceLevel(CompilerOptions.VERSION_1_8); + this.formatterPrefs.number_of_empty_lines_to_preserve = 3; + this.formatterPrefs.blank_lines_at_end_of_method_body = ~0; + String input = getCompilationUnit("Formatter", "", "test522089", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test522089", "B_out.java").getSource()); +} } diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test522089/A_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test522089/A_out.java new file mode 100644 index 0000000000..fa4c30b297 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test522089/A_out.java @@ -0,0 +1,94 @@ +class C { + + private C() { + + + } + + protected C(int a) { + // with comment + + + } + + C(String s) { + /* with comment */ + + + } + + public C(int a, String s) { + this(a); + + + } + + void empty1() { + + + } + + void empty2() { + // with comment + + + } + + void empty3() { + /* with comment */ + + + } + + private void foo() { + ; + + + } + + private int bar(String s) { + Runnable r = new Runnable() { + @Override + public void run() { + System.out.println("running " + s); + + + } + }; + Runnable r2 = () -> { + System.out.println("running " + s); + + }; + new Thread(r).start(); + return 0; + + + } +} + +enum Enum { + A('a'), B('b'), C('c'), D('d'), E('e'),; + Enum(char c) { + setup(c); + + + } + + private void setup(char c) { + System.out.println("setting up c"); + System.out.println("setting up c"); + System.out.println("setting up c"); + + + } +} + +interface Interface { + String method1(); + + default String method2() { + return "something"; + + + } +}
\ No newline at end of file diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test522089/B_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test522089/B_out.java new file mode 100644 index 0000000000..f579bcc738 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test522089/B_out.java @@ -0,0 +1,68 @@ +class C { + + private C() { + } + + protected C(int a) { + // with comment + } + + C(String s) { + /* with comment */ + } + + public C(int a, String s) { + this(a); + } + + void empty1() { + } + + void empty2() { + // with comment + } + + void empty3() { + /* with comment */ + } + + private void foo() { + ; + } + + private int bar(String s) { + Runnable r = new Runnable() { + @Override + public void run() { + System.out.println("running " + s); + } + }; + Runnable r2 = () -> { + System.out.println("running " + s); + + }; + new Thread(r).start(); + return 0; + } +} + +enum Enum { + A('a'), B('b'), C('c'), D('d'), E('e'),; + Enum(char c) { + setup(c); + } + + private void setup(char c) { + System.out.println("setting up c"); + System.out.println("setting up c"); + System.out.println("setting up c"); + } +} + +interface Interface { + String method1(); + + default String method2() { + return "something"; + } +}
\ No newline at end of file diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test522089/in.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test522089/in.java new file mode 100644 index 0000000000..7e78752c7a --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test522089/in.java @@ -0,0 +1,64 @@ +class C { + + private C() { + } + protected C(int a) { + // with comment + } + C(String s) { + /* with comment */ + } + public C(int a, String s) { + this(a); + } + + void empty1() { + + } + void empty2() { + // with comment + } + void empty3() { + /* with comment */ + + } + + private void foo() {;} + + private int bar(String s) { + Runnable r = new Runnable() { + @Override + public void run() { + System.out.println("running " + s); + } + }; + Runnable r2 = () -> { + System.out.println("running " + s); + + }; + new Thread(r).start(); + return 0; + } +} + +enum Enum { + A('a'), B('b'), C('c'), + D('d'), E('e'), + ; + Enum(char c) { + setup(c); + } + private void setup(char c) { + System.out.println("setting up c"); + System.out.println("setting up c"); + System.out.println("setting up c"); + + } +} + +interface Interface { + String method1(); + default String method2() { + return "something"; + } +}
\ No newline at end of file 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 9ad9a36f1b..2949993f1a 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 @@ -589,6 +589,18 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_BLANK_LINES_AT_BEGINNING_OF_METHOD_BODY = JavaCore.PLUGIN_ID + ".formatter.number_of_blank_lines_at_beginning_of_method_body"; //$NON-NLS-1$ /** * <pre> + * FORMATTER / Option to add or remove blank lines at the end of the method body + * - option id: "org.eclipse.jdt.core.formatter.number_of_blank_lines_at_end_of_method_body" + * - possible values: "<n>", where n is an integer. If n is negative, the actual number of + * blank lines is ~n and any excess blank lines are deleted, overriding the + * {@link #FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE} option + * - default: "0" + * </pre> + * @since 3.19 + */ + public static final String FORMATTER_BLANK_LINES_AT_END_OF_METHOD_BODY = JavaCore.PLUGIN_ID + ".formatter.number_of_blank_lines_at_end_of_method_body"; //$NON-NLS-1$ + /** + * <pre> * FORMATTER / Option to add or remove blank lines before a field declaration * - option id: "org.eclipse.jdt.core.formatter.blank_lines_before_field" * - possible values: "<n>", where n is an integer. If n is negative, the actual number of 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 44b1473fb8..318a9d0ae3 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 @@ -202,6 +202,7 @@ public class DefaultCodeFormatterOptions { public int blank_lines_between_import_groups; public int blank_lines_between_type_declarations; public int blank_lines_at_beginning_of_method_body; + public int blank_lines_at_end_of_method_body; public boolean comment_clear_blank_lines_in_javadoc_comment; public boolean comment_clear_blank_lines_in_block_comment; @@ -609,6 +610,7 @@ public class DefaultCodeFormatterOptions { options.put(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_BETWEEN_IMPORT_GROUPS, Integer.toString(this.blank_lines_between_import_groups)); options.put(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_BETWEEN_TYPE_DECLARATIONS, Integer.toString(this.blank_lines_between_type_declarations)); options.put(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_AT_BEGINNING_OF_METHOD_BODY, Integer.toString(this.blank_lines_at_beginning_of_method_body)); + options.put(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_AT_END_OF_METHOD_BODY, Integer.toString(this.blank_lines_at_end_of_method_body)); options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_STATEMENTS_COMPARE_TO_BLOCK, this.indent_statements_compare_to_block ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_STATEMENTS_COMPARE_TO_BODY, this.indent_statements_compare_to_body ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ANNOTATION_DECLARATION_HEADER, this.indent_body_declarations_compare_to_annotation_declaration_header ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); @@ -1373,6 +1375,8 @@ public class DefaultCodeFormatterOptions { this.blank_lines_at_beginning_of_method_body = 0; } } + setInt(settings, DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_AT_END_OF_METHOD_BODY, + v -> this.blank_lines_at_end_of_method_body = v); final Object insertNewLineAfterTypeAnnotationOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_TYPE_ANNOTATION); if (insertNewLineAfterTypeAnnotationOption != null) { this.insert_new_line_after_type_annotation = JavaCore.INSERT.equals(insertNewLineAfterTypeAnnotationOption); @@ -2813,6 +2817,7 @@ public class DefaultCodeFormatterOptions { this.blank_lines_between_import_groups = 1; this.blank_lines_between_type_declarations = 0; this.blank_lines_at_beginning_of_method_body = 0; + this.blank_lines_at_end_of_method_body = 0; this.indent_statements_compare_to_block = true; this.indent_statements_compare_to_body = true; this.indent_body_declarations_compare_to_annotation_declaration_header = true; @@ -3169,6 +3174,7 @@ public class DefaultCodeFormatterOptions { this.blank_lines_between_import_groups = 1; this.blank_lines_between_type_declarations = 1; this.blank_lines_at_beginning_of_method_body = 0; + this.blank_lines_at_end_of_method_body = 0; this.indent_statements_compare_to_block = true; this.indent_statements_compare_to_body = true; this.indent_body_declarations_compare_to_annotation_declaration_header = true; 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 ff5b451359..b3eaa23eb5 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 @@ -292,8 +292,10 @@ public class LineBreaksPreparator extends ASTVisitor { : this.options.brace_position_for_method_declaration; 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 + if (openBrace.getLineBreaksAfter() > 0) { // if not, these are empty braces putBlankLinesAfter(openBrace, this.options.blank_lines_at_beginning_of_method_body); + putBlankLinesBeforeCloseBrace(node, this.options.blank_lines_at_end_of_method_body); + } return true; } |