diff options
author | Mateusz Matela | 2019-08-04 13:26:50 +0000 |
---|---|---|
committer | Mateusz Matela | 2019-08-04 13:26:50 +0000 |
commit | d0c76762c053346997553cc62592c904876efdf6 (patch) | |
tree | b28e1844501a90af30a769ffb54eda953ebbb5c5 | |
parent | 9acb7086514502a716e3f5a9596c8a76f9aa0b8c (diff) | |
download | eclipse.jdt.core-d0c76762c053346997553cc62592c904876efdf6.tar.gz eclipse.jdt.core-d0c76762c053346997553cc62592c904876efdf6.tar.xz eclipse.jdt.core-d0c76762c053346997553cc62592c904876efdf6.zip |
Bug 214283 - [formatter] Blank lines option should consider aI20190805-1800I20190804-1800
virtual/interface method
Change-Id: I1728e1b8be458f42d9856e3155d20ae3555b64c5
Signed-off-by: Mateusz Matela <mateusz.matela@gmail.com>
9 files changed, 170 insertions, 4 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java index 84fe6e8d28..ff4c5f1915 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java @@ -10991,7 +10991,7 @@ public void testBug471090() throws JavaModelException { * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=471364" */ public void testBug471364() throws JavaModelException { - this.formatterPrefs.blank_lines_before_method = 0; + this.formatterPrefs.blank_lines_before_abstract_method = 0; this.formatterPrefs.alignment_for_method_declaration = Alignment.M_COMPACT_SPLIT; String source = "interface Example {\r\n" + 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 b662f74118..39eca9b18b 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 @@ -15124,4 +15124,31 @@ public void testBug549774() throws JavaModelException { " }\n" + "}"); } +/** + * https://bugs.eclipse.org/214283 - [formatter] Blank lines option should consider a virtual/interface method + */ +public void testBug214283a() throws JavaModelException { + this.formatterPrefs.blank_lines_before_abstract_method= 2; + this.formatterPrefs.blank_lines_before_method= 0; + String input = getCompilationUnit("Formatter", "", "test214283", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test214283", "A_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/214283 - [formatter] Blank lines option should consider a virtual/interface method + */ +public void testBug214283b() throws JavaModelException { + this.formatterPrefs.blank_lines_before_abstract_method= ~0; + this.formatterPrefs.blank_lines_before_method= 1; + String input = getCompilationUnit("Formatter", "", "test214283", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test214283", "B_out.java").getSource()); +} +/** + * https://bugs.eclipse.org/214283 - [formatter] Blank lines option should consider a virtual/interface method + */ +public void testBug214283c() throws JavaModelException { + this.formatterPrefs.blank_lines_before_abstract_method= 0; + this.formatterPrefs.blank_lines_before_method= 2; + String input = getCompilationUnit("Formatter", "", "test214283", "in.java").getSource(); + formatSource(input, getCompilationUnit("Formatter", "", "test214283", "C_out.java").getSource()); +} } diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test214283/A_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test214283/A_out.java new file mode 100644 index 0000000000..c13e21489a --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test214283/A_out.java @@ -0,0 +1,32 @@ +abstract class C { + interface I { + void method1(); + + + String method2(); + + + void method3(String s); + default void method4() { + System.out.println("method4"); + System.out.println("method5"); + } + } + + public String field; + + public abstract String methodA(); + + + public abstract int methodB(Object o); + public void methodC() { + System.out.println("methodC"); + } + public String methodD(String s) { + return s + "s"; + } + protected abstract Object methodE(String a); + + + protected abstract void methodF(String... a); +}
\ No newline at end of file diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test214283/B_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test214283/B_out.java new file mode 100644 index 0000000000..5c54d54269 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test214283/B_out.java @@ -0,0 +1,28 @@ +abstract class C { + interface I { + void method1(); + String method2(); + void method3(String s); + + default void method4() { + System.out.println("method4"); + System.out.println("method5"); + } + } + + public String field; + + public abstract String methodA(); + public abstract int methodB(Object o); + + public void methodC() { + System.out.println("methodC"); + } + + public String methodD(String s) { + return s + "s"; + } + + protected abstract Object methodE(String a); + protected abstract void methodF(String... a); +}
\ No newline at end of file diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test214283/C_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test214283/C_out.java new file mode 100644 index 0000000000..1e8f7d4b6c --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test214283/C_out.java @@ -0,0 +1,33 @@ +abstract class C { + interface I { + void method1(); + + String method2(); + void method3(String s); + + + default void method4() { + System.out.println("method4"); + System.out.println("method5"); + } + } + + public String field; + + public abstract String methodA(); + public abstract int methodB(Object o); + + + public void methodC() { + System.out.println("methodC"); + } + + + public String methodD(String s) { + return s + "s"; + } + + + protected abstract Object methodE(String a); + protected abstract void methodF(String... a); +}
\ No newline at end of file diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test214283/in.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test214283/in.java new file mode 100644 index 0000000000..e5a0a9c60d --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test214283/in.java @@ -0,0 +1,23 @@ +abstract class C { + interface I { + void method1(); + + String method2(); + void method3(String s); + default void method4() { + System.out.println("method4"); + System.out.println("method5"); + } + } + public String field; + public abstract String methodA(); + public abstract int methodB(Object o); + public void methodC() { + System.out.println("methodC"); + } + public String methodD(String s) { + return s + "s"; + } + protected abstract Object methodE(String a); + protected abstract void methodF(String ... a); +}
\ 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 2949993f1a..72b0f488bb 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 @@ -649,6 +649,19 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_BLANK_LINES_BEFORE_IMPORTS = JavaCore.PLUGIN_ID + ".formatter.blank_lines_before_imports"; //$NON-NLS-1$ /** * <pre> + * FORMATTER / Option to add or remove blank lines before an abstract method declaration + * - option id: "org.eclipse.jdt.core.formatter.blank_lines_before_abstract_method" + * - 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_BEFORE_ABSTRACT_METHOD = JavaCore.PLUGIN_ID + ".formatter.blank_lines_before_abstract_method"; //$NON-NLS-1$ + + /** + * <pre> * FORMATTER / Option to add or remove blank lines before a member type declaration * - option id: "org.eclipse.jdt.core.formatter.blank_lines_before_member_type" * - possible values: "<n>", where n is an integer. If n is negative, the actual number of @@ -661,7 +674,7 @@ public class DefaultCodeFormatterConstants { public static final String FORMATTER_BLANK_LINES_BEFORE_MEMBER_TYPE = JavaCore.PLUGIN_ID + ".formatter.blank_lines_before_member_type"; //$NON-NLS-1$ /** * <pre> - * FORMATTER / Option to add or remove blank lines before a method declaration + * FORMATTER / Option to add or remove blank lines before a non-abstract method declaration * - option id: "org.eclipse.jdt.core.formatter.blank_lines_before_method" * - 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 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 318a9d0ae3..cb6b020b3e 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 @@ -196,6 +196,7 @@ public class DefaultCodeFormatterOptions { public int blank_lines_after_last_class_body_declaration; public int blank_lines_before_imports; public int blank_lines_before_member_type; + public int blank_lines_before_abstract_method; public int blank_lines_before_method; public int blank_lines_before_new_chunk; public int blank_lines_before_package; @@ -604,6 +605,7 @@ public class DefaultCodeFormatterOptions { options.put(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_AFTER_LAST_CLASS_BODY_DECLARATION, Integer.toString(this.blank_lines_after_last_class_body_declaration)); options.put(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_BEFORE_IMPORTS, Integer.toString(this.blank_lines_before_imports)); options.put(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_BEFORE_MEMBER_TYPE, Integer.toString(this.blank_lines_before_member_type)); + options.put(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_BEFORE_ABSTRACT_METHOD, Integer.toString(this.blank_lines_before_abstract_method)); options.put(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_BEFORE_METHOD, Integer.toString(this.blank_lines_before_method)); options.put(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_BEFORE_NEW_CHUNK, Integer.toString(this.blank_lines_before_new_chunk)); options.put(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_BEFORE_PACKAGE, Integer.toString(this.blank_lines_before_package)); @@ -1327,6 +1329,8 @@ public class DefaultCodeFormatterOptions { this.blank_lines_before_member_type = 0; } } + setInt(settings, DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_BEFORE_ABSTRACT_METHOD, + v -> this.blank_lines_before_abstract_method = v); final Object blankLinesBeforeMethodOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_BEFORE_METHOD); if (blankLinesBeforeMethodOption != null) { try { @@ -2811,6 +2815,7 @@ public class DefaultCodeFormatterOptions { this.blank_lines_after_last_class_body_declaration = 0; this.blank_lines_before_imports = 0; this.blank_lines_before_member_type = 0; + this.blank_lines_before_abstract_method = 0; this.blank_lines_before_method = 0; this.blank_lines_before_new_chunk = 0; this.blank_lines_before_package = 0; @@ -3168,6 +3173,7 @@ public class DefaultCodeFormatterOptions { this.blank_lines_after_last_class_body_declaration = 0; this.blank_lines_before_imports = 1; this.blank_lines_before_member_type = 1; + this.blank_lines_before_abstract_method = 1; this.blank_lines_before_method = 1; this.blank_lines_before_new_chunk = 1; this.blank_lines_before_package = 0; 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 9a72f1f619..9c75ba6f2a 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 @@ -168,8 +168,12 @@ public class LineBreaksPreparator extends ASTVisitor { blankLines = this.options.blank_lines_before_field; } else if (bodyDeclaration instanceof AbstractTypeDeclaration) { blankLines = this.options.blank_lines_before_member_type; - } else if (bodyDeclaration instanceof MethodDeclaration - || bodyDeclaration instanceof AnnotationTypeMemberDeclaration) { + } else if (bodyDeclaration instanceof MethodDeclaration) { + blankLines = ((MethodDeclaration) bodyDeclaration).getBody() == null + && ((MethodDeclaration) previous).getBody() == null + ? this.options.blank_lines_before_abstract_method + : this.options.blank_lines_before_method; + } else if (bodyDeclaration instanceof AnnotationTypeMemberDeclaration) { blankLines = this.options.blank_lines_before_method; } putBlankLinesBefore(bodyDeclaration, blankLines); |