"Replace 'when' with 'if'" intention: do not suggest if 'when' subject is variable declaration

#KT-35528 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-12-19 09:22:47 +09:00
committed by Dmitry Gridin
parent 1cdcef3b08
commit 49e5f170d1
3 changed files with 20 additions and 4 deletions
@@ -11,10 +11,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
import org.jetbrains.kotlin.psi.*
class WhenToIfIntention : SelfTargetingRangeIntention<KtWhenExpression>(KtWhenExpression::class.java, "Replace 'when' with 'if'"),
LowPriorityAction {
@@ -24,6 +21,7 @@ class WhenToIfIntention : SelfTargetingRangeIntention<KtWhenExpression>(KtWhenEx
val lastEntry = entries.last()
if (entries.any { it != lastEntry && it.isElse }) return null
if (entries.all { it.isElse }) return null // 'when' with only 'else' branch is not supported
if (element.subjectExpression is KtProperty) return null
return element.whenKeyword.textRange
}
@@ -0,0 +1,13 @@
// IS_APPLICABLE: false
enum class Type {
HYDRO,
PYRO
}
fun select(t: Type): Int {
return <caret>when (val i = t.ordinal) {
0 -> 1
1 -> 42
else -> i
}
}
@@ -2525,6 +2525,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/branched/ifWhen/whenToIf/kt13884.kt");
}
@TestMetadata("variableSubject.kt")
public void testVariableSubject() throws Exception {
runTest("idea/testData/intentions/branched/ifWhen/whenToIf/variableSubject.kt");
}
@TestMetadata("whenWithDotQualifiedExpression.kt")
public void testWhenWithDotQualifiedExpression() throws Exception {
runTest("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithDotQualifiedExpression.kt");