Create from Usage: Forbid "Create local variable/parameter/property" on callable references

#KT-10283 In Progress
This commit is contained in:
Alexey Sedunov
2015-12-07 15:16:22 +03:00
parent 44743aade5
commit 87aebd2cdf
7 changed files with 53 additions and 5 deletions
@@ -33,6 +33,7 @@ import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getAssignmentByLHS
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypeAndBranch
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelectorOrThis
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.callUtil.getCall
@@ -129,7 +130,9 @@ sealed class CreateCallableFromCallActionFactory<E : KtExpression>(
object Property: CreateCallableFromCallActionFactory<KtSimpleNameExpression>() {
override fun getElementOfInterest(diagnostic: Diagnostic): KtSimpleNameExpression? {
return getExpressionOfInterest(diagnostic) as? KtNameReferenceExpression
val refExpr = getExpressionOfInterest(diagnostic) as? KtNameReferenceExpression ?: return null
if (refExpr.getParentOfTypeAndBranch<KtCallableReferenceExpression> { callableReference } != null) return null
return refExpr
}
override fun doCreateCallableInfo(
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.*
import org.jetbrains.kotlin.idea.util.application.executeCommand
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getAssignmentByLHS
import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypeAndBranch
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedElement
import org.jetbrains.kotlin.psi.psiUtil.parents
import org.jetbrains.kotlin.types.Variance
@@ -39,6 +40,7 @@ object CreateLocalVariableActionFactory: KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val refExpr = QuickFixUtil.getParentElementOfType(diagnostic, javaClass<KtNameReferenceExpression>()) ?: return null
if (refExpr.getQualifiedElement() != refExpr) return null
if (refExpr.getParentOfTypeAndBranch<KtCallableReferenceExpression> { callableReference } != null) return null
val propertyName = refExpr.getReferencedName()
@@ -33,10 +33,7 @@ import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinValVar
import org.jetbrains.kotlin.idea.util.getResolutionScope
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getAssignmentByLHS
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedElement
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.parents
import org.jetbrains.kotlin.psi.psiUtil.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.scopes.utils.findClassifier
import org.jetbrains.kotlin.resolve.source.getPsi
@@ -48,6 +45,7 @@ object CreateParameterByRefActionFactory : CreateParameterFromUsageFactory<KtSim
override fun getElementOfInterest(diagnostic: Diagnostic): KtSimpleNameExpression? {
val refExpr = QuickFixUtil.getParentElementOfType(diagnostic, javaClass<KtNameReferenceExpression>()) ?: return null
if (refExpr.getQualifiedElement() != refExpr) return null
if (refExpr.getParentOfTypeAndBranch<KtCallableReferenceExpression> { callableReference } != null) return null
return refExpr
}
@@ -0,0 +1,9 @@
// "Create local variable 'foo'" "false"
// ACTION: Rename reference
// ACTION: Add 'f =' to argument
// ERROR: Unresolved reference: foo
fun test(f: (Int) -> Int) {}
fun refer() {
val v = test(::<caret>foo)
}
@@ -0,0 +1,9 @@
// "Create parameter 'foo'" "false"
// ACTION: Rename reference
// ACTION: Add 'f =' to argument
// ERROR: Unresolved reference: foo
fun test(f: (Int) -> Int) {}
fun refer() {
val v = test(::<caret>foo)
}
@@ -0,0 +1,9 @@
// "Create property 'foo'" "false"
// ACTION: Rename reference
// ACTION: Add 'f =' to argument
// ERROR: Unresolved reference: foo
fun test(f: (Int) -> Int) {}
fun refer() {
val v = test(::<caret>foo)
}
@@ -2558,6 +2558,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("callableRef.kt")
public void testCallableRef() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/callableRef.kt");
doTest(fileName);
}
@TestMetadata("inAccessor.kt")
public void testInAccessor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/inAccessor.kt");
@@ -2693,6 +2699,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("callableRef.kt")
public void testCallableRef() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/parameter/callableRef.kt");
doTest(fileName);
}
@TestMetadata("inAccessorInClass.kt")
public void testInAccessorInClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/parameter/inAccessorInClass.kt");
@@ -3071,6 +3083,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("callableRef.kt")
public void testCallableRef() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/property/callableRef.kt");
doTest(fileName);
}
@TestMetadata("extensionRefInImport.kt")
public void testExtensionRefInImport() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/property/extensionRefInImport.kt");