"Replace 'when' with 'if'" intention: do not suggest if 'when' subject is variable declaration
#KT-35528 Fixed
This commit is contained in:
committed by
Dmitry Gridin
parent
1cdcef3b08
commit
49e5f170d1
+2
-4
@@ -11,10 +11,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.*
|
||||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
|
||||||
import org.jetbrains.kotlin.psi.KtWhenExpression
|
|
||||||
import org.jetbrains.kotlin.psi.buildExpression
|
|
||||||
|
|
||||||
class WhenToIfIntention : SelfTargetingRangeIntention<KtWhenExpression>(KtWhenExpression::class.java, "Replace 'when' with 'if'"),
|
class WhenToIfIntention : SelfTargetingRangeIntention<KtWhenExpression>(KtWhenExpression::class.java, "Replace 'when' with 'if'"),
|
||||||
LowPriorityAction {
|
LowPriorityAction {
|
||||||
@@ -24,6 +21,7 @@ class WhenToIfIntention : SelfTargetingRangeIntention<KtWhenExpression>(KtWhenEx
|
|||||||
val lastEntry = entries.last()
|
val lastEntry = entries.last()
|
||||||
if (entries.any { it != lastEntry && it.isElse }) return null
|
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 (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
|
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");
|
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")
|
@TestMetadata("whenWithDotQualifiedExpression.kt")
|
||||||
public void testWhenWithDotQualifiedExpression() throws Exception {
|
public void testWhenWithDotQualifiedExpression() throws Exception {
|
||||||
runTest("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithDotQualifiedExpression.kt");
|
runTest("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithDotQualifiedExpression.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user