diff --git a/plugins/kotlin/completion/impl-k2/src/org/jetbrains/kotlin/idea/completion/impl/k2/K2CompletionRunner.kt b/plugins/kotlin/completion/impl-k2/src/org/jetbrains/kotlin/idea/completion/impl/k2/K2CompletionRunner.kt index 2d9d729bfbe0..91ce1bde3b67 100644 --- a/plugins/kotlin/completion/impl-k2/src/org/jetbrains/kotlin/idea/completion/impl/k2/K2CompletionRunner.kt +++ b/plugins/kotlin/completion/impl-k2/src/org/jetbrains/kotlin/idea/completion/impl/k2/K2CompletionRunner.kt @@ -16,6 +16,7 @@ import org.jetbrains.kotlin.analysis.api.components.KaCompletionExtensionCandida import org.jetbrains.kotlin.analysis.api.components.expectedType import org.jetbrains.kotlin.analysis.api.components.expressionType import org.jetbrains.kotlin.analysis.api.components.render +import org.jetbrains.kotlin.analysis.api.components.returnType import org.jetbrains.kotlin.analysis.api.impl.base.components.KaBaseIllegalPsiException import org.jetbrains.kotlin.analysis.api.types.KaType import org.jetbrains.kotlin.analysis.api.types.symbol @@ -163,6 +164,7 @@ private fun createWeighingContext( return when (positionContext) { is KotlinNameReferencePositionContext -> { val nameExpression = positionContext.nameExpression + val nameExpressionParent = nameExpression.parent val expectedType = when { // during the sorting of completion suggestions expected type from position and actual types of suggestions are compared; // see `org.jetbrains.kotlin.idea.completion.weighers.ExpectedTypeWeigher`; @@ -171,8 +173,16 @@ private fun createWeighingContext( // TODO: calculate actual types for callable references correctly and use information about expected type positionContext is KotlinCallableReferencePositionContext -> null nameExpression.expectedType != null -> nameExpression.expectedType - nameExpression.parent is KtBinaryExpression -> getEqualityExpectedType(nameExpression) - nameExpression.parent is KtCollectionLiteralExpression -> getAnnotationLiteralExpectedType(nameExpression) + nameExpressionParent is KtBinaryExpression -> getEqualityExpectedType(nameExpression) + nameExpressionParent is KtCollectionLiteralExpression -> getAnnotationLiteralExpectedType(nameExpression) + // TODO: This can be removed after KT-82534 has been fixed + nameExpressionParent is KtPropertyAccessor -> { + if (nameExpressionParent.isGetter) { + nameExpressionParent.property.returnType + } else { + null + } + } else -> null } if (parameters.completionType == CompletionType.SMART diff --git a/plugins/kotlin/completion/testData/weighers/basic/expectedType/propertyAccessor.kt b/plugins/kotlin/completion/testData/weighers/basic/expectedType/propertyAccessor.kt new file mode 100644 index 000000000000..40a106d31ba9 --- /dev/null +++ b/plugins/kotlin/completion/testData/weighers/basic/expectedType/propertyAccessor.kt @@ -0,0 +1,12 @@ +class Foo + +val somePrefixA: Int = 5 +val somePrefixB: Foo = Foo() +val somePrefixC: Int = 5 + +val testing: Foo + get() { + return somePrefix + } + +// ORDER: somePrefixB, somePrefixA, somePrefixC \ No newline at end of file diff --git a/plugins/kotlin/completion/tests-k1/test/org/jetbrains/kotlin/idea/completion/test/weighers/BasicCompletionWeigherTestGenerated.java b/plugins/kotlin/completion/tests-k1/test/org/jetbrains/kotlin/idea/completion/test/weighers/BasicCompletionWeigherTestGenerated.java index 314737e93836..c1b774f7f99e 100644 --- a/plugins/kotlin/completion/tests-k1/test/org/jetbrains/kotlin/idea/completion/test/weighers/BasicCompletionWeigherTestGenerated.java +++ b/plugins/kotlin/completion/tests-k1/test/org/jetbrains/kotlin/idea/completion/test/weighers/BasicCompletionWeigherTestGenerated.java @@ -356,6 +356,11 @@ public abstract class BasicCompletionWeigherTestGenerated extends AbstractBasicC runTest("../testData/weighers/basic/expectedType/MatchingNullableType.kt"); } + @TestMetadata("propertyAccessor.kt") + public void testPropertyAccessor() throws Exception { + runTest("../testData/weighers/basic/expectedType/propertyAccessor.kt"); + } + @TestMetadata("returnFromFunction.kt") public void testReturnFromFunction() throws Exception { runTest("../testData/weighers/basic/expectedType/returnFromFunction.kt"); diff --git a/plugins/kotlin/fir/tests/test/org/jetbrains/kotlin/idea/fir/completion/wheigher/HighLevelWeigherTestGenerated.java b/plugins/kotlin/fir/tests/test/org/jetbrains/kotlin/idea/fir/completion/wheigher/HighLevelWeigherTestGenerated.java index 94b623570645..4597d049e3c1 100644 --- a/plugins/kotlin/fir/tests/test/org/jetbrains/kotlin/idea/fir/completion/wheigher/HighLevelWeigherTestGenerated.java +++ b/plugins/kotlin/fir/tests/test/org/jetbrains/kotlin/idea/fir/completion/wheigher/HighLevelWeigherTestGenerated.java @@ -356,6 +356,11 @@ public abstract class HighLevelWeigherTestGenerated extends AbstractHighLevelWei runTest("../../completion/testData/weighers/basic/expectedType/MatchingNullableType.kt"); } + @TestMetadata("propertyAccessor.kt") + public void testPropertyAccessor() throws Exception { + runTest("../../completion/testData/weighers/basic/expectedType/propertyAccessor.kt"); + } + @TestMetadata("returnFromFunction.kt") public void testReturnFromFunction() throws Exception { runTest("../../completion/testData/weighers/basic/expectedType/returnFromFunction.kt");