Fixed bug in MergeWhenIntention

This commit is contained in:
Valentin Kipyatkov
2015-05-12 17:51:32 +03:00
parent 6456729973
commit 38ac420057
3 changed files with 27 additions and 1 deletions
@@ -21,6 +21,7 @@ import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStableVariable
import org.jetbrains.kotlin.idea.util.psi.patternMatching.matches
import org.jetbrains.kotlin.idea.util.psi.patternMatching.toRange
import org.jetbrains.kotlin.psi.*
@@ -30,7 +31,10 @@ public class MergeWhenIntention : JetSelfTargetingRangeIntention<JetWhenExpressi
override fun applicabilityRange(element: JetWhenExpression): TextRange? {
val next = PsiTreeUtil.skipSiblingsForward(element, javaClass<PsiWhiteSpace>()) as? JetWhenExpression ?: return null
if (!element.getSubjectExpression().matches(next.getSubjectExpression())) return null
val subject1 = element.getSubjectExpression()
val subject2 = next.getSubjectExpression()
if (!subject1.matches(subject2)) return null
if (subject1 != null && !subject1.isStableVariable()) return null
val entries1 = element.getEntries()
val entries2 = next.getEntries()
@@ -0,0 +1,16 @@
// IS_APPLICABLE: false
fun doSomething<T>(a: T) {}
fun test() {
var n = 0
<caret>when (n) {
1 -> n = 2
2 -> n = 1
}
when (n) {
1 -> doSomething("A")
2 -> doSomething("B")
}
}
@@ -1921,6 +1921,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("mergeWithVarSubject.kt")
public void testMergeWithVarSubject() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/branched/when/merge/mergeWithVarSubject.kt");
doTest(fileName);
}
@TestMetadata("mergeWithoutSubject.kt")
public void testMergeWithoutSubject() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/branched/when/merge/mergeWithoutSubject.kt");