"Nested lambda has shadowed implicit parameter": add quickfix to rename shadowing 'it'
This commit is contained in:
committed by
Vyacheslav Gerasimov
parent
97150cb0e2
commit
b7c4248524
+20
-8
@@ -13,6 +13,7 @@ import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.intentions.ReplaceItWithExplicitFunctionLiteralParamIntention
|
||||
import org.jetbrains.kotlin.idea.refactoring.rename.KotlinVariableInplaceRenameHandler
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||
@@ -35,12 +36,13 @@ class NestedLambdaShadowedImplicitParameterInspection : AbstractKotlinInspection
|
||||
implicitParameterReference,
|
||||
"Implicit parameter 'it' of enclosing lambda is shadowed",
|
||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||
AddExplicitParameterNameFix()
|
||||
AddExplicitParameterToOuterLambdaFix(),
|
||||
RenameImplicitParameterFix()
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
private class AddExplicitParameterNameFix : LocalQuickFix {
|
||||
private class AddExplicitParameterToOuterLambdaFix : LocalQuickFix {
|
||||
override fun getName() = "Add explicit parameter name to outer lambda"
|
||||
|
||||
override fun getFamilyName() = name
|
||||
@@ -52,12 +54,22 @@ class NestedLambdaShadowedImplicitParameterInspection : AbstractKotlinInspection
|
||||
val parameter = parentLambda.functionLiteral.getOrCreateParameterList().addParameterBefore(
|
||||
KtPsiFactory(project).createLambdaParameterList("it").parameters.first(), null
|
||||
)
|
||||
val editor = parentLambda.findExistingEditor()
|
||||
if (editor != null) {
|
||||
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.document)
|
||||
editor.caretModel.moveToOffset(parameter.startOffset)
|
||||
KotlinVariableInplaceRenameHandler().doRename(parameter, editor, null)
|
||||
}
|
||||
val editor = parentLambda.findExistingEditor() ?: return
|
||||
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.document)
|
||||
editor.caretModel.moveToOffset(parameter.startOffset)
|
||||
KotlinVariableInplaceRenameHandler().doRename(parameter, editor, null)
|
||||
}
|
||||
}
|
||||
|
||||
private class RenameImplicitParameterFix : LocalQuickFix {
|
||||
override fun getName() = "Rename 'it'"
|
||||
|
||||
override fun getFamilyName() = name
|
||||
|
||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||
val implicitParameterReference = descriptor.psiElement as? KtNameReferenceExpression ?: return
|
||||
val editor = implicitParameterReference.findExistingEditor() ?: return
|
||||
ReplaceItWithExplicitFunctionLiteralParamIntention().applyTo(implicitParameterReference, editor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
@@ -1,3 +1,5 @@
|
||||
// FIX: Add explicit parameter name to outer lambda
|
||||
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
fun bar(s: String) {}
|
||||
|
||||
|
||||
+2
@@ -1,3 +1,5 @@
|
||||
// FIX: Add explicit parameter name to outer lambda
|
||||
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
fun bar(s: String) {}
|
||||
|
||||
|
||||
+2
@@ -1,3 +1,5 @@
|
||||
// FIX: Add explicit parameter name to outer lambda
|
||||
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
fun bar(f: (Int) -> Unit) {}
|
||||
|
||||
|
||||
+2
@@ -1,3 +1,5 @@
|
||||
// FIX: Add explicit parameter name to outer lambda
|
||||
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
fun bar(f: (Int) -> Unit) {}
|
||||
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// FIX: Rename 'it'
|
||||
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
fun bar(s: String) {}
|
||||
|
||||
fun test() {
|
||||
foo {
|
||||
foo {
|
||||
bar(it<caret>)
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// FIX: Rename 'it'
|
||||
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
fun bar(s: String) {}
|
||||
|
||||
fun test() {
|
||||
foo {
|
||||
foo { it1 ->
|
||||
bar(it1)
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+2
@@ -1,3 +1,5 @@
|
||||
// FIX: Add explicit parameter name to outer lambda
|
||||
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
fun bar(s: String) {}
|
||||
|
||||
|
||||
Vendored
+2
@@ -1,3 +1,5 @@
|
||||
// FIX: Add explicit parameter name to outer lambda
|
||||
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
fun bar(s: String) {}
|
||||
|
||||
|
||||
+5
@@ -3466,6 +3466,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
runTest("idea/testData/inspectionsLocal/nestedLambdaShadowedImplicitParameter/implicit2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("implicit3.kt")
|
||||
public void testImplicit3() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/nestedLambdaShadowedImplicitParameter/implicit3.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("implicitGrandParent.kt")
|
||||
public void testImplicitGrandParent() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/nestedLambdaShadowedImplicitParameter/implicitGrandParent.kt");
|
||||
|
||||
Reference in New Issue
Block a user