When to if now add braces for if branches inside #KT-12942 Fixed
(cherry picked from commit 29a7bfe)
This commit is contained in:
committed by
Mikhail Glukhikh
parent
b675b49daf
commit
3fe114fc24
+7
@@ -22,6 +22,7 @@ import com.intellij.openapi.util.TextRange
|
|||||||
import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention
|
import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention
|
||||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.combineWhenConditions
|
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.combineWhenConditions
|
||||||
import org.jetbrains.kotlin.idea.util.CommentSaver
|
import org.jetbrains.kotlin.idea.util.CommentSaver
|
||||||
|
import org.jetbrains.kotlin.psi.KtIfExpression
|
||||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||||
import org.jetbrains.kotlin.psi.KtWhenExpression
|
import org.jetbrains.kotlin.psi.KtWhenExpression
|
||||||
import org.jetbrains.kotlin.psi.buildExpression
|
import org.jetbrains.kotlin.psi.buildExpression
|
||||||
@@ -54,7 +55,13 @@ class WhenToIfIntention : SelfTargetingRangeIntention<KtWhenExpression>(KtWhenEx
|
|||||||
appendFixedText("if (")
|
appendFixedText("if (")
|
||||||
appendExpression(condition)
|
appendExpression(condition)
|
||||||
appendFixedText(")")
|
appendFixedText(")")
|
||||||
|
if (branch is KtIfExpression) {
|
||||||
|
appendFixedText("{ ")
|
||||||
|
}
|
||||||
appendExpression(branch)
|
appendExpression(branch)
|
||||||
|
if (branch is KtIfExpression) {
|
||||||
|
appendFixedText(" }")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (i != entries.lastIndex) {
|
if (i != entries.lastIndex) {
|
||||||
appendFixedText("\n")
|
appendFixedText("\n")
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
// KT-12942: when to if changes semantics
|
||||||
|
fun test(b: Boolean): String {
|
||||||
|
<caret>when (b) {
|
||||||
|
true ->
|
||||||
|
if (true) return "first"
|
||||||
|
false ->
|
||||||
|
if (true) return "second"
|
||||||
|
}
|
||||||
|
|
||||||
|
return "none"
|
||||||
|
}
|
||||||
@@ -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"
|
||||||
|
}
|
||||||
@@ -1711,6 +1711,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("whenWithMultiConditions.kt")
|
||||||
public void testWhenWithMultiConditions() throws Exception {
|
public void testWhenWithMultiConditions() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithMultiConditions.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithMultiConditions.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user