Implement quick-fix "let type implement interface" #KT-11404 Fixed

This commit is contained in:
Kirill Rakhman
2016-03-25 02:54:32 +01:00
committed by Mikhail Glukhikh
parent 2198a8c8d0
commit 1e6f507f5e
16 changed files with 290 additions and 0 deletions
@@ -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
@@ -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<KtClassOrObject>(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)
}
}
@@ -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))
@@ -0,0 +1,14 @@
// "Let 'B' implement interface 'A'" "true"
package let.implement
fun bar() {
foo(B()<caret>)
}
fun foo(a: A) {
}
interface A
interface C
class B : C
@@ -0,0 +1,14 @@
// "Let 'B' implement interface 'A'" "true"
package let.implement
fun bar() {
foo(B()<caret>)
}
fun foo(a: A) {
}
interface A
interface C
class B : C, A
@@ -0,0 +1,13 @@
// "Let 'B' implement interface 'A<Int>'" "true"
package let.implement
fun bar() {
foo(B()<caret>)
}
fun foo(a: A<Int>) {
}
interface A<T>
class B
@@ -0,0 +1,13 @@
// "Let 'B' implement interface 'A<Int>'" "true"
package let.implement
fun bar() {
foo(B()<caret>)
}
fun foo(a: A<Int>) {
}
interface A<T>
class B : A<Int>
@@ -0,0 +1,16 @@
// "Let 'B' implement interface 'A<Int>'" "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<Int> was expected
package let.implement
fun bar() {
foo(B()<caret>)
}
fun foo(a: A<Int>) {
}
interface A<T>
class B : A<String>
@@ -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()<caret>)
}
fun foo(a: A<*>) {
}
interface A<T>
class B
@@ -0,0 +1,13 @@
// "Let 'B' implement interface 'A'" "true"
package let.implement
fun bar() {
foo(B()<caret>)
}
fun foo(a: A) {
}
interface A
class B
@@ -0,0 +1,13 @@
// "Let 'B' implement interface 'A'" "true"
package let.implement
fun bar() {
foo(B()<caret>)
}
fun foo(a: A) {
}
interface A
class B : A
@@ -0,0 +1,14 @@
// "Let 'C' extend interface 'A'" "true"
package let.extend
fun bar() {
foo(B() as C<caret>)
}
fun foo(a: A) {
}
interface A
interface C
class B : C
@@ -0,0 +1,14 @@
// "Let 'C' extend interface 'A'" "true"
package let.extend
fun bar() {
foo(B() as C<caret>)
}
fun foo(a: A) {
}
interface A
interface C : A
class B : C
@@ -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"<caret>)
}
fun foo(a: A) {
}
interface A
@@ -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() {}
@@ -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");