KT-14210 for loop to stdlib: max/min is not recognized if there is additional if

#KT-14210 Fixed
This commit is contained in:
Valentin Kipyatkov
2016-10-14 17:32:35 +03:00
parent 35ed689b9f
commit a89ef54578
6 changed files with 44 additions and 0 deletions
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea.intentions.loopToCallChain.sequence
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isNullExpression
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.*
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.result.FindTransformationMatcher
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.result.MaxOrMinTransformation
import org.jetbrains.kotlin.idea.intentions.negate
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
@@ -69,6 +70,8 @@ abstract class FilterTransformationBase : SequenceTransformation {
while (true) {
currentState = currentState.unwrapBlock()
if (MaxOrMinTransformation.Matcher.match(currentState) != null) break // do not take 'if' which is required for min/max matcher
val (nextTransformation, nextState) = matchOneTransformation(currentState) ?: break
if (nextTransformation !is FilterTransformation) break
assert(nextState.indexVariable == currentState.indexVariable) // indexVariable should not change
@@ -0,0 +1,10 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}.max()'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.max()'"
fun f(list: List<Int>) {
var result = -1
<caret>for (item in list)
if (item % 2 == 0)
if (result <= item)
result = item
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}.max()'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.max()'"
fun f(list: List<Int>) {
val <caret>result = list
.filter { it % 2 == 0 }
.max()
?: -1
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}.max()'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.max()'"
fun f(list: List<Int>) {
val <caret>result = list
.asSequence()
.filter { it % 2 == 0 }
.max()
?: -1
}
@@ -1075,6 +1075,12 @@ public class IntentionTest2Generated extends AbstractIntentionTest2 {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/loopToCallChain/maxMin"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("KT14210.kt")
public void testKT14210() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/maxMin/KT14210.kt");
doTest(fileName);
}
@TestMetadata("max1.kt")
public void testMax1() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/maxMin/max1.kt");
@@ -8983,6 +8983,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/loopToCallChain/maxMin"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("KT14210.kt")
public void testKT14210() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/maxMin/KT14210.kt");
doTest(fileName);
}
@TestMetadata("max1.kt")
public void testMax1() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/maxMin/max1.kt");