diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/FindTransformationMatcher.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/FindTransformationMatcher.kt index 96f9f0fb5a8..99f54980004 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/FindTransformationMatcher.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/FindTransformationMatcher.kt @@ -189,6 +189,11 @@ object FindTransformationMatcher : TransformationMatcher { } } + private class NegatingFindOpetationGenerator(val generator: FindOperationGenerator) : FindOperationGenerator(generator) { + override fun generate(chainedCallGenerator: ChainedCallGenerator): KtExpression = + generator.generate(chainedCallGenerator).negate() + } + private fun generateChainedCall( stdlibFunName: String, chainedCallGenerator: ChainedCallGenerator, @@ -347,19 +352,11 @@ object FindTransformationMatcher : TransformationMatcher { val containsArgument = filterExpression.isFilterForContainsOperation(inputVariable, loop) if (containsArgument != null) { val generator = SimpleGenerator("contains", inputVariable, null, containsArgument) - return if (negated) { - object : FindOperationGenerator(generator) { - override fun generate(chainedCallGenerator: ChainedCallGenerator): KtExpression = - generator.generate(chainedCallGenerator).negate() - } - } - else { - generator - } + return if (negated) NegatingFindOpetationGenerator(generator) else generator } - if (filterExpression is KtPrefixExpression && filterExpression.operationToken == KtTokens.EXCL) { - return SimpleGenerator(if (negated) "any" else "none", inputVariable, filter.asNegatedExpression(reformat)) + if (filterExpression is KtPrefixExpression && filterExpression.operationToken == KtTokens.EXCL && negated) { + return SimpleGenerator("all", inputVariable, filter.asNegatedExpression(reformat)) } return SimpleGenerator(if (negated) "none" else "any", inputVariable, filterExpression) diff --git a/idea/testData/intentions/loopToCallChain/any/KT13998.kt b/idea/testData/intentions/loopToCallChain/any/KT13998.kt index 38f05f2e2d6..b0d4258d41b 100644 --- a/idea/testData/intentions/loopToCallChain/any/KT13998.kt +++ b/idea/testData/intentions/loopToCallChain/any/KT13998.kt @@ -1,5 +1,5 @@ // WITH_RUNTIME -// INTENTION_TEXT: "Replace with 'any{}'" +// INTENTION_TEXT: "Replace with 'all{}'" // IS_APPLICABLE_2: false fun foo(): Boolean { val foo = listOf(true, true) diff --git a/idea/testData/intentions/loopToCallChain/any/KT13998.kt.after b/idea/testData/intentions/loopToCallChain/any/KT13998.kt.after index 909c6e656a1..d8c1de8420d 100644 --- a/idea/testData/intentions/loopToCallChain/any/KT13998.kt.after +++ b/idea/testData/intentions/loopToCallChain/any/KT13998.kt.after @@ -1,7 +1,7 @@ // WITH_RUNTIME -// INTENTION_TEXT: "Replace with 'any{}'" +// INTENTION_TEXT: "Replace with 'all{}'" // IS_APPLICABLE_2: false fun foo(): Boolean { val foo = listOf(true, true) - return foo.any { it } + return foo.all { it } } \ No newline at end of file diff --git a/idea/testData/intentions/loopToCallChain/any/KT13998_1.kt b/idea/testData/intentions/loopToCallChain/any/KT13998_1.kt index 8eb03d58069..7b2bbed19f5 100644 --- a/idea/testData/intentions/loopToCallChain/any/KT13998_1.kt +++ b/idea/testData/intentions/loopToCallChain/any/KT13998_1.kt @@ -1,5 +1,5 @@ // WITH_RUNTIME -// INTENTION_TEXT: "Replace with 'any{}'" +// INTENTION_TEXT: "Replace with 'all{}'" // IS_APPLICABLE_2: false fun foo(): Boolean { val foo = listOf(true, true) diff --git a/idea/testData/intentions/loopToCallChain/any/KT13998_1.kt.after b/idea/testData/intentions/loopToCallChain/any/KT13998_1.kt.after index c2b043ec7c2..0854ca09bd9 100644 --- a/idea/testData/intentions/loopToCallChain/any/KT13998_1.kt.after +++ b/idea/testData/intentions/loopToCallChain/any/KT13998_1.kt.after @@ -1,9 +1,9 @@ // WITH_RUNTIME -// INTENTION_TEXT: "Replace with 'any{}'" +// INTENTION_TEXT: "Replace with 'all{}'" // IS_APPLICABLE_2: false fun foo(): Boolean { val foo = listOf(true, true) - return foo.any { f1(it) && f2(it) } + return foo.all { f1(it) && f2(it) } } fun f1(b: Boolean): Boolean = TODO() diff --git a/idea/testData/intentions/loopToCallChain/any/kt17730.kt b/idea/testData/intentions/loopToCallChain/any/kt17730.kt new file mode 100644 index 00000000000..31dfdcf5ac8 --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/any/kt17730.kt @@ -0,0 +1,12 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with 'all{}'" +// IS_APPLICABLE_2: false +fun foo(): Boolean { + val list = listOf(1, 2, 3, 4, 5) + for (e in list) { + if (!(e <= 3)) { + return false + } + } + return true +} diff --git a/idea/testData/intentions/loopToCallChain/any/kt17730.kt.after b/idea/testData/intentions/loopToCallChain/any/kt17730.kt.after new file mode 100644 index 00000000000..4bed3a08072 --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/any/kt17730.kt.after @@ -0,0 +1,7 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with 'all{}'" +// IS_APPLICABLE_2: false +fun foo(): Boolean { + val list = listOf(1, 2, 3, 4, 5) + return list.all { it <= 3 } +} diff --git a/idea/testData/intentions/loopToCallChain/any/kt17730_1.kt b/idea/testData/intentions/loopToCallChain/any/kt17730_1.kt new file mode 100644 index 00000000000..d3f4603afd7 --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/any/kt17730_1.kt @@ -0,0 +1,12 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with 'any{}'" +// IS_APPLICABLE_2: false +fun foo(): Boolean { + val list = listOf(1, 2, 3, 4, 5) + for (e in list) { + if (!(e <= 3)) { + return true + } + } + return false +} diff --git a/idea/testData/intentions/loopToCallChain/any/kt17730_1.kt.after b/idea/testData/intentions/loopToCallChain/any/kt17730_1.kt.after new file mode 100644 index 00000000000..5a8ee65b1fb --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/any/kt17730_1.kt.after @@ -0,0 +1,7 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with 'any{}'" +// IS_APPLICABLE_2: false +fun foo(): Boolean { + val list = listOf(1, 2, 3, 4, 5) + return list.any { !(it <= 3) } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java index 6039e92a798..bff7605a118 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java @@ -194,6 +194,18 @@ public class IntentionTest2Generated extends AbstractIntentionTest2 { doTest(fileName); } + @TestMetadata("kt17730.kt") + public void testKt17730() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/any/kt17730.kt"); + doTest(fileName); + } + + @TestMetadata("kt17730_1.kt") + public void testKt17730_1() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/any/kt17730_1.kt"); + doTest(fileName); + } + @TestMetadata("none.kt") public void testNone() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/any/none.kt"); diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 99b29767d3a..0d80147e0ac 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -10105,6 +10105,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("kt17730.kt") + public void testKt17730() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/any/kt17730.kt"); + doTest(fileName); + } + + @TestMetadata("kt17730_1.kt") + public void testKt17730_1() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/any/kt17730_1.kt"); + doTest(fileName); + } + @TestMetadata("none.kt") public void testNone() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/any/none.kt");