Rewrite mixed multiple- and single-condition 'when' properly.
#KT-10229 Fixed
This commit is contained in:
+6
-4
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Vendored
+4
-2
@@ -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 */ }
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -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 */ }
|
||||
}
|
||||
}
|
||||
Vendored
+8
@@ -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"
|
||||
}
|
||||
}
|
||||
+8
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user