KT-4820: Intention to remove an unused index variable from a for loop.

This commit is contained in:
Ross Hanson
2014-04-21 09:31:28 -04:00
parent 97010c7d7f
commit 35c5a62b11
15 changed files with 174 additions and 2 deletions
@@ -415,6 +415,7 @@ fun main(args: Array<String>) {
model("intentions/convertToForEachLoop", testMethod = "doTestConvertToForEachLoop")
model("intentions/convertToForEachFunctionCall", testMethod = "doTestConvertToForEachFunctionCall")
model("intentions/addForLoopIndicesIntention", testMethod = "doTestAddForLoopIndicesIntention")
model("intentions/removeForLoopIndicesIntention", testMethod = "doTestRemoveForLoopIndicesIntention")
}
testClass(javaClass<AbstractJetInspectionTest>()) {
@@ -0,0 +1,3 @@
for (x in foo) {
}
@@ -0,0 +1,3 @@
for ((i,x) in foo.withIndices()) {
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention removes the index variable from a for loop iterating over a collection.
</body>
</html>
+5
View File
@@ -661,6 +661,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.intentions.RemoveForLoopIndicesIntention</className>
<category>Kotlin</category>
</intentionAction>S
<intentionAction>
<className>org.jetbrains.jet.plugin.intentions.SwapBinaryExpression</className>
<category>Kotlin</category>
@@ -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
@@ -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<JetForExpression>(
"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<UsageInfo> {
override fun process(t: UsageInfo?): Boolean {
usageCount[0] = usageCount[0] + 1
return true
}
}
findHandler.processElementUsages(indexVar,processor,options)
}
}
usageFinderRunnable.run()
return usageCount[0] == 0
}
}
@@ -0,0 +1,6 @@
//IS_APPLICABLE: FALSE
fun foo(bar: List<String>) {
for (<caret>a in bar) {
}
}
@@ -0,0 +1,7 @@
// IS_APPLICABLE: FALSE
//ERROR: Unresolved reference: withIndices
fun foo(b: List<Int>) {
for ((i, <caret>c) in b.withIndices()) {
return i
}
}
@@ -0,0 +1,6 @@
//ERROR: Unresolved reference: withIndices
fun foo(bar: List<Int>) {
for ((i : <caret>Int, b: Int) in bar.withIndices()) {
}
}
@@ -0,0 +1,6 @@
//ERROR: Unresolved reference: withIndices
fun foo(bar: List<Int>) {
for (b: Int in bar) {
}
}
@@ -0,0 +1,6 @@
//ERROR: Unresolved reference: withIndices
fun foo(bar: List<String>) {
for ((i,<caret>a) in bar.withIndices()) {
}
}
@@ -0,0 +1,6 @@
//ERROR: Unresolved reference: withIndices
fun foo(bar: List<String>) {
for (a in bar) {
}
}
@@ -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);
@@ -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;
}
}