Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMateusz Matela2019-08-11 23:04:11 +0000
committerMateusz Matela2019-08-11 23:04:50 +0000
commit0da21f8ea91a757805c7fad5645bcf49a36c1bb9 (patch)
treee7d42e90b9e6308d35273331b7c75bfe08d98066
parentc19c6a4f496a82dc889f7978fc504c559cb52dec (diff)
downloadeclipse.jdt.core-0da21f8ea91a757805c7fad5645bcf49a36c1bb9.tar.gz
eclipse.jdt.core-0da21f8ea91a757805c7fad5645bcf49a36c1bb9.tar.xz
eclipse.jdt.core-0da21f8ea91a757805c7fad5645bcf49a36c1bb9.zip
Bug 421492 - [formatter] Allow to add blank line(s) before a statement
Change-Id: Ie19ece874c67213319cdba7d3b65159aa9ede820 Signed-off-by: Mateusz Matela <mateusz.matela@gmail.com>
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java32
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Formatter/test421492/A_out.java167
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Formatter/test421492/B_out.java167
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Formatter/test421492/C_out.java187
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Formatter/test421492/D_out.java121
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Formatter/test421492/in.java130
-rw-r--r--org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java24
-rw-r--r--org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CommentsPreparator.java22
-rw-r--r--org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java12
-rw-r--r--org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/LineBreaksPreparator.java18
10 files changed, 872 insertions, 8 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 16b2d3ffca..fd60744ffb 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
@@ -15188,4 +15188,36 @@ public void testBug212867d() throws JavaModelException {
this.formatterPrefs.blank_lines_at_end_of_code_block = ~0;
formatSourceInWorkspace("test212867", "in.java", "D_out.java");
}
+/**
+ * https://bugs.eclipse.org/421492 - [formatter] Allow to add blank line(s) before a statement
+ */
+public void testBug421492a() throws JavaModelException {
+ this.formatterPrefs.blank_lines_before_code_block = 2;
+ this.formatterPrefs.blank_lines_after_code_block = 0;
+ formatSourceInWorkspace("test421492", "in.java", "A_out.java");
+}
+/**
+ * https://bugs.eclipse.org/421492 - [formatter] Allow to add blank line(s) before a statement
+ */
+public void testBug421492b() throws JavaModelException {
+ this.formatterPrefs.blank_lines_before_code_block = 0;
+ this.formatterPrefs.blank_lines_after_code_block = 2;
+ formatSourceInWorkspace("test421492", "in.java", "B_out.java");
+}
+/**
+ * https://bugs.eclipse.org/421492 - [formatter] Allow to add blank line(s) before a statement
+ */
+public void testBug421492c() throws JavaModelException {
+ this.formatterPrefs.blank_lines_before_code_block = 2;
+ this.formatterPrefs.blank_lines_after_code_block = 2;
+ formatSourceInWorkspace("test421492", "in.java", "C_out.java");
+}
+/**
+ * https://bugs.eclipse.org/421492 - [formatter] Allow to add blank line(s) before a statement
+ */
+public void testBug421492d() throws JavaModelException {
+ this.formatterPrefs.blank_lines_before_code_block = ~0;
+ this.formatterPrefs.blank_lines_after_code_block = ~0;
+ formatSourceInWorkspace("test421492", "in.java", "D_out.java");
+}
}
diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test421492/A_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test421492/A_out.java
new file mode 100644
index 0000000000..5e5fbf6ec8
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test421492/A_out.java
@@ -0,0 +1,167 @@
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+
+class Example {
+ static {
+ staticInitialize();
+ }
+ {
+ initialize();
+ }
+
+ void example() {
+
+
+ if (true) {
+ doSomething();
+ } else {
+ doSomethingElse();
+ }
+
+
+ if (true) {
+ doSomething();
+ } else if (false) {
+ doSomethingElse();
+ }
+
+
+ if (1 == 1) {
+ }
+
+
+ // comment
+ if (2 == 2) {
+ }
+ // comment
+
+
+ if (3 == 3) {
+ }
+
+
+ // comment
+ if (4 == 4) {
+ }
+ // double comment
+
+
+ // double comment
+ if (5 == 5) {
+ }
+
+ // double comment
+
+ // double comment
+
+
+ if (6 == 6) {
+ }
+ if (a)
+ instruction();
+
+
+ if (11 == 11) {
+ }
+
+
+ /* comment */
+ if (22 == 22) {
+ }
+ /* comment */
+
+
+ if (33 == 33) {
+ }
+
+
+ /* comment */
+ if (44 == 44) {
+ }
+ /* double comment */
+
+
+ /* double comment */
+ if (55 == 55) {
+ }
+
+ /* double comment */
+
+ /* double comment */
+
+
+ if (66 == 66) {
+ }
+ for (String s : Arrays.asList(""))
+ doSomething(s);
+
+
+ for (int i = 0; i < 10; i++) {
+ aaa();
+
+
+ switch (i) {
+ case 1:
+ System.out.println("one");
+ break;
+ default:
+ System.out.println("no one");
+ break;
+ }
+ aaa();
+ }
+ aaa();
+
+
+ for (String s : Collections.emptyList()) {
+ aaa();
+
+
+ try {
+ doSomething(s);
+ } catch (IOException e) {
+ handleError(e);
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ cleanup();
+ }
+ aaa();
+ }
+ aaa();
+
+
+ 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/test421492/B_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test421492/B_out.java
new file mode 100644
index 0000000000..4489d11dca
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test421492/B_out.java
@@ -0,0 +1,167 @@
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+
+class Example {
+ static {
+ staticInitialize();
+ }
+ {
+ initialize();
+ }
+
+ void example() {
+ if (true) {
+ doSomething();
+ } else {
+ doSomethingElse();
+ }
+
+
+ if (true) {
+ doSomething();
+ } else if (false) {
+ doSomethingElse();
+ }
+
+
+ if (1 == 1) {
+ }
+
+
+ // comment
+ if (2 == 2) {
+ }
+ // comment
+
+
+ if (3 == 3) {
+ }
+
+
+ // comment
+ if (4 == 4) {
+ }
+ // double comment
+
+
+ // double comment
+ if (5 == 5) {
+ }
+
+
+ // double comment
+
+ // double comment
+
+ if (6 == 6) {
+ }
+
+
+ if (a)
+ instruction();
+ if (11 == 11) {
+ }
+
+
+ /* comment */
+ if (22 == 22) {
+ }
+ /* comment */
+
+
+ if (33 == 33) {
+ }
+
+
+ /* comment */
+ if (44 == 44) {
+ }
+ /* double comment */
+
+
+ /* double comment */
+ if (55 == 55) {
+ }
+
+
+ /* double comment */
+
+ /* double comment */
+
+ if (66 == 66) {
+ }
+
+
+ for (String s : Arrays.asList(""))
+ doSomething(s);
+ for (int i = 0; i < 10; i++) {
+ aaa();
+ switch (i) {
+ case 1:
+ System.out.println("one");
+ break;
+ default:
+ System.out.println("no one");
+ break;
+ }
+
+
+ aaa();
+ }
+
+
+ aaa();
+ for (String s : Collections.emptyList()) {
+ aaa();
+ try {
+ doSomething(s);
+ } catch (IOException e) {
+ handleError(e);
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ cleanup();
+ }
+
+
+ aaa();
+ }
+
+
+ aaa();
+ 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/test421492/C_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test421492/C_out.java
new file mode 100644
index 0000000000..0f2cd67714
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test421492/C_out.java
@@ -0,0 +1,187 @@
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+
+class Example {
+ static {
+ staticInitialize();
+ }
+ {
+ initialize();
+ }
+
+ void example() {
+
+
+ if (true) {
+ doSomething();
+ } else {
+ doSomethingElse();
+ }
+
+
+ if (true) {
+ doSomething();
+ } else if (false) {
+ doSomethingElse();
+ }
+
+
+ if (1 == 1) {
+ }
+
+
+ // comment
+ if (2 == 2) {
+ }
+ // comment
+
+
+ if (3 == 3) {
+ }
+
+
+ // comment
+ if (4 == 4) {
+ }
+ // double comment
+
+
+ // double comment
+ if (5 == 5) {
+ }
+
+
+ // double comment
+
+ // double comment
+
+
+ if (6 == 6) {
+ }
+
+
+ if (a)
+ instruction();
+
+
+ if (11 == 11) {
+ }
+
+
+ /* comment */
+ if (22 == 22) {
+ }
+ /* comment */
+
+
+ if (33 == 33) {
+ }
+
+
+ /* comment */
+ if (44 == 44) {
+ }
+ /* double comment */
+
+
+ /* double comment */
+ if (55 == 55) {
+ }
+
+
+ /* double comment */
+
+ /* double comment */
+
+
+ if (66 == 66) {
+ }
+
+
+ for (String s : Arrays.asList(""))
+ doSomething(s);
+
+
+ for (int i = 0; i < 10; i++) {
+ aaa();
+
+
+ switch (i) {
+ case 1:
+ System.out.println("one");
+ break;
+ default:
+ System.out.println("no one");
+ break;
+ }
+
+
+ aaa();
+ }
+
+
+ aaa();
+
+
+ for (String s : Collections.emptyList()) {
+ aaa();
+
+
+ try {
+ doSomething(s);
+ } catch (IOException e) {
+ handleError(e);
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ cleanup();
+ }
+
+
+ aaa();
+ }
+
+
+ aaa();
+
+
+ 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/test421492/D_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test421492/D_out.java
new file mode 100644
index 0000000000..70336c63d8
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test421492/D_out.java
@@ -0,0 +1,121 @@
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+
+class Example {
+ static {
+ staticInitialize();
+ }
+ {
+ initialize();
+ }
+
+ void example() {
+ if (true) {
+ doSomething();
+ } else {
+ doSomethingElse();
+ }
+ if (true) {
+ doSomething();
+ } else if (false) {
+ doSomethingElse();
+ }
+ if (1 == 1) {
+ }
+ // comment
+ if (2 == 2) {
+ }
+ // comment
+ if (3 == 3) {
+ }
+ // comment
+ if (4 == 4) {
+ }
+ // double comment
+
+ // double comment
+ if (5 == 5) {
+ }
+ // double comment
+
+ // double comment
+ if (6 == 6) {
+ }
+ if (a)
+ instruction();
+ if (11 == 11) {
+ }
+ /* comment */
+ if (22 == 22) {
+ }
+ /* comment */
+ if (33 == 33) {
+ }
+ /* comment */
+ if (44 == 44) {
+ }
+ /* double comment */
+
+ /* double comment */
+ if (55 == 55) {
+ }
+ /* double comment */
+
+ /* double comment */
+ if (66 == 66) {
+ }
+ for (String s : Arrays.asList(""))
+ doSomething(s);
+ for (int i = 0; i < 10; i++) {
+ aaa();
+ switch (i) {
+ case 1:
+ System.out.println("one");
+ break;
+ default:
+ System.out.println("no one");
+ break;
+ }
+ aaa();
+ }
+ aaa();
+ for (String s : Collections.emptyList()) {
+ aaa();
+ try {
+ doSomething(s);
+ } catch (IOException e) {
+ handleError(e);
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ cleanup();
+ }
+ aaa();
+ }
+ aaa();
+ 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/test421492/in.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test421492/in.java
new file mode 100644
index 0000000000..5b8fbbe75b
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test421492/in.java
@@ -0,0 +1,130 @@
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+
+class Example {
+ static {
+ staticInitialize();
+ }
+ {
+ initialize();
+ }
+ void example() {
+ if (true) {
+ doSomething();
+ } else {
+ doSomethingElse();
+ }
+ if (true) {
+ doSomething();
+ } else if (false) {
+ doSomethingElse();
+ }
+
+ if (1 == 1) {
+ }
+ // comment
+ if (2 == 2) {
+ }
+ // comment
+
+ if (3 == 3) {
+ }
+
+ // comment
+ if (4 == 4) {
+ }
+ // double comment
+
+ // double comment
+ if (5 == 5) {
+ }
+
+ // double comment
+
+ // double comment
+
+ if (6 == 6) {
+ }
+ if (a)
+ instruction();
+ if (11 == 11) {
+ }
+ /* comment */
+ if (22 == 22) {
+ }
+ /* comment */
+
+ if (33 == 33) {
+ }
+
+ /* comment */
+ if (44 == 44) {
+ }
+ /* double comment */
+
+ /* double comment */
+ if (55 == 55) {
+ }
+
+ /* double comment */
+
+ /* double comment */
+
+ if (66 == 66) {
+ }
+ for (String s : Arrays.asList(""))
+ doSomething(s);
+ for (int i = 0; i < 10; i++) {
+ aaa();
+ switch (i) {
+ case 1:
+ System.out.println("one");
+ break;
+ default:
+ System.out.println("no one");
+ break;
+ }
+ aaa();
+ }
+ aaa();
+ for (String s : Collections.emptyList()) {
+ aaa();
+ try {
+ doSomething(s);
+ } catch (IOException e) {
+ handleError(e);
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ cleanup();
+ }
+ aaa();
+ }
+ aaa();
+ 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/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java
index c505f508e3..19b427e3d2 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
@@ -625,6 +625,30 @@ public class DefaultCodeFormatterConstants {
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 statement containing a code block
+ * - option id: "org.eclipse.jdt.core.formatter.number_of_blank_lines_before_code_block"
+ * - possible values: "&lt;n&gt;", 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_CODE_BLOCK = JavaCore.PLUGIN_ID + ".formatter.number_of_blank_lines_before_code_block"; //$NON-NLS-1$
+ /**
+ * <pre>
+ * FORMATTER / Option to add or remove blank lines after a statement containing a code block
+ * - option id: "org.eclipse.jdt.core.formatter.number_of_blank_lines_after_code_block"
+ * - possible values: "&lt;n&gt;", 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_AFTER_CODE_BLOCK = JavaCore.PLUGIN_ID + ".formatter.number_of_blank_lines_after_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: "&lt;n&gt;", 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 6e3b0e1c3f..9496be12b9 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
@@ -443,16 +443,22 @@ public class CommentsPreparator extends ASTVisitor {
}
if (existingBreaksBefore < existingBreaksAfter && previous != null) {
- commentToken.putLineBreaksAfter(previous.getLineBreaksAfter());
- commentToken.setPreserveLineBreaksAfter(previous.isPreserveLineBreaksAfter());
- previous.clearLineBreaksAfter();
- previous.setPreserveLineBreaksAfter(true);
+ if (previous.isPreserveLineBreaksAfter() || existingBreaksAfter < 2 || next == null
+ || (next.tokenType != TokenNameCOMMENT_LINE && next.tokenType != TokenNameCOMMENT_BLOCK)) {
+ commentToken.putLineBreaksAfter(previous.getLineBreaksAfter());
+ commentToken.setPreserveLineBreaksAfter(previous.isPreserveLineBreaksAfter());
+ previous.clearLineBreaksAfter();
+ 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);
+ if (next.isPreserveLineBreaksBefore() || existingBreaksBefore < 2 || previous == null
+ || (previous.tokenType != TokenNameCOMMENT_LINE && previous.tokenType != TokenNameCOMMENT_BLOCK)) {
+ 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 e1dfa402c5..84de5d1407 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
@@ -206,6 +206,8 @@ public class DefaultCodeFormatterOptions {
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 int blank_lines_before_code_block;
+ public int blank_lines_after_code_block;
public boolean comment_clear_blank_lines_in_javadoc_comment;
public boolean comment_clear_blank_lines_in_block_comment;
@@ -617,6 +619,8 @@ public class DefaultCodeFormatterOptions {
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_BLANK_LINES_BEFORE_CODE_BLOCK, Integer.toString(this.blank_lines_before_code_block));
+ options.put(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_AFTER_CODE_BLOCK, Integer.toString(this.blank_lines_after_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);
@@ -1389,6 +1393,10 @@ public class DefaultCodeFormatterOptions {
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);
+ setInt(settings, DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_BEFORE_CODE_BLOCK,
+ v -> this.blank_lines_before_code_block = v);
+ setInt(settings, DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_AFTER_CODE_BLOCK,
+ v -> this.blank_lines_after_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);
@@ -2833,6 +2841,8 @@ public class DefaultCodeFormatterOptions {
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.blank_lines_before_code_block = 0;
+ this.blank_lines_after_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;
@@ -3193,6 +3203,8 @@ public class DefaultCodeFormatterOptions {
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.blank_lines_before_code_block = 0;
+ this.blank_lines_after_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 59c36282c1..2f6574fc96 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
@@ -332,9 +332,25 @@ public class LineBreaksPreparator extends ASTVisitor {
this.options.blank_lines_at_beginning_of_code_block);
putBlankLinesBeforeCloseBrace(node, this.options.blank_lines_at_end_of_code_block);
+ if (parent instanceof Block) {
+ blankLinesAroundBlock(node, ((Block) parent).statements());
+ } else if (parent instanceof Statement && parent.getParent() instanceof Block) {
+ blankLinesAroundBlock(parent, ((Block) parent.getParent()).statements());
+ }
+
return true;
}
+ private void blankLinesAroundBlock(ASTNode blockStatement, List<ASTNode> siblings) {
+ putBlankLinesBefore(blockStatement, this.options.blank_lines_before_code_block);
+ if (!this.options.put_empty_statement_on_new_line) {
+ int blockIndex = siblings.indexOf(blockStatement);
+ if (blockIndex + 1 < siblings.size() && siblings.get(blockIndex + 1) instanceof EmptyStatement)
+ return;
+ }
+ putBlankLinesAfter(this.tm.lastTokenIn(blockStatement, -1), this.options.blank_lines_after_code_block);
+ }
+
@Override
public boolean visit(SwitchStatement node) {
handleBracedCode(node, node.getExpression(), this.options.brace_position_for_switch,
@@ -386,6 +402,8 @@ public class LineBreaksPreparator extends ASTVisitor {
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);
+ if (node.getParent() instanceof Block)
+ blankLinesAroundBlock(node, ((Block) node.getParent()).statements());
return true;
}

Back to the top