Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMateusz Matela2019-08-18 21:09:51 +0000
committerMateusz Matela2019-08-18 21:18:38 +0000
commitb7cb75ad308cdfd0a16f029d800d9cc0a3c5246e (patch)
tree1204b27b8ba2a798519beeab6d98a2f7d64aeedb
parent8ec6a672dc17ba336bebe0f69f57be0365408647 (diff)
downloadeclipse.jdt.core-b7cb75ad308cdfd0a16f029d800d9cc0a3c5246e.tar.gz
eclipse.jdt.core-b7cb75ad308cdfd0a16f029d800d9cc0a3c5246e.tar.xz
eclipse.jdt.core-b7cb75ad308cdfd0a16f029d800d9cc0a3c5246e.zip
Bug 413193 - [formatter] Blank lines before first declaration and
declarations of same kind not respected in enums Change-Id: Ib6d8219b13a54165d073dc573ec684d9c0e53f3f 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/FormatterBugsTests.java66
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java6
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Formatter/test169131/B_out.java2
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/A_out.java2
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/B_out.java2
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/C_out.java2
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/D_out.java2
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/E_out.java2
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/F_out.java2
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/G_out.java2
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/H_out.java1
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/I_out.java2
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Formatter/test530/A_out.java1
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/Formatter/test626/A_out.java1
-rw-r--r--org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/LineBreaksPreparator.java132
15 files changed, 157 insertions, 68 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 ff4c5f1915..305d9b3b01 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
@@ -11324,6 +11324,7 @@ public void testBug474918() {
"\r\n" +
" private enum E {\r\n" +
" AAA, BBB;\r\n" +
+ "\r\n" +
" int a = 55;\r\n" +
" String sssss = \"ssssss\";\r\n" +
" }\r\n" +
@@ -11390,6 +11391,7 @@ public void testBug474918b() {
"\r\n" +
" private enum E {\r\n" +
" AAA, BBB;\r\n" +
+ "\r\n" +
" int a = 55;\r\n" +
" String sssss = \"ssssss\";\r\n" +
" }\r\n" +
@@ -11456,6 +11458,7 @@ public void testBug474918c() {
"\r\n" +
" private enum E {\r\n" +
" AAA, BBB;\r\n" +
+ "\r\n" +
" int a = 55;\r\n" +
" String sssss = \"ssssss\";\r\n" +
" }\r\n" +
@@ -12592,6 +12595,7 @@ public void testBug500096a() {
String source =
"public enum Test {\n" +
"AAA, BBB;\n" +
+ "\n" +
"Test() {\n" +
"}\n" +
"}";
@@ -12606,6 +12610,7 @@ public void testBug500096b() {
String source =
"public enum Test {\n" +
" AAA, BBB;\n" +
+ "\n" +
"Test() {\n" +
"}\n" +
"}";
@@ -12956,4 +12961,65 @@ public void testBug543780() {
"}";
formatSource(source);
}
+/**
+ * https://bugs.eclipse.org/413193 - [formatter] Blank lines before the first declarations and declarations of same kind not respected in enums
+ */
+public void testBug413193a() {
+ this.formatterPrefs.blank_lines_before_first_class_body_declaration = 2;
+ this.formatterPrefs.blank_lines_after_last_class_body_declaration = 3;
+ this.formatterPrefs.blank_lines_before_new_chunk = 4;
+ formatSource(
+ "public enum TestEnum {\n" +
+ "\n" +
+ "\n" +
+ " ONE, TWO, THREE;\n" +
+ "\n" +
+ "\n" +
+ "\n" +
+ "\n" +
+ " public int foo() {\n" +
+ " return 0;\n" +
+ " }\n" +
+ "\n" +
+ "\n" +
+ "\n" +
+ "}");
+}
+/**
+ * https://bugs.eclipse.org/413193 - [formatter] Blank lines before the first declarations and declarations of same kind not respected in enums
+ */
+public void testBug413193b() {
+ this.formatterPrefs.blank_lines_before_first_class_body_declaration = 2;
+ this.formatterPrefs.blank_lines_after_last_class_body_declaration = 3;
+ this.formatterPrefs.blank_lines_before_new_chunk = 4;
+ formatSource(
+ "public enum TestEnum {\n" +
+ " ONE, TWO, THREE;\n" +
+ "}");
+}
+/**
+ * https://bugs.eclipse.org/413193 - [formatter] Blank lines before the first declarations and declarations of same kind not respected in enums
+ */
+public void testBug413193c() {
+ this.formatterPrefs.blank_lines_before_first_class_body_declaration = ~0;
+ this.formatterPrefs.blank_lines_after_last_class_body_declaration = ~0;
+ this.formatterPrefs.blank_lines_before_new_chunk = ~0;
+ String source =
+ "public enum TestEnum {\n" +
+ "\n" +
+ " ONE, TWO, THREE;\n" +
+ "\n" +
+ " public int foo() {\n" +
+ " return 0;\n" +
+ " }\n" +
+ "\n" +
+ "}";
+ formatSource(source,
+ "public enum TestEnum {\n" +
+ " ONE, TWO, THREE;\n" +
+ " public int foo() {\n" +
+ " return 0;\n" +
+ " }\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 266b0ee01f..71ad555547 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
@@ -10947,6 +10947,7 @@ public void test730() {
"enum Fail1 {\n" +
" A;\n" +
" ;\n" +
+ "\n" +
" {\n" +
" }\n" +
"}"
@@ -10963,6 +10964,7 @@ public void test731() {
"enum Fail2 {\n" +
" A, B;\n" +
" ;\n" +
+ "\n" +
" {\n" +
" }\n" +
"}"
@@ -10979,6 +10981,7 @@ public void test732() {
"enum Fail3 {\n" +
" A;\n" +
" ;\n" +
+ "\n" +
" public void foo() {\n" +
" }\n" +
"}"
@@ -10995,6 +10998,7 @@ public void test733() {
"enum Fail4 {\n" +
" A;\n" +
" ;\n" +
+ "\n" +
" public int i = 0;\n" +
"}"
);
@@ -15086,6 +15090,7 @@ 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;
+ this.formatterPrefs.blank_lines_before_new_chunk = 0;
String input = getCompilationUnit("Formatter", "", "test522089", "in.java").getSource();
formatSource(input, getCompilationUnit("Formatter", "", "test522089", "A_out.java").getSource());
}
@@ -15096,6 +15101,7 @@ 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;
+ this.formatterPrefs.blank_lines_before_new_chunk = 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/test169131/B_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test169131/B_out.java
index 9f2fe319ed..641d726a05 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
@@ -70,6 +70,8 @@ class C5b {
enum Enum {
A, B;
+
+
}
enum Enum2 {
diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/A_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/A_out.java
index 89aa366a81..dac90dcbdd 100644
--- a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/A_out.java
+++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/A_out.java
@@ -123,6 +123,7 @@ public enum TinyEnum {
public enum SmallEnum {
VALUE(0);
+
SmallEnum(int val) {
};
}
@@ -142,6 +143,7 @@ public enum EnumConstants {
return 3;
}
};
+
int getVal() {
return 1;
}
diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/B_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/B_out.java
index 4c112b86ab..0bd3177efd 100644
--- a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/B_out.java
+++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/B_out.java
@@ -114,6 +114,7 @@ public enum TinyEnum {
public enum SmallEnum {
VALUE(0);
+
SmallEnum(int val) {
};
}
@@ -128,6 +129,7 @@ public enum EnumConstants {
int getVal() { return 3; }
};
+
int getVal() { return 1; }
}
diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/C_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/C_out.java
index ec05534b58..dd146bf3d3 100644
--- a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/C_out.java
+++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/C_out.java
@@ -109,6 +109,7 @@ public enum TinyEnum { A; }
public enum SmallEnum {
VALUE(0);
+
SmallEnum(int val) {};
}
@@ -127,6 +128,7 @@ public enum EnumConstants {
return 3;
}
};
+
int getVal() {
return 1;
}
diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/D_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/D_out.java
index a1e2cca718..3b4bfcb70a 100644
--- a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/D_out.java
+++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/D_out.java
@@ -111,6 +111,7 @@ public enum TinyEnum {
public enum SmallEnum {
VALUE(0);
+
SmallEnum(int val) {
};
}
@@ -129,6 +130,7 @@ public enum EnumConstants {
return 3;
}
};
+
int getVal() {
return 1;
}
diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/E_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/E_out.java
index 60cf56fc6a..70fabb789d 100644
--- a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/E_out.java
+++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/E_out.java
@@ -110,6 +110,7 @@ public enum TinyEnum {
public enum SmallEnum {
VALUE(0);
+
SmallEnum(int val) {};
}
@@ -117,6 +118,7 @@ public enum EnumConstants {
EMPTY {},
TINY { int getVal() { return 2; } },
SMALL { int val = 3; int getVal() { return 3; } };
+
int getVal() { return 1; }
}
diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/F_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/F_out.java
index 012f0e3bb1..6c0378f938 100644
--- a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/F_out.java
+++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/F_out.java
@@ -105,6 +105,7 @@ public enum TinyEnum { A; }
public enum SmallEnum {
VALUE(0);
+
SmallEnum(int val) {
};
}
@@ -123,6 +124,7 @@ public enum EnumConstants {
return 3;
}
};
+
int getVal() {
return 1;
}
diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/G_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/G_out.java
index fc1e78cfd5..be297bf3d3 100644
--- a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/G_out.java
+++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/G_out.java
@@ -91,6 +91,7 @@ public enum TinyEnum { A; }
public enum SmallEnum {
VALUE(0);
+
SmallEnum(int val) {};
}
@@ -105,6 +106,7 @@ public enum EnumConstants {
int getVal() { return 3; }
};
+
int getVal() { return 1; }
}
diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/H_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/H_out.java
index 9eba7cd481..0243d335e7 100644
--- a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/H_out.java
+++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/H_out.java
@@ -97,6 +97,7 @@ public enum EnumConstants {
},
TINY { int getVal() { return 2; } },
SMALL { int val = 3; int getVal() { return 3; } };
+
int getVal() { return 1; }
}
diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/I_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/I_out.java
index 82d5839371..f5ddfdcd5b 100644
--- a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/I_out.java
+++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test205973/I_out.java
@@ -100,6 +100,7 @@ public enum TinyEnum { A; }
public enum SmallEnum {
VALUE(0);
+
SmallEnum(int val) {};
}
@@ -111,6 +112,7 @@ public enum EnumConstants {
int getVal() { return 3; }
};
+
int getVal() { return 1; }
}
diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test530/A_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test530/A_out.java
index 9448cebced..9f583dc50d 100644
--- a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test530/A_out.java
+++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test530/A_out.java
@@ -2,6 +2,7 @@ package test530;
public enum A {
TEST;
+
public String field;
public void foo() {
diff --git a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test626/A_out.java b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test626/A_out.java
index 9dd9296e3c..9506e64265 100644
--- a/org.eclipse.jdt.core.tests.model/workspace/Formatter/test626/A_out.java
+++ b/org.eclipse.jdt.core.tests.model/workspace/Formatter/test626/A_out.java
@@ -1,5 +1,6 @@
public enum Enum_16 {
ONE, TWO, THREE;
+
String foo(String str) {
return "";
}
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 231cf3a86f..e4767e48db 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
@@ -26,6 +26,7 @@ import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNamee
import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNamefinally;
import static org.eclipse.jdt.internal.compiler.parser.TerminalTokens.TokenNamewhile;
+import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.dom.ASTNode;
@@ -150,8 +151,6 @@ public class LineBreaksPreparator extends ASTVisitor {
handleBracedCode(node, node.getName(), this.options.brace_position_for_type_declaration,
this.options.indent_body_declarations_compare_to_type_header);
- if (!node.bodyDeclarations().isEmpty())
- putBlankLinesBeforeCloseBrace(node, this.options.blank_lines_after_last_class_body_declaration);
this.declarationModifierVisited = false;
return true;
@@ -180,6 +179,11 @@ public class LineBreaksPreparator extends ASTVisitor {
putBlankLinesBefore(bodyDeclaration, blankLines);
previous = bodyDeclaration;
}
+ if (previous != null) {
+ Token lastToken = this.tm.lastTokenIn(previous.getParent(), -1);
+ if (lastToken.tokenType == TokenNameRBRACE) // otherwise it's a fake type
+ putBlankLinesBefore(lastToken, this.options.blank_lines_after_last_class_body_declaration);
+ }
}
private boolean sameChunk(BodyDeclaration bd1, BodyDeclaration bd2) {
@@ -193,42 +197,21 @@ public class LineBreaksPreparator extends ASTVisitor {
return false;
}
- private void putBlankLinesBefore(ASTNode node, int linesCount) {
- 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) {
- token.putLineBreaksBefore(linesCount + 1);
- } else {
- token.putLineBreaksBefore(~linesCount + 1);
- token.setPreserveLineBreaksBefore(false);
- }
- }
-
- private void putBlankLinesAfter(Token token, int linesCount) {
- if (linesCount >= 0) {
- token.putLineBreaksAfter(linesCount + 1);
- } else {
- token.putLineBreaksAfter(~linesCount + 1);
- token.setPreserveLineBreaksAfter(false);
- }
- }
-
- private void putBlankLinesBeforeCloseBrace(ASTNode node, int blankLinesSetting) {
- putBlankLinesBefore(this.tm.lastTokenIn(node, TokenNameRBRACE), blankLinesSetting);
- }
-
@Override
public boolean visit(EnumDeclaration node) {
handleBracedCode(node, node.getName(), this.options.brace_position_for_enum_declaration,
this.options.indent_body_declarations_compare_to_enum_declaration_header);
- handleBodyDeclarations(node.bodyDeclarations());
+ List<BodyDeclaration> declarations = node.bodyDeclarations();
List<EnumConstantDeclaration> enumConstants = node.enumConstants();
+ if (!declarations.isEmpty()) {
+ if (!enumConstants.isEmpty()) {
+ declarations = new ArrayList<>(declarations);
+ declarations.add(0, enumConstants.get(0));
+ }
+ handleBodyDeclarations(declarations);
+ }
+
for (int i = 0; i < enumConstants.size(); i++) {
EnumConstantDeclaration declaration = enumConstants.get(i);
if (declaration.getJavadoc() != null)
@@ -250,9 +233,6 @@ public class LineBreaksPreparator extends ASTVisitor {
break;
}
- if (!enumConstants.isEmpty() || !node.bodyDeclarations().isEmpty())
- putBlankLinesBeforeCloseBrace(node, this.options.blank_lines_after_last_class_body_declaration);
-
this.declarationModifierVisited = false;
return true;
}
@@ -265,8 +245,6 @@ public class LineBreaksPreparator extends ASTVisitor {
handleBodyDeclarations(node.bodyDeclarations());
if (node.getModifiers() == 0)
this.tm.firstTokenBefore(node.getName(), TokenNameAT).breakBefore();
- if (!node.bodyDeclarations().isEmpty())
- putBlankLinesBeforeCloseBrace(node, this.options.blank_lines_after_last_class_body_declaration);
this.declarationModifierVisited = false;
return true;
@@ -282,8 +260,6 @@ public class LineBreaksPreparator extends ASTVisitor {
this.options.indent_body_declarations_compare_to_type_header);
}
handleBodyDeclarations(node.bodyDeclarations());
- if (!node.bodyDeclarations().isEmpty())
- putBlankLinesBeforeCloseBrace(node, this.options.blank_lines_after_last_class_body_declaration);
return true;
}
@@ -295,11 +271,8 @@ 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);
-
- 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);
+ handleBracedCode(node.getBody(), null, bracePosition, this.options.indent_statements_compare_to_body,
+ this.options.blank_lines_at_beginning_of_method_body, this.options.blank_lines_at_end_of_method_body);
return true;
}
@@ -327,11 +300,8 @@ public class LineBreaksPreparator extends ASTVisitor {
} else if (parent instanceof LambdaExpression) {
bracePosition = this.options.brace_position_for_lambda_body;
}
- 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);
+ handleBracedCode(node, null, bracePosition, this.options.indent_statements_compare_to_block,
+ this.options.blank_lines_at_beginning_of_code_block, this.options.blank_lines_at_end_of_code_block);
if (parent instanceof Block) {
blankLinesAroundBlock(node, ((Block) parent).statements());
@@ -355,15 +325,13 @@ public class LineBreaksPreparator extends ASTVisitor {
@Override
public boolean visit(SwitchStatement node) {
handleBracedCode(node, node.getExpression(), this.options.brace_position_for_switch,
- this.options.indent_switchstatements_compare_to_switch);
+ this.options.indent_switchstatements_compare_to_switch,
+ this.options.blank_lines_at_beginning_of_code_block, this.options.blank_lines_at_end_of_code_block);
List<Statement> statements = node.statements();
doSwitchStatementsIndentation(node, statements);
doSwitchStatementsLineBreaks(statements);
- 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());
@@ -373,16 +341,13 @@ public class LineBreaksPreparator extends ASTVisitor {
@Override
public boolean visit(SwitchExpression node) {
handleBracedCode(node, node.getExpression(), this.options.brace_position_for_switch,
- this.options.indent_switchstatements_compare_to_switch);
+ this.options.indent_switchstatements_compare_to_switch,
+ this.options.blank_lines_at_beginning_of_code_block, this.options.blank_lines_at_end_of_code_block);
List<Statement> statements = node.statements();
doSwitchStatementsIndentation(node, statements);
doSwitchStatementsLineBreaks(statements);
- 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;
}
@@ -664,20 +629,21 @@ public class LineBreaksPreparator extends ASTVisitor {
public boolean visit(ModuleDeclaration node) {
// using settings for type declaration and fields for now, add new settings if necessary
breakLineBefore(node);
+ List<ModuleDirective> statements = node.moduleStatements();
handleBracedCode(node, node.getName(), this.options.brace_position_for_type_declaration,
- this.options.indent_body_declarations_compare_to_type_header);
+ this.options.indent_body_declarations_compare_to_type_header,
+ statements.isEmpty() ? 0 : this.options.blank_lines_before_first_class_body_declaration,
+ statements.isEmpty() ? 0 : this.options.blank_lines_after_last_class_body_declaration);
- List<ModuleDirective> statements = node.moduleStatements();
ModuleDirective previous = null;
for (ModuleDirective statement : statements) {
- int blankLines = previous == null ? this.options.blank_lines_before_first_class_body_declaration
- : previous.getClass().equals(statement.getClass()) ? this.options.blank_lines_before_field
- : this.options.blank_lines_before_new_chunk;
- putBlankLinesBefore(statement, blankLines);
+ if (previous != null) {
+ boolean cameChunk = previous.getClass().equals(statement.getClass());
+ putBlankLinesBefore(statement,
+ cameChunk ? this.options.blank_lines_before_field : this.options.blank_lines_before_new_chunk);
+ }
previous = statement;
}
- if (!statements.isEmpty())
- putBlankLinesBeforeCloseBrace(node, this.options.blank_lines_after_last_class_body_declaration);
this.declarationModifierVisited = false;
return true;
@@ -687,7 +653,37 @@ public class LineBreaksPreparator extends ASTVisitor {
this.tm.firstTokenIn(node, -1).breakBefore();
}
+ private void putBlankLinesBefore(ASTNode node, int linesCount) {
+ 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) {
+ token.putLineBreaksBefore(linesCount + 1);
+ } else {
+ token.putLineBreaksBefore(~linesCount + 1);
+ token.setPreserveLineBreaksBefore(false);
+ }
+ }
+
+ private void putBlankLinesAfter(Token token, int linesCount) {
+ if (linesCount >= 0) {
+ token.putLineBreaksAfter(linesCount + 1);
+ } else {
+ token.putLineBreaksAfter(~linesCount + 1);
+ token.setPreserveLineBreaksAfter(false);
+ }
+ }
+
private void handleBracedCode(ASTNode node, ASTNode nodeBeforeOpenBrace, String bracePosition, boolean indentBody) {
+ handleBracedCode(node, nodeBeforeOpenBrace, bracePosition, indentBody, 0, 0);
+ }
+
+ private void handleBracedCode(ASTNode node, ASTNode nodeBeforeOpenBrace, String bracePosition, boolean indentBody,
+ int blankLinesAfterOpeningBrace, int blankLinesBeforeClosingBrace) {
int openBraceIndex = nodeBeforeOpenBrace == null
? this.tm.firstIndexIn(node, TokenNameLBRACE)
: this.tm.firstIndexAfter(nodeBeforeOpenBrace, TokenNameLBRACE);
@@ -696,8 +692,8 @@ public class LineBreaksPreparator extends ASTVisitor {
Token closeBraceToken = this.tm.get(closeBraceIndex);
handleBracePosition(openBraceToken, closeBraceIndex, bracePosition);
- openBraceToken.breakAfter();
- closeBraceToken.breakBefore();
+ putBlankLinesAfter(openBraceToken, blankLinesAfterOpeningBrace);
+ putBlankLinesBefore(closeBraceToken, blankLinesBeforeClosingBrace);
if (indentBody) {
adjustEmptyLineAfter(openBraceIndex, 1);

Back to the top