New Intention: Replace for loop with forEach
Replaces an expression of the form “for (i in list) { … }” with an
expression of the form “list.forEach { i -> … }”
This commit is contained in:
committed by
Mikhael Bogdanov
parent
dbb28c571b
commit
33fd82cf45
@@ -413,6 +413,7 @@ fun main(args: Array<String>) {
|
||||
model("intentions/makeTypeExplicitInLambda", testMethod = "doTestMakeTypeExplicitInLambda")
|
||||
model("intentions/makeTypeImplicitInLambda", testMethod = "doTestMakeTypeImplicitInLambda")
|
||||
model("intentions/convertToForEachLoop", testMethod = "doTestConvertToForEachLoop")
|
||||
model("intentions/convertToForEachFunctionCall", testMethod = "doTestConvertToForEachFunctionCall")
|
||||
}
|
||||
|
||||
testClass(javaClass<AbstractJetInspectionTest>()) {
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
list.forEach { x -> x.bar() }
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<spot>for (x in list) {
|
||||
x.bar()
|
||||
}</spot>
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention transforms a for loop expression into a function call to "forEach"
|
||||
</body>
|
||||
</html>
|
||||
@@ -595,7 +595,7 @@
|
||||
<className>org.jetbrains.jet.plugin.intentions.MoveLambdaOutsideParenthesesIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.intentions.declarations.SplitPropertyDeclarationIntention</className>
|
||||
<category>Kotlin</category>
|
||||
@@ -701,6 +701,10 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.intentions.ConvertToForEachFunctionCallIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.jet.plugin.inspections.ExplicitGetInspection"
|
||||
displayName="Explicit 'get'"
|
||||
|
||||
@@ -314,6 +314,8 @@ make.type.implicit.in.lambda=Make types implicit in lambda (may break code)
|
||||
make.type.implicit.in.lambda.family=Make Types Implicit In Lambda (May Break Code)
|
||||
convert.to.for.each.loop.intention=Replace with a For Each Loop
|
||||
convert.to.for.each.loop.intention.family=Replace with a For Each Loop
|
||||
convert.to.for.each.function.call.intention=Replace with a forEach Function Call
|
||||
convert.to.for.each.function.call.intention.family=Replace with a forEach Function Call
|
||||
|
||||
property.is.implemented.too.many=Has implementations
|
||||
property.is.overridden.too.many=Is overridden in subclasses
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* 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.JetBlockExpression
|
||||
import org.jetbrains.jet.lang.psi.JetElement
|
||||
import org.jetbrains.jet.lang.psi.JetParameter
|
||||
import org.jetbrains.jet.lang.psi.JetOperationExpression
|
||||
|
||||
public class ConvertToForEachFunctionCallIntention : JetSelfTargetingIntention<JetForExpression>("convert.to.for.each.function.call.intention", javaClass()) {
|
||||
override fun isApplicableTo(element: JetForExpression): Boolean {
|
||||
return element.getLoopRange() != null && element.getLoopParameter() != null && element.getBody() != null
|
||||
}
|
||||
|
||||
override fun applyTo(element: JetForExpression, editor: Editor) {
|
||||
fun buildStatements(statements: List<JetElement>): String {
|
||||
return when {
|
||||
statements.isEmpty() -> ""
|
||||
statements.size() == 1 -> statements[0].getText()
|
||||
else -> statements.fold(StringBuilder(), { acc, h -> acc.append("${h.getText()}\n") }).toString()
|
||||
}
|
||||
}
|
||||
|
||||
fun buildReplacementBodyText(loopParameter: JetParameter, functionBodyText: String): String {
|
||||
return when {
|
||||
loopParameter.getTypeReference() != null -> " (${loopParameter.getText()}) -> $functionBodyText"
|
||||
else -> "${loopParameter.getText()} -> $functionBodyText"
|
||||
}
|
||||
}
|
||||
|
||||
fun buildReceiverText(element: JetForExpression): String {
|
||||
val loopRange = element.getLoopRange()!!
|
||||
|
||||
return when (loopRange) {
|
||||
is JetOperationExpression -> "(${loopRange.getText()})"
|
||||
else -> loopRange.getText()
|
||||
}
|
||||
}
|
||||
|
||||
val body = element.getBody()!!
|
||||
val loopParameter = element.getLoopParameter()!!
|
||||
|
||||
val bodyText = buildReplacementBodyText(loopParameter, when (body) {
|
||||
is JetBlockExpression -> buildStatements(body.getStatements())
|
||||
else -> body.getText()
|
||||
})
|
||||
|
||||
element.replace(JetPsiFactory.createExpression(element.getProject(), "${buildReceiverText(element)}.forEach { $bodyText }"))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
<caret>for (x in 1 rangeTo 2) {
|
||||
x
|
||||
}
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
(1 rangeTo 2).forEach { x -> x }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun foo() {
|
||||
val list = 1..4
|
||||
|
||||
<caret>for (x in list) {
|
||||
x
|
||||
x
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun foo() {
|
||||
val list = 1..4
|
||||
|
||||
list.forEach { x ->
|
||||
x
|
||||
x
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
val list = 1..4
|
||||
|
||||
<caret>for (x: Int in list) {
|
||||
x
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
val list = 1..4
|
||||
|
||||
list.forEach {(x: Int) -> x }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun foo() {
|
||||
val list = 1..4
|
||||
val i = 0
|
||||
|
||||
<caret>for (i in list)
|
||||
i
|
||||
i
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
val list = 1..4
|
||||
val i = 0
|
||||
|
||||
list.forEach { i -> i }
|
||||
i
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
val list = 1..4
|
||||
|
||||
<caret>for (i in list) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
val list = 1..4
|
||||
|
||||
list.forEach { i -> }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
val list = 1..4
|
||||
|
||||
<caret>for (x in list) {
|
||||
x
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
val list = 1..4
|
||||
|
||||
list.forEach { x -> x }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
val list = 1..4
|
||||
|
||||
<caret>for (x: Int in list) 11
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
val list = 1..4
|
||||
|
||||
list.forEach {(x: Int) -> 11 }
|
||||
}
|
||||
@@ -270,6 +270,10 @@ public abstract class AbstractCodeTransformationTest extends LightCodeInsightTes
|
||||
doTestIntention(path, new ConvertToForEachLoopIntention());
|
||||
}
|
||||
|
||||
public void doTestConvertToForEachFunctionCall(@NotNull String path) throws Exception {
|
||||
doTestIntention(path, new ConvertToForEachFunctionCallIntention());
|
||||
}
|
||||
|
||||
private void doTestIntention(@NotNull String path, @NotNull IntentionAction intentionAction) throws Exception {
|
||||
configureByFile(path);
|
||||
|
||||
|
||||
+50
-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})
|
||||
@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})
|
||||
public class CodeTransformationTestGenerated extends AbstractCodeTransformationTest {
|
||||
@TestMetadata("idea/testData/intentions/branched/doubleBangToIfThen")
|
||||
public static class DoubleBangToIfThen extends AbstractCodeTransformationTest {
|
||||
@@ -4155,6 +4155,11 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
|
||||
doTestConvertToForEachLoop("idea/testData/intentions/convertToForEachLoop/infixCall.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("parenthesizedExpression.kt")
|
||||
public void testParenthesizedExpression() throws Exception {
|
||||
doTestConvertToForEachLoop("idea/testData/intentions/convertToForEachLoop/parenthesizedExpression.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
doTestConvertToForEachLoop("idea/testData/intentions/convertToForEachLoop/simple.kt");
|
||||
@@ -4177,6 +4182,49 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/convertToForEachFunctionCall")
|
||||
public static class ConvertToForEachFunctionCall extends AbstractCodeTransformationTest {
|
||||
public void testAllFilesPresentInConvertToForEachFunctionCall() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/intentions/convertToForEachFunctionCall"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("binaryExpressionLoopRange.kt")
|
||||
public void testBinaryExpressionLoopRange() throws Exception {
|
||||
doTestConvertToForEachFunctionCall("idea/testData/intentions/convertToForEachFunctionCall/binaryExpressionLoopRange.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("blockBodyExpression.kt")
|
||||
public void testBlockBodyExpression() throws Exception {
|
||||
doTestConvertToForEachFunctionCall("idea/testData/intentions/convertToForEachFunctionCall/blockBodyExpression.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("iterativeElementTypeSpecified.kt")
|
||||
public void testIterativeElementTypeSpecified() throws Exception {
|
||||
doTestConvertToForEachFunctionCall("idea/testData/intentions/convertToForEachFunctionCall/iterativeElementTypeSpecified.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("noCurlyBraces.kt")
|
||||
public void testNoCurlyBraces() throws Exception {
|
||||
doTestConvertToForEachFunctionCall("idea/testData/intentions/convertToForEachFunctionCall/noCurlyBraces.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("noStatements.kt")
|
||||
public void testNoStatements() throws Exception {
|
||||
doTestConvertToForEachFunctionCall("idea/testData/intentions/convertToForEachFunctionCall/noStatements.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
doTestConvertToForEachFunctionCall("idea/testData/intentions/convertToForEachFunctionCall/simple.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("typeAnnotatedWithNonBlockBody.kt")
|
||||
public void testTypeAnnotatedWithNonBlockBody() throws Exception {
|
||||
doTestConvertToForEachFunctionCall("idea/testData/intentions/convertToForEachFunctionCall/typeAnnotatedWithNonBlockBody.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite("CodeTransformationTestGenerated");
|
||||
suite.addTestSuite(DoubleBangToIfThen.class);
|
||||
@@ -4236,6 +4284,7 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
|
||||
suite.addTestSuite(MakeTypeExplicitInLambda.class);
|
||||
suite.addTestSuite(MakeTypeImplicitInLambda.class);
|
||||
suite.addTestSuite(ConvertToForEachLoop.class);
|
||||
suite.addTestSuite(ConvertToForEachFunctionCall.class);
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user