Create Function From Usage: Quick-fix for invoke convention

This commit is contained in:
Alexey Sedunov
2014-09-24 16:00:39 +04:00
parent cf59af6c7a
commit 67df2b448f
11 changed files with 145 additions and 1 deletions
@@ -232,6 +232,8 @@ public class QuickFixRegistrar {
QuickFixes.factories.put(NO_VALUE_FOR_PARAMETER, CreateBinaryOperationActionFactory.INSTANCE$);
QuickFixes.factories.put(TOO_MANY_ARGUMENTS, CreateBinaryOperationActionFactory.INSTANCE$);
QuickFixes.factories.put(FUNCTION_EXPECTED, CreateInvokeFunctionActionFactory.INSTANCE$);
QuickFixes.factories.put(TYPE_MISMATCH, new QuickFixFactoryForTypeMismatchError());
QuickFixes.factories.put(AUTOCAST_IMPOSSIBLE, CastExpressionFix.createFactoryForAutoCastImpossible());
@@ -0,0 +1,33 @@
package org.jetbrains.jet.plugin.quickfix.createFromUsage.createFunction
import org.jetbrains.jet.plugin.quickfix.JetSingleIntentionActionFactory
import org.jetbrains.jet.lang.diagnostics.Diagnostic
import com.intellij.codeInsight.intention.IntentionAction
import org.jetbrains.jet.plugin.quickfix.QuickFixUtil
import org.jetbrains.jet.lang.psi.JetArrayAccessExpression
import org.jetbrains.jet.lang.types.Variance
import org.jetbrains.jet.lang.psi.JetCallExpression
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns
import org.jetbrains.jet.lang.diagnostics.Errors
object CreateInvokeFunctionActionFactory : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val callExpr = diagnostic.getPsiElement().getParent() as? JetCallExpression ?: return null
val expectedType = Errors.FUNCTION_EXPECTED.cast(diagnostic).getB()
if (expectedType.isError()) return null
val receiverType = TypeInfo(expectedType, Variance.IN_VARIANCE)
val anyType = KotlinBuiltIns.getInstance().getNullableAnyType()
val parameters = callExpr.getValueArguments().map {
ParameterInfo(
it.getArgumentExpression()?.let { TypeInfo(it, Variance.IN_VARIANCE) } ?: TypeInfo(anyType, Variance.IN_VARIANCE),
it.getArgumentName()?.getReferenceExpression()?.getReferencedName()
)
}
val returnType = TypeInfo(callExpr, Variance.OUT_VARIANCE)
return CreateFunctionFromUsageFix(callExpr, FunctionInfo("invoke", receiverType, returnType, parameters))
}
}
@@ -0,0 +1,10 @@
// "Create function 'invoke' from usage" "true"
class A<T>(val n: T)
fun test(): A<String> {
return 1(2, "2")
}
fun Int.invoke(i: Int, s: String): A<String> {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
@@ -0,0 +1,12 @@
// "Create function 'invoke' from usage" "true"
class A<T>(val n: T) {
fun invoke(arg: T, s: String): B<String> {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
class B<T>(val m: T)
fun test(): B<String> {
return A(1)(2, "2")
}
@@ -0,0 +1,12 @@
// "Create function 'invoke' from usage" "true"
class A<T>(val n: T) {
fun <V> invoke(u: T, s: String): B<V> {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
class B<T>(val m: T)
fun test<U, V>(u: U): B<V> {
return A(u)(u, "u")
}
@@ -0,0 +1,11 @@
// "Create function 'invoke' from usage" "true"
class A<T>(val n: T) {
fun invoke(abc: T, ghi: A<T>, def: String): A<T> {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
fun test() {
val a: A<Int> = A(1)(abc = 1, ghi = A(2), def = "s")
}
@@ -0,0 +1,7 @@
// "Create function 'invoke' from usage" "true"
class A<T>(val n: T)
fun test(): A<String> {
return <caret>1(2, "2")
}
@@ -0,0 +1,8 @@
// "Create function 'invoke' from usage" "true"
class A<T>(val n: T)
class B<T>(val m: T)
fun test(): B<String> {
return A(1)<caret>(2, "2")
}
@@ -0,0 +1,8 @@
// "Create function 'invoke' from usage" "true"
class A<T>(val n: T)
class B<T>(val m: T)
fun test<U, V>(u: U): B<V> {
return A(u)<caret>(u, "u")
}
@@ -0,0 +1,7 @@
// "Create function 'invoke' from usage" "true"
class A<T>(val n: T)
fun test() {
val a: A<Int> = A(1)<caret>(abc = 1, ghi = A(2), def = "s")
}
@@ -618,7 +618,7 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
@TestMetadata("idea/testData/quickfix/createFromUsage")
@TestDataPath("$PROJECT_ROOT")
@InnerTestClasses({CreateFromUsage.BinaryOperations.class, CreateFromUsage.Component.class, CreateFromUsage.Get.class, CreateFromUsage.HasNext.class, CreateFromUsage.Iterator.class, CreateFromUsage.Next.class, CreateFromUsage.Set.class, CreateFromUsage.UnaryOperations.class})
@InnerTestClasses({CreateFromUsage.BinaryOperations.class, CreateFromUsage.Component.class, CreateFromUsage.Get.class, CreateFromUsage.HasNext.class, CreateFromUsage.Invoke.class, CreateFromUsage.Iterator.class, CreateFromUsage.Next.class, CreateFromUsage.Set.class, CreateFromUsage.UnaryOperations.class})
@RunWith(org.jetbrains.jet.JUnit3RunnerWithInners.class)
public static class CreateFromUsage extends AbstractQuickFixTest {
public void testAllFilesPresentInCreateFromUsage() throws Exception {
@@ -851,6 +851,40 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
@TestMetadata("idea/testData/quickfix/createFromUsage/invoke")
@TestDataPath("$PROJECT_ROOT")
@RunWith(org.jetbrains.jet.JUnit3RunnerWithInners.class)
public static class Invoke extends AbstractQuickFixTest {
public void testAllFilesPresentInInvoke() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/createFromUsage/invoke"), Pattern.compile("^before(\\w+)\\.kt$"), true);
}
@TestMetadata("beforeInvokeOnLibType.kt")
public void testInvokeOnLibType() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/invoke/beforeInvokeOnLibType.kt");
doTest(fileName);
}
@TestMetadata("beforeInvokeOnUserType.kt")
public void testInvokeOnUserType() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/invoke/beforeInvokeOnUserType.kt");
doTest(fileName);
}
@TestMetadata("beforeInvokeOnUserTypeWithTypeParams.kt")
public void testInvokeOnUserTypeWithTypeParams() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/invoke/beforeInvokeOnUserTypeWithTypeParams.kt");
doTest(fileName);
}
@TestMetadata("beforeInvokeWithExplicitParamNamesOnUserType.kt")
public void testInvokeWithExplicitParamNamesOnUserType() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/invoke/beforeInvokeWithExplicitParamNamesOnUserType.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/quickfix/createFromUsage/iterator")
@TestDataPath("$PROJECT_ROOT")
@RunWith(org.jetbrains.jet.JUnit3RunnerWithInners.class)