Introduce 'when' subject: consider equality right-hand side as subject candidate

This commit is contained in:
Alexey Sedunov
2013-06-10 18:01:21 +04:00
parent e13508c22b
commit f9c4606128
6 changed files with 36 additions and 3 deletions
@@ -28,9 +28,14 @@ public class WhenUtils {
if (condition instanceof JetBinaryExpression) {
JetBinaryExpression binaryExpression = (JetBinaryExpression) condition;
JetExpression lhs = binaryExpression.getLeft();
IElementType op = binaryExpression.getOperationToken();
if (op == JetTokens.EQEQ || op == JetTokens.IN_KEYWORD || op == JetTokens.NOT_IN) {
return ((JetBinaryExpression) condition).getLeft();
if (op == JetTokens.IN_KEYWORD || op == JetTokens.NOT_IN) {
return lhs;
}
if (op == JetTokens.EQEQ) {
return lhs instanceof JetSimpleNameExpression ? lhs : binaryExpression.getRight();
}
}
@@ -144,6 +149,7 @@ public class WhenUtils {
else if (conditionExpression instanceof JetBinaryExpression) {
JetBinaryExpression binaryExpression = (JetBinaryExpression) conditionExpression;
JetExpression lhs = binaryExpression.getLeft();
JetExpression rhs = binaryExpression.getRight();
IElementType op = binaryExpression.getOperationToken();
@@ -154,7 +160,11 @@ public class WhenUtils {
builder.range(rhs, true);
}
else if (op == JetTokens.EQEQ) {
builder.condition(rhs);
if (JetPsiMatcher.checkElementMatch(subject, lhs)) {
builder.condition(rhs);
} else {
builder.condition(lhs);
}
}
else assert false : TRANSFORM_WITHOUT_CHECK;
}
@@ -5,6 +5,7 @@ fun test(n: Int): String {
n in 10..100 -> "average"
n in 100..1000 -> "big"
n == 1000000 -> "million"
2000000 == n -> "two millions"
n !in -100..-10 -> "good"
n is Int -> "unknown"
else -> "unknown"
@@ -5,6 +5,7 @@ fun test(n: Int): String {
in 10..100 -> "average"
in 100..1000 -> "big"
1000000 -> "million"
2000000 -> "two millions"
!in -100..-10 -> "good"
is Int -> "unknown"
else -> "unknown"
@@ -0,0 +1,8 @@
fun test(n: Int): String {
return <caret>when {
n == 0 -> "zero"
1 == n -> "one"
n == 2 -> "two"
else -> "unknown"
}
}
@@ -0,0 +1,8 @@
fun test(n: Int): String {
return <caret>when (n) {
0 -> "zero"
1 -> "one"
2 -> "two"
else -> "unknown"
}
}
@@ -585,6 +585,11 @@ public class CodeTransformationsTestGenerated extends AbstractCodeTransformation
doTestIntroduceWhenSubject("idea/testData/codeInsight/codeTransformations/branched/when/introduceSubject/whenWithSubject.kt");
}
@TestMetadata("whenWithSwappedEqualityTests.kt")
public void testWhenWithSwappedEqualityTests() throws Exception {
doTestIntroduceWhenSubject("idea/testData/codeInsight/codeTransformations/branched/when/introduceSubject/whenWithSwappedEqualityTests.kt");
}
@TestMetadata("whenWithUnmatchedCandidateSubjects.kt")
public void testWhenWithUnmatchedCandidateSubjects() throws Exception {
doTestIntroduceWhenSubject("idea/testData/codeInsight/codeTransformations/branched/when/introduceSubject/whenWithUnmatchedCandidateSubjects.kt");