From a89ef54578a973bff4dade53b2b1903d08d2db97 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Fri, 14 Oct 2016 17:32:35 +0300 Subject: [PATCH] KT-14210 for loop to stdlib: max/min is not recognized if there is additional if #KT-14210 Fixed --- .../loopToCallChain/sequence/FilterTransformation.kt | 3 +++ .../intentions/loopToCallChain/maxMin/KT14210.kt | 10 ++++++++++ .../intentions/loopToCallChain/maxMin/KT14210.kt.after | 9 +++++++++ .../loopToCallChain/maxMin/KT14210.kt.after2 | 10 ++++++++++ .../idea/intentions/IntentionTest2Generated.java | 6 ++++++ .../kotlin/idea/intentions/IntentionTestGenerated.java | 6 ++++++ 6 files changed, 44 insertions(+) create mode 100644 idea/testData/intentions/loopToCallChain/maxMin/KT14210.kt create mode 100644 idea/testData/intentions/loopToCallChain/maxMin/KT14210.kt.after create mode 100644 idea/testData/intentions/loopToCallChain/maxMin/KT14210.kt.after2 diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/FilterTransformation.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/FilterTransformation.kt index 070fdee09d1..95f1edbb933 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/FilterTransformation.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/FilterTransformation.kt @@ -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 diff --git a/idea/testData/intentions/loopToCallChain/maxMin/KT14210.kt b/idea/testData/intentions/loopToCallChain/maxMin/KT14210.kt new file mode 100644 index 00000000000..3efb0b950a4 --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/maxMin/KT14210.kt @@ -0,0 +1,10 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with 'filter{}.max()'" +// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.max()'" +fun f(list: List) { + var result = -1 + for (item in list) + if (item % 2 == 0) + if (result <= item) + result = item +} \ No newline at end of file diff --git a/idea/testData/intentions/loopToCallChain/maxMin/KT14210.kt.after b/idea/testData/intentions/loopToCallChain/maxMin/KT14210.kt.after new file mode 100644 index 00000000000..20c5e32d6d6 --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/maxMin/KT14210.kt.after @@ -0,0 +1,9 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with 'filter{}.max()'" +// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.max()'" +fun f(list: List) { + val result = list + .filter { it % 2 == 0 } + .max() + ?: -1 +} \ No newline at end of file diff --git a/idea/testData/intentions/loopToCallChain/maxMin/KT14210.kt.after2 b/idea/testData/intentions/loopToCallChain/maxMin/KT14210.kt.after2 new file mode 100644 index 00000000000..1c3db9b7e12 --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/maxMin/KT14210.kt.after2 @@ -0,0 +1,10 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with 'filter{}.max()'" +// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.max()'" +fun f(list: List) { + val result = list + .asSequence() + .filter { it % 2 == 0 } + .max() + ?: -1 +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java index 71f6cd62989..e9876f48dbe 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java @@ -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"); diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 7104f632414..647063714aa 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -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");