KT-14210 for loop to stdlib: max/min is not recognized if there is additional if
#KT-14210 Fixed
This commit is contained in:
+3
@@ -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.branchedTransformations.isNullExpression
|
||||||
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.*
|
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.*
|
||||||
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.result.FindTransformationMatcher
|
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.idea.intentions.negate
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
@@ -69,6 +70,8 @@ abstract class FilterTransformationBase : SequenceTransformation {
|
|||||||
while (true) {
|
while (true) {
|
||||||
currentState = currentState.unwrapBlock()
|
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
|
val (nextTransformation, nextState) = matchOneTransformation(currentState) ?: break
|
||||||
if (nextTransformation !is FilterTransformation) break
|
if (nextTransformation !is FilterTransformation) break
|
||||||
assert(nextState.indexVariable == currentState.indexVariable) // indexVariable should not change
|
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);
|
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")
|
@TestMetadata("max1.kt")
|
||||||
public void testMax1() throws Exception {
|
public void testMax1() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/maxMin/max1.kt");
|
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);
|
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")
|
@TestMetadata("max1.kt")
|
||||||
public void testMax1() throws Exception {
|
public void testMax1() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/maxMin/max1.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/maxMin/max1.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user