Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMateusz Matela2015-08-03 22:41:04 +0000
committerMateusz Matela2015-08-14 17:51:31 +0000
commit17a84d4eab07595592188d8d0f5bdd5566f79b61 (patch)
treeffb522d198ed81a2f2d74e3b4460f0fc093e614a
parent2275b76ca4af9ecbe70f599ff0868ef689d9003e (diff)
downloadeclipse.jdt.core-17a84d4eab07595592188d8d0f5bdd5566f79b61.tar.gz
eclipse.jdt.core-17a84d4eab07595592188d8d0f5bdd5566f79b61.tar.xz
eclipse.jdt.core-17a84d4eab07595592188d8d0f5bdd5566f79b61.zip
fixed bug 470506: formatter option "align field in columns" changed in Mars
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java17
-rw-r--r--org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/FieldAligner.java33
-rw-r--r--org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java2
3 files changed, 23 insertions, 29 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 2cb1dc8f01..c8869e3f4e 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
@@ -11066,4 +11066,21 @@ public void testBug470977() throws Exception {
"}";
formatSource(source);
}
+/**
+ * @bug 470506: formatter option "align field in columns" changed in Mars
+ * @test test that fields separated by extra blank lines are not considered separate groups when aligning
+ * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=470506"
+ */
+public void testBug470506() {
+ this.formatterPrefs.align_type_members_on_columns = true;
+ String source =
+ "class C {\r\n" +
+ " private int iii;\r\n" +
+ " String sss;\r\n" +
+ "\r\n" +
+ " protected ArrayList<Integer> aaa;\r\n" +
+ "\r\n" +
+ "}";
+ formatSource(source);
+}
}
diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/FieldAligner.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/FieldAligner.java
index 8321a34ae6..f71135c401 100644
--- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/FieldAligner.java
+++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/FieldAligner.java
@@ -23,7 +23,6 @@ import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
-import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions;
import org.eclipse.jdt.internal.formatter.Token;
import org.eclipse.jdt.internal.formatter.TokenManager;
import org.eclipse.jdt.internal.formatter.TokenTraverser;
@@ -62,56 +61,34 @@ public class FieldAligner {
}
}
- private final List<List<FieldDeclaration>> fieldAlignGroups = new ArrayList<List<FieldDeclaration>>();
+ private final List<List<FieldDeclaration>> fieldAlignGroups = new ArrayList<>();
final TokenManager tm;
- private final DefaultCodeFormatterOptions options;
-
- public FieldAligner(TokenManager tokenManager, DefaultCodeFormatterOptions options) {
+ public FieldAligner(TokenManager tokenManager) {
this.tm = tokenManager;
- this.options = options;
}
public void prepareAlign(TypeDeclaration node) {
List<FieldDeclaration> bodyDeclarations = node.bodyDeclarations();
- ArrayList<FieldDeclaration> alignGroup = new ArrayList<FieldDeclaration>();
- BodyDeclaration previous = null;
+ ArrayList<FieldDeclaration> alignGroup = new ArrayList<>();
for (BodyDeclaration declaration : bodyDeclarations) {
if (!alignGroup.isEmpty()) {
- if ((declaration instanceof FieldDeclaration) && !areSeparated(previous, declaration)) {
+ if ((declaration instanceof FieldDeclaration)) {
alignGroup.add((FieldDeclaration) declaration);
} else {
alignFields(alignGroup);
- alignGroup = new ArrayList<FieldDeclaration>();
+ alignGroup = new ArrayList<>();
}
}
if (alignGroup.isEmpty()) {
if (declaration instanceof FieldDeclaration)
alignGroup.add((FieldDeclaration) declaration);
}
- previous = declaration;
}
alignFields(alignGroup);
}
- private boolean areSeparated(BodyDeclaration declaration1, BodyDeclaration declaration2) {
- // check if there are more empty lines between fields than normal
- if (this.options.number_of_empty_lines_to_preserve <= this.options.blank_lines_before_field)
- return false;
- int maxLineBreaks = 0;
- int from = this.tm.lastIndexIn(declaration1, -1);
- int to = this.tm.firstIndexIn(declaration2, -1);
-
- Token previous = this.tm.get(from);
- for (int i = from + 1; i <= to; i++) {
- Token token = this.tm.get(i);
- maxLineBreaks = Math.max(maxLineBreaks, this.tm.countLineBreaksBetween(previous, token));
- previous = token;
- }
- return maxLineBreaks - 1 > this.options.blank_lines_before_field;
- }
-
private void alignFields(ArrayList<FieldDeclaration> alignGroup) {
if (alignGroup.size() < 2)
return;
diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java
index 259b3e6594..2e1362c577 100644
--- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java
+++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java
@@ -182,7 +182,7 @@ public class WrapPreparator extends ASTVisitor {
if (this.options.align_type_members_on_columns) {
if (this.fieldAligner == null) {
- this.fieldAligner = new FieldAligner(this.tm, this.options);
+ this.fieldAligner = new FieldAligner(this.tm);
}
this.fieldAligner.prepareAlign(node);
}

Back to the top