Replace explicit parameter with it : correct handling of it from outer lambda #KT-12437 Fixed
(cherry picked from commit cb8fe96)
This commit is contained in:
committed by
Mikhail Glukhikh
parent
b44c94a887
commit
bd8c7577e1
+7
-5
@@ -47,17 +47,19 @@ class ReplaceExplicitFunctionLiteralParamWithItIntention() : PsiElementBaseInten
|
|||||||
if (parameter.typeReference != null) return false
|
if (parameter.typeReference != null) return false
|
||||||
|
|
||||||
if (functionLiteral.anyDescendantOfType<KtFunctionLiteral>() { literal ->
|
if (functionLiteral.anyDescendantOfType<KtFunctionLiteral>() { literal ->
|
||||||
literal !== functionLiteral &&
|
literal.usesName(element.text) &&
|
||||||
!literal.hasParameterSpecification() &&
|
(!literal.hasParameterSpecification() || literal.usesName("it"))
|
||||||
literal.anyDescendantOfType<KtSimpleNameExpression> { nameExpr ->
|
|
||||||
nameExpr.getReferencedName() == element.text
|
|
||||||
}
|
|
||||||
} ) return false
|
} ) return false
|
||||||
|
|
||||||
text = "Replace explicit parameter '${parameter.name}' with 'it'"
|
text = "Replace explicit parameter '${parameter.name}' with 'it'"
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun KtFunctionLiteral.usesName(name: String): Boolean =
|
||||||
|
anyDescendantOfType<KtSimpleNameExpression> {
|
||||||
|
nameExpr -> nameExpr.getReferencedName() == name
|
||||||
|
}
|
||||||
|
|
||||||
override fun startInWriteAction(): Boolean = false
|
override fun startInWriteAction(): Boolean = false
|
||||||
|
|
||||||
override fun invoke(project: Project, editor: Editor, element: PsiElement) {
|
override fun invoke(project: Project, editor: Editor, element: PsiElement) {
|
||||||
|
|||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
// See KT-12437: it from outer lambda
|
||||||
|
fun acceptLambda(p: Int, f: (Int) -> Int): Int = f(p)
|
||||||
|
fun use() {
|
||||||
|
acceptLambda(1) {
|
||||||
|
acceptLambda(2) { <caret>p2 -> it * 10 + p2 }
|
||||||
|
}.hashCode()
|
||||||
|
}
|
||||||
@@ -8293,6 +8293,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("notApplicable_itFromOuterLambda.kt")
|
||||||
|
public void testNotApplicable_itFromOuterLambda() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_itFromOuterLambda.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("notApplicable_nestedLiterals.kt")
|
@TestMetadata("notApplicable_nestedLiterals.kt")
|
||||||
public void testNotApplicable_nestedLiterals() throws Exception {
|
public void testNotApplicable_nestedLiterals() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_nestedLiterals.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_nestedLiterals.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user