"Replace if with when": Do not remove block braces if block has single lambda expression #KT-26187 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-08-17 13:38:19 +09:00
committed by Mikhail Glukhikh
parent e59427edab
commit 362e6863ac
6 changed files with 57 additions and 1 deletions
@@ -69,7 +69,9 @@ fun KtExpression.unwrapBlockOrParenthesis(): KtExpression {
val innerExpression = KtPsiUtil.safeDeparenthesize(this, true)
if (innerExpression is KtBlockExpression) {
val statement = innerExpression.statements.singleOrNull() ?: return this
return KtPsiUtil.safeDeparenthesize(statement, true)
val deparenthesized = KtPsiUtil.safeDeparenthesize(statement, true)
if (deparenthesized is KtLambdaExpression) return this
return deparenthesized
}
return innerExpression
}
@@ -0,0 +1,9 @@
fun test(x: Int) {
val f: Function<Int> = <caret>if (x == 0) {
{ 0 }
} else if (x == 1) {
{ 1 }
} else {
{ 2 }
}
}
@@ -0,0 +1,13 @@
fun test(x: Int) {
val f: Function<Int> = when (x) {
0 -> {
{ 0 }
}
1 -> {
{ 1 }
}
else -> {
{ 2 }
}
}
}
@@ -0,0 +1,9 @@
fun test(x: Int) {
val f: Function<Int> = <caret>if (x == 0) {
({ 0 })
} else if (x == 1) {
(({ 1 }))
} else {
((({ 2 })))
}
}
@@ -0,0 +1,13 @@
fun test(x: Int) {
val f: Function<Int> = when (x) {
0 -> {
({ 0 })
}
1 -> {
(({ 1 }))
}
else -> {
((({ 2 })))
}
}
}
@@ -2564,6 +2564,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/branched/ifWhen/ifToWhen/ifWithoutElse.kt");
}
@TestMetadata("lambdaExpression.kt")
public void testLambdaExpression() throws Exception {
runTest("idea/testData/intentions/branched/ifWhen/ifToWhen/lambdaExpression.kt");
}
@TestMetadata("lambdaExpression2.kt")
public void testLambdaExpression2() throws Exception {
runTest("idea/testData/intentions/branched/ifWhen/ifToWhen/lambdaExpression2.kt");
}
@TestMetadata("multipleIfFake.kt")
public void testMultipleIfFake() throws Exception {
runTest("idea/testData/intentions/branched/ifWhen/ifToWhen/multipleIfFake.kt");