diff options
author | Mateusz Matela | 2019-08-08 21:22:09 +0000 |
---|---|---|
committer | Mateusz Matela | 2019-08-10 20:48:46 +0000 |
commit | 2d0ebadd6a1900252e5b1e8c558df1c9fda958c6 (patch) | |
tree | 82fda5b20a324db909b3bb9e4a749b1b5197a568 | |
parent | 714070211387f0595cd8df499e20cf559116fcea (diff) | |
download | eclipse.jdt.core-2d0ebadd6a1900252e5b1e8c558df1c9fda958c6.tar.gz eclipse.jdt.core-2d0ebadd6a1900252e5b1e8c558df1c9fda958c6.tar.xz eclipse.jdt.core-2d0ebadd6a1900252e5b1e8c558df1c9fda958c6.zip |
Bug 212867 - [formatter]: Remove blank lines after '{' and before '}'
Change-Id: I7dc124d6484490d5d69c887c38962a7047d1d0e6
Signed-off-by: Mateusz Matela <mateusz.matela@gmail.com>
16 files changed, 1046 insertions, 13 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 39eca9b18b..16b2d3ffca 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 @@ -351,6 +351,11 @@ public class FormatterRegressionTests extends AbstractJavaModelTests { } } + void formatSourceInWorkspace(String packageName, String inputCuName, String outputCuName) throws JavaModelException { + String input = getCompilationUnit("Formatter", "", packageName, inputCuName).getSource(); + formatSource(input, getCompilationUnit("Formatter", "", packageName, outputCuName).getSource()); + } + private void runTest(String packageName, String compilationUnitName) { DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(DefaultCodeFormatterConstants.getEclipse21Settings()); @@ -15151,4 +15156,36 @@ public void testBug214283c() throws JavaModelException { String input = getCompilationUnit("Formatter", "", "test214283", "in.java").getSource(); formatSource(input, getCompilationUnit("Formatter", "", "test214283", "C_out.java").getSource()); } +/** + * https://bugs.eclipse.org/212867 - [formatter]: Remove blank lines after '{' and before '}' + */ +public void testBug212867a() throws JavaModelException { + this.formatterPrefs.blank_lines_at_beginning_of_code_block = 2; + this.formatterPrefs.blank_lines_at_end_of_code_block = 0; + formatSourceInWorkspace("test212867", "in.java", "A_out.java"); +} +/** + * https://bugs.eclipse.org/212867 - [formatter]: Remove blank lines after '{' and before '}' + */ +public void testBug212867b() throws JavaModelException { + this.formatterPrefs.blank_lines_at_beginning_of_code_block = 0; + this.formatterPrefs.blank_lines_at_end_of_code_block = 2; + formatSourceInWorkspace("test212867", "in.java", "B_out.java"); +} +/** + * https://bugs.eclipse.org/212867 - [formatter]: Remove blank lines after '{' and before '}' + */ +public void testBug212867c() throws JavaModelException { + this.formatterPrefs.blank_lines_at_beginning_of_code_block = 2; + this.formatterPrefs.blank_lines_at_end_of_code_block = 2; + formatSourceInWorkspace("test212867", "in.java", "C_out.java"); +} +/** + * https://bugs.eclipse.org/212867 - [formatter]: Remove blank lines after '{' and before '}' + */ +public void testBug212867d() throws JavaModelException { + this.formatterPrefs.blank_lines_at_beginning_of_code_block = ~0; + this.formatterPrefs.blank_lines_at_end_of_code_block = ~0; + formatSourceInWorkspace("test212867", "in.java", "D_out.java"); +} } diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test169131/A_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test169131/A_out.java index b977fae9e0..e8e3401dca 100644 --- a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test169131/A_out.java +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test169131/A_out.java @@ -37,6 +37,15 @@ public class C2 { public class C3 { int a; + + + // comment + // comment + // comment +} + +public class C3b { + int a; // comment // comment // comment @@ -53,6 +62,15 @@ abstract class C4 { class C5 { int a; + + + /* comment */ + /* comment */ + /* comment */ +} + +class C5b { + int a; /* comment */ /* comment */ /* comment */ diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test169131/B_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test169131/B_out.java index 8886b3741f..9f2fe319ed 100644 --- a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test169131/B_out.java +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test169131/B_out.java @@ -31,6 +31,14 @@ public class C2 { public class C3 { int a; + + // comment + // comment + // comment +} + +public class C3b { + int a; // comment // comment // comment @@ -45,6 +53,14 @@ abstract class C4 { class C5 { int a; + + /* comment */ + /* comment */ + /* comment */ +} + +class C5b { + int a; /* comment */ /* comment */ /* comment */ diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test169131/in.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test169131/in.java index c036352fd2..bce2a0fc99 100644 --- a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test169131/in.java +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test169131/in.java @@ -33,6 +33,13 @@ public class C3 { // comment // comment } +public class C3b { + int a; + // comment + // comment + // comment + +} abstract class C4 { int a; /* comment */ @@ -44,6 +51,13 @@ class C5 { /* comment */ /* comment */ } +class C5b { + int a; + /* comment */ + /* comment */ + /* comment */ + +} enum Enum { A, B; diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test212867/A_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test212867/A_out.java new file mode 100644 index 0000000000..b0c068331a --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test212867/A_out.java @@ -0,0 +1,193 @@ +import java.io.IOException; +import java.util.Collections; + +class Example { + static { + + + staticInitialize(); + } + { + + + initialize(); + } + + void example() { + if (true) { + + + doSomething(); + } else { + + + doSomethingElse(); + } + if (true) { + + + doSomething(); + + } else { + + + doSomethingElse(); + + } + if (1 == 1) { + + + } + if (2 == 2) { + + + } + if (3 == 3) { + + + // comment + } + if (4 == 4) { + + + // comment + } + if (5 == 5) { + // comment + + + } + if (6 == 6) { + + + // comment + + } + if (7 == 7) { + + + // comment + statement(); + // comment + } + if (8 == 8) { + // comment + + + statement(); + + // comment + } + if (33 == 33) { + + + /* comment */ + } + if (44 == 44) { + + + /* comment */ + } + if (55 == 55) { + /* comment */ + + + } + if (66 == 66) { + + + /* comment */ + + } + if (77 == 77) { + + + /* comment */ + statement(); + /* comment */ + } + if (88 == 88) { + /* comment */ + + + statement(); + + /* comment */ + } + for (int i = 0; i < 10; i++) { + + + switch (i) { + + + case 1: + System.out.println("one"); + break; + default: + System.out.println("no one"); + break; + } + } + for (String s : Collections.emptyList()) { + + + try { + + + doSomething(s); + + } catch (IOException e) { + + + handleError(e); + } catch (Exception e) { + + + e.printStackTrace(); + } finally { + + + cleanup(); + + } + } + while (condition()) { + + + step1(); + step2(); + } + do { + + + int a = step3(); + try (Closable c = open()) { + + + a++; + synchronized (this) { + + + step4(a, a); + } + a--; + } catch (Exception e) { + + + a += 10; + Object lambda = () -> { + + + int b = a + 2 * a; + String c = "" + b; + }; + } + } while (!condition2()); + + { // TODO + + + todo(); + } + } +}
\ No newline at end of file diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test212867/B_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test212867/B_out.java new file mode 100644 index 0000000000..b1feea7b26 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test212867/B_out.java @@ -0,0 +1,194 @@ +import java.io.IOException; +import java.util.Collections; + +class Example { + static { + staticInitialize(); + + + } + { + initialize(); + + + } + + void example() { + if (true) { + doSomething(); + + + } else { + doSomethingElse(); + + + } + if (true) { + + doSomething(); + + + } else { + + doSomethingElse(); + + + } + if (1 == 1) { + + + } + if (2 == 2) { + + + } + if (3 == 3) { + + + // comment + } + if (4 == 4) { + + + // comment + } + if (5 == 5) { + // comment + + + } + if (6 == 6) { + + // comment + + + } + if (7 == 7) { + // comment + statement(); + + + // comment + } + if (8 == 8) { + // comment + + statement(); + + + // comment + } + if (33 == 33) { + + + /* comment */ + } + if (44 == 44) { + + + /* comment */ + } + if (55 == 55) { + /* comment */ + + + } + if (66 == 66) { + + /* comment */ + + + } + if (77 == 77) { + /* comment */ + statement(); + + + /* comment */ + } + if (88 == 88) { + /* comment */ + + statement(); + + + /* comment */ + } + for (int i = 0; i < 10; i++) { + switch (i) { + case 1: + System.out.println("one"); + break; + default: + System.out.println("no one"); + break; + + + } + + + } + for (String s : Collections.emptyList()) { + try { + doSomething(s); + + + } catch (IOException e) { + + handleError(e); + + + } catch (Exception e) { + + e.printStackTrace(); + + + } finally { + + cleanup(); + + + } + + + } + while (condition()) { + step1(); + step2(); + + + } + do { + int a = step3(); + try (Closable c = open()) { + a++; + synchronized (this) { + step4(a, a); + + + } + a--; + + + } catch (Exception e) { + a += 10; + Object lambda = () -> { + int b = a + 2 * a; + String c = "" + b; + + + }; + + + } + + + } while (!condition2()); + + { // TODO + todo(); + + + } + } +}
\ No newline at end of file diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test212867/C_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test212867/C_out.java new file mode 100644 index 0000000000..64922e4c9f --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test212867/C_out.java @@ -0,0 +1,237 @@ +import java.io.IOException; +import java.util.Collections; + +class Example { + static { + + + staticInitialize(); + + + } + { + + + initialize(); + + + } + + void example() { + if (true) { + + + doSomething(); + + + } else { + + + doSomethingElse(); + + + } + if (true) { + + + doSomething(); + + + } else { + + + doSomethingElse(); + + + } + if (1 == 1) { + + + } + if (2 == 2) { + + + } + if (3 == 3) { + + + // comment + } + if (4 == 4) { + + + // comment + } + if (5 == 5) { + // comment + + + } + if (6 == 6) { + + + // comment + + + } + if (7 == 7) { + + + // comment + statement(); + + + // comment + } + if (8 == 8) { + // comment + + + statement(); + + + // comment + } + if (33 == 33) { + + + /* comment */ + } + if (44 == 44) { + + + /* comment */ + } + if (55 == 55) { + /* comment */ + + + } + if (66 == 66) { + + + /* comment */ + + + } + if (77 == 77) { + + + /* comment */ + statement(); + + + /* comment */ + } + if (88 == 88) { + /* comment */ + + + statement(); + + + /* comment */ + } + for (int i = 0; i < 10; i++) { + + + switch (i) { + + + case 1: + System.out.println("one"); + break; + default: + System.out.println("no one"); + break; + + + } + + + } + for (String s : Collections.emptyList()) { + + + try { + + + doSomething(s); + + + } catch (IOException e) { + + + handleError(e); + + + } catch (Exception e) { + + + e.printStackTrace(); + + + } finally { + + + cleanup(); + + + } + + + } + while (condition()) { + + + step1(); + step2(); + + + } + do { + + + int a = step3(); + try (Closable c = open()) { + + + a++; + synchronized (this) { + + + step4(a, a); + + + } + a--; + + + } catch (Exception e) { + + + a += 10; + Object lambda = () -> { + + + int b = a + 2 * a; + String c = "" + b; + + + }; + + + } + + + } while (!condition2()); + + { // TODO + + + todo(); + + + } + } +}
\ No newline at end of file diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test212867/D_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test212867/D_out.java new file mode 100644 index 0000000000..b199a84fce --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test212867/D_out.java @@ -0,0 +1,117 @@ +import java.io.IOException; +import java.util.Collections; + +class Example { + static { + staticInitialize(); + } + { + initialize(); + } + + void example() { + if (true) { + doSomething(); + } else { + doSomethingElse(); + } + if (true) { + doSomething(); + } else { + doSomethingElse(); + } + if (1 == 1) { + } + if (2 == 2) { + } + if (3 == 3) { + // comment + } + if (4 == 4) { + // comment + } + if (5 == 5) { + // comment + } + if (6 == 6) { + // comment + } + if (7 == 7) { + // comment + statement(); + // comment + } + if (8 == 8) { + // comment + statement(); + // comment + } + if (33 == 33) { + /* comment */ + } + if (44 == 44) { + /* comment */ + } + if (55 == 55) { + /* comment */ + } + if (66 == 66) { + /* comment */ + } + if (77 == 77) { + /* comment */ + statement(); + /* comment */ + } + if (88 == 88) { + /* comment */ + statement(); + /* comment */ + } + for (int i = 0; i < 10; i++) { + switch (i) { + case 1: + System.out.println("one"); + break; + default: + System.out.println("no one"); + break; + } + } + for (String s : Collections.emptyList()) { + try { + doSomething(s); + } catch (IOException e) { + handleError(e); + } catch (Exception e) { + e.printStackTrace(); + } finally { + cleanup(); + } + } + while (condition()) { + step1(); + step2(); + } + do { + int a = step3(); + try (Closable c = open()) { + a++; + synchronized (this) { + step4(a, a); + } + a--; + } catch (Exception e) { + a += 10; + Object lambda = () -> { + int b = a + 2 * a; + String c = "" + b; + }; + } + } while (!condition2()); + + { // TODO + todo(); + } + } +}
\ No newline at end of file diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test212867/in.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test212867/in.java new file mode 100644 index 0000000000..6470097bc0 --- /dev/null +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test212867/in.java @@ -0,0 +1,138 @@ +import java.io.IOException; +import java.util.Collections; + +class Example { + static { + staticInitialize(); + } + { + initialize(); + } + void example() { + if (true) { + doSomething(); + } else { + doSomethingElse(); + } + if (true) { + + doSomething(); + + } else { + + doSomethingElse(); + + } + if (1 == 1) { + } + if (2 == 2) { + + } + if (3 == 3) { + // comment + } + if (4 == 4) { + + // comment + } + if (5 == 5) { + // comment + + } + if (6 == 6) { + + // comment + + } + if (7 == 7) { + // comment + statement(); + // comment + } + if (8 == 8) { + // comment + + statement(); + + // comment + } + if (33 == 33) { + /* comment */ + } + if (44 == 44) { + + /* comment */ + } + if (55 == 55) { + /* comment */ + + } + if (66 == 66) { + + /* comment */ + + } + if (77 == 77) { + /* comment */ + statement(); + /* comment */ + } + if (88 == 88) { + /* comment */ + + statement(); + + /* comment */ + } + for (int i = 0; i < 10; i++) { + switch (i) { + case 1: + System.out.println("one"); + break; + default: + System.out.println("no one"); + break; + } + } + for (String s : Collections.emptyList()) { + try { + doSomething(s); + + } catch (IOException e) { + + handleError(e); + } catch (Exception e) { + + e.printStackTrace(); + } finally { + + cleanup(); + + } + } + while (condition()) { + step1(); + step2(); + } + do { + int a = step3(); + try (Closable c = open()) { + a++; + synchronized (this) { + step4(a, a); + } + a--; + } catch (Exception e) { + a += 10; + Object lambda = () -> { + int b = a + 2 * a; + String c = "" + b; + }; + } + } while (!condition2()); + + { // TODO + todo(); + } + } +}
\ No newline at end of file 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 index fa4c30b297..939d25b689 100644 --- 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 @@ -6,15 +6,21 @@ class C { } protected C(int a) { + + + // with comment + } + + protected C(byte a) { // with comment } C(String s) { - /* with comment */ + /* with comment */ } public C(int a, String s) { @@ -29,9 +35,9 @@ class C { } void empty2() { - // with comment + // with comment } void empty3() { 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 index f579bcc738..d9dcc11d5f 100644 --- 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 @@ -7,6 +7,10 @@ class C { // with comment } + protected C(byte a) { + // with comment + } + C(String s) { /* with comment */ } 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 index 7e78752c7a..be3e69705b 100644 --- a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test522089/in.java +++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test522089/in.java @@ -5,6 +5,10 @@ class C { protected C(int a) { // with comment } + protected C(byte a) { + // with comment + + } C(String s) { /* with comment */ } 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 72b0f488bb..c505f508e3 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 @@ -601,6 +601,30 @@ public class DefaultCodeFormatterConstants { 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 at the beginning of the code block + * - option id: "org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_code_block" + * - 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_BEGINNING_OF_CODE_BLOCK = JavaCore.PLUGIN_ID + ".formatter.number_of_blank_lines_at_beginning_of_code_block"; //$NON-NLS-1$ + /** + * <pre> + * FORMATTER / Option to add or remove blank lines at the end of the code block + * - option id: "org.eclipse.jdt.core.formatter.number_of_blank_lines_at_end_of_code_block" + * - 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_CODE_BLOCK = JavaCore.PLUGIN_ID + ".formatter.number_of_blank_lines_at_end_of_code_block"; //$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/CommentsPreparator.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CommentsPreparator.java index 8dbb67f6d3..6e3b0e1c3f 100644 --- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CommentsPreparator.java +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CommentsPreparator.java @@ -444,11 +444,15 @@ public class CommentsPreparator extends ASTVisitor { if (existingBreaksBefore < existingBreaksAfter && previous != null) { commentToken.putLineBreaksAfter(previous.getLineBreaksAfter()); + commentToken.setPreserveLineBreaksAfter(previous.isPreserveLineBreaksAfter()); previous.clearLineBreaksAfter(); - } else if (existingBreaksAfter <= existingBreaksBefore && next != null + previous.setPreserveLineBreaksAfter(true); + } else if (existingBreaksAfter < 2 && existingBreaksAfter <= existingBreaksBefore && next != null && next.tokenType != TokenNamepackage /* doesn't apply to a comment before the package declaration */) { commentToken.putLineBreaksBefore(next.getLineBreaksBefore()); + commentToken.setPreserveLineBreaksBefore(next.isPreserveLineBreaksBefore()); next.clearLineBreaksBefore(); + next.setPreserveLineBreaksBefore(true); } } 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 cb6b020b3e..e1dfa402c5 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 @@ -204,6 +204,8 @@ public class DefaultCodeFormatterOptions { 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 int blank_lines_at_beginning_of_code_block; + public int blank_lines_at_end_of_code_block; public boolean comment_clear_blank_lines_in_javadoc_comment; public boolean comment_clear_blank_lines_in_block_comment; @@ -613,6 +615,8 @@ public class DefaultCodeFormatterOptions { 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_BLANK_LINES_AT_BEGINNING_OF_CODE_BLOCK, Integer.toString(this.blank_lines_at_beginning_of_code_block)); + options.put(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_AT_END_OF_CODE_BLOCK, Integer.toString(this.blank_lines_at_end_of_code_block)); 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); @@ -1381,6 +1385,10 @@ public class DefaultCodeFormatterOptions { } setInt(settings, DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_AT_END_OF_METHOD_BODY, v -> this.blank_lines_at_end_of_method_body = v); + setInt(settings, DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_AT_BEGINNING_OF_CODE_BLOCK, + v -> this.blank_lines_at_beginning_of_code_block = v); + setInt(settings, DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_AT_END_OF_CODE_BLOCK, + v -> this.blank_lines_at_end_of_code_block = 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); @@ -2823,6 +2831,8 @@ public class DefaultCodeFormatterOptions { 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.blank_lines_at_beginning_of_code_block = 0; + this.blank_lines_at_end_of_code_block = 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; @@ -3181,6 +3191,8 @@ public class DefaultCodeFormatterOptions { 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.blank_lines_at_beginning_of_code_block = 0; + this.blank_lines_at_end_of_code_block = 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 9c75ba6f2a..59c36282c1 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 @@ -196,11 +196,15 @@ public class LineBreaksPreparator extends ASTVisitor { int index = this.tm.firstIndexIn(node, -1); while (index > 0 && this.tm.get(index - 1).tokenType == TokenNameCOMMENT_JAVADOC) index--; + putBlankLinesBefore(this.tm.get(index), linesCount); + } + + private void putBlankLinesBefore(Token token, int linesCount) { if (linesCount >= 0) { - this.tm.get(index).putLineBreaksBefore(linesCount + 1); + token.putLineBreaksBefore(linesCount + 1); } else { - this.tm.get(index).putLineBreaksBefore(~linesCount + 1); - this.tm.get(index).setPreserveLineBreaksBefore(false); + token.putLineBreaksBefore(~linesCount + 1); + token.setPreserveLineBreaksBefore(false); } } @@ -214,8 +218,7 @@ public class LineBreaksPreparator extends ASTVisitor { } private void putBlankLinesBeforeCloseBrace(ASTNode node, int blankLinesSetting) { - int closeBraceIndex = this.tm.lastIndexIn(node, TokenNameRBRACE); - putBlankLinesAfter(this.tm.get(closeBraceIndex - 1), blankLinesSetting); + putBlankLinesBefore(this.tm.lastTokenIn(node, TokenNameRBRACE), blankLinesSetting); } @Override @@ -292,11 +295,11 @@ public class LineBreaksPreparator extends ASTVisitor { String bracePosition = node.isConstructor() ? this.options.brace_position_for_constructor_declaration : this.options.brace_position_for_method_declaration; handleBracedCode(node.getBody(), null, bracePosition, this.options.indent_statements_compare_to_body); - Token openBrace = this.tm.firstTokenIn(node.getBody(), TokenNameLBRACE); - 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); - } + + putBlankLinesAfter(this.tm.firstTokenIn(node.getBody(), TokenNameLBRACE), + this.options.blank_lines_at_beginning_of_method_body); + putBlankLinesBeforeCloseBrace(node, this.options.blank_lines_at_end_of_method_body); + return true; } @@ -325,6 +328,10 @@ public class LineBreaksPreparator extends ASTVisitor { } handleBracedCode(node, null, bracePosition, this.options.indent_statements_compare_to_block); + putBlankLinesAfter(this.tm.firstTokenIn(node, TokenNameLBRACE), + this.options.blank_lines_at_beginning_of_code_block); + putBlankLinesBeforeCloseBrace(node, this.options.blank_lines_at_end_of_code_block); + return true; } @@ -376,6 +383,10 @@ public class LineBreaksPreparator extends ASTVisitor { breakLineBefore(statement); } + putBlankLinesAfter(this.tm.firstTokenAfter(node.getExpression(), TokenNameLBRACE), + this.options.blank_lines_at_beginning_of_code_block); + putBlankLinesBeforeCloseBrace(node, this.options.blank_lines_at_end_of_code_block); + return true; } @@ -427,6 +438,10 @@ public class LineBreaksPreparator extends ASTVisitor { breakLineBefore(statement); } + putBlankLinesAfter(this.tm.firstTokenAfter(node.getExpression(), TokenNameLBRACE), + this.options.blank_lines_at_beginning_of_code_block); + putBlankLinesBeforeCloseBrace(node, this.options.blank_lines_at_end_of_code_block); + return true; } |