QuickFix: change overloaded operator parameter type or return type

This commit is contained in:
Wojciech Lopata
2013-05-10 00:42:40 +02:00
parent d3492d8e6f
commit 76e648ded3
8 changed files with 96 additions and 1 deletions
@@ -17,13 +17,17 @@
package org.jetbrains.jet.plugin.quickfix;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters2;
import org.jetbrains.jet.lang.diagnostics.Errors;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
@@ -62,6 +66,31 @@ public class QuickFixFactoryForTypeMismatchError implements JetIntentionActionsF
actions.add(new ChangeFunctionReturnTypeFix(function, expressionType));
}
// Fixing overloaded operators:
if (expression instanceof JetOperationExpression) {
ResolvedCall<? extends CallableDescriptor> resolvedCall =
context.get(BindingContext.RESOLVED_CALL, ((JetOperationExpression) expression).getOperationReference());
if (resolvedCall != null) {
PsiElement declaration = BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getResultingDescriptor());
if (declaration instanceof JetFunction) {
actions.add(new ChangeFunctionReturnTypeFix((JetFunction) declaration, expectedType));
}
}
}
if (expression.getParent() instanceof JetBinaryExpression) {
JetBinaryExpression parentBinary = (JetBinaryExpression) expression.getParent();
if (parentBinary.getRight() == expression) {
ResolvedCall<? extends CallableDescriptor> resolvedCall = context.get(BindingContext.RESOLVED_CALL, parentBinary.getOperationReference());
if (resolvedCall != null) {
PsiElement declaration = BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getResultingDescriptor());
if (declaration instanceof JetFunction) {
JetParameter binaryOperatorParameter = ((JetFunction) declaration).getValueParameterList().getParameters().get(0);
actions.add(new ChangeFunctionParameterTypeFix(binaryOperatorParameter, expressionType));
}
}
}
}
// Change type of a function parameter in case TYPE_MISMATCH is reported on expression passed as value argument of call.
// 1) When an argument is a dangling function literal:
JetFunctionLiteralExpression functionLiteralExpression =
@@ -0,0 +1,7 @@
// "Change 'A.not' function return type to 'A'" "true"
trait A {
fun not(): A
fun times(a: A): A
}
fun foo(a: A): A = a * <caret>!(if (true) a else a)
@@ -0,0 +1,8 @@
// "Change 'A.plus' function return type to '() -> Int'" "true"
trait A {
fun plus(a: A): () -> Int
}
fun foo(a: A): () -> Int {
return a + a<caret>
}
@@ -0,0 +1,6 @@
// "Change parameter 'a' type of function 'A.times' to 'String'" "true"
trait A {
fun times(a: String): A
}
fun foo(a: A): A = a * <caret>""
@@ -0,0 +1,7 @@
// "Change 'A.not' function return type to 'A'" "true"
trait A {
fun not(): String
fun times(a: A): A
}
fun foo(a: A): A = a * <caret>!(if (true) a else a)
@@ -0,0 +1,8 @@
// "Change 'A.plus' function return type to '() -> Int'" "true"
trait A {
fun plus(a: A): String
}
fun foo(a: A): () -> Int {
return a + a<caret>
}
@@ -0,0 +1,6 @@
// "Change parameter 'a' type of function 'A.times' to 'String'" "true"
trait A {
fun times(a: A): A
}
fun foo(a: A): A = a * <caret>""
@@ -1240,7 +1240,7 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
@TestMetadata("idea/testData/quickfix/typeMismatch")
@InnerTestClasses({TypeMismatch.Casts.class, TypeMismatch.ComponentFunctionReturnTypeMismatch.class, TypeMismatch.FunctionParameterTypeMismatch.class, TypeMismatch.TypeMismatchOnReturnedExpression.class})
@InnerTestClasses({TypeMismatch.Casts.class, TypeMismatch.ComponentFunctionReturnTypeMismatch.class, TypeMismatch.FixOverloadedOperator.class, TypeMismatch.FunctionParameterTypeMismatch.class, TypeMismatch.TypeMismatchOnReturnedExpression.class})
public static class TypeMismatch extends AbstractQuickFixTest {
public void testAllFilesPresentInTypeMismatch() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/typeMismatch"), Pattern.compile("^before(\\w+)\\.kt$"), true);
@@ -1382,6 +1382,29 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
@TestMetadata("idea/testData/quickfix/typeMismatch/fixOverloadedOperator")
public static class FixOverloadedOperator extends AbstractQuickFixTest {
public void testAllFilesPresentInFixOverloadedOperator() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/typeMismatch/fixOverloadedOperator"), Pattern.compile("^before(\\w+)\\.kt$"), true);
}
@TestMetadata("beforeChangeNotFunctionReturnType.kt")
public void testChangeNotFunctionReturnType() throws Exception {
doTest("idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangeNotFunctionReturnType.kt");
}
@TestMetadata("beforeChangePlusFunctionReturnType.kt")
public void testChangePlusFunctionReturnType() throws Exception {
doTest("idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangePlusFunctionReturnType.kt");
}
@TestMetadata("beforeChangeTimesFunctionParameterType.kt")
public void testChangeTimesFunctionParameterType() throws Exception {
doTest("idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangeTimesFunctionParameterType.kt");
}
}
@TestMetadata("idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch")
public static class FunctionParameterTypeMismatch extends AbstractQuickFixTest {
public void testAllFilesPresentInFunctionParameterTypeMismatch() throws Exception {
@@ -1488,6 +1511,7 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
suite.addTestSuite(TypeMismatch.class);
suite.addTestSuite(Casts.class);
suite.addTestSuite(ComponentFunctionReturnTypeMismatch.class);
suite.addTestSuite(FixOverloadedOperator.class);
suite.addTestSuite(FunctionParameterTypeMismatch.class);
suite.addTestSuite(TypeMismatchOnReturnedExpression.class);
return suite;