diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/WhenToIfIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/WhenToIfIntention.kt index 008bc3c2eed..cdb4a518c98 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/WhenToIfIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/WhenToIfIntention.kt @@ -22,6 +22,7 @@ import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.branchedTransformations.combineWhenConditions import org.jetbrains.kotlin.idea.util.CommentSaver +import org.jetbrains.kotlin.psi.KtIfExpression import org.jetbrains.kotlin.psi.KtPsiFactory import org.jetbrains.kotlin.psi.KtWhenExpression import org.jetbrains.kotlin.psi.buildExpression @@ -54,7 +55,13 @@ class WhenToIfIntention : SelfTargetingRangeIntention(KtWhenEx appendFixedText("if (") appendExpression(condition) appendFixedText(")") + if (branch is KtIfExpression) { + appendFixedText("{ ") + } appendExpression(branch) + if (branch is KtIfExpression) { + appendFixedText(" }") + } } if (i != entries.lastIndex) { appendFixedText("\n") diff --git a/idea/testData/intentions/branched/ifWhen/whenToIf/whenWithIf.kt b/idea/testData/intentions/branched/ifWhen/whenToIf/whenWithIf.kt new file mode 100644 index 00000000000..42dce20f3d2 --- /dev/null +++ b/idea/testData/intentions/branched/ifWhen/whenToIf/whenWithIf.kt @@ -0,0 +1,11 @@ +// KT-12942: when to if changes semantics +fun test(b: Boolean): String { + when (b) { + true -> + if (true) return "first" + false -> + if (true) return "second" + } + + return "none" +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifWhen/whenToIf/whenWithIf.kt.after b/idea/testData/intentions/branched/ifWhen/whenToIf/whenWithIf.kt.after new file mode 100644 index 00000000000..9aa8bfea32c --- /dev/null +++ b/idea/testData/intentions/branched/ifWhen/whenToIf/whenWithIf.kt.after @@ -0,0 +1,10 @@ +// KT-12942: when to if changes semantics +fun test(b: Boolean): String { + if (b == true) { + if (true) return "first" + } else if (b == false) { + if (true) return "second" + } + + return "none" +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index ea01b78300e..e8f0832e0d1 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -1711,6 +1711,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("whenWithIf.kt") + public void testWhenWithIf() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithIf.kt"); + doTest(fileName); + } + @TestMetadata("whenWithMultiConditions.kt") public void testWhenWithMultiConditions() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithMultiConditions.kt");