Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSarika Sinha2019-04-17 15:41:59 -0400
committerSarika Sinha2019-04-30 01:21:00 -0400
commit370706729e5939ce2b98d8283385374cd4917cfd (patch)
tree8ddca6403114c8dadb2d20c3d07aa653940b93f7
parent0af93e735d1c71697728854a488fb34b4c2d280b (diff)
downloadeclipse.jdt.core-370706729e5939ce2b98d8283385374cd4917cfd.tar.gz
eclipse.jdt.core-370706729e5939ce2b98d8283385374cd4917cfd.tar.xz
eclipse.jdt.core-370706729e5939ce2b98d8283385374cd4917cfd.zip
Bug 546146 - [12] AST Rewrite should use current formatter settingsI20190501-0525I20190430-1800
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java43
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java4
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java67
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java4
4 files changed, 83 insertions, 35 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java
index 1303ab2207..f10badf274 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java
@@ -63,11 +63,11 @@ public class ASTRewritingSwitchExpressionsTest extends ASTRewritingTest {
buf.append(" switch (i) {\n");
buf.append(" }\n");
buf.append(" switch (i) {\n");
- buf.append(" case 1, 2->\n");
+ buf.append(" case 1, 2 ->\n");
buf.append(" i= 1;\n");
- buf.append(" case 3->\n");
+ buf.append(" case 3 -> \n");
buf.append(" i= 3;\n");
- buf.append(" default->\n");
+ buf.append(" default -> \n");
buf.append(" i= 4;\n");
buf.append(" }\n");
buf.append(" }\n");
@@ -143,14 +143,13 @@ public class ASTRewritingSwitchExpressionsTest extends ASTRewritingTest {
buf.append("public class E {\n");
buf.append(" public void foo(int i) {\n");
buf.append(" switch (x) {\n");
- buf.append(" case 1, 2->\n");
- buf.append(" return;\n");
- buf.append(" default->\n");
+ buf.append(" case 1, 2 -> return;\n");
+ buf.append(" default ->\n");
buf.append(" }\n");
buf.append(" switch (i) {\n");
- buf.append(" case 10, 3, 12->\n");
+ buf.append(" case 10, 3, 12 -> \n");
buf.append(" i= 3;\n");
- buf.append(" default->\n");
+ buf.append(" default -> \n");
buf.append(" i= 4;\n");
buf.append(" }\n");
buf.append(" }\n");
@@ -165,12 +164,9 @@ public class ASTRewritingSwitchExpressionsTest extends ASTRewritingTest {
buf.append("public class E {\n");
buf.append(" public void foo(int i) {\n");
buf.append(" switch (i) {\n");
- buf.append(" case 1, 2->\n");
- buf.append(" i= 1;\n");
- buf.append(" case 3->\n");
- buf.append(" i= 3;\n");
- buf.append(" default->\n");
- buf.append(" i= 4;\n");
+ buf.append(" case 1, 2 -> i= 1;\n");
+ buf.append(" case 3 -> i= 3;\n");
+ buf.append(" default -> i= 4;\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
@@ -209,14 +205,10 @@ public class ASTRewritingSwitchExpressionsTest extends ASTRewritingTest {
buf.append("public class E {\n");
buf.append(" public void foo(int i) {\n");
buf.append(" switch (i) {\n");
- buf.append(" case 1, 2->\n");
- buf.append(" i= 1;\n");
- buf.append(" case 3->\n");
- buf.append(" i= 3;\n");
- buf.append(" case 1024->\n");
- buf.append(" break 2048;\n");
- buf.append(" default->\n");
- buf.append(" i= 4;\n");
+ buf.append(" case 1, 2 -> i= 1;\n");
+ buf.append(" case 3 -> i= 3;\n");
+ buf.append(" case 1024 -> break 2048;\n");
+ buf.append(" default -> i= 4;\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
@@ -295,10 +287,9 @@ public class ASTRewritingSwitchExpressionsTest extends ASTRewritingTest {
buf.append(" int z = 100;\n");
buf.append(" break z;\n");
buf.append(" }\n");
- buf.append(" case 100, 200->\n");
- buf.append(" {\n");
- buf.append(" break 2048;\n");
- buf.append(" }\n");
+ buf.append(" case 100, 200 -> {\n");
+ buf.append(" break 2048;\n");
+ buf.append("}\n");
buf.append(" default -> {\n");
buf.append(" break 12;\n");
buf.append(" }\n");
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java
index e59d7e91c6..028519f632 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java
@@ -1509,14 +1509,14 @@ public class NaiveASTFlattener extends ASTVisitor {
if (node.getAST().apiLevel() >= JLS12) {
if (node.isDefault()) {
this.buffer.append("default");//$NON-NLS-1$
- this.buffer.append(node.isSwitchLabeledRule() ? "->" : ":");//$NON-NLS-1$ //$NON-NLS-2$
+ this.buffer.append(node.isSwitchLabeledRule() ? " ->" : ":");//$NON-NLS-1$ //$NON-NLS-2$
} else {
this.buffer.append("case ");//$NON-NLS-1$
for (Iterator it = node.expressions().iterator(); it.hasNext(); ) {
Expression t = (Expression) it.next();
t.accept(this);
this.buffer.append(it.hasNext() ? ", " : //$NON-NLS-1$
- node.isSwitchLabeledRule() ? "->" : ":");//$NON-NLS-1$ //$NON-NLS-2$
+ node.isSwitchLabeledRule() ? " ->" : ":");//$NON-NLS-1$ //$NON-NLS-2$
}
}
}
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java
index 095bdacc70..ba2e0eeab0 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java
@@ -22,6 +22,7 @@ import java.util.Stack;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.*;
import org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer;
import org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange;
@@ -1130,7 +1131,7 @@ public final class ASTRewriteAnalyzer extends ASTVisitor {
return buf.toString();
}
- private ASTNode getNode(int nodeIndex) {
+ protected ASTNode getNode(int nodeIndex) {
ASTNode elem= (ASTNode) this.list[nodeIndex].getOriginalValue();
if (elem == null) {
elem= (ASTNode) this.list[nodeIndex].getNewValue();
@@ -3546,9 +3547,61 @@ public final class ASTRewriteAnalyzer extends ASTVisitor {
return false;
}
+ class SwitchListLabeledRuleRewriter extends SwitchListRewriter {
+
+ public SwitchListLabeledRuleRewriter(int initialIndent) {
+ super(initialIndent);
+ }
+
+ @Override
+ protected String getSeparatorString(int nodeIndex, int nextNodeIndex) {
+ boolean isSwitchLabelRule = isSwitchLabeledRule(nodeIndex, nextNodeIndex);
+ String spaceDelim = JavaCore.INSERT.equals(ASTRewriteAnalyzer.this.options.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_ARROW_IN_SWITCH_CASE))? " ":""; //$NON-NLS-1$ //$NON-NLS-2$
+ String lineDelim = isSwitchLabelRule ? spaceDelim : getLineDelimiter();
+
+ StringBuffer buf= new StringBuffer(lineDelim);
+ buf.append(createIndentString(getNodeIndent(nextNodeIndex)));
+ return buf.toString();
+ }
+
+ @Override
+ protected int getNodeIndent(int nodeIndex) {
+ int indent= getInitialIndent();
+ if (this.indentSwitchStatementsCompareToCases) {
+ RewriteEvent event = this.list[nodeIndex];
+ int changeKind = event.getChangeKind();
+ ASTNode node;
+ if (changeKind == RewriteEvent.INSERTED || changeKind == RewriteEvent.REPLACED) {
+ node= (ASTNode)event.getNewValue();
+ } else {
+ node= (ASTNode)event.getOriginalValue();
+ }
+ if (node.getNodeType() != ASTNode.SWITCH_CASE) {
+ ASTNode prevNode = getNode(nodeIndex -1);
+ if (prevNode.getNodeType() == ASTNode.SWITCH_CASE && ((SwitchCase)prevNode).isSwitchLabeledRule()) {
+ return 0;
+ } else {
+ indent++;
+ }
+ }
+ }
+ return indent;
+ }
+
+ private boolean isSwitchLabeledRule(int nodeIndex, int nextNodeIndex) {
+ ASTNode curr= getNode(nodeIndex);
+ ASTNode next= getNode(nodeIndex +1);
+ int currKind= curr.getNodeType();
+ if (currKind == ASTNode.SWITCH_CASE && next instanceof Statement && ((SwitchCase)curr).isSwitchLabeledRule()) {
+ return true;
+ }
+ return false;
+ }
+ }
+
class SwitchListRewriter extends ParagraphListRewriter {
- private boolean indentSwitchStatementsCompareToCases;
+ protected boolean indentSwitchStatementsCompareToCases;
public SwitchListRewriter(int initialIndent) {
super(initialIndent, 0);
@@ -3657,7 +3710,7 @@ public final class ASTRewriteAnalyzer extends ASTVisitor {
insertIndent++;
}
- ParagraphListRewriter listRewriter= new SwitchListRewriter(insertIndent);
+ ParagraphListRewriter listRewriter= new SwitchListLabeledRuleRewriter(insertIndent);
StringBuffer leadString= new StringBuffer();
leadString.append(getLineDelimiter());
leadString.append(createIndentString(insertIndent));
@@ -3687,8 +3740,12 @@ public final class ASTRewriteAnalyzer extends ASTVisitor {
if (DefaultCodeFormatterConstants.TRUE.equals(this.options.get(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH))) {
insertIndent++;
}
-
- ParagraphListRewriter listRewriter= new SwitchListRewriter(insertIndent);
+ ParagraphListRewriter listRewriter;
+ if (node.getAST().apiLevel() >= JLS12_INTERNAL) {
+ listRewriter= new SwitchListLabeledRuleRewriter(insertIndent);
+ } else {
+ listRewriter= new SwitchListRewriter(insertIndent);
+ }
StringBuffer leadString= new StringBuffer();
leadString.append(getLineDelimiter());
leadString.append(createIndentString(insertIndent));
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java
index a9a3c8a959..e8590da9a2 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java
@@ -986,7 +986,7 @@ public class ASTRewriteFlattener extends ASTVisitor {
if (node.getAST().apiLevel() >= JLS12) {
if (node.isDefault()) {
this.result.append("default");//$NON-NLS-1$
- this.result.append(getBooleanAttribute(node, SwitchCase.SWITCH_LABELED_RULE_PROPERTY) ? "->" : ":");//$NON-NLS-1$ //$NON-NLS-2$
+ this.result.append(getBooleanAttribute(node, SwitchCase.SWITCH_LABELED_RULE_PROPERTY) ? " ->" : ":");//$NON-NLS-1$ //$NON-NLS-2$
} else {
this.result.append("case ");//$NON-NLS-1$
for (Iterator it = node.expressions().iterator(); it.hasNext(); ) {
@@ -994,7 +994,7 @@ public class ASTRewriteFlattener extends ASTVisitor {
t.accept(this);
this.result.append(it.hasNext() ? ", " : ""); //$NON-NLS-1$ //$NON-NLS-2$
}
- this.result.append(getBooleanAttribute(node, SwitchCase.SWITCH_LABELED_RULE_PROPERTY) ? "->" : ":");//$NON-NLS-1$ //$NON-NLS-2$
+ this.result.append(getBooleanAttribute(node, SwitchCase.SWITCH_LABELED_RULE_PROPERTY) ? " ->" : ":");//$NON-NLS-1$ //$NON-NLS-2$
}
} else {
ASTNode expression= getChildNode(node, INTERNAL_SWITCH_EXPRESSION_PROPERTY);

Back to the top