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:
Mikhail Glukhikh
2016-06-16 18:02:10 +03:00
committed by Mikhail Glukhikh
parent b44c94a887
commit bd8c7577e1
3 changed files with 22 additions and 5 deletions
@@ -47,17 +47,19 @@ class ReplaceExplicitFunctionLiteralParamWithItIntention() : PsiElementBaseInten
if (parameter.typeReference != null) return false
if (functionLiteral.anyDescendantOfType<KtFunctionLiteral>() { literal ->
literal !== functionLiteral &&
!literal.hasParameterSpecification() &&
literal.anyDescendantOfType<KtSimpleNameExpression> { nameExpr ->
nameExpr.getReferencedName() == element.text
}
literal.usesName(element.text) &&
(!literal.hasParameterSpecification() || literal.usesName("it"))
} ) return false
text = "Replace explicit parameter '${parameter.name}' with 'it'"
return true
}
private fun KtFunctionLiteral.usesName(name: String): Boolean =
anyDescendantOfType<KtSimpleNameExpression> {
nameExpr -> nameExpr.getReferencedName() == name
}
override fun startInWriteAction(): Boolean = false
override fun invoke(project: Project, editor: Editor, element: PsiElement) {
@@ -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);
}
@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")
public void testNotApplicable_nestedLiterals() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_nestedLiterals.kt");