Get rid of excessive replaces
This commit is contained in:
+2
-1
@@ -47,6 +47,7 @@ public class BranchedFoldingUtils {
|
||||
}
|
||||
|
||||
if (assignment.getParent() instanceof JetBlockExpression) {
|
||||
//noinspection ConstantConditions
|
||||
return !JetPsiUtil.checkVariableDeclarationInBlock((JetBlockExpression) assignment.getParent(), assignment.getLeft().getText());
|
||||
}
|
||||
|
||||
@@ -239,7 +240,7 @@ public class BranchedFoldingUtils {
|
||||
assertNotNull(elseRoot);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
JetIfExpression newIfExpression = JetPsiFactory.createIf(project, condition, thenRoot, elseRoot, false, false);
|
||||
JetIfExpression newIfExpression = JetPsiFactory.createIf(project, condition, thenRoot, elseRoot);
|
||||
JetReturnExpression newReturnExpression = JetPsiFactory.createReturn(project, newIfExpression);
|
||||
|
||||
newIfExpression = (JetIfExpression)newReturnExpression.getReturnedExpression();
|
||||
|
||||
+5
-4
@@ -35,11 +35,10 @@ public class BranchedUnfoldingUtils {
|
||||
|
||||
if (JetPsiUtil.isAssignment(root)) {
|
||||
JetBinaryExpression assignment = (JetBinaryExpression)root;
|
||||
JetExpression lhs = assignment.getLeft();
|
||||
|
||||
assertNotNull(assignment.getLeft());
|
||||
|
||||
JetExpression rhs = assignment.getRight();
|
||||
|
||||
if (!(lhs instanceof JetSimpleNameExpression)) return null;
|
||||
|
||||
if (rhs instanceof JetIfExpression) return UnfoldableKind.ASSIGNMENT_TO_IF;
|
||||
if (rhs instanceof JetWhenExpression) return UnfoldableKind.ASSIGNMENT_TO_WHEN;
|
||||
} else if (root instanceof JetReturnExpression) {
|
||||
@@ -75,6 +74,7 @@ public class BranchedUnfoldingUtils {
|
||||
assertNotNull(thenExpr);
|
||||
assertNotNull(elseExpr);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
thenExpr.replace(JetPsiFactory.createBinaryExpression(project, lhs, op, thenExpr));
|
||||
elseExpr.replace(JetPsiFactory.createBinaryExpression(project, lhs, op, elseExpr));
|
||||
|
||||
@@ -97,6 +97,7 @@ public class BranchedUnfoldingUtils {
|
||||
|
||||
assertNotNull(currExpr);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
currExpr.replace(JetPsiFactory.createBinaryExpression(project, lhs, op, currExpr));
|
||||
}
|
||||
|
||||
|
||||
-27
@@ -1,27 +0,0 @@
|
||||
package org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.JetExpression;
|
||||
|
||||
public class GuardedExpression {
|
||||
@NotNull
|
||||
private final JetExpression condition;
|
||||
|
||||
@NotNull
|
||||
private final JetExpression baseExpression;
|
||||
|
||||
public GuardedExpression(@NotNull JetExpression condition, @NotNull JetExpression baseExpression) {
|
||||
this.condition = condition;
|
||||
this.baseExpression = baseExpression;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JetExpression getCondition() {
|
||||
return condition;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JetExpression getBaseExpression() {
|
||||
return baseExpression;
|
||||
}
|
||||
}
|
||||
+29
-79
@@ -1,6 +1,5 @@
|
||||
package org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lexer.JetTokens;
|
||||
@@ -8,6 +7,8 @@ import org.jetbrains.jet.lexer.JetTokens;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.jet.lang.psi.JetPsiUnparsingUtils.*;
|
||||
|
||||
public class IfWhenUtils {
|
||||
|
||||
public static final String TRANSFORM_WITHOUT_CHECK =
|
||||
@@ -71,8 +72,7 @@ public class IfWhenUtils {
|
||||
}
|
||||
|
||||
public static void transformIfToWhen(@NotNull JetIfExpression ifExpression) {
|
||||
List<MultiGuardedExpression> positiveBranches = new ArrayList<MultiGuardedExpression>();
|
||||
JetExpression elseExpression = null;
|
||||
JetPsiFactory.WhenBuilder builder = new JetPsiFactory.WhenBuilder();
|
||||
|
||||
JetIfExpression currIfExpression = ifExpression;
|
||||
do {
|
||||
@@ -84,123 +84,73 @@ public class IfWhenUtils {
|
||||
assertNotNull(thenBranch);
|
||||
assertNotNull(elseBranch);
|
||||
|
||||
List<JetExpression> orBranches = splitExpressionToOrBranches(condition);
|
||||
for (JetExpression orBranch : orBranches) {
|
||||
builder.condition(orBranch);
|
||||
}
|
||||
//noinspection ConstantConditions
|
||||
positiveBranches.add(new MultiGuardedExpression(splitExpressionToOrBranches(condition), thenBranch));
|
||||
builder.branchExpression(thenBranch);
|
||||
|
||||
if (elseBranch instanceof JetIfExpression) {
|
||||
currIfExpression = (JetIfExpression) elseBranch;
|
||||
}
|
||||
else {
|
||||
currIfExpression = null;
|
||||
elseExpression = elseBranch;
|
||||
//noinspection ConstantConditions
|
||||
builder.elseEntry(elseBranch);
|
||||
}
|
||||
} while (currIfExpression != null);
|
||||
|
||||
JetPsiFactory.WhenTemplateBuilder builder = new JetPsiFactory.WhenTemplateBuilder(false);
|
||||
for (MultiGuardedExpression positiveBranch : positiveBranches) {
|
||||
builder.addBranchWithMultiCondition(positiveBranch.getConditions().size());
|
||||
}
|
||||
|
||||
JetWhenExpression whenExpression = builder.toExpression(ifExpression.getProject());
|
||||
|
||||
int i = 0;
|
||||
List<JetWhenEntry> entries = whenExpression.getEntries();
|
||||
for (JetWhenEntry entry : entries) {
|
||||
if (entry.isElse()) {
|
||||
//noinspection ConstantConditions
|
||||
entry.getExpression().replace(elseExpression);
|
||||
break;
|
||||
}
|
||||
|
||||
MultiGuardedExpression branch = positiveBranches.get(i++);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
entry.getExpression().replace(branch.getBaseExpression());
|
||||
|
||||
int j = 0;
|
||||
JetWhenCondition[] conditions = entry.getConditions();
|
||||
for (JetWhenCondition condition : conditions) {
|
||||
assert condition instanceof JetWhenConditionWithExpression : TRANSFORM_WITHOUT_CHECK;
|
||||
|
||||
JetExpression conditionExpression = ((JetWhenConditionWithExpression) condition).getExpression();
|
||||
assertNotNull(conditionExpression);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
conditionExpression.replace(branch.getConditions().get(j++));
|
||||
}
|
||||
}
|
||||
|
||||
ifExpression.replace(whenExpression);
|
||||
ifExpression.replace(builder.toExpression(ifExpression.getProject()));
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
private static JetExpression combineWhenConditions(Project project, JetWhenCondition[] conditions, JetExpression subject) {
|
||||
private static String combineWhenConditions(JetWhenCondition[] conditions, JetExpression subject) {
|
||||
int n = conditions.length;
|
||||
assert n > 0 : TRANSFORM_WITHOUT_CHECK;
|
||||
if (n == 0) return "";
|
||||
|
||||
JetWhenCondition condition = conditions[n - 1];
|
||||
JetWhenCondition condition = conditions[0];
|
||||
assert condition != null : TRANSFORM_WITHOUT_CHECK;
|
||||
|
||||
JetExpression resultExpr = WhenUtils.whenConditionToExpression(condition, subject);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
String text = WhenUtils.whenConditionToExpressionText(condition, subject);
|
||||
if (n > 1) {
|
||||
resultExpr = JetPsiFactory.createParenthesizedExpressionIfNeeded(project, resultExpr);
|
||||
text = parenthesizeTextIfNeeded(text);
|
||||
}
|
||||
sb.append(text);
|
||||
|
||||
for (int i = n - 2; i >= 0; i--) {
|
||||
for (int i = 1; i < n; i++) {
|
||||
JetWhenCondition currCondition = conditions[i];
|
||||
|
||||
assert currCondition != null : TRANSFORM_WITHOUT_CHECK;
|
||||
|
||||
resultExpr = JetPsiFactory.createBinaryExpression(
|
||||
project,
|
||||
JetPsiFactory.createParenthesizedExpressionIfNeeded(project, WhenUtils.whenConditionToExpression(currCondition, subject)),
|
||||
"||",
|
||||
resultExpr);
|
||||
sb.append(" || ").append(parenthesizeTextIfNeeded(WhenUtils.whenConditionToExpressionText(currCondition, subject)));
|
||||
}
|
||||
|
||||
return resultExpr;
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static void transformWhenToIf(@NotNull JetWhenExpression whenExpression) {
|
||||
Project project = whenExpression.getProject();
|
||||
|
||||
JetExpression elseExpression = null;
|
||||
List<GuardedExpression> positiveBranches = new ArrayList<GuardedExpression>();
|
||||
JetPsiFactory.IfChainBuilder builder = new JetPsiFactory.IfChainBuilder();
|
||||
|
||||
List<JetWhenEntry> entries = whenExpression.getEntries();
|
||||
for (JetWhenEntry entry : entries) {
|
||||
JetExpression branch = entry.getExpression();
|
||||
|
||||
assertNotNull(branch);
|
||||
|
||||
if (entry.isElse()) {
|
||||
elseExpression = branch;
|
||||
//noinspection ConstantConditions
|
||||
builder.elseBranch(branch);
|
||||
} else {
|
||||
JetExpression branchCondition = combineWhenConditions(project, entry.getConditions(), whenExpression.getSubjectExpression());
|
||||
JetExpression branchExpression = entry.getExpression();
|
||||
String branchConditionText = combineWhenConditions(entry.getConditions(), whenExpression.getSubjectExpression());
|
||||
|
||||
JetExpression branchExpression = entry.getExpression();
|
||||
assertNotNull(branchExpression);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
positiveBranches.add(new GuardedExpression(branchCondition, branchExpression));
|
||||
builder.ifBranch(branchConditionText, branchExpression.getText());
|
||||
}
|
||||
}
|
||||
|
||||
assertNotNull(elseExpression);
|
||||
assert !positiveBranches.isEmpty() : TRANSFORM_WITHOUT_CHECK;
|
||||
|
||||
JetExpression outerExpression = elseExpression;
|
||||
|
||||
for (int i = positiveBranches.size() - 1; i >= 0; i--) {
|
||||
GuardedExpression branch = positiveBranches.get(i);
|
||||
|
||||
outerExpression = JetPsiFactory.createIf(
|
||||
project,
|
||||
branch.getCondition(), branch.getBaseExpression(), outerExpression,
|
||||
!(branch.getBaseExpression() instanceof JetBlockExpression), !(outerExpression instanceof JetBlockExpression));
|
||||
}
|
||||
|
||||
//noinspection ConstantConditions
|
||||
whenExpression.replace(outerExpression);
|
||||
whenExpression.replace(builder.toExpression(whenExpression.getProject()));
|
||||
}
|
||||
}
|
||||
|
||||
-29
@@ -1,29 +0,0 @@
|
||||
package org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.JetExpression;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MultiGuardedExpression {
|
||||
@NotNull
|
||||
private final List<JetExpression> conditions;
|
||||
|
||||
@NotNull
|
||||
private final JetExpression baseExpression;
|
||||
|
||||
public MultiGuardedExpression(@NotNull List<JetExpression> conditions, @NotNull JetExpression baseExpression) {
|
||||
this.conditions = conditions;
|
||||
this.baseExpression = baseExpression;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<JetExpression> getConditions() {
|
||||
return conditions;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JetExpression getBaseExpression() {
|
||||
return baseExpression;
|
||||
}
|
||||
}
|
||||
+58
-154
@@ -1,11 +1,12 @@
|
||||
package org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lexer.JetTokens;
|
||||
|
||||
import static org.jetbrains.jet.lang.psi.JetPsiUnparsingUtils.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class WhenUtils {
|
||||
@@ -87,7 +88,6 @@ public class WhenUtils {
|
||||
|
||||
public static void flattenWhen(@NotNull JetWhenExpression whenExpression) {
|
||||
JetExpression subjectExpression = whenExpression.getSubjectExpression();
|
||||
boolean hasSubject = subjectExpression != null;
|
||||
|
||||
JetExpression elseBranch = JetPsiUtil.getWhenElseBranch(whenExpression);
|
||||
assert elseBranch instanceof JetWhenExpression : TRANSFORM_WITHOUT_CHECK;
|
||||
@@ -97,38 +97,36 @@ public class WhenUtils {
|
||||
List<JetWhenEntry> outerEntries = whenExpression.getEntries();
|
||||
List<JetWhenEntry> innerEntries = nestedWhenExpression.getEntries();
|
||||
|
||||
JetWhenExpression newWhenExpression = new JetPsiFactory.WhenTemplateBuilder(hasSubject)
|
||||
.addBranchesWithSingleCondition(outerEntries.size() + innerEntries.size() - 2)
|
||||
.toExpression(whenExpression.getProject());
|
||||
JetPsiFactory.WhenBuilder builder = new JetPsiFactory.WhenBuilder(subjectExpression);
|
||||
|
||||
if (hasSubject) {
|
||||
JetExpression dummySubjectExpression = newWhenExpression.getSubjectExpression();
|
||||
assertNotNull(dummySubjectExpression);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
dummySubjectExpression.replace(subjectExpression);
|
||||
}
|
||||
|
||||
List<JetWhenEntry> newEntries = newWhenExpression.getEntries();
|
||||
|
||||
int i = 0;
|
||||
for (JetWhenEntry entry : outerEntries) {
|
||||
if (!entry.isElse()) {
|
||||
newEntries.get(i++).replace(entry);
|
||||
}
|
||||
}
|
||||
for (JetWhenEntry entry : innerEntries) {
|
||||
newEntries.get(i++).replace(entry);
|
||||
if (entry.isElse()) continue;
|
||||
|
||||
builder.entry(entry);
|
||||
}
|
||||
|
||||
whenExpression.replace(newWhenExpression);
|
||||
for (JetWhenEntry entry : innerEntries) {
|
||||
builder.entry(entry);
|
||||
}
|
||||
|
||||
whenExpression.replace(builder.toExpression(whenExpression.getProject()));
|
||||
}
|
||||
|
||||
private static JetWhenExpression createWhenTemplateWithSubject(@NotNull JetWhenExpression whenExpression) {
|
||||
JetPsiFactory.WhenTemplateBuilder builder = new JetPsiFactory.WhenTemplateBuilder(true);
|
||||
public static void introduceWhenSubject(@NotNull JetWhenExpression whenExpression) {
|
||||
JetExpression subject = getWhenSubjectCandidate(whenExpression);
|
||||
assertNotNull(subject);
|
||||
|
||||
JetPsiFactory.WhenBuilder builder = new JetPsiFactory.WhenBuilder(subject);
|
||||
|
||||
for (JetWhenEntry entry : whenExpression.getEntries()) {
|
||||
if (entry.isElse()) continue;
|
||||
JetExpression branchExpression = entry.getExpression();
|
||||
assertNotNull(branchExpression);
|
||||
|
||||
if (entry.isElse()) {
|
||||
//noinspection ConstantConditions
|
||||
builder.elseEntry(branchExpression);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (JetWhenCondition condition : entry.getConditions()) {
|
||||
assert condition instanceof JetWhenConditionWithExpression : TRANSFORM_WITHOUT_CHECK;
|
||||
@@ -136,81 +134,13 @@ public class WhenUtils {
|
||||
JetExpression conditionExpression = ((JetWhenConditionWithExpression) condition).getExpression();
|
||||
|
||||
if (conditionExpression instanceof JetIsExpression) {
|
||||
builder.addIsCondition(((JetIsExpression) conditionExpression).isNegated());
|
||||
}
|
||||
else if (conditionExpression instanceof JetBinaryExpression) {
|
||||
JetBinaryExpression binaryExpression = (JetBinaryExpression) conditionExpression;
|
||||
JetIsExpression isExpression = (JetIsExpression) conditionExpression;
|
||||
|
||||
IElementType op = binaryExpression.getOperationToken();
|
||||
if (op == JetTokens.IN_KEYWORD) {
|
||||
builder.addInCondition(false);
|
||||
}
|
||||
else if (op == JetTokens.NOT_IN) {
|
||||
builder.addInCondition(true);
|
||||
}
|
||||
else if (op == JetTokens.EQEQ) {
|
||||
builder.addExpressionCondition();
|
||||
}
|
||||
else assert false : TRANSFORM_WITHOUT_CHECK;
|
||||
}
|
||||
else assert false : TRANSFORM_WITHOUT_CHECK;
|
||||
}
|
||||
|
||||
builder.finishBranch();
|
||||
}
|
||||
|
||||
return builder.toExpression(whenExpression.getProject());
|
||||
}
|
||||
|
||||
public static void introduceWhenSubject(@NotNull JetWhenExpression whenExpression) {
|
||||
JetExpression subject = getWhenSubjectCandidate(whenExpression);
|
||||
assertNotNull(subject);
|
||||
|
||||
JetWhenExpression newWhenExpression = createWhenTemplateWithSubject(whenExpression);
|
||||
|
||||
JetExpression newSubject = newWhenExpression.getSubjectExpression();
|
||||
assertNotNull(newSubject);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
newSubject.replace(subject);
|
||||
|
||||
int i = 0;
|
||||
List<JetWhenEntry> entries = whenExpression.getEntries();
|
||||
List<JetWhenEntry> newEntries = newWhenExpression.getEntries();
|
||||
for (JetWhenEntry newEntry : newEntries) {
|
||||
JetWhenEntry entry = entries.get(i++);
|
||||
|
||||
JetExpression branchExpression = entry.getExpression();
|
||||
assertNotNull(branchExpression);
|
||||
|
||||
JetExpression newBranchExpression = newEntry.getExpression();
|
||||
assertNotNull(newBranchExpression);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
newBranchExpression.replace(branchExpression);
|
||||
|
||||
int j = 0;
|
||||
JetWhenCondition[] conditions = entry.getConditions();
|
||||
JetWhenCondition[] newConditions = newEntry.getConditions();
|
||||
|
||||
for (JetWhenCondition newCondition : newConditions) {
|
||||
JetWhenCondition condition = conditions[j++];
|
||||
|
||||
assert condition instanceof JetWhenConditionWithExpression : TRANSFORM_WITHOUT_CHECK;
|
||||
|
||||
JetExpression conditionExpression = ((JetWhenConditionWithExpression) condition).getExpression();
|
||||
|
||||
if (conditionExpression instanceof JetIsExpression) {
|
||||
assert newCondition instanceof JetWhenConditionIsPattern : TRANSFORM_WITHOUT_CHECK;
|
||||
|
||||
JetTypeReference typeReference = ((JetIsExpression) conditionExpression).getTypeRef();
|
||||
JetTypeReference typeReference = isExpression.getTypeRef();
|
||||
assertNotNull(typeReference);
|
||||
|
||||
JetTypeReference newTypeReference = ((JetWhenConditionIsPattern) newCondition).getTypeRef();
|
||||
assertNotNull(newTypeReference);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
newTypeReference.replace(typeReference);
|
||||
builder.pattern(typeReference, isExpression.isNegated());
|
||||
}
|
||||
else if (conditionExpression instanceof JetBinaryExpression) {
|
||||
JetBinaryExpression binaryExpression = (JetBinaryExpression) conditionExpression;
|
||||
@@ -219,48 +149,31 @@ public class WhenUtils {
|
||||
assertNotNull(rhs);
|
||||
|
||||
IElementType op = binaryExpression.getOperationToken();
|
||||
if (op == JetTokens.IN_KEYWORD || op == JetTokens.NOT_IN) {
|
||||
assert newCondition instanceof JetWhenConditionInRange : TRANSFORM_WITHOUT_CHECK;
|
||||
|
||||
JetExpression newRangeExpression = ((JetWhenConditionInRange) newCondition).getRangeExpression();
|
||||
assertNotNull(newRangeExpression);
|
||||
|
||||
if (op == JetTokens.IN_KEYWORD) {
|
||||
//noinspection ConstantConditions
|
||||
newRangeExpression.replace(rhs);
|
||||
builder.range(rhs, false);
|
||||
}
|
||||
else if (op == JetTokens.NOT_IN) {
|
||||
//noinspection ConstantConditions
|
||||
builder.range(rhs, true);
|
||||
}
|
||||
else if (op == JetTokens.EQEQ) {
|
||||
assert newCondition instanceof JetWhenConditionWithExpression : TRANSFORM_WITHOUT_CHECK;
|
||||
|
||||
JetExpression newConditionExpression = ((JetWhenConditionWithExpression) newCondition).getExpression();
|
||||
assertNotNull(newConditionExpression);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
newConditionExpression.replace(rhs);
|
||||
builder.condition(rhs);
|
||||
}
|
||||
else assert false : TRANSFORM_WITHOUT_CHECK;
|
||||
}
|
||||
else assert false : TRANSFORM_WITHOUT_CHECK;
|
||||
}
|
||||
|
||||
//noinspection ConstantConditions
|
||||
builder.branchExpression(branchExpression);
|
||||
}
|
||||
|
||||
whenExpression.replace(newWhenExpression);
|
||||
whenExpression.replace(builder.toExpression(whenExpression.getProject()));
|
||||
}
|
||||
|
||||
private static JetWhenExpression createWhenTemplateWithoutSubject(@NotNull JetWhenExpression whenExpression) {
|
||||
JetPsiFactory.WhenTemplateBuilder builder = new JetPsiFactory.WhenTemplateBuilder(false);
|
||||
|
||||
for (JetWhenEntry entry : whenExpression.getEntries()) {
|
||||
if (!entry.isElse()) {
|
||||
builder.addBranchWithMultiCondition(entry.getConditions().length);
|
||||
}
|
||||
}
|
||||
|
||||
return builder.toExpression(whenExpression.getProject());
|
||||
}
|
||||
|
||||
static JetExpression whenConditionToExpression(@NotNull JetWhenCondition condition, JetExpression subject) {
|
||||
Project project = condition.getProject();
|
||||
|
||||
static String whenConditionToExpressionText(@NotNull JetWhenCondition condition, JetExpression subject) {
|
||||
if (condition instanceof JetWhenConditionIsPattern) {
|
||||
JetWhenConditionIsPattern patternCondition = (JetWhenConditionIsPattern) condition;
|
||||
|
||||
@@ -270,7 +183,7 @@ public class WhenUtils {
|
||||
assertNotNull(subject);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
return JetPsiFactory.createIsExpression(project, subject, typeReference, patternCondition.isNegated());
|
||||
return toBinaryExpression(subject, (patternCondition.isNegated() ? "!is" : "is"), typeReference);
|
||||
}
|
||||
|
||||
if (condition instanceof JetWhenConditionInRange) {
|
||||
@@ -282,7 +195,7 @@ public class WhenUtils {
|
||||
assertNotNull(subject);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
return JetPsiFactory.createBinaryExpression(project, subject, rangeCondition.getOperationReference().getText(), rangeExpression);
|
||||
return toBinaryExpression(subject, rangeCondition.getOperationReference().getText(), rangeExpression);
|
||||
}
|
||||
|
||||
assert condition instanceof JetWhenConditionWithExpression : TRANSFORM_WITHOUT_CHECK;
|
||||
@@ -291,45 +204,36 @@ public class WhenUtils {
|
||||
assertNotNull(conditionExpression);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
return subject != null ? JetPsiFactory.createBinaryExpression(project, subject, "==", conditionExpression) : conditionExpression;
|
||||
return subject != null ?
|
||||
toBinaryExpression(parenthesizeIfNeeded(subject), "==", parenthesizeIfNeeded(conditionExpression))
|
||||
: conditionExpression.getText();
|
||||
}
|
||||
|
||||
public static void eliminateWhenSubject(@NotNull JetWhenExpression whenExpression) {
|
||||
JetExpression subject = whenExpression.getSubjectExpression();
|
||||
assertNotNull(subject);
|
||||
|
||||
JetWhenExpression newWhenExpression = createWhenTemplateWithoutSubject(whenExpression);
|
||||
|
||||
int i = 0;
|
||||
List<JetWhenEntry> entries = whenExpression.getEntries();
|
||||
List<JetWhenEntry> newEntries = newWhenExpression.getEntries();
|
||||
for (JetWhenEntry newEntry : newEntries) {
|
||||
JetWhenEntry entry = entries.get(i++);
|
||||
JetPsiFactory.WhenBuilder builder = new JetPsiFactory.WhenBuilder();
|
||||
|
||||
for (JetWhenEntry entry : whenExpression.getEntries()) {
|
||||
JetExpression branchExpression = entry.getExpression();
|
||||
assertNotNull(branchExpression);
|
||||
|
||||
JetExpression newBranchExpression = newEntry.getExpression();
|
||||
assertNotNull(newBranchExpression);
|
||||
if (entry.isElse()) {
|
||||
//noinspection ConstantConditions
|
||||
builder.elseEntry(branchExpression);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
for (JetWhenCondition condition : entry.getConditions()) {
|
||||
builder.condition(whenConditionToExpressionText(condition, subject));
|
||||
}
|
||||
|
||||
//noinspection ConstantConditions
|
||||
newBranchExpression.replace(branchExpression);
|
||||
|
||||
int j = 0;
|
||||
JetWhenCondition[] conditions = entry.getConditions();
|
||||
JetWhenCondition[] newConditions = newEntry.getConditions();
|
||||
|
||||
for (JetWhenCondition newCondition : newConditions) {
|
||||
JetWhenCondition condition = conditions[j++];
|
||||
|
||||
JetExpression newConditionExpression = ((JetWhenConditionWithExpression) newCondition).getExpression();
|
||||
assertNotNull(newConditionExpression);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
newConditionExpression.replace(whenConditionToExpression(condition, subject));
|
||||
}
|
||||
builder.branchExpression(branchExpression);
|
||||
}
|
||||
|
||||
whenExpression.replace(newWhenExpression);
|
||||
whenExpression.replace(builder.toExpression(whenExpression.getProject()));
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
fun test(n: Int): String {
|
||||
<caret>return when (n) {
|
||||
1 -> "one"
|
||||
else -> "two"
|
||||
1 -> "one"
|
||||
else -> "two"
|
||||
}
|
||||
}
|
||||
+8
-7
@@ -1,11 +1,12 @@
|
||||
fun test(n: Int): String {
|
||||
<caret>when(n) {
|
||||
1 -> {
|
||||
println("***")
|
||||
return "one"
|
||||
}
|
||||
else -> {
|
||||
println("***")
|
||||
return "two"
|
||||
1 -> {
|
||||
println("***")
|
||||
return "one"
|
||||
}
|
||||
else -> {
|
||||
println("***")
|
||||
return "two"
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
-7
@@ -1,11 +1,12 @@
|
||||
fun test(n: Int): String {
|
||||
<caret>return when(n) {
|
||||
1 -> {
|
||||
println("***")
|
||||
"one"
|
||||
}
|
||||
else -> {
|
||||
println("***")
|
||||
"two"
|
||||
1 -> {
|
||||
println("***")
|
||||
"one"
|
||||
}
|
||||
else -> {
|
||||
println("***")
|
||||
"two"
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-6
@@ -1,9 +1,6 @@
|
||||
fun test(n: Int): String {
|
||||
return <caret>if (n == 0)
|
||||
"zero"
|
||||
else if (n == 1)
|
||||
"one"
|
||||
else if (n == 2)
|
||||
"two"
|
||||
return <caret>if (n == 0) "zero"
|
||||
else if (n == 1) "one"
|
||||
else if (n == 2) "two"
|
||||
else "unknown"
|
||||
}
|
||||
+3
-6
@@ -1,9 +1,6 @@
|
||||
fun test(n: Int): String {
|
||||
return <caret>if ((n < 0) || (n > 1000))
|
||||
"unknown"
|
||||
else if (n <= 10)
|
||||
"small"
|
||||
else if (n <= 100)
|
||||
"average"
|
||||
return <caret>if ((n < 0) || (n > 1000)) "unknown"
|
||||
else if (n <= 10) "small"
|
||||
else if (n <= 100) "average"
|
||||
else "big"
|
||||
}
|
||||
+3
-6
@@ -1,9 +1,6 @@
|
||||
fun test(obj: Any): String {
|
||||
return <caret>if (obj !is Iterable<*>)
|
||||
"not iterable"
|
||||
else if (obj !is Collection<*>)
|
||||
"not collection"
|
||||
else if (obj !is MutableCollection<*>)
|
||||
"not mutable collection"
|
||||
return <caret>if (obj !is Iterable<*>) "not iterable"
|
||||
else if (obj !is Collection<*>) "not collection"
|
||||
else if (obj !is MutableCollection<*>) "not mutable collection"
|
||||
else "unknown"
|
||||
}
|
||||
+3
-6
@@ -1,9 +1,6 @@
|
||||
fun test(n: Int): String {
|
||||
return <caret>if (n !in 0..1000)
|
||||
"unknown"
|
||||
else if (n !in 0..100)
|
||||
"big"
|
||||
else if (n !in 0..10)
|
||||
"average"
|
||||
return <caret>if (n !in 0..1000) "unknown"
|
||||
else if (n !in 0..100) "big"
|
||||
else if (n !in 0..10) "average"
|
||||
else "small"
|
||||
}
|
||||
+3
-6
@@ -1,9 +1,6 @@
|
||||
fun test(obj: Any): String {
|
||||
return <caret>if (obj is String)
|
||||
"string"
|
||||
else if (obj is Int)
|
||||
"int"
|
||||
else if (obj is Class<*>)
|
||||
"class"
|
||||
return <caret>if (obj is String) "string"
|
||||
else if (obj is Int) "int"
|
||||
else if (obj is Class<*>) "class"
|
||||
else "unknown"
|
||||
}
|
||||
+3
-6
@@ -1,9 +1,6 @@
|
||||
fun test(n: Int): String {
|
||||
return <caret>if (n in 0..10)
|
||||
"small"
|
||||
else if (n in 10..100)
|
||||
"average"
|
||||
else if (n in 100..1000)
|
||||
"big"
|
||||
return <caret>if (n in 0..10) "small"
|
||||
else if (n in 10..100) "average"
|
||||
else if (n in 100..1000) "big"
|
||||
else "unknown"
|
||||
}
|
||||
+3
-6
@@ -1,9 +1,6 @@
|
||||
fun test(n: Int): String {
|
||||
return <caret>if ((n in 0..5) || (n in 5..10))
|
||||
"small"
|
||||
else if ((n in 10..50) || (n in 50..100))
|
||||
"average"
|
||||
else if ((n in 100..500) || (n in 500..1000))
|
||||
"big"
|
||||
return <caret>if ((n in 0..5) || (n in 5..10)) "small"
|
||||
else if ((n in 10..50) || (n in 50..100)) "average"
|
||||
else if ((n in 100..500) || (n in 500..1000)) "big"
|
||||
else "unknown"
|
||||
}
|
||||
+3
-6
@@ -1,9 +1,6 @@
|
||||
fun test(n: Int): String {
|
||||
return <caret>if (n in 0..10)
|
||||
"small"
|
||||
else if (n in 10..100)
|
||||
"average"
|
||||
else if (n in 100..1000)
|
||||
"big"
|
||||
return <caret>if (n in 0..10) "small"
|
||||
else if (n in 10..100) "average"
|
||||
else if (n in 100..1000) "big"
|
||||
else "unknown"
|
||||
}
|
||||
Reference in New Issue
Block a user