From 1e6f507f5e98541167fa28141b1c4c8885f77b1e Mon Sep 17 00:00:00 2001 From: Kirill Rakhman Date: Fri, 25 Mar 2016 02:54:32 +0100 Subject: [PATCH] Implement quick-fix "let type implement interface" #KT-11404 Fixed --- .../org/jetbrains/kotlin/types/TypeUtils.kt | 4 + .../idea/quickfix/LetImplementInterfaceFix.kt | 76 +++++++++++++++++++ .../QuickFixFactoryForTypeMismatchError.kt | 9 +++ .../letClassImplementAdditionalInterface.kt | 14 ++++ ...ClassImplementAdditionalInterface.kt.after | 14 ++++ .../letClassImplementGenericInterface.kt | 13 ++++ ...letClassImplementGenericInterface.kt.after | 13 ++++ .../letClassImplementGenericInterfaceTwice.kt | 16 ++++ .../letClassImplementGenericStarInterface.kt | 17 +++++ .../letClassImplementInterface.kt | 13 ++++ .../letClassImplementInterface.kt.after | 13 ++++ .../letInterfaceExtendInterface.kt | 14 ++++ .../letInterfaceExtendInterface.kt.after | 14 ++++ .../letStringImplementInterface.kt | 17 +++++ .../notApplicableToConstructor.kt | 1 + .../idea/quickfix/QuickFixTestGenerated.java | 42 ++++++++++ 16 files changed, 290 insertions(+) create mode 100644 idea/src/org/jetbrains/kotlin/idea/quickfix/LetImplementInterfaceFix.kt create mode 100644 idea/testData/quickfix/typeMismatch/letClassImplementAdditionalInterface.kt create mode 100644 idea/testData/quickfix/typeMismatch/letClassImplementAdditionalInterface.kt.after create mode 100644 idea/testData/quickfix/typeMismatch/letClassImplementGenericInterface.kt create mode 100644 idea/testData/quickfix/typeMismatch/letClassImplementGenericInterface.kt.after create mode 100644 idea/testData/quickfix/typeMismatch/letClassImplementGenericInterfaceTwice.kt create mode 100644 idea/testData/quickfix/typeMismatch/letClassImplementGenericStarInterface.kt create mode 100644 idea/testData/quickfix/typeMismatch/letClassImplementInterface.kt create mode 100644 idea/testData/quickfix/typeMismatch/letClassImplementInterface.kt.after create mode 100644 idea/testData/quickfix/typeMismatch/letInterfaceExtendInterface.kt create mode 100644 idea/testData/quickfix/typeMismatch/letInterfaceExtendInterface.kt.after create mode 100644 idea/testData/quickfix/typeMismatch/letStringImplementInterface.kt diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt index e70e9b55eef..2786b021fde 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt @@ -17,6 +17,8 @@ package org.jetbrains.kotlin.types.typeUtil import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.resolve.DescriptorUtils @@ -60,6 +62,8 @@ fun KotlinType.isBooleanOrNullableBoolean(): Boolean = KotlinBuiltIns.isBooleanO fun KotlinType.isTypeParameter(): Boolean = TypeUtils.isTypeParameter(this) +fun KotlinType.isInterface(): Boolean = (constructor.declarationDescriptor as? ClassDescriptor)?.kind == ClassKind.INTERFACE + fun KotlinType?.isArrayOfNothing(): Boolean { if (this == null || !KotlinBuiltIns.isArray(this)) return false diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/LetImplementInterfaceFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/LetImplementInterfaceFix.kt new file mode 100644 index 00000000000..c73a7fe35e0 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/LetImplementInterfaceFix.kt @@ -0,0 +1,76 @@ +/* + * Copyright 2010-2016 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.kotlin.idea.quickfix + +import com.intellij.codeInsight.intention.LowPriorityAction +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import com.intellij.psi.PsiFile +import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.containsStarProjections +import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers +import org.jetbrains.kotlin.idea.util.ShortenReferences +import org.jetbrains.kotlin.psi.KtClassOrObject +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.KtPsiFactory +import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.kotlin.types.typeUtil.isInterface + +class LetImplementInterfaceFix( + element: KtClassOrObject, + expectedType: KotlinType, + expressionType: KotlinType +) : KotlinQuickFixAction(element), LowPriorityAction { + + private fun KotlinType.renderShort() = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(this) + + private val expectedTypeName = expectedType.renderShort() + + private val expectedTypeNameSourceCode = IdeDescriptorRenderers.SOURCE_CODE.renderType(expectedType) + + private val prefix: String + + private val validExpectedType: Boolean + + init { + val verb = if (expressionType.isInterface()) "extend" else "implement" + prefix = "Let '${expressionType.renderShort()}' $verb" + + validExpectedType = with (expectedType) { + isInterface() && + !containsStarProjections() && + constructor !in expressionType.constructor.supertypes.map(KotlinType::getConstructor) + } + } + + override fun getFamilyName() = "Let type implement interface" + override fun getText(): String { + return "$prefix interface '$expectedTypeName'" + } + + override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean { + if (!super.isAvailable(project, editor, file)) return false + if (!validExpectedType) return false + + return true + } + + override fun invoke(project: Project, editor: Editor?, file: KtFile) { + val superTypeEntry = KtPsiFactory(element).createSuperTypeEntry(expectedTypeNameSourceCode) + val entryElement = element.addSuperTypeListEntry(superTypeEntry) + ShortenReferences.DEFAULT.process(entryElement) + } +} \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.kt index 5229d1fee0c..b87089142d5 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.kt @@ -29,12 +29,14 @@ import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil import org.jetbrains.kotlin.idea.util.approximateWithResolvableType import org.jetbrains.kotlin.idea.util.getResolutionScope import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.resolve.bindingContextUtil.getTargetFunction import org.jetbrains.kotlin.resolve.calls.callUtil.getParentResolvedCall import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.callUtil.getValueArgumentForExpression import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.kotlin.types.typeUtil.isInterface import org.jetbrains.kotlin.types.typeUtil.isPrimitiveNumberType import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf import org.jetbrains.kotlin.types.typeUtil.makeNullable @@ -84,6 +86,13 @@ class QuickFixFactoryForTypeMismatchError : KotlinIntentionActionsFactory() { actions.add(NumberConversionFix(diagnosticElement, expectedType)) } + if (expectedType.isInterface()) { + val expressionTypeDeclaration = expressionType.constructor.declarationDescriptor?.let { + DescriptorToSourceUtils.descriptorToDeclaration(it) + } as? KtClassOrObject + expressionTypeDeclaration?.let { actions.add(LetImplementInterfaceFix(it, expectedType, expressionType)) } + } + // We don't want to cast a cast or type-asserted expression: if (diagnosticElement !is KtBinaryExpressionWithTypeRHS && diagnosticElement.parent !is KtBinaryExpressionWithTypeRHS) { actions.add(CastExpressionFix(diagnosticElement, expectedType)) diff --git a/idea/testData/quickfix/typeMismatch/letClassImplementAdditionalInterface.kt b/idea/testData/quickfix/typeMismatch/letClassImplementAdditionalInterface.kt new file mode 100644 index 00000000000..1b1a918297d --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/letClassImplementAdditionalInterface.kt @@ -0,0 +1,14 @@ +// "Let 'B' implement interface 'A'" "true" +package let.implement + +fun bar() { + foo(B()) +} + + +fun foo(a: A) { +} + +interface A +interface C +class B : C \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/letClassImplementAdditionalInterface.kt.after b/idea/testData/quickfix/typeMismatch/letClassImplementAdditionalInterface.kt.after new file mode 100644 index 00000000000..0cf6d8bd76d --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/letClassImplementAdditionalInterface.kt.after @@ -0,0 +1,14 @@ +// "Let 'B' implement interface 'A'" "true" +package let.implement + +fun bar() { + foo(B()) +} + + +fun foo(a: A) { +} + +interface A +interface C +class B : C, A \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/letClassImplementGenericInterface.kt b/idea/testData/quickfix/typeMismatch/letClassImplementGenericInterface.kt new file mode 100644 index 00000000000..36d82fabd4a --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/letClassImplementGenericInterface.kt @@ -0,0 +1,13 @@ +// "Let 'B' implement interface 'A'" "true" +package let.implement + +fun bar() { + foo(B()) +} + + +fun foo(a: A) { +} + +interface A +class B \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/letClassImplementGenericInterface.kt.after b/idea/testData/quickfix/typeMismatch/letClassImplementGenericInterface.kt.after new file mode 100644 index 00000000000..b76342eee3e --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/letClassImplementGenericInterface.kt.after @@ -0,0 +1,13 @@ +// "Let 'B' implement interface 'A'" "true" +package let.implement + +fun bar() { + foo(B()) +} + + +fun foo(a: A) { +} + +interface A +class B : A \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/letClassImplementGenericInterfaceTwice.kt b/idea/testData/quickfix/typeMismatch/letClassImplementGenericInterfaceTwice.kt new file mode 100644 index 00000000000..4fd2d7510ae --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/letClassImplementGenericInterfaceTwice.kt @@ -0,0 +1,16 @@ +// "Let 'B' implement interface 'A'" "false" +// ACTION: Change parameter 'a' type of function 'let.implement.foo' to 'B' +// ACTION: Convert to expression body +// ERROR: Type mismatch: inferred type is B but A was expected +package let.implement + +fun bar() { + foo(B()) +} + + +fun foo(a: A) { +} + +interface A +class B : A \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/letClassImplementGenericStarInterface.kt b/idea/testData/quickfix/typeMismatch/letClassImplementGenericStarInterface.kt new file mode 100644 index 00000000000..df1dcd0300f --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/letClassImplementGenericStarInterface.kt @@ -0,0 +1,17 @@ +// "Let 'B' implement interface 'A<*>'" "false" +// ACTION: Change parameter 'a' type of function 'let.implement.foo' to 'B' +// ACTION: Convert to expression body +// ERROR: Type mismatch: inferred type is B but A<*> was expected + +package let.implement + +fun bar() { + foo(B()) +} + + +fun foo(a: A<*>) { +} + +interface A +class B \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/letClassImplementInterface.kt b/idea/testData/quickfix/typeMismatch/letClassImplementInterface.kt new file mode 100644 index 00000000000..c7e21200ed4 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/letClassImplementInterface.kt @@ -0,0 +1,13 @@ +// "Let 'B' implement interface 'A'" "true" +package let.implement + +fun bar() { + foo(B()) +} + + +fun foo(a: A) { +} + +interface A +class B \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/letClassImplementInterface.kt.after b/idea/testData/quickfix/typeMismatch/letClassImplementInterface.kt.after new file mode 100644 index 00000000000..94a683ed56d --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/letClassImplementInterface.kt.after @@ -0,0 +1,13 @@ +// "Let 'B' implement interface 'A'" "true" +package let.implement + +fun bar() { + foo(B()) +} + + +fun foo(a: A) { +} + +interface A +class B : A \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/letInterfaceExtendInterface.kt b/idea/testData/quickfix/typeMismatch/letInterfaceExtendInterface.kt new file mode 100644 index 00000000000..cdbac7f16fc --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/letInterfaceExtendInterface.kt @@ -0,0 +1,14 @@ +// "Let 'C' extend interface 'A'" "true" +package let.extend + +fun bar() { + foo(B() as C) +} + + +fun foo(a: A) { +} + +interface A +interface C +class B : C \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/letInterfaceExtendInterface.kt.after b/idea/testData/quickfix/typeMismatch/letInterfaceExtendInterface.kt.after new file mode 100644 index 00000000000..d4aa291f2c3 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/letInterfaceExtendInterface.kt.after @@ -0,0 +1,14 @@ +// "Let 'C' extend interface 'A'" "true" +package let.extend + +fun bar() { + foo(B() as C) +} + + +fun foo(a: A) { +} + +interface A +interface C : A +class B : C \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/letStringImplementInterface.kt b/idea/testData/quickfix/typeMismatch/letStringImplementInterface.kt new file mode 100644 index 00000000000..5fa0d8beb54 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/letStringImplementInterface.kt @@ -0,0 +1,17 @@ +// "Let 'String' implement interface 'A'" "false" +// ACTION: Change parameter 'a' type of function 'let.implement.foo' to 'String' +// ACTION: Convert to expression body +// ACTION: To raw string literal +// ERROR: Type mismatch: inferred type is String but A was expected + +package let.implement + +fun bar() { + foo("Hello") +} + + +fun foo(a: A) { +} + +interface A \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/notApplicableToConstructor.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/notApplicableToConstructor.kt index 942e939dc57..f93209b86db 100644 --- a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/notApplicableToConstructor.kt +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/notApplicableToConstructor.kt @@ -1,6 +1,7 @@ // "Change 'A' function return type to 'B'" "false" // ACTION: Change 'b' type to 'A' // ACTION: Convert property initializer to getter +// ACTION: Let 'A' implement interface 'B' // ERROR: Type mismatch: inferred type is A but B was expected class A constructor() {} diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 4b0dee8f0b4..f872ee8c9fe 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -7330,6 +7330,48 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest(fileName); } + @TestMetadata("letClassImplementAdditionalInterface.kt") + public void testLetClassImplementAdditionalInterface() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/letClassImplementAdditionalInterface.kt"); + doTest(fileName); + } + + @TestMetadata("letClassImplementGenericInterface.kt") + public void testLetClassImplementGenericInterface() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/letClassImplementGenericInterface.kt"); + doTest(fileName); + } + + @TestMetadata("letClassImplementGenericInterfaceTwice.kt") + public void testLetClassImplementGenericInterfaceTwice() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/letClassImplementGenericInterfaceTwice.kt"); + doTest(fileName); + } + + @TestMetadata("letClassImplementGenericStarInterface.kt") + public void testLetClassImplementGenericStarInterface() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/letClassImplementGenericStarInterface.kt"); + doTest(fileName); + } + + @TestMetadata("letClassImplementInterface.kt") + public void testLetClassImplementInterface() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/letClassImplementInterface.kt"); + doTest(fileName); + } + + @TestMetadata("letInterfaceExtendInterface.kt") + public void testLetInterfaceExtendInterface() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/letInterfaceExtendInterface.kt"); + doTest(fileName); + } + + @TestMetadata("letStringImplementInterface.kt") + public void testLetStringImplementInterface() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/letStringImplementInterface.kt"); + doTest(fileName); + } + @TestMetadata("localClassInReturn1.kt") public void testLocalClassInReturn1() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/localClassInReturn1.kt");