Replace explicit parameter with 'it': do not suggest when lambda is directly under "when" or "if"

Relates to #KT-35320
This commit is contained in:
Toshiaki Kameyama
2019-12-06 14:13:43 +01:00
committed by Vladimir Dolzhenko
parent daab07ea38
commit 6b2c87020b
11 changed files with 89 additions and 0 deletions
@@ -33,6 +33,9 @@ import org.jetbrains.kotlin.idea.core.copied
import org.jetbrains.kotlin.idea.references.resolveMainReferenceToDescriptors
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.*
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
@@ -53,6 +56,8 @@ class ReplaceExplicitFunctionLiteralParamWithItIntention : PsiElementBaseIntenti
}) return false
val lambda = functionLiteral.parent as? KtLambdaExpression ?: return false
val lambdaParent = lambda.parent
if (lambdaParent is KtWhenEntry || lambdaParent is KtContainerNodeForControlStructureBody) return false
val call = lambda.getStrictParentOfType<KtCallExpression>()
if (call != null) {
val argumentIndex = call.valueArguments.indexOfFirst { it.getArgumentExpression() == lambda }
@@ -0,0 +1,4 @@
fun test(i: Int) {
val p: (String) -> Boolean =
if (i == 1) { { <caret>s -> true } } else { { s -> false } }
}
@@ -0,0 +1,4 @@
fun test(i: Int) {
val p: (String) -> Boolean =
if (i == 1) { { true } } else { { s -> false } }
}
@@ -0,0 +1,4 @@
fun test(i: Int) {
val p: (String) -> Boolean =
if (i == 1) { { s -> true } } else { { <caret>s -> false } }
}
@@ -0,0 +1,4 @@
fun test(i: Int) {
val p: (String) -> Boolean =
if (i == 1) { { s -> true } } else { { false } }
}
@@ -0,0 +1,9 @@
fun test(v: Boolean): (String) -> Int {
return when (v) {
true -> { { <caret>x -> taskOne(x) } }
false -> { x -> taskTwo(x) }
}
}
fun taskOne(s: String) = s.length
fun taskTwo(s: String) = 42
@@ -0,0 +1,9 @@
fun test(v: Boolean): (String) -> Int {
return when (v) {
true -> { { taskOne(it) } }
false -> { x -> taskTwo(x) }
}
}
fun taskOne(s: String) = s.length
fun taskTwo(s: String) = 42
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
fun test(i: Int) {
val p: (String) -> Boolean =
if (i == 1) { <caret>s -> true } else { s -> false }
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
fun test(i: Int) {
val p: (String) -> Boolean =
if (i == 1) { s -> true } else { <caret>s -> false }
}
@@ -0,0 +1,10 @@
// IS_APPLICABLE: false
fun test(v: Boolean): (String) -> Int {
return when (v) {
true -> { { x -> taskOne(x) } }
false -> { <caret>x -> taskTwo(x) }
}
}
fun taskOne(s: String) = s.length
fun taskTwo(s: String) = 42
@@ -14732,6 +14732,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true);
}
@TestMetadata("applicable_InIf.kt")
public void testApplicable_InIf() throws Exception {
runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIf.kt");
}
@TestMetadata("applicable_InIfElse.kt")
public void testApplicable_InIfElse() throws Exception {
runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIfElse.kt");
}
@TestMetadata("applicable_cursofOverParamInInnerLiteral.kt")
public void testApplicable_cursofOverParamInInnerLiteral() throws Exception {
runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_cursofOverParamInInnerLiteral.kt");
@@ -14757,6 +14767,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_inPropertyInitializer.kt");
}
@TestMetadata("applicable_inWhenEntry.kt")
public void testApplicable_inWhenEntry() throws Exception {
runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_inWhenEntry.kt");
}
@TestMetadata("applicable_nestedLiteralsNoUseInside.kt")
public void testApplicable_nestedLiteralsNoUseInside() throws Exception {
runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_nestedLiteralsNoUseInside.kt");
@@ -14767,6 +14782,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_qualifiedExpression.kt");
}
@TestMetadata("notApplicable_InIf.kt")
public void testNotApplicable_InIf() throws Exception {
runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_InIf.kt");
}
@TestMetadata("notApplicable_InIfElse.kt")
public void testNotApplicable_InIfElse() throws Exception {
runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_InIfElse.kt");
}
@TestMetadata("notApplicable_alreadyUsesImplicitIt.kt")
public void testNotApplicable_alreadyUsesImplicitIt() throws Exception {
runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_alreadyUsesImplicitIt.kt");
@@ -14782,6 +14807,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_hasMultipleParameters.kt");
}
@TestMetadata("notApplicable_inWhenEntry.kt")
public void testNotApplicable_inWhenEntry() throws Exception {
runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_inWhenEntry.kt");
}
@TestMetadata("notApplicable_itFromOuterLambda.kt")
public void testNotApplicable_itFromOuterLambda() throws Exception {
runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_itFromOuterLambda.kt");