Map RETURN_TYPE_MISMATCH_ON_OVERRIDE to ChangeFunctionReturnTypeFix

This commit is contained in:
Wojciech Lopata
2013-03-16 16:31:07 +01:00
committed by Andrey Breslav
parent c281796e64
commit cdd41f0fa1
13 changed files with 35 additions and 125 deletions
@@ -57,8 +57,6 @@ add.semicolon.after.invocation=Add semicolon after invocation of ''{0}''
add.semicolon.family=Add Semicolon
change.function.return.type=Change ''{0}'' function return type to ''{1}''
remove.function.return.type=Remove explicitly specified return type in ''{0}'' function
change.return.type.to.match.overridden.method=Change return type to ''{0}''
remove.return.type.to.match.overridden.method=Remove explicitly specified return type to match overridden method
change.property.type.to.match.overridden.property=Change property type to ''{0}''
change.type.family=Change Type
@@ -40,6 +40,7 @@ import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.plugin.JetBundle;
import org.jetbrains.jet.plugin.caches.resolve.KotlinCacheManagerUtil;
import org.jetbrains.jet.plugin.intentions.SpecifyTypeExplicitlyAction;
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
@@ -150,4 +151,19 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction<JetFunction>
}
};
}
@NotNull
public static JetIntentionActionFactory createFactoryForReturnTypeMismatchOnOverride() {
return new JetIntentionActionFactory() {
@Nullable
@Override
public IntentionAction createAction(Diagnostic diagnostic) {
JetFunction function = QuickFixUtil.getParentElementOfType(diagnostic, JetFunction.class);
assert function != null : "RETURN_TYPE_MISMATCH_ON_OVERRIDE reported on element that is not within any function";
BindingContext context = KotlinCacheManagerUtil.getDeclarationsBindingContext(function);
JetType matchingReturnType = QuickFixUtil.findLowerBoundOfOverriddenCallablesReturnTypes(context, function);
return matchingReturnType == null ? null : new ChangeFunctionReturnTypeFix(function, matchingReturnType);
}
};
}
}
@@ -1,104 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.quickfix;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
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.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetFunction;
import org.jetbrains.jet.lang.psi.JetPsiFactory;
import org.jetbrains.jet.lang.psi.JetTypeReference;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.plugin.JetBundle;
import org.jetbrains.jet.plugin.caches.resolve.KotlinCacheManager;
import org.jetbrains.jet.plugin.caches.resolve.KotlinCacheManagerUtil;
import org.jetbrains.jet.plugin.intentions.SpecifyTypeExplicitlyAction;
import org.jetbrains.jet.plugin.project.TargetPlatform;
public class ChangeReturnTypeToMatchOverriddenMethodFix extends JetIntentionAction<JetFunction> {
private JetType matchingReturnType;
public ChangeReturnTypeToMatchOverriddenMethodFix(@NotNull JetFunction function) {
super(function);
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!super.isAvailable(project, editor, file)) {
return false;
}
BindingContext context = KotlinCacheManagerUtil.getDeclarationsBindingContext((JetFile) file);
matchingReturnType = QuickFixUtil.findLowerBoundOfOverriddenCallablesReturnTypes(context, element);
return matchingReturnType != null;
}
@NotNull
@Override
public String getText() {
if (KotlinBuiltIns.getInstance().isUnit(matchingReturnType)) {
return JetBundle.message("remove.return.type.to.match.overridden.method");
}
else {
return JetBundle.message("change.return.type.to.match.overridden.method", matchingReturnType);
}
}
@NotNull
@Override
public String getFamilyName() {
return JetBundle.message("change.type.family");
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
if (KotlinBuiltIns.getInstance().isUnit(matchingReturnType)) {
SpecifyTypeExplicitlyAction.removeTypeAnnotation(element);
}
else {
JetTypeReference returnTypeReference = element.getReturnTypeRef();
if (returnTypeReference == null) {
SpecifyTypeExplicitlyAction.addTypeAnnotation(project, editor, element, matchingReturnType);
}
else {
PsiElement newReturnType = JetPsiFactory.createType(project, matchingReturnType.toString());
returnTypeReference.replace(newReturnType);
}
}
}
@NotNull
public static JetIntentionActionFactory createFactory() {
return new JetIntentionActionFactory() {
@Nullable
@Override
public IntentionAction createAction(Diagnostic diagnostic) {
JetFunction function = QuickFixUtil.getParentElementOfType(diagnostic, JetFunction.class);
return function == null ? null : new ChangeReturnTypeToMatchOverriddenMethodFix(function);
}
};
}
}
@@ -210,8 +210,8 @@ public class QuickFixes {
factories.put(DANGLING_FUNCTION_LITERAL_ARGUMENT_SUSPECTED, AddSemicolonAfterFunctionCallFix.createFactory());
factories.put(RETURN_TYPE_MISMATCH_ON_OVERRIDE, ChangeReturnTypeToMatchOverriddenMethodFix.createFactory());
factories.put(PROPERTY_TYPE_MISMATCH_ON_OVERRIDE, ChangePropertyTypeToMatchOverriddenPropertyFix.createFactory());
factories.put(RETURN_TYPE_MISMATCH_ON_OVERRIDE, ChangeFunctionReturnTypeFix.createFactoryForReturnTypeMismatchOnOverride());
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());
@@ -1,4 +1,4 @@
// "Change return type to 'T'" "true"
// "Change 'B.foo' function return type to 'T'" "true"
open class S {}
open class T : S() {}
@@ -11,5 +11,5 @@ trait X {
}
abstract class B : A(), X {
override abstract fun foo() : T;
override abstract fun foo(): T
}
@@ -1,8 +1,8 @@
// "Change return type to 'Int'" "true"
// "Change 'B.foo' function return type to 'Int'" "true"
abstract class A {
abstract fun foo() : Int;
}
abstract class B : A() {
abstract override fun foo() : Int;
abstract override fun foo(): Int
}
@@ -1,8 +1,8 @@
// "Change return type to 'Int'" "true"
// "Change 'B.foo' function return type to 'Int'" "true"
abstract class A {
abstract fun foo() : Int;
}
abstract class B : A() {
abstract override fun foo(): Int;
abstract override fun foo<caret>(): Int
}
@@ -1,4 +1,4 @@
// "Remove explicitly specified return type to match overridden method" "true"
// "Remove explicitly specified return type in 'A.remove' function" "true"
abstract class A : java.util.Iterator<Int> {
public abstract override fun remove();
}
@@ -1,4 +1,4 @@
// "Change return type to 'T'" "true"
// "Change 'B.foo' function return type to 'T'" "true"
open class S {}
open class T : S() {}
@@ -11,5 +11,5 @@ trait X {
}
abstract class B : A(), X {
override abstract fun foo() : Int<caret>;
override abstract fun foo(): Int<caret>
}
@@ -1,6 +1,6 @@
// "Change return type to 'Int'" "false"
// "Change return type to 'Long'" "false"
// "Remove explicitly specified return type to match overridden method" "false"
// "Change 'B.foo' function return type to 'Int'" "false"
// "Change 'B.foo' function return type to 'Long'" "false"
// "Remove explicitly specified return type in 'B.foo' function" "false"
// ERROR: <html>Return type is 'jet.String', which is not a subtype of overridden<br/><b>internal</b> <b>abstract</b> <b>fun</b> foo() : jet.Int <i>defined in</i> A</html>
abstract class A {
abstract fun foo() : Int;
@@ -11,5 +11,5 @@ trait X {
}
abstract class B : A(), X {
abstract override fun foo() : String<caret>;
abstract override fun foo() : String<caret>
}
@@ -1,8 +1,8 @@
// "Change return type to 'Int'" "true"
// "Change 'B.foo' function return type to 'Int'" "true"
abstract class A {
abstract fun foo() : Int;
}
abstract class B : A() {
abstract override fun foo() : Long<caret>;
abstract override fun foo(): Long<caret>
}
@@ -1,8 +1,8 @@
// "Change return type to 'Int'" "true"
// "Change 'B.foo' function return type to 'Int'" "true"
abstract class A {
abstract fun foo() : Int;
}
abstract class B : A() {
abstract override fun foo<caret>();
abstract override fun foo<caret>()
}
@@ -1,4 +1,4 @@
// "Remove explicitly specified return type to match overridden method" "true"
// "Remove explicitly specified return type in 'A.remove' function" "true"
abstract class A : java.util.Iterator<Int> {
public abstract override fun remove() : Int<caret>;
}