Introduce 'when' subject: consider equality right-hand side as subject candidate
This commit is contained in:
+13
-3
@@ -28,9 +28,14 @@ public class WhenUtils {
|
|||||||
|
|
||||||
if (condition instanceof JetBinaryExpression) {
|
if (condition instanceof JetBinaryExpression) {
|
||||||
JetBinaryExpression binaryExpression = (JetBinaryExpression) condition;
|
JetBinaryExpression binaryExpression = (JetBinaryExpression) condition;
|
||||||
|
JetExpression lhs = binaryExpression.getLeft();
|
||||||
|
|
||||||
IElementType op = binaryExpression.getOperationToken();
|
IElementType op = binaryExpression.getOperationToken();
|
||||||
if (op == JetTokens.EQEQ || op == JetTokens.IN_KEYWORD || op == JetTokens.NOT_IN) {
|
if (op == JetTokens.IN_KEYWORD || op == JetTokens.NOT_IN) {
|
||||||
return ((JetBinaryExpression) condition).getLeft();
|
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) {
|
else if (conditionExpression instanceof JetBinaryExpression) {
|
||||||
JetBinaryExpression binaryExpression = (JetBinaryExpression) conditionExpression;
|
JetBinaryExpression binaryExpression = (JetBinaryExpression) conditionExpression;
|
||||||
|
|
||||||
|
JetExpression lhs = binaryExpression.getLeft();
|
||||||
JetExpression rhs = binaryExpression.getRight();
|
JetExpression rhs = binaryExpression.getRight();
|
||||||
|
|
||||||
IElementType op = binaryExpression.getOperationToken();
|
IElementType op = binaryExpression.getOperationToken();
|
||||||
@@ -154,7 +160,11 @@ public class WhenUtils {
|
|||||||
builder.range(rhs, true);
|
builder.range(rhs, true);
|
||||||
}
|
}
|
||||||
else if (op == JetTokens.EQEQ) {
|
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;
|
else assert false : TRANSFORM_WITHOUT_CHECK;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
@@ -5,6 +5,7 @@ fun test(n: Int): String {
|
|||||||
n in 10..100 -> "average"
|
n in 10..100 -> "average"
|
||||||
n in 100..1000 -> "big"
|
n in 100..1000 -> "big"
|
||||||
n == 1000000 -> "million"
|
n == 1000000 -> "million"
|
||||||
|
2000000 == n -> "two millions"
|
||||||
n !in -100..-10 -> "good"
|
n !in -100..-10 -> "good"
|
||||||
n is Int -> "unknown"
|
n is Int -> "unknown"
|
||||||
else -> "unknown"
|
else -> "unknown"
|
||||||
|
|||||||
+1
@@ -5,6 +5,7 @@ fun test(n: Int): String {
|
|||||||
in 10..100 -> "average"
|
in 10..100 -> "average"
|
||||||
in 100..1000 -> "big"
|
in 100..1000 -> "big"
|
||||||
1000000 -> "million"
|
1000000 -> "million"
|
||||||
|
2000000 -> "two millions"
|
||||||
!in -100..-10 -> "good"
|
!in -100..-10 -> "good"
|
||||||
is Int -> "unknown"
|
is Int -> "unknown"
|
||||||
else -> "unknown"
|
else -> "unknown"
|
||||||
|
|||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
fun test(n: Int): String {
|
||||||
|
return <caret>when {
|
||||||
|
n == 0 -> "zero"
|
||||||
|
1 == n -> "one"
|
||||||
|
n == 2 -> "two"
|
||||||
|
else -> "unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
fun test(n: Int): String {
|
||||||
|
return <caret>when (n) {
|
||||||
|
0 -> "zero"
|
||||||
|
1 -> "one"
|
||||||
|
2 -> "two"
|
||||||
|
else -> "unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -585,6 +585,11 @@ public class CodeTransformationsTestGenerated extends AbstractCodeTransformation
|
|||||||
doTestIntroduceWhenSubject("idea/testData/codeInsight/codeTransformations/branched/when/introduceSubject/whenWithSubject.kt");
|
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")
|
@TestMetadata("whenWithUnmatchedCandidateSubjects.kt")
|
||||||
public void testWhenWithUnmatchedCandidateSubjects() throws Exception {
|
public void testWhenWithUnmatchedCandidateSubjects() throws Exception {
|
||||||
doTestIntroduceWhenSubject("idea/testData/codeInsight/codeTransformations/branched/when/introduceSubject/whenWithUnmatchedCandidateSubjects.kt");
|
doTestIntroduceWhenSubject("idea/testData/codeInsight/codeTransformations/branched/when/introduceSubject/whenWithUnmatchedCandidateSubjects.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user