diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index 0b5176a6b8a..5c533243888 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -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 diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java index ff2d408d836..13c25c2a86e 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java @@ -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 } }; } + + @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); + } + }; + } } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeReturnTypeToMatchOverriddenMethodFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeReturnTypeToMatchOverriddenMethodFix.java deleted file mode 100644 index 812e7cefa12..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeReturnTypeToMatchOverriddenMethodFix.java +++ /dev/null @@ -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 { - 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); - } - }; - } -} diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index 3bd2db28197..582fb53edf2 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -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()); diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/afterReturnTypeMismatchOnMultipleOverride.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/afterReturnTypeMismatchOnMultipleOverride.kt index fc23fe09d3c..8ffbf3dc546 100644 --- a/idea/testData/quickfix/override/typeMismatchOnOverride/afterReturnTypeMismatchOnMultipleOverride.kt +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/afterReturnTypeMismatchOnMultipleOverride.kt @@ -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 } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/afterReturnTypeMismatchOnOverrideIntLong.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/afterReturnTypeMismatchOnOverrideIntLong.kt index 4a1a153b6b1..e310ec83c86 100644 --- a/idea/testData/quickfix/override/typeMismatchOnOverride/afterReturnTypeMismatchOnOverrideIntLong.kt +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/afterReturnTypeMismatchOnOverrideIntLong.kt @@ -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 } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/afterReturnTypeMismatchOnOverrideIntUnit.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/afterReturnTypeMismatchOnOverrideIntUnit.kt index 8a1af1e423a..1993fa45e50 100644 --- a/idea/testData/quickfix/override/typeMismatchOnOverride/afterReturnTypeMismatchOnOverrideIntUnit.kt +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/afterReturnTypeMismatchOnOverrideIntUnit.kt @@ -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 } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/afterReturnTypeMismatchOnOverrideUnitInt.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/afterReturnTypeMismatchOnOverrideUnitInt.kt index 7cd9bbe922e..9a7ac2bfd0e 100644 --- a/idea/testData/quickfix/override/typeMismatchOnOverride/afterReturnTypeMismatchOnOverrideUnitInt.kt +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/afterReturnTypeMismatchOnOverrideUnitInt.kt @@ -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 { public abstract override fun remove(); } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnMultipleOverride.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnMultipleOverride.kt index 5f932fcf981..e4661ed3213 100644 --- a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnMultipleOverride.kt +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnMultipleOverride.kt @@ -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; + override abstract fun foo(): Int } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnMultipleOverrideAmbiguity.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnMultipleOverrideAmbiguity.kt index a11b75f863e..0065c209685 100644 --- a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnMultipleOverrideAmbiguity.kt +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnMultipleOverrideAmbiguity.kt @@ -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: Return type is 'jet.String', which is not a subtype of overridden
internal abstract fun foo() : jet.Int defined in A abstract class A { abstract fun foo() : Int; @@ -11,5 +11,5 @@ trait X { } abstract class B : A(), X { - abstract override fun foo() : String; + abstract override fun foo() : String } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnOverrideIntLong.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnOverrideIntLong.kt index 7b3ca9bcdf7..84e867c0e20 100644 --- a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnOverrideIntLong.kt +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnOverrideIntLong.kt @@ -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; + abstract override fun foo(): Long } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnOverrideIntUnit.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnOverrideIntUnit.kt index f0aa7b8f576..7c2dabae0ab 100644 --- a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnOverrideIntUnit.kt +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnOverrideIntUnit.kt @@ -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(); + abstract override fun foo() } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnOverrideUnitInt.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnOverrideUnitInt.kt index 1201c693311..fa00806769e 100644 --- a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnOverrideUnitInt.kt +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeReturnTypeMismatchOnOverrideUnitInt.kt @@ -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 { public abstract override fun remove() : Int; }