KT-4820: Intention to introduce an index variable to a for loop over an ordered collection via "withIndices()."
This commit is contained in:
@@ -414,6 +414,7 @@ fun main(args: Array<String>) {
|
||||
model("intentions/makeTypeImplicitInLambda", testMethod = "doTestMakeTypeImplicitInLambda")
|
||||
model("intentions/convertToForEachLoop", testMethod = "doTestConvertToForEachLoop")
|
||||
model("intentions/convertToForEachFunctionCall", testMethod = "doTestConvertToForEachFunctionCall")
|
||||
model("intentions/addForLoopIndicesIntention", testMethod = "doTestAddForLoopIndicesIntention")
|
||||
}
|
||||
|
||||
testClass(javaClass<AbstractJetInspectionTest>()) {
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
for ((i, x) in foo.withIndices()) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
for (x in foo) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention adds an index variable to a for loop iterating over a collection.
|
||||
</body>
|
||||
</html>
|
||||
@@ -656,6 +656,11 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.intentions.AddForLoopIndicesIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.intentions.SwapBinaryExpression</className>
|
||||
<category>Kotlin</category>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<JetForExpression>(
|
||||
"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<ModuleDescriptor>())!!
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
//IS_APPLICABLE: FALSE
|
||||
//ERROR: Unresolved reference: withIndices
|
||||
fun b(c: List<String>) {
|
||||
for ((<caret>indexVariable, d) in c.withIndices()) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// IS_APPLICABLE: FALSE
|
||||
// ERROR: For-loop range must have an iterator() method
|
||||
fun foo(bar: Map<String, String>) {
|
||||
for (a <caret>in bar) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// ERROR: Unresolved reference: listOf
|
||||
fun a() {
|
||||
val b = listOf(1,2,3,4,5)
|
||||
for (<caret>c : Int in b) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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()) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// ERROR: Unresolved reference: listOf
|
||||
fun a() {
|
||||
val b = listOf(1,2,3,4,5)
|
||||
for (<caret>c in b) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// ERROR: Unresolved reference: listOf
|
||||
fun a() {
|
||||
val b = listOf(1,2,3,4,5)
|
||||
for ((index, c) in b.withIndices()) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
//ERROR: Unresolved reference: SortedMap
|
||||
fun a(b: SortedMap<Int, String>) {
|
||||
for (<caret>c in b) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
//ERROR: Unresolved reference: SortedMap
|
||||
fun a(b: SortedMap<Int, String>) {
|
||||
for ((index, c) in b.withIndices()) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
+35
-1
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user