Create From Usage: Support EXPRESSION_EXPECTED_PACKAGE_FOUND diagnostic

This commit is contained in:
Alexey Sedunov
2014-10-07 18:18:35 +04:00
parent c73c581b11
commit 2e6d4b3501
11 changed files with 104 additions and 3 deletions
@@ -238,10 +238,13 @@ public class QuickFixRegistrar {
QuickFixes.factories.put(UNRESOLVED_REFERENCE, CreateFunctionOrPropertyFromCallActionFactory.INSTANCE$);
QuickFixes.factories.put(NO_VALUE_FOR_PARAMETER, CreateFunctionOrPropertyFromCallActionFactory.INSTANCE$);
QuickFixes.factories.put(TOO_MANY_ARGUMENTS, CreateFunctionOrPropertyFromCallActionFactory.INSTANCE$);
QuickFixes.factories.put(EXPRESSION_EXPECTED_PACKAGE_FOUND, CreateFunctionOrPropertyFromCallActionFactory.INSTANCE$);
QuickFixes.factories.put(UNRESOLVED_REFERENCE, CreateLocalVariableActionFactory.INSTANCE$);
QuickFixes.factories.put(EXPRESSION_EXPECTED_PACKAGE_FOUND, CreateLocalVariableActionFactory.INSTANCE$);
QuickFixes.factories.put(UNRESOLVED_REFERENCE, CreateParameterActionFactory.INSTANCE$);
QuickFixes.factories.put(EXPRESSION_EXPECTED_PACKAGE_FOUND, CreateParameterActionFactory.INSTANCE$);
QuickFixes.factories.put(FUNCTION_EXPECTED, CreateInvokeFunctionActionFactory.INSTANCE$);
@@ -23,12 +23,15 @@ import org.jetbrains.jet.lang.psi.JetClassBody
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.lang.psi.psiUtil.getAssignmentByLHS
import org.jetbrains.jet.lang.resolve.BindingContext
import org.jetbrains.jet.lang.psi.JetTypeReference
object CreateFunctionOrPropertyFromCallActionFactory : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val diagElement = diagnostic.getPsiElement()
if (diagElement.getParentByType(javaClass<JetTypeReference>()) != null) return null
val callExpr = when (diagnostic.getFactory()) {
in Errors.UNRESOLVED_REFERENCE_DIAGNOSTICS -> {
in Errors.UNRESOLVED_REFERENCE_DIAGNOSTICS, Errors.EXPRESSION_EXPECTED_PACKAGE_FOUND -> {
val parent = diagElement.getParent()
if (parent is JetCallExpression && parent.getCalleeExpression() == diagElement) parent else diagElement
}
@@ -52,10 +55,10 @@ object CreateFunctionOrPropertyFromCallActionFactory : JetSingleIntentionActionF
if (callParent is JetQualifiedExpression && callParent.getSelectorExpression() == callExpr) callParent else callExpr
val context = AnalyzerFacadeWithCache.getContextForElement(callExpr)
val receiver = callExpr.getCall(context)?.getExplicitReceiver() ?: return null
val receiver = callExpr.getCall(context)?.getExplicitReceiver()
val receiverType = when (receiver) {
ReceiverValue.NO_RECEIVER -> TypeInfo.Empty
null, ReceiverValue.NO_RECEIVER -> TypeInfo.Empty
is Qualifier -> {
val qualifierType = context[BindingContext.EXPRESSION_TYPE, receiver.expression] ?: return null
TypeInfo(qualifierType, Variance.IN_VARIANCE)
@@ -0,0 +1,11 @@
// "Create function 'foo' from usage" "true"
package foo
fun test() {
foo(2, "2")
}
fun foo(i: Int, s: String) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
@@ -0,0 +1,7 @@
// "Create function 'foo' from usage" "true"
package foo
fun test() {
<caret>foo(2, "2")
}
@@ -0,0 +1,11 @@
// "Create local variable 'foo'" "true"
// ACTION: Create parameter 'foo'
// ERROR: Variable 'foo' must be initialized
package foo
fun test(): Int {
val foo: Int
return foo
}
@@ -0,0 +1,9 @@
// "Create local variable 'foo'" "true"
// ACTION: Create parameter 'foo'
// ERROR: Variable 'foo' must be initialized
package foo
fun test(): Int {
return <caret>foo
}
@@ -0,0 +1,8 @@
// "Create parameter 'foo'" "true"
package foo
fun test(n: Int,
foo: Int) {
val t: Int = foo
}
@@ -0,0 +1,7 @@
// "Create parameter 'foo'" "true"
package foo
fun test(n: Int) {
val t: Int = <caret>foo
}
@@ -0,0 +1,10 @@
// "Create property 'foo' from usage" "true"
// ERROR: Property must be initialized
package foo
fun test(): Int {
return foo
}
val foo: Int
@@ -0,0 +1,8 @@
// "Create property 'foo' from usage" "true"
// ERROR: Property must be initialized
package foo
fun test(): Int {
return <caret>foo
}
@@ -814,6 +814,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("beforeFunWithPackageName.kt")
public void testFunWithPackageName() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforeFunWithPackageName.kt");
doTest(fileName);
}
@TestMetadata("beforeInLambda.kt")
public void testInLambda() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforeInLambda.kt");
@@ -1263,6 +1269,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("beforeLocalWithPackageName.kt")
public void testLocalWithPackageName() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/beforeLocalWithPackageName.kt");
doTest(fileName);
}
@TestMetadata("beforeOnTopLevel.kt")
public void testOnTopLevel() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/localVariable/beforeOnTopLevel.kt");
@@ -1495,6 +1507,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("beforeWithPackageName.kt")
public void testWithPackageName() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/parameter/beforeWithPackageName.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/quickfix/createFromUsage/createVariable/property")
@@ -1565,6 +1583,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("beforeTopLevelValWithPackageName.kt")
public void testTopLevelValWithPackageName() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/property/beforeTopLevelValWithPackageName.kt");
doTest(fileName);
}
@TestMetadata("beforeValOnClassNoClassObject.kt")
public void testValOnClassNoClassObject() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/property/beforeValOnClassNoClassObject.kt");