Rewrite mixed multiple- and single-condition 'when' properly.

#KT-10229 Fixed
This commit is contained in:
Dmitry Petrov
2015-11-30 14:43:03 +03:00
parent f7a780f32c
commit f0e467e474
10 changed files with 66 additions and 12 deletions
@@ -52,10 +52,12 @@ class CommaInWhenConditionWithoutArgumentFix(element: PsiElement) : KotlinQuickF
private fun replaceCommasWithOrsInWhenExpression(whenExpression: KtWhenExpression) {
for (whenEntry in whenExpression.entries) {
val conditionsData = getConditionsDataOrNull(whenEntry) ?: return
val replacement = KtPsiFactory(whenEntry).combineWhenConditions(conditionsData.conditions, null) ?: return
whenEntry.deleteChildRange(conditionsData.first, conditionsData.last)
whenEntry.addBefore(replacement, conditionsData.arrow)
if (whenEntry.conditions.size > 1) {
val conditionsData = getConditionsDataOrNull(whenEntry) ?: return
val replacement = KtPsiFactory(whenEntry).combineWhenConditions(conditionsData.conditions, null) ?: return
whenEntry.deleteChildRange(conditionsData.first, conditionsData.last)
whenEntry.addBefore(replacement, conditionsData.arrow)
}
}
}
@@ -2,8 +2,10 @@
fun test(i: Int, j: Int) {
var b = false
when {
i > 0<caret>, j > 0 -> { /* some code 1 */ }
i < 0, j < 0 -> { /* some code 2 */ }
i == 0 -> { /* code 1 */ }
i > 0<caret>, j > 0 -> { /* code 2 */ }
j == 0 -> { /* code 3 */ }
i < 0, j < 0, j > i -> { /* code 4 */ }
else -> { /* other code */ }
}
}
@@ -2,8 +2,10 @@
fun test(i: Int, j: Int) {
var b = false
when {
<caret>i > 0 || j > 0 -> { /* some code 1 */ }
i < 0 || j < 0 -> { /* some code 2 */ }
i == 0 -> { /* code 1 */ }
i > 0 || j > 0 -> { /* code 2 */ }
j == 0 -> { /* code 3 */ }
i < 0 || j < 0 || j > i -> { /* code 4 */ }
else -> { /* other code */ }
}
}
@@ -0,0 +1,8 @@
// "Replace ',' with '||' in when" "true"
fun test(a: Boolean, b: Boolean, c: Boolean) {
val c = when {
a<caret>, b -> "a"
c -> "b"
else -> "e"
}
}
@@ -0,0 +1,8 @@
// "Replace ',' with '||' in when" "true"
fun test(a: Boolean, b: Boolean, c: Boolean) {
val c = when {
a || b -> "a"
c -> "b"
else -> "e"
}
}
@@ -2,8 +2,10 @@
fun test(i: Int, j: Int) {
var b = false
when {
i > 0<caret>, j > 0 -> { /* some code 1 */ }
i < 0, j < 0 -> { /* some code 2 */ }
i == 0 -> { /* code 1 */ }
i > 0<caret>, j > 0 -> { /* code 2 */ }
j == 0 -> { /* code 3 */ }
i < 0, j < 0, j > i -> { /* code 4 */ }
else -> { /* other code */ }
}
}
@@ -2,8 +2,10 @@
fun test(i: Int, j: Int) {
var b = false
when {
<caret>i > 0 || j > 0 -> { /* some code 1 */ }
i < 0 || j < 0 -> { /* some code 2 */ }
i == 0 -> { /* code 1 */ }
i > 0 || j > 0 -> { /* code 2 */ }
j == 0 -> { /* code 3 */ }
i < 0 || j < 0 || j > i -> { /* code 4 */ }
else -> { /* other code */ }
}
}
@@ -0,0 +1,8 @@
// "Replace ',' with '||' in when" "true"
fun test(a: Boolean, b: Boolean, c: Boolean) {
val c = when {
a<caret>, b -> "a"
c -> "b"
else -> "e"
}
}
@@ -0,0 +1,8 @@
// "Replace ',' with '||' in when" "true"
fun test(a: Boolean, b: Boolean, c: Boolean) {
val c = when {
a || b -> "a"
c -> "b"
else -> "e"
}
}
@@ -4506,6 +4506,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/migration/commasInWhenWithoutArgument/commasInConditionWithNoArguments.kt");
doTest(fileName);
}
@TestMetadata("commasInConditionWithNoArguments2.kt")
public void testCommasInConditionWithNoArguments2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/migration/commasInWhenWithoutArgument/commasInConditionWithNoArguments2.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/quickfix/migration/conflictingExtension")
@@ -7275,6 +7281,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("commasInConditionWithNoArguments2.kt")
public void testCommasInConditionWithNoArguments2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/when/commasInConditionWithNoArguments2.kt");
doTest(fileName);
}
@TestMetadata("continueInWhen.kt")
public void testContinueInWhen() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/when/continueInWhen.kt");