diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt index 8e244f65afa..91cffb8bb66 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt @@ -34,7 +34,8 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode public class AddForLoopIndicesIntention : SelfTargetingRangeIntention(javaClass(), "Add indices to 'for' loop"), LowPriorityAction { - private val WITH_INDEX_FQ_NAME = "kotlin.withIndex" + private val WITH_INDEX_NAME = "withIndex" + private val WITH_INDEX_FQ_NAMES = listOf("collections", "sequences", "text", "ranges").map { "kotlin.$it.$WITH_INDEX_NAME" }.toSet() override fun applicabilityRange(element: KtForExpression): TextRange? { if (element.loopParameter == null) return null @@ -43,14 +44,14 @@ public class AddForLoopIndicesIntention : SelfTargetingRangeIntention(javaClass(), "Replace with a 'for' loop") { + private val FOR_EACH_NAME = "forEach" + private val FOR_EACH_FQ_NAMES = listOf("collections", "sequences", "text", "ranges").map { "kotlin.$it.$FOR_EACH_NAME" }.toSet() + + override fun isApplicableTo(element: KtSimpleNameExpression): Boolean { - if (element.getReferencedName() != "forEach") return false + if (element.getReferencedName() != FOR_EACH_NAME) return false val data = extractData(element) ?: return false if (data.functionLiteral.getValueParameters().size() > 1) return false @@ -61,7 +65,7 @@ public class ConvertForEachToForLoopIntention : SelfTargetingOffsetIndependentIn } ?: return null) as KtExpression //TODO: submit bug val resolvedCall = expression.getResolvedCall(expression.analyze()) ?: return null - if (DescriptorUtils.getFqName(resolvedCall.getResultingDescriptor()).toString() != "kotlin.forEach") return null + if (DescriptorUtils.getFqName(resolvedCall.getResultingDescriptor()).toString() !in FOR_EACH_FQ_NAMES) return null val receiver = resolvedCall.getCall().getExplicitReceiver() as? ExpressionReceiver ?: return null val argument = resolvedCall.getCall().getValueArguments().singleOrNull() ?: return null diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt index d24244108ce..a916f4c6103 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt @@ -41,7 +41,8 @@ public class RemoveForLoopIndicesInspection : IntentionBasedInspection(javaClass(), "Remove indices in 'for' loop") { - private val WITH_INDEX_FQ_NAME = "kotlin.withIndex" + private val WITH_INDEX_NAME = "withIndex" + private val WITH_INDEX_FQ_NAMES = listOf("collections", "sequences", "text", "ranges").map { "kotlin.$it.$WITH_INDEX_NAME" }.toSet() override fun applicabilityRange(element: KtForExpression): TextRange? { val loopRange = element.loopRange as? KtDotQualifiedExpression ?: return null @@ -51,7 +52,7 @@ public class RemoveForLoopIndicesIntention : SelfTargetingRangeIntention { it.equals('a') } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertForEachToForLoop/simpleCharSequence.kt.after b/idea/testData/intentions/convertForEachToForLoop/simpleCharSequence.kt.after new file mode 100644 index 00000000000..60df7970264 --- /dev/null +++ b/idea/testData/intentions/convertForEachToForLoop/simpleCharSequence.kt.after @@ -0,0 +1,8 @@ +// WITH_RUNTIME +fun foo() { + val x = "abcd" + + for (it in x) { + it.equals('a') + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertForEachToForLoop/simpleSequence.kt b/idea/testData/intentions/convertForEachToForLoop/simpleSequence.kt new file mode 100644 index 00000000000..c9b0b0e0935 --- /dev/null +++ b/idea/testData/intentions/convertForEachToForLoop/simpleSequence.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME +fun foo() { + val x = (1..4).asSequence() + + x.forEach { it.equals(1) } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertForEachToForLoop/simpleSequence.kt.after b/idea/testData/intentions/convertForEachToForLoop/simpleSequence.kt.after new file mode 100644 index 00000000000..9b7e01fa651 --- /dev/null +++ b/idea/testData/intentions/convertForEachToForLoop/simpleSequence.kt.after @@ -0,0 +1,8 @@ +// WITH_RUNTIME +fun foo() { + val x = (1..4).asSequence() + + for (it in x) { + it.equals(1) + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndicesOverCharSequence.kt b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndicesOverCharSequence.kt new file mode 100644 index 00000000000..4d777f5bb87 --- /dev/null +++ b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndicesOverCharSequence.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME +fun foo(bar: CharSequence) { + for ((i,a) in bar.withIndex()) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndicesOverCharSequence.kt.after b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndicesOverCharSequence.kt.after new file mode 100644 index 00000000000..c87a0a9ce8c --- /dev/null +++ b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndicesOverCharSequence.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +fun foo(bar: CharSequence) { + for (a in bar) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndicesOverSequence.kt b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndicesOverSequence.kt new file mode 100644 index 00000000000..5107e31cc73 --- /dev/null +++ b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndicesOverSequence.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME +fun foo(bar: Sequence) { + for ((i,a) in bar.withIndex()) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndicesOverSequence.kt.after b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndicesOverSequence.kt.after new file mode 100644 index 00000000000..31f7622c51e --- /dev/null +++ b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndicesOverSequence.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +fun foo(bar: Sequence) { + for (a in bar) { + + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 3c219231c5c..ec5a5bb06d3 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -3187,6 +3187,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("simpleCharSequence.kt") + public void testSimpleCharSequence() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertForEachToForLoop/simpleCharSequence.kt"); + doTest(fileName); + } + + @TestMetadata("simpleSequence.kt") + public void testSimpleSequence() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertForEachToForLoop/simpleSequence.kt"); + doTest(fileName); + } + @TestMetadata("typeArgumentPresent.kt") public void testTypeArgumentPresent() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertForEachToForLoop/typeArgumentPresent.kt"); @@ -6932,6 +6944,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt"); doTest(fileName); } + + @TestMetadata("simpleLoopWithIndicesOverCharSequence.kt") + public void testSimpleLoopWithIndicesOverCharSequence() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndicesOverCharSequence.kt"); + doTest(fileName); + } + + @TestMetadata("simpleLoopWithIndicesOverSequence.kt") + public void testSimpleLoopWithIndicesOverSequence() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndicesOverSequence.kt"); + doTest(fileName); + } } @TestMetadata("idea/testData/intentions/removeUnnecessaryParentheses")