Quickfix for COMPARE_TO_TYPE_MISMATCH

This commit is contained in:
Wojciech Lopata
2013-03-16 14:08:33 +01:00
committed by Andrey Breslav
parent 13022922af
commit c281796e64
5 changed files with 39 additions and 0 deletions
@@ -26,6 +26,7 @@ import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.SimpleFunctionDescriptor;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
@@ -131,4 +132,22 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction<JetFunction>
}
};
}
@NotNull
public static JetIntentionActionFactory createFactoryForCompareToTypeMismatch() {
return new JetIntentionActionFactory() {
@Nullable
@Override
public IntentionAction createAction(Diagnostic diagnostic) {
JetBinaryExpression expression = QuickFixUtil.getParentElementOfType(diagnostic, JetBinaryExpression.class);
assert expression != null : "COMPARE_TO_TYPE_MISMATCH reported on element that is not within any expression";
BindingContext context = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) expression.getContainingFile()).getBindingContext();
ResolvedCall<? extends CallableDescriptor> resolvedCall = context.get(BindingContext.RESOLVED_CALL, expression.getOperationReference());
if (resolvedCall == null) return null;
PsiElement compareTo = BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getCandidateDescriptor());
if (!(compareTo instanceof JetFunction)) return null;
return new ChangeFunctionReturnTypeFix((JetFunction) compareTo, KotlinBuiltIns.getInstance().getIntType());
}
};
}
}
@@ -214,6 +214,7 @@ public class QuickFixes {
factories.put(PROPERTY_TYPE_MISMATCH_ON_OVERRIDE, ChangePropertyTypeToMatchOverriddenPropertyFix.createFactory());
factories.put(COMPONENT_FUNCTION_RETURN_TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForComponentFunctionReturnTypeMismatch());
factories.put(HAS_NEXT_FUNCTION_TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForHasNextFunctionTypeMismatch());
factories.put(COMPARE_TO_TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForCompareToTypeMismatch());
factories.put(PLATFORM_CLASS_MAPPED_TO_KOTLIN, MapPlatformClassToKotlinFix.createFactory());
}
@@ -0,0 +1,7 @@
// "Change 'A.compareTo' function return type to 'Int'" "true"
trait A {
fun compareTo(other: Any): Int
}
fun foo(x: A) {
if (x <<caret> 0) {}
}
@@ -0,0 +1,7 @@
// "Change 'A.compareTo' function return type to 'Int'" "true"
trait A {
fun compareTo(other: Any): String
}
fun foo(x: A) {
if (x <<caret> 0) {}
}
@@ -977,6 +977,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
public void testChangeReturnTypeWhenValueParameterListIsAbsent() throws Exception {
doTest("idea/testData/quickfix/typeMismatch/beforeChangeReturnTypeWhenValueParameterListIsAbsent.kt");
}
@TestMetadata("beforeCompareToTypeMismatch.kt")
public void testCompareToTypeMismatch() throws Exception {
doTest("idea/testData/quickfix/typeMismatch/beforeCompareToTypeMismatch.kt");
}
@TestMetadata("beforeComponentFunctionReturnTypeMismatch1.kt")
public void testComponentFunctionReturnTypeMismatch1() throws Exception {