Transform 'if' to 'when' with subject if possible

This commit is contained in:
Alexey Sedunov
2013-05-15 15:43:11 +04:00
parent b2b7bce99a
commit 158c2753b8
10 changed files with 61 additions and 43 deletions
@@ -110,7 +110,12 @@ public class IfWhenUtils {
}
} while (currIfExpression != null);
ifExpression.replace(builder.toExpression(ifExpression.getProject()));
JetWhenExpression whenExpression = builder.toExpression(ifExpression.getProject());
if (WhenUtils.checkIntroduceWhenSubject(whenExpression)) {
whenExpression = WhenUtils.introduceWhenSubject(whenExpression);
}
ifExpression.replace(whenExpression);
}
private static String combineWhenConditions(JetWhenCondition[] conditions, JetExpression subject) {
@@ -86,7 +86,8 @@ public class WhenUtils {
return whenExpression.getSubjectExpression() instanceof JetSimpleNameExpression;
}
public static void flattenWhen(@NotNull JetWhenExpression whenExpression) {
@NotNull
public static JetWhenExpression flattenWhen(@NotNull JetWhenExpression whenExpression) {
JetExpression subjectExpression = whenExpression.getSubjectExpression();
JetExpression elseBranch = whenExpression.getElseExpression();
@@ -109,10 +110,14 @@ public class WhenUtils {
builder.entry(entry);
}
whenExpression.replace(builder.toExpression(whenExpression.getProject()));
JetWhenExpression newWhenExpression = builder.toExpression(whenExpression.getProject());
whenExpression.replace(newWhenExpression);
return newWhenExpression;
}
public static void introduceWhenSubject(@NotNull JetWhenExpression whenExpression) {
@NotNull
public static JetWhenExpression introduceWhenSubject(@NotNull JetWhenExpression whenExpression) {
JetExpression subject = getWhenSubjectCandidate(whenExpression);
assertNotNull(subject);
@@ -158,9 +163,13 @@ public class WhenUtils {
builder.branchExpression(branchExpression);
}
whenExpression.replace(builder.toExpression(whenExpression.getProject()));
JetWhenExpression newWhenExpression = builder.toExpression(whenExpression.getProject());
whenExpression.replace(newWhenExpression);
return newWhenExpression;
}
@NotNull
static String whenConditionToExpressionText(@NotNull JetWhenCondition condition, JetExpression subject) {
if (condition instanceof JetWhenConditionIsPattern) {
JetWhenConditionIsPattern patternCondition = (JetWhenConditionIsPattern) condition;
@@ -182,7 +191,8 @@ public class WhenUtils {
return conditionExpression != null ? conditionExpression.getText() : "";
}
public static void eliminateWhenSubject(@NotNull JetWhenExpression whenExpression) {
@NotNull
public static JetWhenExpression eliminateWhenSubject(@NotNull JetWhenExpression whenExpression) {
JetExpression subject = whenExpression.getSubjectExpression();
assertNotNull(subject);
@@ -204,6 +214,9 @@ public class WhenUtils {
builder.branchExpression(branchExpression);
}
whenExpression.replace(builder.toExpression(whenExpression.getProject()));
JetWhenExpression newWhenExpression = builder.toExpression(whenExpression.getProject());
whenExpression.replace(newWhenExpression);
return newWhenExpression;
}
}
@@ -1,8 +1,8 @@
fun test(n: Int): String {
return <caret>when {
n == 0 -> "zero"
n == 1 -> "one"
n == 2 -> "two"
return <caret>when (n) {
0 -> "zero"
1 -> "one"
2 -> "two"
else -> "unknown"
}
}
@@ -1,8 +1,8 @@
fun test(obj: Any): String {
return <caret>when {
obj is String -> "string"
obj is Int -> "int"
obj is Class<*> -> "class"
return <caret>when (obj) {
is String -> "string"
is Int -> "int"
is Class<*> -> "class"
else -> "unknown"
}
}
@@ -1,8 +1,8 @@
fun test(obj: Any): String {
return <caret>when {
obj !is Iterable<*> -> "not iterable"
obj !is Collection<*> -> "not collection"
obj !is MutableCollection<*> -> "not mutable collection"
return <caret>when (obj) {
!is Iterable<*> -> "not iterable"
!is Collection<*> -> "not collection"
!is MutableCollection<*> -> "not mutable collection"
else -> "unknown"
}
}
@@ -1,8 +1,8 @@
fun test(n: Int): String {
return <caret>when {
n !in 0..1000 -> "unknown"
n !in 0..100 -> "big"
n !in 0..10 -> "average"
return <caret>when (n) {
!in 0..1000 -> "unknown"
!in 0..100 -> "big"
!in 0..10 -> "average"
else -> "small"
}
}
@@ -1,8 +1,8 @@
fun test(n: Int): String {
return <caret>when {
n in 0..10 -> "small"
n in 10..100 -> "average"
n in 100..1000 -> "big"
return <caret>when (n) {
in 0..10 -> "small"
in 10..100 -> "average"
in 100..1000 -> "big"
else -> "unknown"
}
}
@@ -1,8 +1,8 @@
fun test(n: Int): String {
return <caret>when {
n in 0..5, n in 5..10 -> "small"
n in 10..50, n in 50..100 -> "average"
n in 100..500, n in 500..1000 -> "big"
return <caret>when (n) {
in 0..5, in 5..10 -> "small"
in 10..50, in 50..100 -> "average"
in 100..500, in 500..1000 -> "big"
else -> "unknown"
}
}
@@ -1,8 +1,8 @@
fun test(n: Int): String {
return <caret>when {
n in 0..5, n in 5..10 -> "small"
n in 10..50, n in 50..100 -> "average"
n in 100..500, n in 500..1000 -> "big"
return <caret>when (n) {
in 0..5, in 5..10 -> "small"
in 10..50, in 50..100 -> "average"
in 100..500, in 500..1000 -> "big"
else -> "unknown"
}
}
@@ -1,12 +1,12 @@
fun test(n: Int): String {
return <caret>when {
n !is Int -> "???"
n in 0..10 -> "small"
n in 10..100 -> "average"
n in 100..1000 -> "big"
n == 1000000 -> "million"
n !in -100..-10 -> "good"
n is Int -> "unknown"
return <caret>when (n) {
!is Int -> "???"
in 0..10 -> "small"
in 10..100 -> "average"
in 100..1000 -> "big"
1000000 -> "million"
!in -100..-10 -> "good"
is Int -> "unknown"
else -> "unknown"
}
}