ChangeFunctionReturnTypeFix for TYPE_MISMATCH error

This commit is contained in:
Wojciech Lopata
2013-04-24 14:32:44 +02:00
parent 893cbd6b48
commit 9b11db7a74
7 changed files with 43 additions and 0 deletions
@@ -185,4 +185,24 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction<JetNamedFunc
}
};
}
@NotNull
public static JetIntentionActionFactory createFactoryForTypeMismatch() {
return new JetIntentionActionFactory() {
@Nullable
@Override
public IntentionAction createAction(Diagnostic diagnostic) {
JetExpression expression = (JetExpression) diagnostic.getPsiElement();
JetNamedFunction function = QuickFixUtil.getParentElementOfType(diagnostic, JetNamedFunction.class);
if (function != null && (function.getInitializer() == expression || expression.getParent() instanceof JetReturnExpression)) {
BindingContext context = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) diagnostic.getPsiFile()).getBindingContext();
JetType type = context.get(BindingContext.EXPRESSION_TYPE, expression);
assert type != null : "Expression type mismatch, but expression has no type";
return new ChangeFunctionReturnTypeFix(function, type);
}
return null;
}
};
}
}
@@ -222,6 +222,7 @@ public class QuickFixes {
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(TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForTypeMismatch());
factories.put(EXPECTED_PARAMETER_TYPE_MISMATCH, ChangeTypeFix.createFactoryForExpectedParameterTypeMismatch());
factories.put(EXPECTED_RETURN_TYPE_MISMATCH, ChangeTypeFix.createFactoryForExpectedReturnTypeMismatch());
@@ -0,0 +1,2 @@
// "Change 'foo' function return type to 'String'" "true"
fun foo(): String = <caret>""
@@ -0,0 +1,4 @@
// "Change 'foo' function return type to 'String'" "true"
fun foo(): String {
return ""<caret>
}
@@ -0,0 +1,2 @@
// "Change 'foo' function return type to 'String'" "true"
fun foo(): Int = <caret>""
@@ -0,0 +1,4 @@
// "Change 'foo' function return type to 'String'" "true"
fun foo() {
return ""<caret>
}
@@ -1063,6 +1063,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest("idea/testData/quickfix/typeMismatch/beforeReturnTypeMismatch.kt");
}
@TestMetadata("beforeTypeMismatchInInitializer.kt")
public void testTypeMismatchInInitializer() throws Exception {
doTest("idea/testData/quickfix/typeMismatch/beforeTypeMismatchInInitializer.kt");
}
@TestMetadata("beforeTypeMismatchInReturnStatement.kt")
public void testTypeMismatchInReturnStatement() throws Exception {
doTest("idea/testData/quickfix/typeMismatch/beforeTypeMismatchInReturnStatement.kt");
}
}
@TestMetadata("idea/testData/quickfix/typeProjection")