From 4dde458e84bc82bb03d4e150b3ddf00abfad3111 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Thu, 31 Mar 2016 20:47:06 +0300 Subject: [PATCH] Smart completion after "by" works for generic properties too --- .../testData/smart/propertyDelegate/GenericVal.kt | 10 ++++++++++ .../testData/smart/propertyDelegate/GenericVar.kt | 10 ++++++++++ .../test/JvmSmartCompletionTestGenerated.java | 12 ++++++++++++ .../org/jetbrains/kotlin/idea/core/ExpectedInfos.kt | 11 ++++------- .../kotlin/idea/core/TypesWithOperatorDetector.kt | 6 +++--- 5 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 idea/idea-completion/testData/smart/propertyDelegate/GenericVal.kt create mode 100644 idea/idea-completion/testData/smart/propertyDelegate/GenericVar.kt diff --git a/idea/idea-completion/testData/smart/propertyDelegate/GenericVal.kt b/idea/idea-completion/testData/smart/propertyDelegate/GenericVal.kt new file mode 100644 index 00000000000..a95a5958ebe --- /dev/null +++ b/idea/idea-completion/testData/smart/propertyDelegate/GenericVal.kt @@ -0,0 +1,10 @@ +import kotlin.reflect.KProperty + +class X { + operator fun getValue(thisRef: List, property: KProperty<*>): String = "" +} + +val List.property: T by + +// EXIST: lazy +// EXIST: X \ No newline at end of file diff --git a/idea/idea-completion/testData/smart/propertyDelegate/GenericVar.kt b/idea/idea-completion/testData/smart/propertyDelegate/GenericVar.kt new file mode 100644 index 00000000000..590f240fdc9 --- /dev/null +++ b/idea/idea-completion/testData/smart/propertyDelegate/GenericVar.kt @@ -0,0 +1,10 @@ +import kotlin.reflect.KProperty + +class X { + operator fun getValue(thisRef: List, property: KProperty<*>): String = "" + operator fun setValue(thisRef: List, property: KProperty<*>, value: String){} +} + +var List.property: T by + +// EXIST: X \ No newline at end of file diff --git a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/JvmSmartCompletionTestGenerated.java b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/JvmSmartCompletionTestGenerated.java index 158c8a7c031..ea8dff0ff80 100644 --- a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/JvmSmartCompletionTestGenerated.java +++ b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/JvmSmartCompletionTestGenerated.java @@ -1510,6 +1510,18 @@ public class JvmSmartCompletionTestGenerated extends AbstractJvmSmartCompletionT doTest(fileName); } + @TestMetadata("GenericVal.kt") + public void testGenericVal() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/smart/propertyDelegate/GenericVal.kt"); + doTest(fileName); + } + + @TestMetadata("GenericVar.kt") + public void testGenericVar() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/smart/propertyDelegate/GenericVar.kt"); + doTest(fileName); + } + @TestMetadata("Order.kt") public void testOrder() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/smart/propertyDelegate/Order.kt"); diff --git a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ExpectedInfos.kt b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ExpectedInfos.kt index 12bfb7474e6..c9c9e7f8352 100644 --- a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ExpectedInfos.kt +++ b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ExpectedInfos.kt @@ -612,11 +612,11 @@ class ExpectedInfos( val property = resolutionFacade.resolveToDescriptor(propertyDeclaration) as? PropertyDescriptor ?: return null val scope = expressionWithType.getResolutionScope(bindingContext, resolutionFacade) - val propertyOwnerType = property.extensionReceiverParameter?.type - ?: property.dispatchReceiverParameter?.type + val propertyOwnerType = property.fuzzyExtensionReceiverType() + ?: property.dispatchReceiverParameter?.type?.let { FuzzyType(it, emptyList()) } ?: return null - val explicitPropertyType = property.returnType?.check { propertyDeclaration.typeReference != null } + val explicitPropertyType = property.fuzzyReturnType()?.check { propertyDeclaration.typeReference != null } val typesWithGetDetector = TypesWithGetValueDetector(scope, indicesHelper, propertyOwnerType, explicitPropertyType) val typesWithSetDetector = if (property.isVar) TypesWithSetValueDetector(scope, indicesHelper, propertyOwnerType) else null @@ -629,10 +629,7 @@ class ExpectedInfos( val substitutedType = FuzzyType(getOperatorSubstitutor.substitute(descriptorType.type, Variance.INVARIANT)!!, descriptorType.freeParameters) val (setValueOperator, setOperatorSubstitutor) = typesWithSetDetector.findOperator(substitutedType) ?: return null - val propertyType = if (explicitPropertyType != null) - FuzzyType(explicitPropertyType, emptyList()) - else - getValueOperator.fuzzyReturnType()!! + val propertyType = explicitPropertyType ?: getValueOperator.fuzzyReturnType()!! val setParamType = FuzzyType(setValueOperator.valueParameters.last().type, setValueOperator.typeParameters) val setParamTypeSubstitutor = setParamType.checkIsSuperTypeOf(propertyType) ?: return null return TypeSubstitutor.createChainedSubstitutor(getOperatorSubstitutor.substitution, diff --git a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/TypesWithOperatorDetector.kt b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/TypesWithOperatorDetector.kt index 12d445d0747..669ddb696c5 100644 --- a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/TypesWithOperatorDetector.kt +++ b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/TypesWithOperatorDetector.kt @@ -119,8 +119,8 @@ class TypesWithContainsDetector( class TypesWithGetValueDetector( scope: LexicalScope, indicesHelper: KotlinIndicesHelper?, - private val propertyOwnerType: KotlinType, - private val propertyType: KotlinType? + private val propertyOwnerType: FuzzyType, + private val propertyType: FuzzyType? ) : TypesWithOperatorDetector(OperatorNameConventions.GET_VALUE, scope, indicesHelper) { override fun checkIsSuitableByType(operator: FunctionDescriptor, freeTypeParams: Collection): TypeSubstitutor? { @@ -138,7 +138,7 @@ class TypesWithGetValueDetector( class TypesWithSetValueDetector( scope: LexicalScope, indicesHelper: KotlinIndicesHelper?, - private val propertyOwnerType: KotlinType + private val propertyOwnerType: FuzzyType ) : TypesWithOperatorDetector(OperatorNameConventions.SET_VALUE, scope, indicesHelper) { override fun checkIsSuitableByType(operator: FunctionDescriptor, freeTypeParams: Collection): TypeSubstitutor? {