From 97010c7d7f2878e76e5ead8761022e3d3f19e4b3 Mon Sep 17 00:00:00 2001 From: Ross Hanson Date: Fri, 11 Apr 2014 13:07:26 -0400 Subject: [PATCH 01/19] KT-4820: Intention to introduce an index variable to a for loop over an ordered collection via "withIndices()." --- .../jet/generators/tests/GenerateTests.kt | 1 + .../after.kt.template | 3 + .../before.kt.template | 3 + .../description.html | 5 ++ idea/src/META-INF/plugin.xml | 5 ++ .../jetbrains/jet/plugin/JetBundle.properties | 2 + .../intentions/AddForLoopIndicesIntention.kt | 87 +++++++++++++++++++ .../inapplicableExistingIndices.kt | 7 ++ .../inapplicableUnorderedCollection.kt | 7 ++ .../listWithType.kt | 7 ++ .../listWithType.kt.after | 7 ++ .../simpleIntList.kt | 7 ++ .../simpleIntList.kt.after | 7 ++ .../simpleSortedMap.kt | 6 ++ .../simpleSortedMap.kt.after | 6 ++ .../AbstractCodeTransformationTest.java | 4 + .../CodeTransformationTestGenerated.java | 36 +++++++- 17 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 idea/resources/intentionDescriptions/AddForLoopIndicesIntention/after.kt.template create mode 100644 idea/resources/intentionDescriptions/AddForLoopIndicesIntention/before.kt.template create mode 100644 idea/resources/intentionDescriptions/AddForLoopIndicesIntention/description.html create mode 100644 idea/src/org/jetbrains/jet/plugin/intentions/AddForLoopIndicesIntention.kt create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/inapplicableUnorderedCollection.kt create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt.after create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt.after diff --git a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt index b7cda2aeb85..e017cb32dca 100644 --- a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt @@ -414,6 +414,7 @@ fun main(args: Array) { model("intentions/makeTypeImplicitInLambda", testMethod = "doTestMakeTypeImplicitInLambda") model("intentions/convertToForEachLoop", testMethod = "doTestConvertToForEachLoop") model("intentions/convertToForEachFunctionCall", testMethod = "doTestConvertToForEachFunctionCall") + model("intentions/addForLoopIndicesIntention", testMethod = "doTestAddForLoopIndicesIntention") } testClass(javaClass()) { diff --git a/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/after.kt.template b/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/after.kt.template new file mode 100644 index 00000000000..6c743b16877 --- /dev/null +++ b/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/after.kt.template @@ -0,0 +1,3 @@ +for ((i, x) in foo.withIndices()) { + +} \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/before.kt.template b/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/before.kt.template new file mode 100644 index 00000000000..8b1fae6e977 --- /dev/null +++ b/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/before.kt.template @@ -0,0 +1,3 @@ +for (x in foo) { + +} \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/description.html b/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/description.html new file mode 100644 index 00000000000..2017c1d8723 --- /dev/null +++ b/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/description.html @@ -0,0 +1,5 @@ + + +This intention adds an index variable to a for loop iterating over a collection. + + diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 4aec75214d2..5ec97031ea1 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -656,6 +656,11 @@ Kotlin + + org.jetbrains.jet.plugin.intentions.AddForLoopIndicesIntention + Kotlin + + org.jetbrains.jet.plugin.intentions.SwapBinaryExpression Kotlin diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index 49c3e205664..e1b8ecbd9aa 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -292,6 +292,8 @@ convert.negated.boolean.sequence=Replace negated sequence with DeMorgan equivale convert.negated.boolean.sequence.family=Replace negated sequence with DeMorgan equivalent convert.negated.expression.with.demorgans.law=Convert negated expression to DeMorgan's equivalent convert.negated.expression.with.demorgans.law.family=Convert negated expression to DeMorgan's equivalent +add.for.loop.indices=Add indices to for loop +add.for.loop.indices.family=Add indices to for loop split.if=Split into 2 if's split.if.family=Split If replace.with.operator.assign.intention=Replace with an Operator-Assign Expression diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/AddForLoopIndicesIntention.kt b/idea/src/org/jetbrains/jet/plugin/intentions/AddForLoopIndicesIntention.kt new file mode 100644 index 00000000000..e35eac6c5d8 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/intentions/AddForLoopIndicesIntention.kt @@ -0,0 +1,87 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.intentions + +import com.intellij.openapi.editor.Editor +import org.jetbrains.jet.lang.psi.JetForExpression +import org.jetbrains.jet.lang.psi.JetPsiFactory +import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression +import com.intellij.psi.util.PsiTreeUtil +import com.intellij.psi.PsiDocumentManager +import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache +import org.jetbrains.jet.di.InjectorForMacros +import org.jetbrains.jet.lang.resolve.BindingContext +import org.jetbrains.jet.lang.types.TypeUtils +import org.jetbrains.jet.lang.resolve.ObservableBindingTrace +import org.jetbrains.jet.lang.resolve.BindingTraceContext +import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo +import org.jetbrains.jet.lang.psi.JetParenthesizedExpression +import com.intellij.codeInsight.template.TemplateBuilderImpl +import com.intellij.codeInsight.template.impl.TemplateManagerImpl +import org.jetbrains.jet.lang.resolve.DescriptorUtils +import org.jetbrains.jet.lang.descriptors.ModuleDescriptor + + +public class AddForLoopIndicesIntention : JetSelfTargetingIntention( + "add.for.loop.indices", javaClass()) { + override fun applyTo(element: JetForExpression, editor: Editor) { + val loopRange = element.getLoopRange()!! + val newRangeText = "${loopRange.getText()}.withIndices()" + val newRange = JetPsiFactory.createExpression(editor.getProject(), newRangeText) + + //Roundabout way to create new multiparameter element so as not to incorrectly trigger syntax error highlighting + val loopParameter = element.getLoopParameter()!! + val parenthesizedParam = JetPsiFactory.createExpression(editor.getProject(), "(index)") as JetParenthesizedExpression + val indexElement = parenthesizedParam.getExpression()!! + val comma = JetPsiFactory.createComma(editor.getProject()) + val newParamElement = JetPsiFactory.createExpression(editor.getProject(), " ${loopParameter.getText()}") + parenthesizedParam.addAfter(newParamElement, indexElement) + parenthesizedParam.addAfter(comma, indexElement) + + loopParameter.replace(parenthesizedParam) + loopRange.replace(newRange) + + val multiParameter = PsiTreeUtil.findChildOfType(element, indexElement.javaClass)!! + + editor.getCaretModel().moveToOffset(multiParameter.getTextOffset()) + val templateBuilder = TemplateBuilderImpl(multiParameter) + templateBuilder.replaceElement(multiParameter, "index") + val manager = TemplateManagerImpl(editor.getProject()) + PsiDocumentManager.getInstance(editor.getProject()!!).doPostponedOperationsAndUnblockDocument(editor.getDocument()) + manager.startTemplate(editor, templateBuilder.buildInlineTemplate()!!) + } + + override fun isApplicableTo(element: JetForExpression): Boolean { + if (element.getLoopParameter() == null) return false + val range = element.getLoopRange() ?: return false + if (range is JetDotQualifiedExpression) { + val selector = (range as JetDotQualifiedExpression).getSelectorExpression() ?: return true + if (selector.getText().equals("withIndices()")) return false + } + + val potentialExpression = JetPsiFactory.createExpression(element.getProject(), "${range.getText()}.withIndices()") + val bindingContext = AnalyzerFacadeWithCache.getContextForElement(element) + val scope = bindingContext[BindingContext.RESOLUTION_SCOPE, element] ?: return false + val module = DescriptorUtils.getParentOfType(scope.getContainingDeclaration(), javaClass())!! + val components = InjectorForMacros(element.getProject(), module) + val dataFlowInfo = bindingContext[BindingContext.NON_DEFAULT_EXPRESSION_DATA_FLOW, element] ?: DataFlowInfo.EMPTY + val bindingTrace = ObservableBindingTrace(BindingTraceContext()) + val expressionType = components.getExpressionTypingServices()!!.getTypeInfo(scope, potentialExpression, TypeUtils.NO_EXPECTED_TYPE, dataFlowInfo, bindingTrace) + return expressionType.getType() != null + } + +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt new file mode 100644 index 00000000000..486762bc85c --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt @@ -0,0 +1,7 @@ +//IS_APPLICABLE: FALSE +//ERROR: Unresolved reference: withIndices +fun b(c: List) { + for ((indexVariable, d) in c.withIndices()) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableUnorderedCollection.kt b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableUnorderedCollection.kt new file mode 100644 index 00000000000..7a8f65b05a4 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableUnorderedCollection.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: FALSE +// ERROR: For-loop range must have an iterator() method +fun foo(bar: Map) { + for (a in bar) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt b/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt new file mode 100644 index 00000000000..87e8b8a6e66 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt @@ -0,0 +1,7 @@ +// ERROR: Unresolved reference: listOf +fun a() { + val b = listOf(1,2,3,4,5) + for (c : Int in b) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after new file mode 100644 index 00000000000..652142c4963 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after @@ -0,0 +1,7 @@ +// ERROR: Unresolved reference: listOf +fun a() { + val b = listOf(1,2,3,4,5) + for ((index, c : Int) in b.withIndices()) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt b/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt new file mode 100644 index 00000000000..b3a5c63ecf1 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt @@ -0,0 +1,7 @@ +// ERROR: Unresolved reference: listOf +fun a() { + val b = listOf(1,2,3,4,5) + for (c in b) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt.after new file mode 100644 index 00000000000..51d6a853ab6 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt.after @@ -0,0 +1,7 @@ +// ERROR: Unresolved reference: listOf +fun a() { + val b = listOf(1,2,3,4,5) + for ((index, c) in b.withIndices()) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt b/idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt new file mode 100644 index 00000000000..dea2fc57b32 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt @@ -0,0 +1,6 @@ +//ERROR: Unresolved reference: SortedMap +fun a(b: SortedMap) { + for (c in b) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt.after new file mode 100644 index 00000000000..dffa48df2f5 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt.after @@ -0,0 +1,6 @@ +//ERROR: Unresolved reference: SortedMap +fun a(b: SortedMap) { + for ((index, c) in b.withIndices()) { + + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java b/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java index 3c807bcdf24..1bc7e13a38b 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java @@ -266,6 +266,10 @@ public abstract class AbstractCodeTransformationTest extends LightCodeInsightTes doTestIntention(path, new MakeTypeImplicitInLambdaIntention()); } + public void doTestAddForLoopIndicesIntention(@NotNull String path) throws Exception { + doTestIntention(path, new AddForLoopIndicesIntention()); + } + public void doTestConvertToForEachLoop(@NotNull String path) throws Exception { doTestIntention(path, new ConvertToForEachLoopIntention()); } diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java index f1fdf1a6ed1..cfac143f5e0 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java @@ -30,7 +30,7 @@ import org.jetbrains.jet.plugin.intentions.AbstractCodeTransformationTest; /** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ @SuppressWarnings("all") -@InnerTestClasses({CodeTransformationTestGenerated.DoubleBangToIfThen.class, CodeTransformationTestGenerated.IfThenToDoubleBang.class, CodeTransformationTestGenerated.ElvisToIfThen.class, CodeTransformationTestGenerated.IfThenToElvis.class, CodeTransformationTestGenerated.SafeAccessToIfThen.class, CodeTransformationTestGenerated.IfThenToSafeAccess.class, CodeTransformationTestGenerated.IfToAssignment.class, CodeTransformationTestGenerated.IfToReturn.class, CodeTransformationTestGenerated.IfToReturnAsymmetrically.class, CodeTransformationTestGenerated.WhenToAssignment.class, CodeTransformationTestGenerated.WhenToReturn.class, CodeTransformationTestGenerated.AssignmentToIf.class, CodeTransformationTestGenerated.AssignmentToWhen.class, CodeTransformationTestGenerated.PropertyToIf.class, CodeTransformationTestGenerated.PropertyToWhen.class, CodeTransformationTestGenerated.ReturnToIf.class, CodeTransformationTestGenerated.ReturnToWhen.class, CodeTransformationTestGenerated.IfToWhen.class, CodeTransformationTestGenerated.WhenToIf.class, CodeTransformationTestGenerated.Flatten.class, CodeTransformationTestGenerated.Merge.class, CodeTransformationTestGenerated.IntroduceSubject.class, CodeTransformationTestGenerated.EliminateSubject.class, CodeTransformationTestGenerated.Split.class, CodeTransformationTestGenerated.Join.class, CodeTransformationTestGenerated.ConvertMemberToExtension.class, CodeTransformationTestGenerated.ReconstructedType.class, CodeTransformationTestGenerated.RemoveUnnecessaryParentheses.class, CodeTransformationTestGenerated.ReplaceWithDotQualifiedMethodCall.class, CodeTransformationTestGenerated.ReplaceWithInfixFunctionCall.class, CodeTransformationTestGenerated.RemoveCurlyBracesFromTemplate.class, CodeTransformationTestGenerated.MoveLambdaInsideParentheses.class, CodeTransformationTestGenerated.MoveLambdaOutsideParentheses.class, CodeTransformationTestGenerated.ReplaceExplicitFunctionLiteralParamWithIt.class, CodeTransformationTestGenerated.ReplaceItWithExplicitFunctionLiteralParam.class, CodeTransformationTestGenerated.RemoveBraces.class, CodeTransformationTestGenerated.AddBraces.class, CodeTransformationTestGenerated.ReplaceGetIntention.class, CodeTransformationTestGenerated.ReplaceContainsIntention.class, CodeTransformationTestGenerated.ReplaceBinaryInfixIntention.class, CodeTransformationTestGenerated.ReplaceUnaryPrefixIntention.class, CodeTransformationTestGenerated.ReplaceInvokeIntention.class, CodeTransformationTestGenerated.SimplifyNegatedBinaryExpressionIntention.class, CodeTransformationTestGenerated.ConvertNegatedBooleanSequence.class, CodeTransformationTestGenerated.ConvertNegatedExpressionWithDemorgansLaw.class, CodeTransformationTestGenerated.SwapBinaryExpression.class, CodeTransformationTestGenerated.SplitIf.class, CodeTransformationTestGenerated.ReplaceWithOperatorAssign.class, CodeTransformationTestGenerated.ReplaceWithTraditionalAssignment.class, CodeTransformationTestGenerated.SimplifyBooleanWithConstants.class, CodeTransformationTestGenerated.InsertExplicitTypeArguments.class, CodeTransformationTestGenerated.RemoveExplicitTypeArguments.class, CodeTransformationTestGenerated.ConvertAssertToIf.class, CodeTransformationTestGenerated.ConvertIfToAssert.class, CodeTransformationTestGenerated.MakeTypeExplicitInLambda.class, CodeTransformationTestGenerated.MakeTypeImplicitInLambda.class, CodeTransformationTestGenerated.ConvertToForEachLoop.class, CodeTransformationTestGenerated.ConvertToForEachFunctionCall.class}) +@InnerTestClasses({CodeTransformationTestGenerated.DoubleBangToIfThen.class, CodeTransformationTestGenerated.IfThenToDoubleBang.class, CodeTransformationTestGenerated.ElvisToIfThen.class, CodeTransformationTestGenerated.IfThenToElvis.class, CodeTransformationTestGenerated.SafeAccessToIfThen.class, CodeTransformationTestGenerated.IfThenToSafeAccess.class, CodeTransformationTestGenerated.IfToAssignment.class, CodeTransformationTestGenerated.IfToReturn.class, CodeTransformationTestGenerated.IfToReturnAsymmetrically.class, CodeTransformationTestGenerated.WhenToAssignment.class, CodeTransformationTestGenerated.WhenToReturn.class, CodeTransformationTestGenerated.AssignmentToIf.class, CodeTransformationTestGenerated.AssignmentToWhen.class, CodeTransformationTestGenerated.PropertyToIf.class, CodeTransformationTestGenerated.PropertyToWhen.class, CodeTransformationTestGenerated.ReturnToIf.class, CodeTransformationTestGenerated.ReturnToWhen.class, CodeTransformationTestGenerated.IfToWhen.class, CodeTransformationTestGenerated.WhenToIf.class, CodeTransformationTestGenerated.Flatten.class, CodeTransformationTestGenerated.Merge.class, CodeTransformationTestGenerated.IntroduceSubject.class, CodeTransformationTestGenerated.EliminateSubject.class, CodeTransformationTestGenerated.Split.class, CodeTransformationTestGenerated.Join.class, CodeTransformationTestGenerated.ConvertMemberToExtension.class, CodeTransformationTestGenerated.ReconstructedType.class, CodeTransformationTestGenerated.RemoveUnnecessaryParentheses.class, CodeTransformationTestGenerated.ReplaceWithDotQualifiedMethodCall.class, CodeTransformationTestGenerated.ReplaceWithInfixFunctionCall.class, CodeTransformationTestGenerated.RemoveCurlyBracesFromTemplate.class, CodeTransformationTestGenerated.MoveLambdaInsideParentheses.class, CodeTransformationTestGenerated.MoveLambdaOutsideParentheses.class, CodeTransformationTestGenerated.ReplaceExplicitFunctionLiteralParamWithIt.class, CodeTransformationTestGenerated.ReplaceItWithExplicitFunctionLiteralParam.class, CodeTransformationTestGenerated.RemoveBraces.class, CodeTransformationTestGenerated.AddBraces.class, CodeTransformationTestGenerated.ReplaceGetIntention.class, CodeTransformationTestGenerated.ReplaceContainsIntention.class, CodeTransformationTestGenerated.ReplaceBinaryInfixIntention.class, CodeTransformationTestGenerated.ReplaceUnaryPrefixIntention.class, CodeTransformationTestGenerated.ReplaceInvokeIntention.class, CodeTransformationTestGenerated.SimplifyNegatedBinaryExpressionIntention.class, CodeTransformationTestGenerated.ConvertNegatedBooleanSequence.class, CodeTransformationTestGenerated.ConvertNegatedExpressionWithDemorgansLaw.class, CodeTransformationTestGenerated.SwapBinaryExpression.class, CodeTransformationTestGenerated.SplitIf.class, CodeTransformationTestGenerated.ReplaceWithOperatorAssign.class, CodeTransformationTestGenerated.ReplaceWithTraditionalAssignment.class, CodeTransformationTestGenerated.SimplifyBooleanWithConstants.class, CodeTransformationTestGenerated.InsertExplicitTypeArguments.class, CodeTransformationTestGenerated.RemoveExplicitTypeArguments.class, CodeTransformationTestGenerated.ConvertAssertToIf.class, CodeTransformationTestGenerated.ConvertIfToAssert.class, CodeTransformationTestGenerated.MakeTypeExplicitInLambda.class, CodeTransformationTestGenerated.MakeTypeImplicitInLambda.class, CodeTransformationTestGenerated.ConvertToForEachLoop.class, CodeTransformationTestGenerated.ConvertToForEachFunctionCall.class, CodeTransformationTestGenerated.AddForLoopIndicesIntention.class}) public class CodeTransformationTestGenerated extends AbstractCodeTransformationTest { @TestMetadata("idea/testData/intentions/branched/doubleBangToIfThen") public static class DoubleBangToIfThen extends AbstractCodeTransformationTest { @@ -4239,6 +4239,39 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT doTestConvertToForEachFunctionCall("idea/testData/intentions/convertToForEachFunctionCall/typeAnnotatedWithNonBlockBody.kt"); } + } + + @TestMetadata("idea/testData/intentions/addForLoopIndicesIntention") + public static class AddForLoopIndicesIntention extends AbstractCodeTransformationTest { + public void testAllFilesPresentInAddForLoopIndicesIntention() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/intentions/addForLoopIndicesIntention"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("inapplicableExistingIndices.kt") + public void testInapplicableExistingIndices() throws Exception { + doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt"); + } + + @TestMetadata("inapplicableUnorderedCollection.kt") + public void testInapplicableUnorderedCollection() throws Exception { + doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/inapplicableUnorderedCollection.kt"); + } + + @TestMetadata("listWithType.kt") + public void testListWithType() throws Exception { + doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt"); + } + + @TestMetadata("simpleIntList.kt") + public void testSimpleIntList() throws Exception { + doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt"); + } + + @TestMetadata("simpleSortedMap.kt") + public void testSimpleSortedMap() throws Exception { + doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt"); + } + } public static Test suite() { @@ -4301,6 +4334,7 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT suite.addTestSuite(MakeTypeImplicitInLambda.class); suite.addTestSuite(ConvertToForEachLoop.class); suite.addTestSuite(ConvertToForEachFunctionCall.class); + suite.addTestSuite(AddForLoopIndicesIntention.class); return suite; } } From 35c5a62b111a149d9f04b980baf807e54592a598 Mon Sep 17 00:00:00 2001 From: Ross Hanson Date: Mon, 21 Apr 2014 09:31:28 -0400 Subject: [PATCH 02/19] KT-4820: Intention to remove an unused index variable from a for loop. --- .../jet/generators/tests/GenerateTests.kt | 1 + .../after.kt.template | 3 + .../before.kt.template | 3 + .../description.html | 5 ++ idea/src/META-INF/plugin.xml | 5 ++ .../jetbrains/jet/plugin/JetBundle.properties | 2 + .../RemoveForLoopIndicesIntention.kt | 83 +++++++++++++++++++ .../inapplicableForLoop.kt | 6 ++ .../inapplicableIndexUse.kt | 7 ++ .../loopWithType.kt | 6 ++ .../loopWithType.kt.after | 6 ++ .../simpleLoopWithIndices.kt | 6 ++ .../simpleLoopWithIndices.kt.after | 6 ++ .../AbstractCodeTransformationTest.java | 4 + .../CodeTransformationTestGenerated.java | 33 +++++++- 15 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/after.kt.template create mode 100644 idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/before.kt.template create mode 100644 idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/description.html create mode 100644 idea/src/org/jetbrains/jet/plugin/intentions/RemoveForLoopIndicesIntention.kt create mode 100644 idea/testData/intentions/removeForLoopIndicesIntention/inapplicableForLoop.kt create mode 100644 idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt create mode 100644 idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt create mode 100644 idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt.after create mode 100644 idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt create mode 100644 idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt.after diff --git a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt index e017cb32dca..786f8c455bf 100644 --- a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt @@ -415,6 +415,7 @@ fun main(args: Array) { model("intentions/convertToForEachLoop", testMethod = "doTestConvertToForEachLoop") model("intentions/convertToForEachFunctionCall", testMethod = "doTestConvertToForEachFunctionCall") model("intentions/addForLoopIndicesIntention", testMethod = "doTestAddForLoopIndicesIntention") + model("intentions/removeForLoopIndicesIntention", testMethod = "doTestRemoveForLoopIndicesIntention") } testClass(javaClass()) { diff --git a/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/after.kt.template b/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/after.kt.template new file mode 100644 index 00000000000..8b1fae6e977 --- /dev/null +++ b/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/after.kt.template @@ -0,0 +1,3 @@ +for (x in foo) { + +} \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/before.kt.template b/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/before.kt.template new file mode 100644 index 00000000000..362674c6894 --- /dev/null +++ b/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/before.kt.template @@ -0,0 +1,3 @@ +for ((i,x) in foo.withIndices()) { + +} \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/description.html b/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/description.html new file mode 100644 index 00000000000..dfdfe1e2a0c --- /dev/null +++ b/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/description.html @@ -0,0 +1,5 @@ + + +This intention removes the index variable from a for loop iterating over a collection. + + diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 5ec97031ea1..89c1a612d3e 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -661,6 +661,11 @@ Kotlin + + org.jetbrains.jet.plugin.intentions.RemoveForLoopIndicesIntention + Kotlin + S + org.jetbrains.jet.plugin.intentions.SwapBinaryExpression Kotlin diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index e1b8ecbd9aa..46ccada2a0d 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -294,6 +294,8 @@ convert.negated.expression.with.demorgans.law=Convert negated expression to DeMo convert.negated.expression.with.demorgans.law.family=Convert negated expression to DeMorgan's equivalent add.for.loop.indices=Add indices to for loop add.for.loop.indices.family=Add indices to for loop +remove.for.loop.indices=Remove indices from for loop +remove.for.loop.indices.family=Remove indices from for loop split.if=Split into 2 if's split.if.family=Split If replace.with.operator.assign.intention=Replace with an Operator-Assign Expression diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/RemoveForLoopIndicesIntention.kt b/idea/src/org/jetbrains/jet/plugin/intentions/RemoveForLoopIndicesIntention.kt new file mode 100644 index 00000000000..593902be45e --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/intentions/RemoveForLoopIndicesIntention.kt @@ -0,0 +1,83 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.intentions + +import org.jetbrains.jet.lang.psi.JetForExpression +import com.intellij.openapi.editor.Editor +import org.jetbrains.jet.lang.psi.JetPsiFactory +import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression +import org.jetbrains.jet.lang.psi.JetPsiUtil +import org.jetbrains.jet.lang.cfg.PseudocodeVariablesData.VariableUseState +import org.jetbrains.jet.lang.cfg.pseudocode.PseudocodeUtil +import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache +import org.jetbrains.jet.lang.psi.JetFile +import org.jetbrains.jet.lang.resolve.ObservableBindingTrace +import org.jetbrains.jet.lang.resolve.BindingTraceContext +import org.jetbrains.jet.lang.cfg.JetFlowInformationProvider +import org.jetbrains.jet.lang.cfg.JetControlFlowProcessor +import com.intellij.debugger.jdi.LocalVariablesUtil +import com.siyeh.ig.psiutils.VariableAccessUtils +import com.google.dart.compiler.util.AstUtil +import com.intellij.find.FindManager +import com.intellij.find.impl.FindManagerImpl +import com.intellij.usageView.UsageInfo +import com.intellij.util.Processor +import org.jetbrains.jet.plugin.findUsages.KotlinPropertyFindUsagesOptions + +public class RemoveForLoopIndicesIntention : JetSelfTargetingIntention( + "remove.for.loop.indices", javaClass()) { + override fun applyTo(element: JetForExpression, editor: Editor) { + val parameter = element.getMultiParameter()!! + val range = element.getLoopRange() as JetDotQualifiedExpression + val parameters = parameter.getEntries() + if (parameters.size() == 2) { + parameter.replace(parameters[1]) + } else { + JetPsiUtil.deleteElementWithDelimiters(parameters[0]) + } + + range.replace(range.getReceiverExpression()) + } + + override fun isApplicableTo(element: JetForExpression): Boolean { + if (element.getMultiParameter() == null) return false + val range = element.getLoopRange() as? JetDotQualifiedExpression ?: return false + val selector = range.getSelectorExpression() ?: return false + if (!selector.textMatches("withIndices()")) return false + + val indexVar = element.getMultiParameter()!!.getEntries()[0] + + val findManager = FindManager.getInstance(element.getProject()) as FindManagerImpl + val findHandler = findManager.getFindUsagesManager().getFindUsagesHandler(indexVar,false) ?: return false + val options = KotlinPropertyFindUsagesOptions(element.getProject()) + val usageCount = array(0) + val usageFinderRunnable = object : Runnable { + override fun run() { + val processor = object : Processor { + override fun process(t: UsageInfo?): Boolean { + usageCount[0] = usageCount[0] + 1 + return true + } + } + findHandler.processElementUsages(indexVar,processor,options) + } + } + usageFinderRunnable.run() + return usageCount[0] == 0 + } + +} \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableForLoop.kt b/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableForLoop.kt new file mode 100644 index 00000000000..6a70ccdd704 --- /dev/null +++ b/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableForLoop.kt @@ -0,0 +1,6 @@ +//IS_APPLICABLE: FALSE +fun foo(bar: List) { + for (a in bar) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt b/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt new file mode 100644 index 00000000000..554c2be803d --- /dev/null +++ b/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: FALSE +//ERROR: Unresolved reference: withIndices +fun foo(b: List) { + for ((i, c) in b.withIndices()) { + return i + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt b/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt new file mode 100644 index 00000000000..ce29523424a --- /dev/null +++ b/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt @@ -0,0 +1,6 @@ +//ERROR: Unresolved reference: withIndices +fun foo(bar: List) { + for ((i : Int, b: Int) in bar.withIndices()) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt.after b/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt.after new file mode 100644 index 00000000000..401456e86c3 --- /dev/null +++ b/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt.after @@ -0,0 +1,6 @@ +//ERROR: Unresolved reference: withIndices +fun foo(bar: List) { + for (b: Int in bar) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt b/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt new file mode 100644 index 00000000000..b66702744fd --- /dev/null +++ b/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt @@ -0,0 +1,6 @@ +//ERROR: Unresolved reference: withIndices +fun foo(bar: List) { + for ((i,a) in bar.withIndices()) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt.after b/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt.after new file mode 100644 index 00000000000..08489a0e8a4 --- /dev/null +++ b/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt.after @@ -0,0 +1,6 @@ +//ERROR: Unresolved reference: withIndices +fun foo(bar: List) { + for (a in bar) { + + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java b/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java index 1bc7e13a38b..1cd83727e07 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java @@ -278,6 +278,10 @@ public abstract class AbstractCodeTransformationTest extends LightCodeInsightTes doTestIntention(path, new ConvertToForEachFunctionCallIntention()); } + public void doTestRemoveForLoopIndicesIntention(@NotNull String path) throws Exception { + doTestIntention(path, new RemoveForLoopIndicesIntention()); + } + private void doTestIntention(@NotNull String path, @NotNull IntentionAction intentionAction) throws Exception { configureByFile(path); diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java index cfac143f5e0..a89feba5e2d 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java @@ -30,7 +30,7 @@ import org.jetbrains.jet.plugin.intentions.AbstractCodeTransformationTest; /** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ @SuppressWarnings("all") -@InnerTestClasses({CodeTransformationTestGenerated.DoubleBangToIfThen.class, CodeTransformationTestGenerated.IfThenToDoubleBang.class, CodeTransformationTestGenerated.ElvisToIfThen.class, CodeTransformationTestGenerated.IfThenToElvis.class, CodeTransformationTestGenerated.SafeAccessToIfThen.class, CodeTransformationTestGenerated.IfThenToSafeAccess.class, CodeTransformationTestGenerated.IfToAssignment.class, CodeTransformationTestGenerated.IfToReturn.class, CodeTransformationTestGenerated.IfToReturnAsymmetrically.class, CodeTransformationTestGenerated.WhenToAssignment.class, CodeTransformationTestGenerated.WhenToReturn.class, CodeTransformationTestGenerated.AssignmentToIf.class, CodeTransformationTestGenerated.AssignmentToWhen.class, CodeTransformationTestGenerated.PropertyToIf.class, CodeTransformationTestGenerated.PropertyToWhen.class, CodeTransformationTestGenerated.ReturnToIf.class, CodeTransformationTestGenerated.ReturnToWhen.class, CodeTransformationTestGenerated.IfToWhen.class, CodeTransformationTestGenerated.WhenToIf.class, CodeTransformationTestGenerated.Flatten.class, CodeTransformationTestGenerated.Merge.class, CodeTransformationTestGenerated.IntroduceSubject.class, CodeTransformationTestGenerated.EliminateSubject.class, CodeTransformationTestGenerated.Split.class, CodeTransformationTestGenerated.Join.class, CodeTransformationTestGenerated.ConvertMemberToExtension.class, CodeTransformationTestGenerated.ReconstructedType.class, CodeTransformationTestGenerated.RemoveUnnecessaryParentheses.class, CodeTransformationTestGenerated.ReplaceWithDotQualifiedMethodCall.class, CodeTransformationTestGenerated.ReplaceWithInfixFunctionCall.class, CodeTransformationTestGenerated.RemoveCurlyBracesFromTemplate.class, CodeTransformationTestGenerated.MoveLambdaInsideParentheses.class, CodeTransformationTestGenerated.MoveLambdaOutsideParentheses.class, CodeTransformationTestGenerated.ReplaceExplicitFunctionLiteralParamWithIt.class, CodeTransformationTestGenerated.ReplaceItWithExplicitFunctionLiteralParam.class, CodeTransformationTestGenerated.RemoveBraces.class, CodeTransformationTestGenerated.AddBraces.class, CodeTransformationTestGenerated.ReplaceGetIntention.class, CodeTransformationTestGenerated.ReplaceContainsIntention.class, CodeTransformationTestGenerated.ReplaceBinaryInfixIntention.class, CodeTransformationTestGenerated.ReplaceUnaryPrefixIntention.class, CodeTransformationTestGenerated.ReplaceInvokeIntention.class, CodeTransformationTestGenerated.SimplifyNegatedBinaryExpressionIntention.class, CodeTransformationTestGenerated.ConvertNegatedBooleanSequence.class, CodeTransformationTestGenerated.ConvertNegatedExpressionWithDemorgansLaw.class, CodeTransformationTestGenerated.SwapBinaryExpression.class, CodeTransformationTestGenerated.SplitIf.class, CodeTransformationTestGenerated.ReplaceWithOperatorAssign.class, CodeTransformationTestGenerated.ReplaceWithTraditionalAssignment.class, CodeTransformationTestGenerated.SimplifyBooleanWithConstants.class, CodeTransformationTestGenerated.InsertExplicitTypeArguments.class, CodeTransformationTestGenerated.RemoveExplicitTypeArguments.class, CodeTransformationTestGenerated.ConvertAssertToIf.class, CodeTransformationTestGenerated.ConvertIfToAssert.class, CodeTransformationTestGenerated.MakeTypeExplicitInLambda.class, CodeTransformationTestGenerated.MakeTypeImplicitInLambda.class, CodeTransformationTestGenerated.ConvertToForEachLoop.class, CodeTransformationTestGenerated.ConvertToForEachFunctionCall.class, CodeTransformationTestGenerated.AddForLoopIndicesIntention.class}) +@InnerTestClasses({CodeTransformationTestGenerated.DoubleBangToIfThen.class, CodeTransformationTestGenerated.IfThenToDoubleBang.class, CodeTransformationTestGenerated.ElvisToIfThen.class, CodeTransformationTestGenerated.IfThenToElvis.class, CodeTransformationTestGenerated.SafeAccessToIfThen.class, CodeTransformationTestGenerated.IfThenToSafeAccess.class, CodeTransformationTestGenerated.IfToAssignment.class, CodeTransformationTestGenerated.IfToReturn.class, CodeTransformationTestGenerated.IfToReturnAsymmetrically.class, CodeTransformationTestGenerated.WhenToAssignment.class, CodeTransformationTestGenerated.WhenToReturn.class, CodeTransformationTestGenerated.AssignmentToIf.class, CodeTransformationTestGenerated.AssignmentToWhen.class, CodeTransformationTestGenerated.PropertyToIf.class, CodeTransformationTestGenerated.PropertyToWhen.class, CodeTransformationTestGenerated.ReturnToIf.class, CodeTransformationTestGenerated.ReturnToWhen.class, CodeTransformationTestGenerated.IfToWhen.class, CodeTransformationTestGenerated.WhenToIf.class, CodeTransformationTestGenerated.Flatten.class, CodeTransformationTestGenerated.Merge.class, CodeTransformationTestGenerated.IntroduceSubject.class, CodeTransformationTestGenerated.EliminateSubject.class, CodeTransformationTestGenerated.Split.class, CodeTransformationTestGenerated.Join.class, CodeTransformationTestGenerated.ConvertMemberToExtension.class, CodeTransformationTestGenerated.ReconstructedType.class, CodeTransformationTestGenerated.RemoveUnnecessaryParentheses.class, CodeTransformationTestGenerated.ReplaceWithDotQualifiedMethodCall.class, CodeTransformationTestGenerated.ReplaceWithInfixFunctionCall.class, CodeTransformationTestGenerated.RemoveCurlyBracesFromTemplate.class, CodeTransformationTestGenerated.MoveLambdaInsideParentheses.class, CodeTransformationTestGenerated.MoveLambdaOutsideParentheses.class, CodeTransformationTestGenerated.ReplaceExplicitFunctionLiteralParamWithIt.class, CodeTransformationTestGenerated.ReplaceItWithExplicitFunctionLiteralParam.class, CodeTransformationTestGenerated.RemoveBraces.class, CodeTransformationTestGenerated.AddBraces.class, CodeTransformationTestGenerated.ReplaceGetIntention.class, CodeTransformationTestGenerated.ReplaceContainsIntention.class, CodeTransformationTestGenerated.ReplaceBinaryInfixIntention.class, CodeTransformationTestGenerated.ReplaceUnaryPrefixIntention.class, CodeTransformationTestGenerated.ReplaceInvokeIntention.class, CodeTransformationTestGenerated.SimplifyNegatedBinaryExpressionIntention.class, CodeTransformationTestGenerated.ConvertNegatedBooleanSequence.class, CodeTransformationTestGenerated.ConvertNegatedExpressionWithDemorgansLaw.class, CodeTransformationTestGenerated.SwapBinaryExpression.class, CodeTransformationTestGenerated.SplitIf.class, CodeTransformationTestGenerated.ReplaceWithOperatorAssign.class, CodeTransformationTestGenerated.ReplaceWithTraditionalAssignment.class, CodeTransformationTestGenerated.SimplifyBooleanWithConstants.class, CodeTransformationTestGenerated.InsertExplicitTypeArguments.class, CodeTransformationTestGenerated.RemoveExplicitTypeArguments.class, CodeTransformationTestGenerated.ConvertAssertToIf.class, CodeTransformationTestGenerated.ConvertIfToAssert.class, CodeTransformationTestGenerated.MakeTypeExplicitInLambda.class, CodeTransformationTestGenerated.MakeTypeImplicitInLambda.class, CodeTransformationTestGenerated.ConvertToForEachLoop.class, CodeTransformationTestGenerated.ConvertToForEachFunctionCall.class, CodeTransformationTestGenerated.AddForLoopIndicesIntention.class, CodeTransformationTestGenerated.RemoveForLoopIndicesIntention.class}) public class CodeTransformationTestGenerated extends AbstractCodeTransformationTest { @TestMetadata("idea/testData/intentions/branched/doubleBangToIfThen") public static class DoubleBangToIfThen extends AbstractCodeTransformationTest { @@ -4241,7 +4241,7 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT } - @TestMetadata("idea/testData/intentions/addForLoopIndicesIntention") + @TestMetadata("idea/testData/intentions/addForLoopIndicesIntention") public static class AddForLoopIndicesIntention extends AbstractCodeTransformationTest { public void testAllFilesPresentInAddForLoopIndicesIntention() throws Exception { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/intentions/addForLoopIndicesIntention"), Pattern.compile("^(.+)\\.kt$"), true); @@ -4274,6 +4274,34 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT } + @TestMetadata("idea/testData/intentions/removeForLoopIndicesIntention") + public static class RemoveForLoopIndicesIntention extends AbstractCodeTransformationTest { + public void testAllFilesPresentInRemoveForLoopIndicesIntention() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/intentions/removeForLoopIndicesIntention"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("inapplicableForLoop.kt") + public void testInapplicableForLoop() throws Exception { + doTestRemoveForLoopIndicesIntention("idea/testData/intentions/removeForLoopIndicesIntention/inapplicableForLoop.kt"); + } + + @TestMetadata("inapplicableIndexUse.kt") + public void testInapplicableIndexUse() throws Exception { + doTestRemoveForLoopIndicesIntention("idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt"); + } + + @TestMetadata("loopWithType.kt") + public void testLoopWithType() throws Exception { + doTestRemoveForLoopIndicesIntention("idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt"); + } + + @TestMetadata("simpleLoopWithIndices.kt") + public void testSimpleLoopWithIndices() throws Exception { + doTestRemoveForLoopIndicesIntention("idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt"); + } + + } + public static Test suite() { TestSuite suite = new TestSuite("CodeTransformationTestGenerated"); suite.addTestSuite(DoubleBangToIfThen.class); @@ -4335,6 +4363,7 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT suite.addTestSuite(ConvertToForEachLoop.class); suite.addTestSuite(ConvertToForEachFunctionCall.class); suite.addTestSuite(AddForLoopIndicesIntention.class); + suite.addTestSuite(RemoveForLoopIndicesIntention.class); return suite; } } From bbbdfd25d9a80167e787a70175ec787ce4e7b700 Mon Sep 17 00:00:00 2001 From: Ross Hanson Date: Fri, 9 May 2014 05:36:46 -0400 Subject: [PATCH 03/19] KT-4820: Added new tests, introduced functionality to detect overridden functions, other minor fixes --- .../before.kt.template | 2 +- .../intentions/AddForLoopIndicesIntention.kt | 28 +++++------ .../RemoveForLoopIndicesIntention.kt | 49 +++++++------------ .../inapplicableExistingIndices.kt | 2 +- .../inapplicableOverridenFunction.kt | 9 ++++ .../inapplicableSyntaxError.kt | 7 +++ .../inapplicableUnorderedCollection.kt | 2 +- .../addForLoopIndicesIntention/intArray.kt | 6 +++ .../intArray.kt.after | 6 +++ .../addForLoopIndicesIntention/iterable.kt | 6 +++ .../iterable.kt.after | 6 +++ .../listWithType.kt | 2 +- .../listWithType.kt.after | 2 +- .../addForLoopIndicesIntention/objectArray.kt | 6 +++ .../objectArray.kt.after | 6 +++ .../simpleIntList.kt | 2 +- .../simpleIntList.kt.after | 2 +- .../simpleSortedMap.kt | 6 --- .../simpleSortedMap.kt.after | 6 --- .../addForLoopIndicesIntention/stream.kt | 6 +++ .../stream.kt.after | 6 +++ .../addForLoopIndicesIntention/string.kt | 6 +++ .../string.kt.after | 6 +++ .../inapplicableIndexUse.kt | 6 ++- .../inapplicableOverridenFunction.kt | 11 +++++ .../loopWithType.kt | 2 +- .../loopWithType.kt.after | 2 +- .../simpleLoopWithIndices.kt | 2 +- .../simpleLoopWithIndices.kt.after | 2 +- .../CodeTransformationTestGenerated.java | 41 ++++++++++++++-- 30 files changed, 170 insertions(+), 75 deletions(-) create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/inapplicableSyntaxError.kt create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/intArray.kt create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/intArray.kt.after create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/iterable.kt create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/iterable.kt.after create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt.after delete mode 100644 idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt delete mode 100644 idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt.after create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/stream.kt create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/stream.kt.after create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/string.kt create mode 100644 idea/testData/intentions/addForLoopIndicesIntention/string.kt.after create mode 100644 idea/testData/intentions/removeForLoopIndicesIntention/inapplicableOverridenFunction.kt diff --git a/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/before.kt.template b/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/before.kt.template index 362674c6894..6c743b16877 100644 --- a/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/before.kt.template +++ b/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/before.kt.template @@ -1,3 +1,3 @@ -for ((i,x) in foo.withIndices()) { +for ((i, x) in foo.withIndices()) { } \ No newline at end of file diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/AddForLoopIndicesIntention.kt b/idea/src/org/jetbrains/jet/plugin/intentions/AddForLoopIndicesIntention.kt index e35eac6c5d8..1fc57a46af1 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/AddForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/jet/plugin/intentions/AddForLoopIndicesIntention.kt @@ -23,18 +23,13 @@ import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression import com.intellij.psi.util.PsiTreeUtil import com.intellij.psi.PsiDocumentManager import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache -import org.jetbrains.jet.di.InjectorForMacros import org.jetbrains.jet.lang.resolve.BindingContext -import org.jetbrains.jet.lang.types.TypeUtils -import org.jetbrains.jet.lang.resolve.ObservableBindingTrace -import org.jetbrains.jet.lang.resolve.BindingTraceContext -import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo import org.jetbrains.jet.lang.psi.JetParenthesizedExpression import com.intellij.codeInsight.template.TemplateBuilderImpl import com.intellij.codeInsight.template.impl.TemplateManagerImpl import org.jetbrains.jet.lang.resolve.DescriptorUtils -import org.jetbrains.jet.lang.descriptors.ModuleDescriptor - +import org.jetbrains.jet.analyzer.analyzeInContext +import org.jetbrains.jet.lang.psi.JetCallExpression public class AddForLoopIndicesIntention : JetSelfTargetingIntention( "add.for.loop.indices", javaClass()) { @@ -69,19 +64,20 @@ public class AddForLoopIndicesIntention : JetSelfTargetingIntention())!! - val components = InjectorForMacros(element.getProject(), module) - val dataFlowInfo = bindingContext[BindingContext.NON_DEFAULT_EXPRESSION_DATA_FLOW, element] ?: DataFlowInfo.EMPTY - val bindingTrace = ObservableBindingTrace(BindingTraceContext()) - val expressionType = components.getExpressionTypingServices()!!.getTypeInfo(scope, potentialExpression, TypeUtils.NO_EXPECTED_TYPE, dataFlowInfo, bindingTrace) - return expressionType.getType() != null + val functionSelector = potentialExpression.getSelectorExpression() as JetCallExpression + val updatedContext = potentialExpression.analyzeInContext(scope) + val callScope = updatedContext[BindingContext.RESOLVED_CALL, functionSelector.getCalleeExpression()!!] ?: return false + val callFqName = DescriptorUtils.getFqNameSafe(callScope.getCandidateDescriptor()) + return callFqName.toString() == "kotlin.withIndices" } } \ No newline at end of file diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/RemoveForLoopIndicesIntention.kt b/idea/src/org/jetbrains/jet/plugin/intentions/RemoveForLoopIndicesIntention.kt index 593902be45e..2bbbee48faa 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/RemoveForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/jet/plugin/intentions/RemoveForLoopIndicesIntention.kt @@ -18,25 +18,16 @@ package org.jetbrains.jet.plugin.intentions import org.jetbrains.jet.lang.psi.JetForExpression import com.intellij.openapi.editor.Editor -import org.jetbrains.jet.lang.psi.JetPsiFactory import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression import org.jetbrains.jet.lang.psi.JetPsiUtil -import org.jetbrains.jet.lang.cfg.PseudocodeVariablesData.VariableUseState -import org.jetbrains.jet.lang.cfg.pseudocode.PseudocodeUtil import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache -import org.jetbrains.jet.lang.psi.JetFile -import org.jetbrains.jet.lang.resolve.ObservableBindingTrace -import org.jetbrains.jet.lang.resolve.BindingTraceContext -import org.jetbrains.jet.lang.cfg.JetFlowInformationProvider -import org.jetbrains.jet.lang.cfg.JetControlFlowProcessor -import com.intellij.debugger.jdi.LocalVariablesUtil -import com.siyeh.ig.psiutils.VariableAccessUtils -import com.google.dart.compiler.util.AstUtil import com.intellij.find.FindManager import com.intellij.find.impl.FindManagerImpl import com.intellij.usageView.UsageInfo -import com.intellij.util.Processor import org.jetbrains.jet.plugin.findUsages.KotlinPropertyFindUsagesOptions +import org.jetbrains.jet.lang.resolve.BindingContext +import org.jetbrains.jet.lang.psi.JetCallExpression +import org.jetbrains.jet.lang.resolve.DescriptorUtils public class RemoveForLoopIndicesIntention : JetSelfTargetingIntention( "remove.for.loop.indices", javaClass()) { @@ -46,7 +37,8 @@ public class RemoveForLoopIndicesIntention : JetSelfTargetingIntention { - override fun process(t: UsageInfo?): Boolean { - usageCount[0] = usageCount[0] + 1 - return true - } - } - findHandler.processElementUsages(indexVar,processor,options) - } - } - usageFinderRunnable.run() - return usageCount[0] == 0 + var usageCount = 0 + val processorLambda: (UsageInfo?) -> Boolean = { t -> usageCount++; false } + findHandler.processElementUsages(indexVar, processorLambda, options) + return usageCount == 0 } } \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt index 486762bc85c..b9b2b899071 100644 --- a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt +++ b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt @@ -1,5 +1,5 @@ //IS_APPLICABLE: FALSE -//ERROR: Unresolved reference: withIndices +// WITH_RUNTIME fun b(c: List) { for ((indexVariable, d) in c.withIndices()) { diff --git a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt new file mode 100644 index 00000000000..c16ceeb8a97 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt @@ -0,0 +1,9 @@ +// WITH_RUNTIME +// IS_APPLICABLE: FALSE +fun String.withIndices(): Int = 42 + +fun foo(s: String) { + for (a in s) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableSyntaxError.kt b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableSyntaxError.kt new file mode 100644 index 00000000000..130fb1e80ba --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableSyntaxError.kt @@ -0,0 +1,7 @@ +//IS_APPLICABLE: FALSE +//ERROR: Unresolved reference: b +fun foo() { + for (a in b) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableUnorderedCollection.kt b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableUnorderedCollection.kt index 7a8f65b05a4..42c9ecb7ee7 100644 --- a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableUnorderedCollection.kt +++ b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableUnorderedCollection.kt @@ -1,5 +1,5 @@ // IS_APPLICABLE: FALSE -// ERROR: For-loop range must have an iterator() method +// WITH_RUNTIME fun foo(bar: Map) { for (a in bar) { diff --git a/idea/testData/intentions/addForLoopIndicesIntention/intArray.kt b/idea/testData/intentions/addForLoopIndicesIntention/intArray.kt new file mode 100644 index 00000000000..40ab0ca1785 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/intArray.kt @@ -0,0 +1,6 @@ +//WITH_RUNTIME +fun foo(bar: IntArray) { + for (a in bar) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/intArray.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/intArray.kt.after new file mode 100644 index 00000000000..317eb113f87 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/intArray.kt.after @@ -0,0 +1,6 @@ +//WITH_RUNTIME +fun foo(bar: IntArray) { + for ((index, a) in bar.withIndices()) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/iterable.kt b/idea/testData/intentions/addForLoopIndicesIntention/iterable.kt new file mode 100644 index 00000000000..aca19b4b400 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/iterable.kt @@ -0,0 +1,6 @@ +//WITH_RUNTIME +fun foo(bar: Iterable) { + for (a in bar) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/iterable.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/iterable.kt.after new file mode 100644 index 00000000000..4351c3856a9 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/iterable.kt.after @@ -0,0 +1,6 @@ +//WITH_RUNTIME +fun foo(bar: Iterable) { + for ((index, a) in bar.withIndices()) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt b/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt index 87e8b8a6e66..0cadfa4e63b 100644 --- a/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt +++ b/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt @@ -1,4 +1,4 @@ -// ERROR: Unresolved reference: listOf +// WITH_RUNTIME fun a() { val b = listOf(1,2,3,4,5) for (c : Int in b) { diff --git a/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after index 652142c4963..633543f3ffe 100644 --- a/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after +++ b/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after @@ -1,4 +1,4 @@ -// ERROR: Unresolved reference: listOf +// WITH_RUNTIME fun a() { val b = listOf(1,2,3,4,5) for ((index, c : Int) in b.withIndices()) { diff --git a/idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt b/idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt new file mode 100644 index 00000000000..32c665ac8a3 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt @@ -0,0 +1,6 @@ +//WITH_RUNTIME +fun foo(bar: Array) { + for (a in bar) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt.after new file mode 100644 index 00000000000..07f226b0945 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt.after @@ -0,0 +1,6 @@ +//WITH_RUNTIME +fun foo(bar: Array) { + for ((index, a) in bar.withIndices()) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt b/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt index b3a5c63ecf1..f7472f3e066 100644 --- a/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt +++ b/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt @@ -1,4 +1,4 @@ -// ERROR: Unresolved reference: listOf +// WITH_RUNTIME fun a() { val b = listOf(1,2,3,4,5) for (c in b) { diff --git a/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt.after index 51d6a853ab6..68e44be3929 100644 --- a/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt.after +++ b/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt.after @@ -1,4 +1,4 @@ -// ERROR: Unresolved reference: listOf +// WITH_RUNTIME fun a() { val b = listOf(1,2,3,4,5) for ((index, c) in b.withIndices()) { diff --git a/idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt b/idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt deleted file mode 100644 index dea2fc57b32..00000000000 --- a/idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt +++ /dev/null @@ -1,6 +0,0 @@ -//ERROR: Unresolved reference: SortedMap -fun a(b: SortedMap) { - for (c in b) { - - } -} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt.after deleted file mode 100644 index dffa48df2f5..00000000000 --- a/idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt.after +++ /dev/null @@ -1,6 +0,0 @@ -//ERROR: Unresolved reference: SortedMap -fun a(b: SortedMap) { - for ((index, c) in b.withIndices()) { - - } -} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/stream.kt b/idea/testData/intentions/addForLoopIndicesIntention/stream.kt new file mode 100644 index 00000000000..94eb1690af9 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/stream.kt @@ -0,0 +1,6 @@ +//WITH_RUNTIME +fun foo(bar: Stream) { + for (a in bar) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/stream.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/stream.kt.after new file mode 100644 index 00000000000..15ba5726654 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/stream.kt.after @@ -0,0 +1,6 @@ +//WITH_RUNTIME +fun foo(bar: Stream) { + for ((index, a) in bar.withIndices()) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/string.kt b/idea/testData/intentions/addForLoopIndicesIntention/string.kt new file mode 100644 index 00000000000..d838de6f97e --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/string.kt @@ -0,0 +1,6 @@ +//WITH_RUNTIME +fun foo(bar: String) { + for (a in bar) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/string.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/string.kt.after new file mode 100644 index 00000000000..1d015a4fd97 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndicesIntention/string.kt.after @@ -0,0 +1,6 @@ +//WITH_RUNTIME +fun foo(bar: String) { + for ((index, a) in bar.withIndices()) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt b/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt index 554c2be803d..9f043ae07e4 100644 --- a/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt +++ b/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt @@ -1,7 +1,9 @@ // IS_APPLICABLE: FALSE -//ERROR: Unresolved reference: withIndices -fun foo(b: List) { +//WITH_RUNTIME + +fun foo(b: List) : Int { for ((i, c) in b.withIndices()) { return i } + return 0 } \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableOverridenFunction.kt b/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableOverridenFunction.kt new file mode 100644 index 00000000000..7f3a7fd589d --- /dev/null +++ b/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableOverridenFunction.kt @@ -0,0 +1,11 @@ +// WITH_RUNTIME +// IS_APPLICABLE: FALSE +import java.util.LinkedList + +fun Int.withIndices(): List> = LinkedList>() + +fun foo(s: Int) { + for ((index, a) in s.withIndices()) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt b/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt index ce29523424a..116f672c62a 100644 --- a/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt +++ b/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt @@ -1,4 +1,4 @@ -//ERROR: Unresolved reference: withIndices +//WITH_RUNTIME fun foo(bar: List) { for ((i : Int, b: Int) in bar.withIndices()) { diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt.after b/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt.after index 401456e86c3..59f246f69bd 100644 --- a/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt.after +++ b/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt.after @@ -1,4 +1,4 @@ -//ERROR: Unresolved reference: withIndices +//WITH_RUNTIME fun foo(bar: List) { for (b: Int in bar) { diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt b/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt index b66702744fd..f734f259947 100644 --- a/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt +++ b/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt @@ -1,4 +1,4 @@ -//ERROR: Unresolved reference: withIndices +//WITH_RUNTIME fun foo(bar: List) { for ((i,a) in bar.withIndices()) { diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt.after b/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt.after index 08489a0e8a4..1a04724adc8 100644 --- a/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt.after +++ b/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt.after @@ -1,4 +1,4 @@ -//ERROR: Unresolved reference: withIndices +//WITH_RUNTIME fun foo(bar: List) { for (a in bar) { diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java index a89feba5e2d..07b8675f937 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java @@ -4252,24 +4252,54 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt"); } + @TestMetadata("inapplicableOverridenFunction.kt") + public void testInapplicableOverridenFunction() throws Exception { + doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt"); + } + + @TestMetadata("inapplicableSyntaxError.kt") + public void testInapplicableSyntaxError() throws Exception { + doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/inapplicableSyntaxError.kt"); + } + @TestMetadata("inapplicableUnorderedCollection.kt") public void testInapplicableUnorderedCollection() throws Exception { doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/inapplicableUnorderedCollection.kt"); } + @TestMetadata("intArray.kt") + public void testIntArray() throws Exception { + doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/intArray.kt"); + } + + @TestMetadata("iterable.kt") + public void testIterable() throws Exception { + doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/iterable.kt"); + } + @TestMetadata("listWithType.kt") public void testListWithType() throws Exception { doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt"); } + @TestMetadata("objectArray.kt") + public void testObjectArray() throws Exception { + doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt"); + } + @TestMetadata("simpleIntList.kt") public void testSimpleIntList() throws Exception { doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt"); } - @TestMetadata("simpleSortedMap.kt") - public void testSimpleSortedMap() throws Exception { - doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt"); + @TestMetadata("stream.kt") + public void testStream() throws Exception { + doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/stream.kt"); + } + + @TestMetadata("string.kt") + public void testString() throws Exception { + doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/string.kt"); } } @@ -4290,6 +4320,11 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT doTestRemoveForLoopIndicesIntention("idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt"); } + @TestMetadata("inapplicableOverridenFunction.kt") + public void testInapplicableOverridenFunction() throws Exception { + doTestRemoveForLoopIndicesIntention("idea/testData/intentions/removeForLoopIndicesIntention/inapplicableOverridenFunction.kt"); + } + @TestMetadata("loopWithType.kt") public void testLoopWithType() throws Exception { doTestRemoveForLoopIndicesIntention("idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt"); From 21e25e8509375dc55bb3d4f621c7f49248bacc54 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 2 Jul 2015 13:16:34 +0200 Subject: [PATCH 04/19] KT-4820: Intention to introduce/remove indices from a for-loop (update to modern Kotlin) --- idea/src/META-INF/plugin.xml | 5 - .../intentions/AddForLoopIndicesIntention.kt | 61 +++++----- .../RemoveForLoopIndicesIntention.kt | 53 ++++---- .../addForLoopIndicesIntention/.intention | 1 + .../inapplicableExistingIndices.kt | 2 +- .../inapplicableOverridenFunction.kt | 2 +- .../intArray.kt.after | 2 +- .../iterable.kt.after | 2 +- .../listWithType.kt.after | 2 +- .../objectArray.kt.after | 2 +- .../simpleIntList.kt.after | 2 +- .../stream.kt.after | 2 +- .../string.kt.after | 2 +- .../removeForLoopIndicesIntention/.intention | 1 + .../inapplicableIndexUse.kt | 2 +- .../inapplicableOverridenFunction.kt | 5 +- .../loopWithType.kt | 2 +- .../simpleLoopWithIndices.kt | 2 +- .../intentions/IntentionTestGenerated.java | 114 ++++++++++++++++++ 19 files changed, 184 insertions(+), 80 deletions(-) diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index fa8065d6150..27eeb9b18f4 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -803,11 +803,6 @@ Kotlin S - - org.jetbrains.jet.plugin.intentions.SwapBinaryExpression - Kotlin - - org.jetbrains.kotlin.idea.intentions.SwapBinaryExpressionIntention Kotlin diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt index 5030329a8d2..05fd49970d3 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt @@ -16,34 +16,33 @@ package org.jetbrains.kotlin.idea.intentions -import com.intellij.openapi.editor.Editor -import org.jetbrains.jet.lang.psi.JetForExpression -import org.jetbrains.jet.lang.psi.JetPsiFactory -import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression -import com.intellij.psi.util.PsiTreeUtil -import com.intellij.psi.PsiDocumentManager -import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache -import org.jetbrains.jet.lang.resolve.BindingContext -import org.jetbrains.jet.lang.psi.JetParenthesizedExpression import com.intellij.codeInsight.template.TemplateBuilderImpl +import com.intellij.codeInsight.template.TemplateManager import com.intellij.codeInsight.template.impl.TemplateManagerImpl -import org.jetbrains.jet.lang.resolve.DescriptorUtils -import org.jetbrains.jet.analyzer.analyzeInContext -import org.jetbrains.jet.lang.psi.JetCallExpression +import com.intellij.openapi.editor.Editor +import com.intellij.psi.PsiDocumentManager +import com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.kotlin.analyzer.analyzeInContext +import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.DescriptorUtils public class AddForLoopIndicesIntention : JetSelfTargetingIntention( - "add.for.loop.indices", javaClass()) { + javaClass(), "Add indices to 'for' loop") { override fun applyTo(element: JetForExpression, editor: Editor) { val loopRange = element.getLoopRange()!! - val newRangeText = "${loopRange.getText()}.withIndices()" - val newRange = JetPsiFactory.createExpression(editor.getProject(), newRangeText) + val newRangeText = "${loopRange.getText()}.withIndex()" + val project = editor.getProject()!! + val psiFactory = JetPsiFactory(project) + val newRange = psiFactory.createExpression(newRangeText) //Roundabout way to create new multiparameter element so as not to incorrectly trigger syntax error highlighting val loopParameter = element.getLoopParameter()!! - val parenthesizedParam = JetPsiFactory.createExpression(editor.getProject(), "(index)") as JetParenthesizedExpression + val parenthesizedParam = psiFactory.createExpression("(index)") as JetParenthesizedExpression val indexElement = parenthesizedParam.getExpression()!! - val comma = JetPsiFactory.createComma(editor.getProject()) - val newParamElement = JetPsiFactory.createExpression(editor.getProject(), " ${loopParameter.getText()}") + val comma = psiFactory.createComma() + val newParamElement = psiFactory.createExpression(loopParameter.getText()) parenthesizedParam.addAfter(newParamElement, indexElement) parenthesizedParam.addAfter(comma, indexElement) @@ -55,29 +54,31 @@ public class AddForLoopIndicesIntention : JetSelfTargetingIntention= body.getTextRange().getStartOffset()) return false + val range = element.getLoopRange() ?: return false if (range is JetDotQualifiedExpression) { val selector = range.getSelectorExpression() ?: return true - if (selector.getText() == "withIndices()") return false + if (selector.getText() == "withIndex()") return false } - val potentialExpression = JetPsiFactory.createExpression(element.getProject(), - "${range.getText()}.withIndices()") as JetDotQualifiedExpression + val psiFactory = JetPsiFactory(element.getProject()) + val potentialExpression = psiFactory.createExpression("${range.getText()}.withIndex()") as JetDotQualifiedExpression - val bindingContext = AnalyzerFacadeWithCache.getContextForElement(element) + val bindingContext = element.analyze() val scope = bindingContext[BindingContext.RESOLUTION_SCOPE, element] ?: return false val functionSelector = potentialExpression.getSelectorExpression() as JetCallExpression val updatedContext = potentialExpression.analyzeInContext(scope) - val callScope = updatedContext[BindingContext.RESOLVED_CALL, functionSelector.getCalleeExpression()!!] ?: return false + val call = updatedContext[BindingContext.CALL, functionSelector.getCalleeExpression()] ?: return false + val callScope = updatedContext[BindingContext.RESOLVED_CALL, call] ?: return false val callFqName = DescriptorUtils.getFqNameSafe(callScope.getCandidateDescriptor()) - return callFqName.toString() == "kotlin.withIndices" + return callFqName.toString() == "kotlin.withIndex" } - -} \ No newline at end of file +} diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt index 638781790ef..649db97d58f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt @@ -16,55 +16,48 @@ package org.jetbrains.kotlin.idea.intentions -import org.jetbrains.jet.lang.psi.JetForExpression -import com.intellij.openapi.editor.Editor -import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression -import org.jetbrains.jet.lang.psi.JetPsiUtil -import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache import com.intellij.find.FindManager import com.intellij.find.impl.FindManagerImpl +import com.intellij.openapi.editor.Editor +import com.intellij.psi.search.searches.ReferencesSearch import com.intellij.usageView.UsageInfo -import org.jetbrains.jet.plugin.findUsages.KotlinPropertyFindUsagesOptions -import org.jetbrains.jet.lang.resolve.BindingContext -import org.jetbrains.jet.lang.psi.JetCallExpression -import org.jetbrains.jet.lang.resolve.DescriptorUtils +import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.findUsages.KotlinPropertyFindUsagesOptions +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.DescriptorUtils public class RemoveForLoopIndicesIntention : JetSelfTargetingIntention( - "remove.for.loop.indices", javaClass()) { + javaClass(), "Remove indices in 'for' loop") { override fun applyTo(element: JetForExpression, editor: Editor) { val parameter = element.getMultiParameter()!! val range = element.getLoopRange() as JetDotQualifiedExpression val parameters = parameter.getEntries() - if (parameters.size() == 2) { - parameter.replace(parameters[1]) - } - else { - JetPsiUtil.deleteElementWithDelimiters(parameters[0]) - } + + val loop = JetPsiFactory(element).createExpression("for (${parameters[1].getText()} in _) {}") as JetForExpression + parameter.replace(loop.getLoopParameter()!!) range.replace(range.getReceiverExpression()) } - override fun isApplicableTo(element: JetForExpression): Boolean { + override fun isApplicableTo(element: JetForExpression, caretOffset: Int): Boolean { val multiParameter = element.getMultiParameter() ?: return false + if (multiParameter.getEntries().size() != 2) return false val range = element.getLoopRange() as? JetDotQualifiedExpression ?: return false val selector = range.getSelectorExpression() as? JetCallExpression ?: return false - if (!selector.textMatches("withIndices()")) return false + if (!selector.textMatches("withIndex()")) return false - val bindingContext = AnalyzerFacadeWithCache.getContextForElement(element) - val callResolution = bindingContext[BindingContext.RESOLVED_CALL, selector.getCalleeExpression()!!] ?: return false + val body = element.getBody() + if (body != null && caretOffset >= body.getTextRange().getStartOffset()) return false + + val bindingContext = element.analyze() + val call = bindingContext[BindingContext.CALL, selector.getCalleeExpression()] ?: return false + val callResolution = bindingContext[BindingContext.RESOLVED_CALL, call] ?: return false val fqName = DescriptorUtils.getFqNameSafe(callResolution.getCandidateDescriptor()) - if (fqName.toString() != "kotlin.withIndices") return false + if (fqName.toString() != "kotlin.withIndex") return false val indexVar = multiParameter.getEntries()[0] - val findManager = FindManager.getInstance(element.getProject()) as FindManagerImpl - val findHandler = findManager.getFindUsagesManager().getFindUsagesHandler(indexVar, false) ?: return false - val options = KotlinPropertyFindUsagesOptions(element.getProject()) - var usageCount = 0 - val processorLambda: (UsageInfo?) -> Boolean = { t -> usageCount++; false } - findHandler.processElementUsages(indexVar, processorLambda, options) - return usageCount == 0 + return ReferencesSearch.search(indexVar).findFirst() == null } - -} \ No newline at end of file +} diff --git a/idea/testData/intentions/addForLoopIndicesIntention/.intention b/idea/testData/intentions/addForLoopIndicesIntention/.intention index e69de29bb2d..8e1d6530547 100644 --- a/idea/testData/intentions/addForLoopIndicesIntention/.intention +++ b/idea/testData/intentions/addForLoopIndicesIntention/.intention @@ -0,0 +1 @@ +org.jetbrains.kotlin.idea.intentions.AddForLoopIndicesIntention \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt index b9b2b899071..08b3a0ec6c9 100644 --- a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt +++ b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt @@ -1,7 +1,7 @@ //IS_APPLICABLE: FALSE // WITH_RUNTIME fun b(c: List) { - for ((indexVariable, d) in c.withIndices()) { + for ((indexVariable, d) in c.withIndex()) { } } \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt index c16ceeb8a97..5ce28ace4b3 100644 --- a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt +++ b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt @@ -1,6 +1,6 @@ // WITH_RUNTIME // IS_APPLICABLE: FALSE -fun String.withIndices(): Int = 42 +fun String.withIndex(): Int = 42 fun foo(s: String) { for (a in s) { diff --git a/idea/testData/intentions/addForLoopIndicesIntention/intArray.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/intArray.kt.after index 317eb113f87..b4ab124628b 100644 --- a/idea/testData/intentions/addForLoopIndicesIntention/intArray.kt.after +++ b/idea/testData/intentions/addForLoopIndicesIntention/intArray.kt.after @@ -1,6 +1,6 @@ //WITH_RUNTIME fun foo(bar: IntArray) { - for ((index, a) in bar.withIndices()) { + for ((index, a) in bar.withIndex()) { } } \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/iterable.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/iterable.kt.after index 4351c3856a9..902f7c2661f 100644 --- a/idea/testData/intentions/addForLoopIndicesIntention/iterable.kt.after +++ b/idea/testData/intentions/addForLoopIndicesIntention/iterable.kt.after @@ -1,6 +1,6 @@ //WITH_RUNTIME fun foo(bar: Iterable) { - for ((index, a) in bar.withIndices()) { + for ((index, a) in bar.withIndex()) { } } \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after index 633543f3ffe..09d634c6844 100644 --- a/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after +++ b/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after @@ -1,7 +1,7 @@ // WITH_RUNTIME fun a() { val b = listOf(1,2,3,4,5) - for ((index, c : Int) in b.withIndices()) { + for ((index, c : Int) in b.withIndex()) { } } \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt.after index 07f226b0945..3a439a7c204 100644 --- a/idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt.after +++ b/idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt.after @@ -1,6 +1,6 @@ //WITH_RUNTIME fun foo(bar: Array) { - for ((index, a) in bar.withIndices()) { + for ((index, a) in bar.withIndex()) { } } \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt.after index 68e44be3929..72b7ca64e6e 100644 --- a/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt.after +++ b/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt.after @@ -1,7 +1,7 @@ // WITH_RUNTIME fun a() { val b = listOf(1,2,3,4,5) - for ((index, c) in b.withIndices()) { + for ((index, c) in b.withIndex()) { } } \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/stream.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/stream.kt.after index 15ba5726654..65080a14bda 100644 --- a/idea/testData/intentions/addForLoopIndicesIntention/stream.kt.after +++ b/idea/testData/intentions/addForLoopIndicesIntention/stream.kt.after @@ -1,6 +1,6 @@ //WITH_RUNTIME fun foo(bar: Stream) { - for ((index, a) in bar.withIndices()) { + for ((index, a) in bar.withIndex()) { } } \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndicesIntention/string.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/string.kt.after index 1d015a4fd97..7d5610a30fd 100644 --- a/idea/testData/intentions/addForLoopIndicesIntention/string.kt.after +++ b/idea/testData/intentions/addForLoopIndicesIntention/string.kt.after @@ -1,6 +1,6 @@ //WITH_RUNTIME fun foo(bar: String) { - for ((index, a) in bar.withIndices()) { + for ((index, a) in bar.withIndex()) { } } \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/.intention b/idea/testData/intentions/removeForLoopIndicesIntention/.intention index e69de29bb2d..013371784a9 100644 --- a/idea/testData/intentions/removeForLoopIndicesIntention/.intention +++ b/idea/testData/intentions/removeForLoopIndicesIntention/.intention @@ -0,0 +1 @@ +org.jetbrains.kotlin.idea.intentions.RemoveForLoopIndicesIntention \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt b/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt index 9f043ae07e4..6d6948cb0c6 100644 --- a/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt +++ b/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt @@ -2,7 +2,7 @@ //WITH_RUNTIME fun foo(b: List) : Int { - for ((i, c) in b.withIndices()) { + for ((i, c) in b.withIndex()) { return i } return 0 diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableOverridenFunction.kt b/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableOverridenFunction.kt index 7f3a7fd589d..1442ced4db1 100644 --- a/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableOverridenFunction.kt +++ b/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableOverridenFunction.kt @@ -1,11 +1,10 @@ // WITH_RUNTIME // IS_APPLICABLE: FALSE -import java.util.LinkedList -fun Int.withIndices(): List> = LinkedList>() +fun Int.withIndex(): List> = linkedListOf>() fun foo(s: Int) { - for ((index, a) in s.withIndices()) { + for ((index, a) in s.withIndex()) { } } \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt b/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt index 116f672c62a..9ad127ad0b0 100644 --- a/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt +++ b/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt @@ -1,6 +1,6 @@ //WITH_RUNTIME fun foo(bar: List) { - for ((i : Int, b: Int) in bar.withIndices()) { + for ((i : Int, b: Int) in bar.withIndex()) { } } \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt b/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt index f734f259947..187c87358a6 100644 --- a/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt +++ b/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt @@ -1,6 +1,6 @@ //WITH_RUNTIME fun foo(bar: List) { - for ((i,a) in bar.withIndices()) { + for ((i,a) in bar.withIndex()) { } } \ 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 f068e1782f7..ab1120754ac 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -92,6 +92,81 @@ public class IntentionTestGenerated extends AbstractIntentionTest { } } + @TestMetadata("idea/testData/intentions/addForLoopIndicesIntention") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class AddForLoopIndicesIntention extends AbstractIntentionTest { + public void testAllFilesPresentInAddForLoopIndicesIntention() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addForLoopIndicesIntention"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); + } + + @TestMetadata("inapplicableExistingIndices.kt") + public void testInapplicableExistingIndices() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt"); + doTest(fileName); + } + + @TestMetadata("inapplicableOverridenFunction.kt") + public void testInapplicableOverridenFunction() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt"); + doTest(fileName); + } + + @TestMetadata("inapplicableSyntaxError.kt") + public void testInapplicableSyntaxError() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/inapplicableSyntaxError.kt"); + doTest(fileName); + } + + @TestMetadata("inapplicableUnorderedCollection.kt") + public void testInapplicableUnorderedCollection() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/inapplicableUnorderedCollection.kt"); + doTest(fileName); + } + + @TestMetadata("intArray.kt") + public void testIntArray() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/intArray.kt"); + doTest(fileName); + } + + @TestMetadata("iterable.kt") + public void testIterable() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/iterable.kt"); + doTest(fileName); + } + + @TestMetadata("listWithType.kt") + public void testListWithType() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt"); + doTest(fileName); + } + + @TestMetadata("objectArray.kt") + public void testObjectArray() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt"); + doTest(fileName); + } + + @TestMetadata("simpleIntList.kt") + public void testSimpleIntList() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt"); + doTest(fileName); + } + + @TestMetadata("stream.kt") + public void testStream() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/stream.kt"); + doTest(fileName); + } + + @TestMetadata("string.kt") + public void testString() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/string.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/intentions/addNameToArgument") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -6089,6 +6164,45 @@ public class IntentionTestGenerated extends AbstractIntentionTest { } + @TestMetadata("idea/testData/intentions/removeForLoopIndicesIntention") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class RemoveForLoopIndicesIntention extends AbstractIntentionTest { + public void testAllFilesPresentInRemoveForLoopIndicesIntention() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeForLoopIndicesIntention"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); + } + + @TestMetadata("inapplicableForLoop.kt") + public void testInapplicableForLoop() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndicesIntention/inapplicableForLoop.kt"); + doTest(fileName); + } + + @TestMetadata("inapplicableIndexUse.kt") + public void testInapplicableIndexUse() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt"); + doTest(fileName); + } + + @TestMetadata("inapplicableOverridenFunction.kt") + public void testInapplicableOverridenFunction() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndicesIntention/inapplicableOverridenFunction.kt"); + doTest(fileName); + } + + @TestMetadata("loopWithType.kt") + public void testLoopWithType() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt"); + doTest(fileName); + } + + @TestMetadata("simpleLoopWithIndices.kt") + public void testSimpleLoopWithIndices() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/intentions/removeUnnecessaryParentheses") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) From ae55131b458e96bf639ec51c89c62933dc9f277a Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 2 Jul 2015 13:23:17 +0200 Subject: [PATCH 05/19] fix typos --- idea/src/META-INF/plugin.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 27eeb9b18f4..e16b0715bf2 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -796,12 +796,12 @@ org.jetbrains.kotlin.idea.intentions.AddForLoopIndicesIntention Kotlin - S + org.jetbrains.kotlin.idea.intentions.RemoveForLoopIndicesIntention Kotlin - S + org.jetbrains.kotlin.idea.intentions.SwapBinaryExpressionIntention From a81ed335d0ea1af2dcaccfe7e8c050e1b67135f1 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 21 Jul 2015 11:37:19 +0300 Subject: [PATCH 06/19] Renamed test data folders --- .../.intention | 0 .../inapplicableExistingIndices.kt | 0 .../inapplicableOverridenFunction.kt | 0 .../inapplicableSyntaxError.kt | 0 .../inapplicableUnorderedCollection.kt | 0 .../intArray.kt | 0 .../intArray.kt.after | 0 .../iterable.kt | 0 .../iterable.kt.after | 0 .../listWithType.kt | 0 .../listWithType.kt.after | 0 .../objectArray.kt | 0 .../objectArray.kt.after | 0 .../simpleIntList.kt | 0 .../simpleIntList.kt.after | 0 .../stream.kt | 0 .../stream.kt.after | 0 .../string.kt | 0 .../string.kt.after | 0 .../.intention | 0 .../inapplicableForLoop.kt | 0 .../inapplicableIndexUse.kt | 0 .../inapplicableOverridenFunction.kt | 0 .../loopWithType.kt | 0 .../loopWithType.kt.after | 0 .../simpleLoopWithIndices.kt | 0 .../simpleLoopWithIndices.kt.after | 0 .../intentions/IntentionTestGenerated.java | 48 +++++++++---------- 28 files changed, 24 insertions(+), 24 deletions(-) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/.intention (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/inapplicableExistingIndices.kt (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/inapplicableOverridenFunction.kt (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/inapplicableSyntaxError.kt (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/inapplicableUnorderedCollection.kt (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/intArray.kt (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/intArray.kt.after (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/iterable.kt (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/iterable.kt.after (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/listWithType.kt (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/listWithType.kt.after (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/objectArray.kt (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/objectArray.kt.after (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/simpleIntList.kt (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/simpleIntList.kt.after (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/stream.kt (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/stream.kt.after (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/string.kt (100%) rename idea/testData/intentions/{addForLoopIndicesIntention => addForLoopIndices}/string.kt.after (100%) rename idea/testData/intentions/{removeForLoopIndicesIntention => removeForLoopIndices}/.intention (100%) rename idea/testData/intentions/{removeForLoopIndicesIntention => removeForLoopIndices}/inapplicableForLoop.kt (100%) rename idea/testData/intentions/{removeForLoopIndicesIntention => removeForLoopIndices}/inapplicableIndexUse.kt (100%) rename idea/testData/intentions/{removeForLoopIndicesIntention => removeForLoopIndices}/inapplicableOverridenFunction.kt (100%) rename idea/testData/intentions/{removeForLoopIndicesIntention => removeForLoopIndices}/loopWithType.kt (100%) rename idea/testData/intentions/{removeForLoopIndicesIntention => removeForLoopIndices}/loopWithType.kt.after (100%) rename idea/testData/intentions/{removeForLoopIndicesIntention => removeForLoopIndices}/simpleLoopWithIndices.kt (100%) rename idea/testData/intentions/{removeForLoopIndicesIntention => removeForLoopIndices}/simpleLoopWithIndices.kt.after (100%) diff --git a/idea/testData/intentions/addForLoopIndicesIntention/.intention b/idea/testData/intentions/addForLoopIndices/.intention similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/.intention rename to idea/testData/intentions/addForLoopIndices/.intention diff --git a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt b/idea/testData/intentions/addForLoopIndices/inapplicableExistingIndices.kt similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt rename to idea/testData/intentions/addForLoopIndices/inapplicableExistingIndices.kt diff --git a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt b/idea/testData/intentions/addForLoopIndices/inapplicableOverridenFunction.kt similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt rename to idea/testData/intentions/addForLoopIndices/inapplicableOverridenFunction.kt diff --git a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableSyntaxError.kt b/idea/testData/intentions/addForLoopIndices/inapplicableSyntaxError.kt similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/inapplicableSyntaxError.kt rename to idea/testData/intentions/addForLoopIndices/inapplicableSyntaxError.kt diff --git a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableUnorderedCollection.kt b/idea/testData/intentions/addForLoopIndices/inapplicableUnorderedCollection.kt similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/inapplicableUnorderedCollection.kt rename to idea/testData/intentions/addForLoopIndices/inapplicableUnorderedCollection.kt diff --git a/idea/testData/intentions/addForLoopIndicesIntention/intArray.kt b/idea/testData/intentions/addForLoopIndices/intArray.kt similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/intArray.kt rename to idea/testData/intentions/addForLoopIndices/intArray.kt diff --git a/idea/testData/intentions/addForLoopIndicesIntention/intArray.kt.after b/idea/testData/intentions/addForLoopIndices/intArray.kt.after similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/intArray.kt.after rename to idea/testData/intentions/addForLoopIndices/intArray.kt.after diff --git a/idea/testData/intentions/addForLoopIndicesIntention/iterable.kt b/idea/testData/intentions/addForLoopIndices/iterable.kt similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/iterable.kt rename to idea/testData/intentions/addForLoopIndices/iterable.kt diff --git a/idea/testData/intentions/addForLoopIndicesIntention/iterable.kt.after b/idea/testData/intentions/addForLoopIndices/iterable.kt.after similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/iterable.kt.after rename to idea/testData/intentions/addForLoopIndices/iterable.kt.after diff --git a/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt b/idea/testData/intentions/addForLoopIndices/listWithType.kt similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt rename to idea/testData/intentions/addForLoopIndices/listWithType.kt diff --git a/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after b/idea/testData/intentions/addForLoopIndices/listWithType.kt.after similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after rename to idea/testData/intentions/addForLoopIndices/listWithType.kt.after diff --git a/idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt b/idea/testData/intentions/addForLoopIndices/objectArray.kt similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt rename to idea/testData/intentions/addForLoopIndices/objectArray.kt diff --git a/idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt.after b/idea/testData/intentions/addForLoopIndices/objectArray.kt.after similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt.after rename to idea/testData/intentions/addForLoopIndices/objectArray.kt.after diff --git a/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt b/idea/testData/intentions/addForLoopIndices/simpleIntList.kt similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt rename to idea/testData/intentions/addForLoopIndices/simpleIntList.kt diff --git a/idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt.after b/idea/testData/intentions/addForLoopIndices/simpleIntList.kt.after similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt.after rename to idea/testData/intentions/addForLoopIndices/simpleIntList.kt.after diff --git a/idea/testData/intentions/addForLoopIndicesIntention/stream.kt b/idea/testData/intentions/addForLoopIndices/stream.kt similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/stream.kt rename to idea/testData/intentions/addForLoopIndices/stream.kt diff --git a/idea/testData/intentions/addForLoopIndicesIntention/stream.kt.after b/idea/testData/intentions/addForLoopIndices/stream.kt.after similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/stream.kt.after rename to idea/testData/intentions/addForLoopIndices/stream.kt.after diff --git a/idea/testData/intentions/addForLoopIndicesIntention/string.kt b/idea/testData/intentions/addForLoopIndices/string.kt similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/string.kt rename to idea/testData/intentions/addForLoopIndices/string.kt diff --git a/idea/testData/intentions/addForLoopIndicesIntention/string.kt.after b/idea/testData/intentions/addForLoopIndices/string.kt.after similarity index 100% rename from idea/testData/intentions/addForLoopIndicesIntention/string.kt.after rename to idea/testData/intentions/addForLoopIndices/string.kt.after diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/.intention b/idea/testData/intentions/removeForLoopIndices/.intention similarity index 100% rename from idea/testData/intentions/removeForLoopIndicesIntention/.intention rename to idea/testData/intentions/removeForLoopIndices/.intention diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableForLoop.kt b/idea/testData/intentions/removeForLoopIndices/inapplicableForLoop.kt similarity index 100% rename from idea/testData/intentions/removeForLoopIndicesIntention/inapplicableForLoop.kt rename to idea/testData/intentions/removeForLoopIndices/inapplicableForLoop.kt diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt b/idea/testData/intentions/removeForLoopIndices/inapplicableIndexUse.kt similarity index 100% rename from idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt rename to idea/testData/intentions/removeForLoopIndices/inapplicableIndexUse.kt diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/inapplicableOverridenFunction.kt b/idea/testData/intentions/removeForLoopIndices/inapplicableOverridenFunction.kt similarity index 100% rename from idea/testData/intentions/removeForLoopIndicesIntention/inapplicableOverridenFunction.kt rename to idea/testData/intentions/removeForLoopIndices/inapplicableOverridenFunction.kt diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt b/idea/testData/intentions/removeForLoopIndices/loopWithType.kt similarity index 100% rename from idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt rename to idea/testData/intentions/removeForLoopIndices/loopWithType.kt diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt.after b/idea/testData/intentions/removeForLoopIndices/loopWithType.kt.after similarity index 100% rename from idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt.after rename to idea/testData/intentions/removeForLoopIndices/loopWithType.kt.after diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt similarity index 100% rename from idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt rename to idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt diff --git a/idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt.after b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt.after similarity index 100% rename from idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt.after rename to idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt.after diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 393dfac7e67..dbb199e7046 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -92,77 +92,77 @@ public class IntentionTestGenerated extends AbstractIntentionTest { } } - @TestMetadata("idea/testData/intentions/addForLoopIndicesIntention") + @TestMetadata("idea/testData/intentions/addForLoopIndices") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class AddForLoopIndicesIntention extends AbstractIntentionTest { - public void testAllFilesPresentInAddForLoopIndicesIntention() throws Exception { - JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addForLoopIndicesIntention"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); + public static class AddForLoopIndices extends AbstractIntentionTest { + public void testAllFilesPresentInAddForLoopIndices() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addForLoopIndices"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); } @TestMetadata("inapplicableExistingIndices.kt") public void testInapplicableExistingIndices() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt"); + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/inapplicableExistingIndices.kt"); doTest(fileName); } @TestMetadata("inapplicableOverridenFunction.kt") public void testInapplicableOverridenFunction() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt"); + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/inapplicableOverridenFunction.kt"); doTest(fileName); } @TestMetadata("inapplicableSyntaxError.kt") public void testInapplicableSyntaxError() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/inapplicableSyntaxError.kt"); + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/inapplicableSyntaxError.kt"); doTest(fileName); } @TestMetadata("inapplicableUnorderedCollection.kt") public void testInapplicableUnorderedCollection() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/inapplicableUnorderedCollection.kt"); + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/inapplicableUnorderedCollection.kt"); doTest(fileName); } @TestMetadata("intArray.kt") public void testIntArray() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/intArray.kt"); + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/intArray.kt"); doTest(fileName); } @TestMetadata("iterable.kt") public void testIterable() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/iterable.kt"); + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/iterable.kt"); doTest(fileName); } @TestMetadata("listWithType.kt") public void testListWithType() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt"); + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/listWithType.kt"); doTest(fileName); } @TestMetadata("objectArray.kt") public void testObjectArray() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt"); + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/objectArray.kt"); doTest(fileName); } @TestMetadata("simpleIntList.kt") public void testSimpleIntList() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt"); + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/simpleIntList.kt"); doTest(fileName); } @TestMetadata("stream.kt") public void testStream() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/stream.kt"); + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/stream.kt"); doTest(fileName); } @TestMetadata("string.kt") public void testString() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndicesIntention/string.kt"); + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/string.kt"); doTest(fileName); } } @@ -6164,41 +6164,41 @@ public class IntentionTestGenerated extends AbstractIntentionTest { } - @TestMetadata("idea/testData/intentions/removeForLoopIndicesIntention") + @TestMetadata("idea/testData/intentions/removeForLoopIndices") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class RemoveForLoopIndicesIntention extends AbstractIntentionTest { - public void testAllFilesPresentInRemoveForLoopIndicesIntention() throws Exception { - JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeForLoopIndicesIntention"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); + public static class RemoveForLoopIndices extends AbstractIntentionTest { + public void testAllFilesPresentInRemoveForLoopIndices() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeForLoopIndices"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); } @TestMetadata("inapplicableForLoop.kt") public void testInapplicableForLoop() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndicesIntention/inapplicableForLoop.kt"); + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndices/inapplicableForLoop.kt"); doTest(fileName); } @TestMetadata("inapplicableIndexUse.kt") public void testInapplicableIndexUse() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt"); + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndices/inapplicableIndexUse.kt"); doTest(fileName); } @TestMetadata("inapplicableOverridenFunction.kt") public void testInapplicableOverridenFunction() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndicesIntention/inapplicableOverridenFunction.kt"); + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndices/inapplicableOverridenFunction.kt"); doTest(fileName); } @TestMetadata("loopWithType.kt") public void testLoopWithType() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt"); + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndices/loopWithType.kt"); doTest(fileName); } @TestMetadata("simpleLoopWithIndices.kt") public void testSimpleLoopWithIndices() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndicesIntention/simpleLoopWithIndices.kt"); + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt"); doTest(fileName); } } From dee6bb2f4959f7d71fd109c0b7a52b4631de7d2e Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 21 Jul 2015 11:39:59 +0300 Subject: [PATCH 07/19] Fixed test --- .../intentions/addForLoopIndices/sequence.kt | 6 ++++++ .../{stream.kt.after => sequence.kt.after} | 4 ++-- idea/testData/intentions/addForLoopIndices/stream.kt | 6 ------ .../idea/intentions/IntentionTestGenerated.java | 12 ++++++------ 4 files changed, 14 insertions(+), 14 deletions(-) create mode 100644 idea/testData/intentions/addForLoopIndices/sequence.kt rename idea/testData/intentions/addForLoopIndices/{stream.kt.after => sequence.kt.after} (50%) delete mode 100644 idea/testData/intentions/addForLoopIndices/stream.kt diff --git a/idea/testData/intentions/addForLoopIndices/sequence.kt b/idea/testData/intentions/addForLoopIndices/sequence.kt new file mode 100644 index 00000000000..cc90fb6fd4e --- /dev/null +++ b/idea/testData/intentions/addForLoopIndices/sequence.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME +fun foo(bar: Sequence) { + for (a in bar) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndices/stream.kt.after b/idea/testData/intentions/addForLoopIndices/sequence.kt.after similarity index 50% rename from idea/testData/intentions/addForLoopIndices/stream.kt.after rename to idea/testData/intentions/addForLoopIndices/sequence.kt.after index 65080a14bda..29a31946b3f 100644 --- a/idea/testData/intentions/addForLoopIndices/stream.kt.after +++ b/idea/testData/intentions/addForLoopIndices/sequence.kt.after @@ -1,5 +1,5 @@ -//WITH_RUNTIME -fun foo(bar: Stream) { +// WITH_RUNTIME +fun foo(bar: Sequence) { for ((index, a) in bar.withIndex()) { } diff --git a/idea/testData/intentions/addForLoopIndices/stream.kt b/idea/testData/intentions/addForLoopIndices/stream.kt deleted file mode 100644 index 94eb1690af9..00000000000 --- a/idea/testData/intentions/addForLoopIndices/stream.kt +++ /dev/null @@ -1,6 +0,0 @@ -//WITH_RUNTIME -fun foo(bar: Stream) { - 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 dbb199e7046..9da117906a6 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -148,15 +148,15 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } - @TestMetadata("simpleIntList.kt") - public void testSimpleIntList() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/simpleIntList.kt"); + @TestMetadata("sequence.kt") + public void testSequence() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/sequence.kt"); doTest(fileName); } - @TestMetadata("stream.kt") - public void testStream() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/stream.kt"); + @TestMetadata("simpleIntList.kt") + public void testSimpleIntList() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/simpleIntList.kt"); doTest(fileName); } From 0c9401c953fbe520f1f80bd060ff2ffbe5f5823b Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 21 Jul 2015 13:10:33 +0300 Subject: [PATCH 08/19] Renamed and correct test data --- .../{listWithType.kt => explicitParamType.kt} | 0 ...pe.kt.after => explicitParamType.kt.after} | 0 .../inapplicableExistingIndices.kt | 2 +- ...eredCollection.kt => inapplicableOnMap.kt} | 0 .../inapplicableSyntaxError.kt | 7 ----- .../inapplicableUnresolved.kt | 7 +++++ .../intentions/addForLoopIndices/intArray.kt | 2 +- .../addForLoopIndices/intArray.kt.after | 2 +- .../intentions/addForLoopIndices/iterable.kt | 2 +- .../addForLoopIndices/iterable.kt.after | 2 +- .../addForLoopIndices/objectArray.kt | 2 +- .../addForLoopIndices/objectArray.kt.after | 2 +- .../intentions/addForLoopIndices/string.kt | 2 +- .../addForLoopIndices/string.kt.after | 2 +- .../inapplicableForLoop.kt | 2 +- .../inapplicableIndexUse.kt | 2 +- .../removeForLoopIndices/loopWithType.kt | 2 +- .../loopWithType.kt.after | 2 +- .../simpleLoopWithIndices.kt | 2 +- .../simpleLoopWithIndices.kt.after | 2 +- .../intentions/IntentionTestGenerated.java | 30 +++++++++---------- 21 files changed, 37 insertions(+), 37 deletions(-) rename idea/testData/intentions/addForLoopIndices/{listWithType.kt => explicitParamType.kt} (100%) rename idea/testData/intentions/addForLoopIndices/{listWithType.kt.after => explicitParamType.kt.after} (100%) rename idea/testData/intentions/addForLoopIndices/{inapplicableUnorderedCollection.kt => inapplicableOnMap.kt} (100%) delete mode 100644 idea/testData/intentions/addForLoopIndices/inapplicableSyntaxError.kt create mode 100644 idea/testData/intentions/addForLoopIndices/inapplicableUnresolved.kt diff --git a/idea/testData/intentions/addForLoopIndices/listWithType.kt b/idea/testData/intentions/addForLoopIndices/explicitParamType.kt similarity index 100% rename from idea/testData/intentions/addForLoopIndices/listWithType.kt rename to idea/testData/intentions/addForLoopIndices/explicitParamType.kt diff --git a/idea/testData/intentions/addForLoopIndices/listWithType.kt.after b/idea/testData/intentions/addForLoopIndices/explicitParamType.kt.after similarity index 100% rename from idea/testData/intentions/addForLoopIndices/listWithType.kt.after rename to idea/testData/intentions/addForLoopIndices/explicitParamType.kt.after diff --git a/idea/testData/intentions/addForLoopIndices/inapplicableExistingIndices.kt b/idea/testData/intentions/addForLoopIndices/inapplicableExistingIndices.kt index 08b3a0ec6c9..b67c1a2c735 100644 --- a/idea/testData/intentions/addForLoopIndices/inapplicableExistingIndices.kt +++ b/idea/testData/intentions/addForLoopIndices/inapplicableExistingIndices.kt @@ -1,4 +1,4 @@ -//IS_APPLICABLE: FALSE +// IS_APPLICABLE: false // WITH_RUNTIME fun b(c: List) { for ((indexVariable, d) in c.withIndex()) { diff --git a/idea/testData/intentions/addForLoopIndices/inapplicableUnorderedCollection.kt b/idea/testData/intentions/addForLoopIndices/inapplicableOnMap.kt similarity index 100% rename from idea/testData/intentions/addForLoopIndices/inapplicableUnorderedCollection.kt rename to idea/testData/intentions/addForLoopIndices/inapplicableOnMap.kt diff --git a/idea/testData/intentions/addForLoopIndices/inapplicableSyntaxError.kt b/idea/testData/intentions/addForLoopIndices/inapplicableSyntaxError.kt deleted file mode 100644 index 130fb1e80ba..00000000000 --- a/idea/testData/intentions/addForLoopIndices/inapplicableSyntaxError.kt +++ /dev/null @@ -1,7 +0,0 @@ -//IS_APPLICABLE: FALSE -//ERROR: Unresolved reference: b -fun foo() { - for (a in b) { - - } -} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndices/inapplicableUnresolved.kt b/idea/testData/intentions/addForLoopIndices/inapplicableUnresolved.kt new file mode 100644 index 00000000000..c397c9898eb --- /dev/null +++ b/idea/testData/intentions/addForLoopIndices/inapplicableUnresolved.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: false +// ERROR: Unresolved reference: b +fun foo() { + for (a in b) { + + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndices/intArray.kt b/idea/testData/intentions/addForLoopIndices/intArray.kt index 40ab0ca1785..358656430d6 100644 --- a/idea/testData/intentions/addForLoopIndices/intArray.kt +++ b/idea/testData/intentions/addForLoopIndices/intArray.kt @@ -1,4 +1,4 @@ -//WITH_RUNTIME +// WITH_RUNTIME fun foo(bar: IntArray) { for (a in bar) { diff --git a/idea/testData/intentions/addForLoopIndices/intArray.kt.after b/idea/testData/intentions/addForLoopIndices/intArray.kt.after index b4ab124628b..c4b6f424b0e 100644 --- a/idea/testData/intentions/addForLoopIndices/intArray.kt.after +++ b/idea/testData/intentions/addForLoopIndices/intArray.kt.after @@ -1,4 +1,4 @@ -//WITH_RUNTIME +// WITH_RUNTIME fun foo(bar: IntArray) { for ((index, a) in bar.withIndex()) { diff --git a/idea/testData/intentions/addForLoopIndices/iterable.kt b/idea/testData/intentions/addForLoopIndices/iterable.kt index aca19b4b400..414782c9a9a 100644 --- a/idea/testData/intentions/addForLoopIndices/iterable.kt +++ b/idea/testData/intentions/addForLoopIndices/iterable.kt @@ -1,4 +1,4 @@ -//WITH_RUNTIME +// WITH_RUNTIME fun foo(bar: Iterable) { for (a in bar) { diff --git a/idea/testData/intentions/addForLoopIndices/iterable.kt.after b/idea/testData/intentions/addForLoopIndices/iterable.kt.after index 902f7c2661f..673b64d48de 100644 --- a/idea/testData/intentions/addForLoopIndices/iterable.kt.after +++ b/idea/testData/intentions/addForLoopIndices/iterable.kt.after @@ -1,4 +1,4 @@ -//WITH_RUNTIME +// WITH_RUNTIME fun foo(bar: Iterable) { for ((index, a) in bar.withIndex()) { diff --git a/idea/testData/intentions/addForLoopIndices/objectArray.kt b/idea/testData/intentions/addForLoopIndices/objectArray.kt index 32c665ac8a3..a907c08aba7 100644 --- a/idea/testData/intentions/addForLoopIndices/objectArray.kt +++ b/idea/testData/intentions/addForLoopIndices/objectArray.kt @@ -1,4 +1,4 @@ -//WITH_RUNTIME +// WITH_RUNTIME fun foo(bar: Array) { for (a in bar) { diff --git a/idea/testData/intentions/addForLoopIndices/objectArray.kt.after b/idea/testData/intentions/addForLoopIndices/objectArray.kt.after index 3a439a7c204..0f51422ec66 100644 --- a/idea/testData/intentions/addForLoopIndices/objectArray.kt.after +++ b/idea/testData/intentions/addForLoopIndices/objectArray.kt.after @@ -1,4 +1,4 @@ -//WITH_RUNTIME +// WITH_RUNTIME fun foo(bar: Array) { for ((index, a) in bar.withIndex()) { diff --git a/idea/testData/intentions/addForLoopIndices/string.kt b/idea/testData/intentions/addForLoopIndices/string.kt index d838de6f97e..f2da1ff5806 100644 --- a/idea/testData/intentions/addForLoopIndices/string.kt +++ b/idea/testData/intentions/addForLoopIndices/string.kt @@ -1,4 +1,4 @@ -//WITH_RUNTIME +// WITH_RUNTIME fun foo(bar: String) { for (a in bar) { diff --git a/idea/testData/intentions/addForLoopIndices/string.kt.after b/idea/testData/intentions/addForLoopIndices/string.kt.after index 7d5610a30fd..4eaba4e1f2d 100644 --- a/idea/testData/intentions/addForLoopIndices/string.kt.after +++ b/idea/testData/intentions/addForLoopIndices/string.kt.after @@ -1,4 +1,4 @@ -//WITH_RUNTIME +// WITH_RUNTIME fun foo(bar: String) { for ((index, a) in bar.withIndex()) { diff --git a/idea/testData/intentions/removeForLoopIndices/inapplicableForLoop.kt b/idea/testData/intentions/removeForLoopIndices/inapplicableForLoop.kt index 6a70ccdd704..1d06d512929 100644 --- a/idea/testData/intentions/removeForLoopIndices/inapplicableForLoop.kt +++ b/idea/testData/intentions/removeForLoopIndices/inapplicableForLoop.kt @@ -1,4 +1,4 @@ -//IS_APPLICABLE: FALSE +// IS_APPLICABLE: false fun foo(bar: List) { for (a in bar) { diff --git a/idea/testData/intentions/removeForLoopIndices/inapplicableIndexUse.kt b/idea/testData/intentions/removeForLoopIndices/inapplicableIndexUse.kt index 6d6948cb0c6..8832d6b4661 100644 --- a/idea/testData/intentions/removeForLoopIndices/inapplicableIndexUse.kt +++ b/idea/testData/intentions/removeForLoopIndices/inapplicableIndexUse.kt @@ -1,5 +1,5 @@ // IS_APPLICABLE: FALSE -//WITH_RUNTIME +// WITH_RUNTIME fun foo(b: List) : Int { for ((i, c) in b.withIndex()) { diff --git a/idea/testData/intentions/removeForLoopIndices/loopWithType.kt b/idea/testData/intentions/removeForLoopIndices/loopWithType.kt index 9ad127ad0b0..22bccaea436 100644 --- a/idea/testData/intentions/removeForLoopIndices/loopWithType.kt +++ b/idea/testData/intentions/removeForLoopIndices/loopWithType.kt @@ -1,4 +1,4 @@ -//WITH_RUNTIME +// WITH_RUNTIME fun foo(bar: List) { for ((i : Int, b: Int) in bar.withIndex()) { diff --git a/idea/testData/intentions/removeForLoopIndices/loopWithType.kt.after b/idea/testData/intentions/removeForLoopIndices/loopWithType.kt.after index 59f246f69bd..918d3ab32f8 100644 --- a/idea/testData/intentions/removeForLoopIndices/loopWithType.kt.after +++ b/idea/testData/intentions/removeForLoopIndices/loopWithType.kt.after @@ -1,4 +1,4 @@ -//WITH_RUNTIME +// WITH_RUNTIME fun foo(bar: List) { for (b: Int in bar) { diff --git a/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt index 187c87358a6..067a31938eb 100644 --- a/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt +++ b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt @@ -1,4 +1,4 @@ -//WITH_RUNTIME +// WITH_RUNTIME fun foo(bar: List) { for ((i,a) in bar.withIndex()) { diff --git a/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt.after b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt.after index 1a04724adc8..8b1907b3c3d 100644 --- a/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt.after +++ b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt.after @@ -1,4 +1,4 @@ -//WITH_RUNTIME +// WITH_RUNTIME fun foo(bar: List) { for (a in bar) { diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 9da117906a6..fb676ac4ec2 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -100,27 +100,33 @@ public class IntentionTestGenerated extends AbstractIntentionTest { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addForLoopIndices"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); } + @TestMetadata("explicitParamType.kt") + public void testExplicitParamType() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/explicitParamType.kt"); + doTest(fileName); + } + @TestMetadata("inapplicableExistingIndices.kt") public void testInapplicableExistingIndices() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/inapplicableExistingIndices.kt"); doTest(fileName); } + @TestMetadata("inapplicableOnMap.kt") + public void testInapplicableOnMap() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/inapplicableOnMap.kt"); + doTest(fileName); + } + @TestMetadata("inapplicableOverridenFunction.kt") public void testInapplicableOverridenFunction() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/inapplicableOverridenFunction.kt"); doTest(fileName); } - @TestMetadata("inapplicableSyntaxError.kt") - public void testInapplicableSyntaxError() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/inapplicableSyntaxError.kt"); - doTest(fileName); - } - - @TestMetadata("inapplicableUnorderedCollection.kt") - public void testInapplicableUnorderedCollection() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/inapplicableUnorderedCollection.kt"); + @TestMetadata("inapplicableUnresolved.kt") + public void testInapplicableUnresolved() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/inapplicableUnresolved.kt"); doTest(fileName); } @@ -136,12 +142,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } - @TestMetadata("listWithType.kt") - public void testListWithType() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/listWithType.kt"); - doTest(fileName); - } - @TestMetadata("objectArray.kt") public void testObjectArray() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/objectArray.kt"); From 8ffc1bb3a6d14ed3da43b33a4f3686329390f3e3 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 21 Jul 2015 13:16:04 +0300 Subject: [PATCH 09/19] One more test added --- .../intentions/addForLoopIndices/inapplicableInBody.kt | 7 +++++++ .../kotlin/idea/intentions/IntentionTestGenerated.java | 6 ++++++ 2 files changed, 13 insertions(+) create mode 100644 idea/testData/intentions/addForLoopIndices/inapplicableInBody.kt diff --git a/idea/testData/intentions/addForLoopIndices/inapplicableInBody.kt b/idea/testData/intentions/addForLoopIndices/inapplicableInBody.kt new file mode 100644 index 00000000000..587f8ff9da1 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndices/inapplicableInBody.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: false +// WITH_RUNTIME +fun foo() { + for (a in listOf(1, 2, 3)) { + + } +} \ 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 fb676ac4ec2..36d3715fd69 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -112,6 +112,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("inapplicableInBody.kt") + public void testInapplicableInBody() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/inapplicableInBody.kt"); + doTest(fileName); + } + @TestMetadata("inapplicableOnMap.kt") public void testInapplicableOnMap() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/inapplicableOnMap.kt"); From e046dc967c2091fc379455ab272ca72de06de8c0 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 21 Jul 2015 11:46:35 +0300 Subject: [PATCH 10/19] Rewritten AddForLoopIndicesIntention to not use by text generation & checks --- .../kotlin/resolve/DescriptorUtils.kt | 7 ++ .../intentions/AddForLoopIndicesIntention.kt | 90 +++++++++---------- 2 files changed, 52 insertions(+), 45 deletions(-) diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt index 047d2535957..6813e8ac6cb 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt @@ -23,6 +23,7 @@ import org.jetbrains.kotlin.descriptors.ClassKind.ENUM_ENTRY import org.jetbrains.kotlin.descriptors.ClassKind.OBJECT import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.FqNameUnsafe import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.types.JetType @@ -36,6 +37,12 @@ public fun DeclarationDescriptor.getImportableDescriptor(): DeclarationDescripto } } +public val DeclarationDescriptor.fqNameUnsafe: FqNameUnsafe + get() = DescriptorUtils.getFqName(this) + +public val DeclarationDescriptor.fqNameSafe: FqName + get() = DescriptorUtils.getFqNameSafe(this) + public val DeclarationDescriptor.isExtension: Boolean get() = this is CallableDescriptor && getExtensionReceiverParameter() != null diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt index 05fd49970d3..b34ad7086ab 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt @@ -18,67 +18,67 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.codeInsight.template.TemplateBuilderImpl import com.intellij.codeInsight.template.TemplateManager -import com.intellij.codeInsight.template.impl.TemplateManagerImpl import com.intellij.openapi.editor.Editor +import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiDocumentManager -import com.intellij.psi.util.PsiTreeUtil import org.jetbrains.kotlin.analyzer.analyzeInContext import org.jetbrains.kotlin.idea.caches.resolve.analyze -import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.idea.core.replaced +import org.jetbrains.kotlin.psi.JetExpression +import org.jetbrains.kotlin.psi.JetForExpression +import org.jetbrains.kotlin.psi.JetPsiFactory +import org.jetbrains.kotlin.psi.createExpressionByPattern +import org.jetbrains.kotlin.psi.psiUtil.endOffset +import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.resolve.BindingContext -import org.jetbrains.kotlin.resolve.DescriptorUtils +import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall +import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode -public class AddForLoopIndicesIntention : JetSelfTargetingIntention( - javaClass(), "Add indices to 'for' loop") { +public class AddForLoopIndicesIntention : JetSelfTargetingRangeIntention(javaClass(), "Add indices to 'for' loop") { override fun applyTo(element: JetForExpression, editor: Editor) { - val loopRange = element.getLoopRange()!! - val newRangeText = "${loopRange.getText()}.withIndex()" - val project = editor.getProject()!! - val psiFactory = JetPsiFactory(project) - val newRange = psiFactory.createExpression(newRangeText) + val loopRange = element.loopRange!! + val loopParameter = element.loopParameter!! + val project = element.project + val psiFactory = JetPsiFactory(element) - //Roundabout way to create new multiparameter element so as not to incorrectly trigger syntax error highlighting - val loopParameter = element.getLoopParameter()!! - val parenthesizedParam = psiFactory.createExpression("(index)") as JetParenthesizedExpression - val indexElement = parenthesizedParam.getExpression()!! - val comma = psiFactory.createComma() - val newParamElement = psiFactory.createExpression(loopParameter.getText()) - parenthesizedParam.addAfter(newParamElement, indexElement) - parenthesizedParam.addAfter(comma, indexElement) + loopRange.replace(createWithIndexExpression(loopRange)) - loopParameter.replace(parenthesizedParam) - loopRange.replace(newRange) + var multiParameter = (psiFactory.createExpressionByPattern("for((index, $0) in x){}", loopParameter.text) as JetForExpression).multiParameter!! - val multiParameter = PsiTreeUtil.findChildOfType(element, indexElement.javaClass)!! + multiParameter = loopParameter.replaced(multiParameter) - editor.getCaretModel().moveToOffset(multiParameter.getTextOffset()) - val templateBuilder = TemplateBuilderImpl(multiParameter) - templateBuilder.replaceElement(multiParameter, "index") - PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument()) + val indexVariable = multiParameter.entries[0] + editor.caretModel.moveToOffset(indexVariable.startOffset) + + val templateBuilder = TemplateBuilderImpl(indexVariable) + templateBuilder.replaceElement(indexVariable, "index") + PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.document) TemplateManager.getInstance(project).startTemplate(editor, templateBuilder.buildInlineTemplate()!!) } - override fun isApplicableTo(element: JetForExpression, caretOffset: Int): Boolean { - if (element.getLoopParameter() == null) return false - val body = element.getBody() - if (body != null && caretOffset >= body.getTextRange().getStartOffset()) return false + private val WITH_INDEX_FQ_NAME = "kotlin.withIndex" - val range = element.getLoopRange() ?: return false - if (range is JetDotQualifiedExpression) { - val selector = range.getSelectorExpression() ?: return true - if (selector.getText() == "withIndex()") return false - } + override fun applicabilityRange(element: JetForExpression): TextRange? { + if (element.loopParameter == null) return null + val loopRange = element.loopRange ?: return null - val psiFactory = JetPsiFactory(element.getProject()) - val potentialExpression = psiFactory.createExpression("${range.getText()}.withIndex()") as JetDotQualifiedExpression + val bindingContext = element.analyze(BodyResolveMode.PARTIAL) - val bindingContext = element.analyze() - val scope = bindingContext[BindingContext.RESOLUTION_SCOPE, element] ?: return false - val functionSelector = potentialExpression.getSelectorExpression() as JetCallExpression - val updatedContext = potentialExpression.analyzeInContext(scope) - val call = updatedContext[BindingContext.CALL, functionSelector.getCalleeExpression()] ?: return false - val callScope = updatedContext[BindingContext.RESOLVED_CALL, call] ?: return false - val callFqName = DescriptorUtils.getFqNameSafe(callScope.getCandidateDescriptor()) - return callFqName.toString() == "kotlin.withIndex" + val resolvedCall = loopRange.getResolvedCall(bindingContext) + if (resolvedCall?.resultingDescriptor?.fqNameUnsafe?.asString() == WITH_INDEX_FQ_NAME) return null // already withIndex() call + + val resolutionScope = bindingContext[BindingContext.RESOLUTION_SCOPE, element] ?: return null + val potentialExpression = createWithIndexExpression(loopRange) + + val newBindingContext = potentialExpression.analyzeInContext(resolutionScope) + val newResolvedCall = potentialExpression.getResolvedCall(newBindingContext) ?: return null + if (newResolvedCall.resultingDescriptor.fqNameUnsafe.asString() != WITH_INDEX_FQ_NAME) return null + + return TextRange(element.startOffset, element.body?.startOffset ?: element.endOffset) + } + + private fun createWithIndexExpression(originalExpression: JetExpression): JetExpression { + return JetPsiFactory(originalExpression).createExpressionByPattern("$0.withIndex()", originalExpression) } } From 511bb133f5a2c8ddef998a1c31d461a614aa48fb Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 21 Jul 2015 13:19:50 +0300 Subject: [PATCH 11/19] Reordered methods --- .../intentions/AddForLoopIndicesIntention.kt | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt index b34ad7086ab..2e3802c453d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt @@ -36,27 +36,6 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode public class AddForLoopIndicesIntention : JetSelfTargetingRangeIntention(javaClass(), "Add indices to 'for' loop") { - override fun applyTo(element: JetForExpression, editor: Editor) { - val loopRange = element.loopRange!! - val loopParameter = element.loopParameter!! - val project = element.project - val psiFactory = JetPsiFactory(element) - - loopRange.replace(createWithIndexExpression(loopRange)) - - var multiParameter = (psiFactory.createExpressionByPattern("for((index, $0) in x){}", loopParameter.text) as JetForExpression).multiParameter!! - - multiParameter = loopParameter.replaced(multiParameter) - - val indexVariable = multiParameter.entries[0] - editor.caretModel.moveToOffset(indexVariable.startOffset) - - val templateBuilder = TemplateBuilderImpl(indexVariable) - templateBuilder.replaceElement(indexVariable, "index") - PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.document) - TemplateManager.getInstance(project).startTemplate(editor, templateBuilder.buildInlineTemplate()!!) - } - private val WITH_INDEX_FQ_NAME = "kotlin.withIndex" override fun applicabilityRange(element: JetForExpression): TextRange? { @@ -78,6 +57,27 @@ public class AddForLoopIndicesIntention : JetSelfTargetingRangeIntention Date: Tue, 21 Jul 2015 13:20:14 +0300 Subject: [PATCH 12/19] AddForLoopIndicesIntention made low priority --- .../kotlin/idea/intentions/AddForLoopIndicesIntention.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt index 2e3802c453d..5fc9b1a092c 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt @@ -16,6 +16,7 @@ package org.jetbrains.kotlin.idea.intentions +import com.intellij.codeInsight.intention.LowPriorityAction import com.intellij.codeInsight.template.TemplateBuilderImpl import com.intellij.codeInsight.template.TemplateManager import com.intellij.openapi.editor.Editor @@ -35,7 +36,7 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode -public class AddForLoopIndicesIntention : JetSelfTargetingRangeIntention(javaClass(), "Add indices to 'for' loop") { +public class AddForLoopIndicesIntention : JetSelfTargetingRangeIntention(javaClass(), "Add indices to 'for' loop"), LowPriorityAction { private val WITH_INDEX_FQ_NAME = "kotlin.withIndex" override fun applicabilityRange(element: JetForExpression): TextRange? { From 7611f480c1cac70500f7887c2b8dbd1f2b76d0ef Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 21 Jul 2015 13:26:13 +0300 Subject: [PATCH 13/19] More consistent intention texts --- .../idea/intentions/AddForLoopIndicesIntention.kt | 2 +- .../intentions/ConvertForEachToForLoopIntention.kt | 2 +- .../idea/intentions/RemoveForLoopIndicesIntention.kt | 11 +++++------ 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt index 5fc9b1a092c..14188c8b707 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt @@ -36,7 +36,7 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode -public class AddForLoopIndicesIntention : JetSelfTargetingRangeIntention(javaClass(), "Add indices to 'for' loop"), LowPriorityAction { +public class AddForLoopIndicesIntention : JetSelfTargetingRangeIntention(javaClass(), "Add indices to for-loop"), LowPriorityAction { private val WITH_INDEX_FQ_NAME = "kotlin.withIndex" override fun applicabilityRange(element: JetForExpression): TextRange? { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertForEachToForLoopIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertForEachToForLoopIntention.kt index 8d0a2966598..0b6d56a50d2 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertForEachToForLoopIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertForEachToForLoopIntention.kt @@ -24,7 +24,7 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver -public class ConvertForEachToForLoopIntention : JetSelfTargetingOffsetIndependentIntention(javaClass(), "Replace with a for loop") { +public class ConvertForEachToForLoopIntention : JetSelfTargetingOffsetIndependentIntention(javaClass(), "Replace with a for-loop") { override fun isApplicableTo(element: JetSimpleNameExpression): Boolean { if (element.getReferencedName() != "forEach") return false diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt index 649db97d58f..af0b5a4d853 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt @@ -16,19 +16,18 @@ package org.jetbrains.kotlin.idea.intentions -import com.intellij.find.FindManager -import com.intellij.find.impl.FindManagerImpl import com.intellij.openapi.editor.Editor import com.intellij.psi.search.searches.ReferencesSearch -import com.intellij.usageView.UsageInfo import org.jetbrains.kotlin.idea.caches.resolve.analyze -import org.jetbrains.kotlin.idea.findUsages.KotlinPropertyFindUsagesOptions -import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.JetCallExpression +import org.jetbrains.kotlin.psi.JetDotQualifiedExpression +import org.jetbrains.kotlin.psi.JetForExpression +import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.DescriptorUtils public class RemoveForLoopIndicesIntention : JetSelfTargetingIntention( - javaClass(), "Remove indices in 'for' loop") { + javaClass(), "Remove indices in for-loop") { override fun applyTo(element: JetForExpression, editor: Editor) { val parameter = element.getMultiParameter()!! val range = element.getLoopRange() as JetDotQualifiedExpression From d5b5245dc85948751b91a6244e281fbc93add232 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 21 Jul 2015 13:34:21 +0300 Subject: [PATCH 14/19] No need to create template with one field --- .../idea/intentions/AddForLoopIndicesIntention.kt | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt index 14188c8b707..5d67294a056 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt @@ -17,11 +17,8 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.codeInsight.intention.LowPriorityAction -import com.intellij.codeInsight.template.TemplateBuilderImpl -import com.intellij.codeInsight.template.TemplateManager import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange -import com.intellij.psi.PsiDocumentManager import org.jetbrains.kotlin.analyzer.analyzeInContext import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.core.replaced @@ -61,7 +58,6 @@ public class AddForLoopIndicesIntention : JetSelfTargetingRangeIntention Date: Tue, 21 Jul 2015 13:35:29 +0300 Subject: [PATCH 15/19] Reordered methods --- .../RemoveForLoopIndicesIntention.kt | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt index af0b5a4d853..eaff1961b28 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt @@ -26,19 +26,7 @@ import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.DescriptorUtils -public class RemoveForLoopIndicesIntention : JetSelfTargetingIntention( - javaClass(), "Remove indices in for-loop") { - override fun applyTo(element: JetForExpression, editor: Editor) { - val parameter = element.getMultiParameter()!! - val range = element.getLoopRange() as JetDotQualifiedExpression - val parameters = parameter.getEntries() - - val loop = JetPsiFactory(element).createExpression("for (${parameters[1].getText()} in _) {}") as JetForExpression - parameter.replace(loop.getLoopParameter()!!) - - range.replace(range.getReceiverExpression()) - } - +public class RemoveForLoopIndicesIntention : JetSelfTargetingIntention(javaClass(), "Remove indices in for-loop") { override fun isApplicableTo(element: JetForExpression, caretOffset: Int): Boolean { val multiParameter = element.getMultiParameter() ?: return false if (multiParameter.getEntries().size() != 2) return false @@ -59,4 +47,15 @@ public class RemoveForLoopIndicesIntention : JetSelfTargetingIntention Date: Tue, 21 Jul 2015 13:45:49 +0300 Subject: [PATCH 16/19] Lot of code corrections in RemoveForLoopIndicesIntention --- .../RemoveForLoopIndicesIntention.kt | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt index eaff1961b28..4495c4bed60 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt @@ -17,45 +17,46 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.openapi.editor.Editor +import com.intellij.openapi.util.TextRange import com.intellij.psi.search.searches.ReferencesSearch import org.jetbrains.kotlin.idea.caches.resolve.analyze -import org.jetbrains.kotlin.psi.JetCallExpression import org.jetbrains.kotlin.psi.JetDotQualifiedExpression import org.jetbrains.kotlin.psi.JetForExpression import org.jetbrains.kotlin.psi.JetPsiFactory -import org.jetbrains.kotlin.resolve.BindingContext -import org.jetbrains.kotlin.resolve.DescriptorUtils +import org.jetbrains.kotlin.psi.createExpressionByPattern +import org.jetbrains.kotlin.psi.psiUtil.endOffset +import org.jetbrains.kotlin.psi.psiUtil.startOffset +import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall +import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode -public class RemoveForLoopIndicesIntention : JetSelfTargetingIntention(javaClass(), "Remove indices in for-loop") { - override fun isApplicableTo(element: JetForExpression, caretOffset: Int): Boolean { - val multiParameter = element.getMultiParameter() ?: return false - if (multiParameter.getEntries().size() != 2) return false - val range = element.getLoopRange() as? JetDotQualifiedExpression ?: return false - val selector = range.getSelectorExpression() as? JetCallExpression ?: return false +public class RemoveForLoopIndicesIntention : JetSelfTargetingRangeIntention(javaClass(), "Remove indices in for-loop") { + private val WITH_INDEX_FQ_NAME = "kotlin.withIndex" - if (!selector.textMatches("withIndex()")) return false + override fun applicabilityRange(element: JetForExpression): TextRange? { + val loopRange = element.loopRange as? JetDotQualifiedExpression ?: return null + val multiParameter = element.multiParameter ?: return null + if (multiParameter.entries.size() != 2) return null - val body = element.getBody() - if (body != null && caretOffset >= body.getTextRange().getStartOffset()) return false + val bindingContext = element.analyze(BodyResolveMode.PARTIAL) - val bindingContext = element.analyze() - val call = bindingContext[BindingContext.CALL, selector.getCalleeExpression()] ?: return false - val callResolution = bindingContext[BindingContext.RESOLVED_CALL, call] ?: return false - val fqName = DescriptorUtils.getFqNameSafe(callResolution.getCandidateDescriptor()) - if (fqName.toString() != "kotlin.withIndex") return false + val resolvedCall = loopRange.getResolvedCall(bindingContext) + if (resolvedCall?.resultingDescriptor?.fqNameUnsafe?.asString() != WITH_INDEX_FQ_NAME) return null - val indexVar = multiParameter.getEntries()[0] - return ReferencesSearch.search(indexVar).findFirst() == null + val indexVar = multiParameter.entries[0] + if (ReferencesSearch.search(indexVar).any()) return null + + return TextRange(element.startOffset, element.body?.startOffset ?: element.endOffset) } override fun applyTo(element: JetForExpression, editor: Editor) { - val parameter = element.getMultiParameter()!! - val range = element.getLoopRange() as JetDotQualifiedExpression - val parameters = parameter.getEntries() + val multiParameter = element.multiParameter!! + val loopRange = element.loopRange as JetDotQualifiedExpression - val loop = JetPsiFactory(element).createExpression("for (${parameters[1].getText()} in _) {}") as JetForExpression - parameter.replace(loop.getLoopParameter()!!) + val elementVar = multiParameter.entries[1] + val loop = JetPsiFactory(element).createExpressionByPattern("for ($0 in _) {}", elementVar.text) as JetForExpression + multiParameter.replace(loop.loopParameter!!) - range.replace(range.getReceiverExpression()) + loopRange.replace(loopRange.receiverExpression) } } From 6339ad4ec6ca3d684c68c603dc14196827b770f6 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 21 Jul 2015 14:04:51 +0300 Subject: [PATCH 17/19] Added inspection based on RemoveForLoopIndicesIntention --- .../RemoveForLoopIndices.html | 5 +++++ .../AddForLoopIndicesIntention/after.kt.template | 2 +- .../before.kt.template | 2 +- .../after.kt.template | 2 +- .../before.kt.template | 2 +- idea/src/META-INF/plugin.xml | 7 +++++++ .../intentions/RemoveForLoopIndicesIntention.kt | 16 +++++++++++++--- .../removeForLoopIndices/inapplicableIndexUse.kt | 2 +- .../removeForLoopIndices/loopWithType.kt | 2 +- .../simpleLoopWithIndices.kt | 2 +- 10 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 idea/resources/inspectionDescriptions/RemoveForLoopIndices.html diff --git a/idea/resources/inspectionDescriptions/RemoveForLoopIndices.html b/idea/resources/inspectionDescriptions/RemoveForLoopIndices.html new file mode 100644 index 00000000000..54ffaaedaee --- /dev/null +++ b/idea/resources/inspectionDescriptions/RemoveForLoopIndices.html @@ -0,0 +1,5 @@ + + +This inspection reports for loops iterating over a collection of values using "withIndex()" function with index variable not used in the loop body. + + diff --git a/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/after.kt.template b/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/after.kt.template index 6c743b16877..a940c7a3e81 100644 --- a/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/after.kt.template +++ b/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/after.kt.template @@ -1,3 +1,3 @@ -for ((i, x) in foo.withIndices()) { +for ((i, x) in foo.withIndices()) { } \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/before.kt.template b/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/before.kt.template index 8b1fae6e977..ef41e9fadce 100644 --- a/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/before.kt.template +++ b/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/before.kt.template @@ -1,3 +1,3 @@ -for (x in foo) { +for (x in foo) { } \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/after.kt.template b/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/after.kt.template index 8b1fae6e977..ef41e9fadce 100644 --- a/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/after.kt.template +++ b/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/after.kt.template @@ -1,3 +1,3 @@ -for (x in foo) { +for (x in foo) { } \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/before.kt.template b/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/before.kt.template index 6c743b16877..a940c7a3e81 100644 --- a/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/before.kt.template +++ b/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/before.kt.template @@ -1,3 +1,3 @@ -for ((i, x) in foo.withIndices()) { +for ((i, x) in foo.withIndices()) { } \ No newline at end of file diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 4e29e4aeba4..5cfd263d0af 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -1081,6 +1081,13 @@ cleanupTool="true" level="WARNING"/> + + diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt index 4495c4bed60..572b7e40062 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt @@ -16,20 +16,30 @@ package org.jetbrains.kotlin.idea.intentions +import com.intellij.codeInspection.ProblemHighlightType import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange import com.intellij.psi.search.searches.ReferencesSearch import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.editor.fixers.range +import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection import org.jetbrains.kotlin.psi.JetDotQualifiedExpression import org.jetbrains.kotlin.psi.JetForExpression import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.psi.createExpressionByPattern -import org.jetbrains.kotlin.psi.psiUtil.endOffset -import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode +public class RemoveForLoopIndicesInspection : IntentionBasedInspection( + listOf(IntentionBasedInspection.IntentionData(RemoveForLoopIndicesIntention())), + "Index is not used in the loop body", + javaClass() +) { + override val problemHighlightType: ProblemHighlightType + get() = ProblemHighlightType.LIKE_UNUSED_SYMBOL +} + public class RemoveForLoopIndicesIntention : JetSelfTargetingRangeIntention(javaClass(), "Remove indices in for-loop") { private val WITH_INDEX_FQ_NAME = "kotlin.withIndex" @@ -46,7 +56,7 @@ public class RemoveForLoopIndicesIntention : JetSelfTargetingRangeIntention) : Int { - for ((i, c) in b.withIndex()) { + for ((i, c) in b.withIndex()) { return i } return 0 diff --git a/idea/testData/intentions/removeForLoopIndices/loopWithType.kt b/idea/testData/intentions/removeForLoopIndices/loopWithType.kt index 22bccaea436..1c055d425a6 100644 --- a/idea/testData/intentions/removeForLoopIndices/loopWithType.kt +++ b/idea/testData/intentions/removeForLoopIndices/loopWithType.kt @@ -1,6 +1,6 @@ // WITH_RUNTIME fun foo(bar: List) { - for ((i : Int, b: Int) in bar.withIndex()) { + for ((i : Int, b: Int) in bar.withIndex()) { } } \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt index 067a31938eb..e907ccc131b 100644 --- a/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt +++ b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt @@ -1,6 +1,6 @@ // WITH_RUNTIME fun foo(bar: List) { - for ((i,a) in bar.withIndex()) { + for ((i,a) in bar.withIndex()) { } } \ No newline at end of file From 16ae9e38618809316ba6406bfe4448b37699bdd8 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 21 Jul 2015 15:23:30 +0300 Subject: [PATCH 18/19] More consistent intention texts 2 --- .../kotlin/idea/intentions/AddForLoopIndicesIntention.kt | 2 +- .../kotlin/idea/intentions/ConvertForEachToForLoopIntention.kt | 2 +- .../idea/intentions/ConvertToForEachFunctionCallIntention.kt | 2 +- .../kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt index 5d67294a056..4abfc21b589 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt @@ -33,7 +33,7 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode -public class AddForLoopIndicesIntention : JetSelfTargetingRangeIntention(javaClass(), "Add indices to for-loop"), LowPriorityAction { +public class AddForLoopIndicesIntention : JetSelfTargetingRangeIntention(javaClass(), "Add indices to 'for' loop"), LowPriorityAction { private val WITH_INDEX_FQ_NAME = "kotlin.withIndex" override fun applicabilityRange(element: JetForExpression): TextRange? { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertForEachToForLoopIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertForEachToForLoopIntention.kt index 0b6d56a50d2..f348afa0d4f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertForEachToForLoopIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertForEachToForLoopIntention.kt @@ -24,7 +24,7 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver -public class ConvertForEachToForLoopIntention : JetSelfTargetingOffsetIndependentIntention(javaClass(), "Replace with a for-loop") { +public class ConvertForEachToForLoopIntention : JetSelfTargetingOffsetIndependentIntention(javaClass(), "Replace with a 'for' loop") { override fun isApplicableTo(element: JetSimpleNameExpression): Boolean { if (element.getReferencedName() != "forEach") return false diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToForEachFunctionCallIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToForEachFunctionCallIntention.kt index 0bc8076d28c..e0903266e68 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToForEachFunctionCallIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToForEachFunctionCallIntention.kt @@ -25,7 +25,7 @@ import org.jetbrains.kotlin.psi.createExpressionByPattern import org.jetbrains.kotlin.psi.psiUtil.contentRange import org.jetbrains.kotlin.psi.psiUtil.endOffset -public class ConvertToForEachFunctionCallIntention : JetSelfTargetingIntention(javaClass(), "Replace with a forEach function call") { +public class ConvertToForEachFunctionCallIntention : JetSelfTargetingIntention(javaClass(), "Replace with a 'forEach' function call") { override fun isApplicableTo(element: JetForExpression, caretOffset: Int): Boolean { val rParen = element.getRightParenthesis() ?: return false if (caretOffset > rParen.endOffset) return false // available only on the loop header, not in the body diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt index 572b7e40062..abf076e9210 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt @@ -40,7 +40,7 @@ public class RemoveForLoopIndicesInspection : IntentionBasedInspection(javaClass(), "Remove indices in for-loop") { +public class RemoveForLoopIndicesIntention : JetSelfTargetingRangeIntention(javaClass(), "Remove indices in 'for' loop") { private val WITH_INDEX_FQ_NAME = "kotlin.withIndex" override fun applicabilityRange(element: JetForExpression): TextRange? { From 82cd58a5569f2b4d9c994f6279ced67253a07149 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 21 Jul 2015 15:51:58 +0300 Subject: [PATCH 19/19] Restored live template but made it more useful --- .../intentions/AddForLoopIndicesIntention.kt | 36 ++++++++++++++++--- .../explicitParamType.kt.after | 2 +- .../addForLoopIndices/intArray.kt.after | 2 +- .../addForLoopIndices/iterable.kt.after | 2 +- .../intentions/addForLoopIndices/noBody.kt | 4 +++ .../addForLoopIndices/noBody.kt.after | 4 +++ .../addForLoopIndices/objectArray.kt.after | 2 +- .../intentions/addForLoopIndices/sequence.kt | 5 ++- .../addForLoopIndices/sequence.kt.after | 5 ++- .../addForLoopIndices/simpleIntList.kt.after | 2 +- .../intentions/addForLoopIndices/string.kt | 2 +- .../addForLoopIndices/string.kt.after | 2 +- .../intentions/IntentionTestGenerated.java | 6 ++++ 13 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 idea/testData/intentions/addForLoopIndices/noBody.kt create mode 100644 idea/testData/intentions/addForLoopIndices/noBody.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt index 4abfc21b589..c327ebc3dcd 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt @@ -17,15 +17,14 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.codeInsight.intention.LowPriorityAction +import com.intellij.codeInsight.template.TemplateBuilderImpl import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange +import com.intellij.psi.PsiDocumentManager import org.jetbrains.kotlin.analyzer.analyzeInContext import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.core.replaced -import org.jetbrains.kotlin.psi.JetExpression -import org.jetbrains.kotlin.psi.JetForExpression -import org.jetbrains.kotlin.psi.JetPsiFactory -import org.jetbrains.kotlin.psi.createExpressionByPattern +import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.resolve.BindingContext @@ -68,7 +67,34 @@ public class AddForLoopIndicesIntention : JetSelfTargetingRangeIntention { + val statement = body.statements.firstOrNull() + if (statement != null) { + templateBuilder.setEndVariableBefore(statement) + } + else { + templateBuilder.setEndVariableAfter(body.lBrace) + } + } + + null -> forExpression.rightParenthesis.let { templateBuilder.setEndVariableAfter(it) } + + else -> templateBuilder.setEndVariableBefore(body) + } + + templateBuilder.run(editor, true) } private fun createWithIndexExpression(originalExpression: JetExpression): JetExpression { diff --git a/idea/testData/intentions/addForLoopIndices/explicitParamType.kt.after b/idea/testData/intentions/addForLoopIndices/explicitParamType.kt.after index 09d634c6844..0d9a18f1439 100644 --- a/idea/testData/intentions/addForLoopIndices/explicitParamType.kt.after +++ b/idea/testData/intentions/addForLoopIndices/explicitParamType.kt.after @@ -1,7 +1,7 @@ // WITH_RUNTIME fun a() { val b = listOf(1,2,3,4,5) - for ((index, c : Int) in b.withIndex()) { + for ((index, c : Int) in b.withIndex()) { } } \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndices/intArray.kt.after b/idea/testData/intentions/addForLoopIndices/intArray.kt.after index c4b6f424b0e..1be0bfb6bf9 100644 --- a/idea/testData/intentions/addForLoopIndices/intArray.kt.after +++ b/idea/testData/intentions/addForLoopIndices/intArray.kt.after @@ -1,6 +1,6 @@ // WITH_RUNTIME fun foo(bar: IntArray) { - for ((index, a) in bar.withIndex()) { + for ((index, a) in bar.withIndex()) { } } \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndices/iterable.kt.after b/idea/testData/intentions/addForLoopIndices/iterable.kt.after index 673b64d48de..f96fe487f34 100644 --- a/idea/testData/intentions/addForLoopIndices/iterable.kt.after +++ b/idea/testData/intentions/addForLoopIndices/iterable.kt.after @@ -1,6 +1,6 @@ // WITH_RUNTIME fun foo(bar: Iterable) { - for ((index, a) in bar.withIndex()) { + for ((index, a) in bar.withIndex()) { } } \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndices/noBody.kt b/idea/testData/intentions/addForLoopIndices/noBody.kt new file mode 100644 index 00000000000..e47e3432851 --- /dev/null +++ b/idea/testData/intentions/addForLoopIndices/noBody.kt @@ -0,0 +1,4 @@ +// WITH_RUNTIME +fun foo(bar: IntArray) { + for (a in bar) +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndices/noBody.kt.after b/idea/testData/intentions/addForLoopIndices/noBody.kt.after new file mode 100644 index 00000000000..66fb942fd4c --- /dev/null +++ b/idea/testData/intentions/addForLoopIndices/noBody.kt.after @@ -0,0 +1,4 @@ +// WITH_RUNTIME +fun foo(bar: IntArray) { + for ((index, a) in bar.withIndex()) +} \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndices/objectArray.kt.after b/idea/testData/intentions/addForLoopIndices/objectArray.kt.after index 0f51422ec66..097db825620 100644 --- a/idea/testData/intentions/addForLoopIndices/objectArray.kt.after +++ b/idea/testData/intentions/addForLoopIndices/objectArray.kt.after @@ -1,6 +1,6 @@ // WITH_RUNTIME fun foo(bar: Array) { - for ((index, a) in bar.withIndex()) { + for ((index, a) in bar.withIndex()) { } } \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndices/sequence.kt b/idea/testData/intentions/addForLoopIndices/sequence.kt index cc90fb6fd4e..71f1e5be815 100644 --- a/idea/testData/intentions/addForLoopIndices/sequence.kt +++ b/idea/testData/intentions/addForLoopIndices/sequence.kt @@ -1,6 +1,5 @@ // WITH_RUNTIME fun foo(bar: Sequence) { - for (a in bar) { - - } + for (a in bar) + print(a) } \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndices/sequence.kt.after b/idea/testData/intentions/addForLoopIndices/sequence.kt.after index 29a31946b3f..c1fbaf64dab 100644 --- a/idea/testData/intentions/addForLoopIndices/sequence.kt.after +++ b/idea/testData/intentions/addForLoopIndices/sequence.kt.after @@ -1,6 +1,5 @@ // WITH_RUNTIME fun foo(bar: Sequence) { - for ((index, a) in bar.withIndex()) { - - } + for ((index, a) in bar.withIndex()) + print(a) } \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndices/simpleIntList.kt.after b/idea/testData/intentions/addForLoopIndices/simpleIntList.kt.after index 72b7ca64e6e..4a3c9653a5d 100644 --- a/idea/testData/intentions/addForLoopIndices/simpleIntList.kt.after +++ b/idea/testData/intentions/addForLoopIndices/simpleIntList.kt.after @@ -1,7 +1,7 @@ // WITH_RUNTIME fun a() { val b = listOf(1,2,3,4,5) - for ((index, c) in b.withIndex()) { + for ((index, c) in b.withIndex()) { } } \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndices/string.kt b/idea/testData/intentions/addForLoopIndices/string.kt index f2da1ff5806..6fa3a2bd2a0 100644 --- a/idea/testData/intentions/addForLoopIndices/string.kt +++ b/idea/testData/intentions/addForLoopIndices/string.kt @@ -1,6 +1,6 @@ // WITH_RUNTIME fun foo(bar: String) { for (a in bar) { - + print(a) } } \ No newline at end of file diff --git a/idea/testData/intentions/addForLoopIndices/string.kt.after b/idea/testData/intentions/addForLoopIndices/string.kt.after index 4eaba4e1f2d..0956ed2394a 100644 --- a/idea/testData/intentions/addForLoopIndices/string.kt.after +++ b/idea/testData/intentions/addForLoopIndices/string.kt.after @@ -1,6 +1,6 @@ // WITH_RUNTIME fun foo(bar: String) { for ((index, a) in bar.withIndex()) { - + print(a) } } \ 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 36d3715fd69..6d1333cd1e0 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -148,6 +148,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("noBody.kt") + public void testNoBody() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/noBody.kt"); + doTest(fileName); + } + @TestMetadata("objectArray.kt") public void testObjectArray() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/objectArray.kt");