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

#KT-10119 Fixed
This commit is contained in:
Alexey Sedunov
2015-11-26 18:17:24 +03:00
parent 3c87ccc5ff
commit 2dcc38b92f
11 changed files with 77 additions and 13 deletions
@@ -129,7 +129,7 @@ sealed class CreateCallableFromCallActionFactory<E : KtExpression>(
object Property: CreateCallableFromCallActionFactory<KtSimpleNameExpression>() {
override fun getElementOfInterest(diagnostic: Diagnostic): KtSimpleNameExpression? {
return getExpressionOfInterest(diagnostic) as? KtSimpleNameExpression
return getExpressionOfInterest(diagnostic) as? KtNameReferenceExpression
}
override fun doCreateCallableInfo(
@@ -27,7 +27,6 @@ import org.jetbrains.kotlin.idea.quickfix.KotlinSingleIntentionActionFactory
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.CreateFromUsageFixBase
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.*
import org.jetbrains.kotlin.idea.util.application.executeCommand
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getAssignmentByLHS
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedElement
@@ -38,9 +37,8 @@ import java.util.*
object CreateLocalVariableActionFactory: KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val refExpr = QuickFixUtil.getParentElementOfType(diagnostic, javaClass<KtSimpleNameExpression>()) ?: return null
val refExpr = QuickFixUtil.getParentElementOfType(diagnostic, javaClass<KtNameReferenceExpression>()) ?: return null
if (refExpr.getQualifiedElement() != refExpr) return null
if (refExpr.getReferencedNameElementType() != KtTokens.IDENTIFIER) return null
val propertyName = refExpr.getReferencedName()
@@ -54,7 +52,7 @@ object CreateLocalVariableActionFactory: KotlinSingleIntentionActionFactory() {
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val assignment = refExpr.getAssignmentByLHS()
val varExpected = assignment != null
var originalElement = assignment ?: refExpr
var originalElement: KtExpression = assignment ?: refExpr
val actualContainer = when (container) {
is KtBlockExpression -> container
@@ -24,15 +24,14 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.util.getResolutionScope
import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.getExpressionForTypeGuess
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.getTypeParameters
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.guessTypes
import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinParameterInfo
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.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getAssignmentByLHS
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedElement
@@ -47,9 +46,8 @@ import java.util.*
object CreateParameterByRefActionFactory : CreateParameterFromUsageFactory<KtSimpleNameExpression>() {
override fun getElementOfInterest(diagnostic: Diagnostic): KtSimpleNameExpression? {
val refExpr = QuickFixUtil.getParentElementOfType(diagnostic, javaClass<KtSimpleNameExpression>()) ?: return null
val refExpr = QuickFixUtil.getParentElementOfType(diagnostic, javaClass<KtNameReferenceExpression>()) ?: return null
if (refExpr.getQualifiedElement() != refExpr) return null
if (refExpr.getReferencedNameElementType() != KtTokens.IDENTIFIER) return null
return refExpr
}
@@ -1,11 +1,7 @@
// "Import" "false"
// ERROR: Unresolved reference: foo
// ACTION: Create extension function 'foo'
// ACTION: Create extension property 'foo'
// ACTION: Create local variable 'foo'
// ACTION: Create member function 'foo'
// ACTION: Create member property 'foo'
// ACTION: Create parameter 'foo'
// ACTION: Replace infix call with ordinary call
package h
@@ -0,0 +1,7 @@
// "Create local variable 'foo'" "false"
// ERROR: Unresolved reference: foo
// ACTION: Create extension function 'foo'
// ACTION: Replace infix call with ordinary call
fun refer() {
1 <caret>foo 2
}
@@ -0,0 +1,5 @@
// "Create local variable 'foo'" "false"
// ERROR: Unresolved reference: @foo
fun refer() {
val v = this@<caret>foo
}
@@ -0,0 +1,7 @@
// "Create parameter 'foo'" "false"
// ERROR: Unresolved reference: foo
// ACTION: Create extension function 'foo'
// ACTION: Replace infix call with ordinary call
fun refer() {
1 <caret>foo 2
}
@@ -0,0 +1,5 @@
// "Create parameter 'foo'" "false"
// ERROR: Unresolved reference: @foo
fun refer() {
val v = this@<caret>foo
}
@@ -0,0 +1,7 @@
// "Create property 'foo'" "false"
// ERROR: Unresolved reference: foo
// ACTION: Create extension function 'foo'
// ACTION: Replace infix call with ordinary call
fun refer() {
1 <caret>foo 2
}
@@ -0,0 +1,5 @@
// "Create property 'foo'" "false"
// ERROR: Unresolved reference: @foo
fun refer() {
val v = this@<caret>foo
}
@@ -2558,6 +2558,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("inBinaryOperation.kt")
public void testInBinaryOperation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/inBinaryOperation.kt");
doTest(fileName);
}
@TestMetadata("inClass.kt")
public void testInClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/inClass.kt");
@@ -2576,6 +2582,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("inLabelRef.kt")
public void testInLabelRef() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/inLabelRef.kt");
doTest(fileName);
}
@TestMetadata("inLambdaNoParams.kt")
public void testInLambdaNoParams() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/inLambdaNoParams.kt");
@@ -2723,6 +2735,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("inBinaryOperation.kt")
public void testInBinaryOperation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/parameter/inBinaryOperation.kt");
doTest(fileName);
}
@TestMetadata("inClassInitializer.kt")
public void testInClassInitializer() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/parameter/inClassInitializer.kt");
@@ -2777,6 +2795,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("inLabelRef.kt")
public void testInLabelRef() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/parameter/inLabelRef.kt");
doTest(fileName);
}
@TestMetadata("inLambdaNoParams.kt")
public void testInLambdaNoParams() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/parameter/inLambdaNoParams.kt");
@@ -3041,6 +3065,18 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("inBinaryOperation.kt")
public void testInBinaryOperation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/property/inBinaryOperation.kt");
doTest(fileName);
}
@TestMetadata("inLabelRef.kt")
public void testInLabelRef() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/property/inLabelRef.kt");
doTest(fileName);
}
@TestMetadata("inconsistentTypes.kt")
public void testInconsistentTypes() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/property/inconsistentTypes.kt");