From 0ef2dad8a3d989076002b48b83eb7868a1d4fbb8 Mon Sep 17 00:00:00 2001 From: Frederik Haselmeier Date: Wed, 19 Nov 2025 12:50:02 +0100 Subject: [PATCH] [kotlin] KTIJ-36482 Fixed completion not having an expected type for property accessor without body ^KTIJ-36482 fixed GitOrigin-RevId: 9f4af6ae6025bf27202775eab253a181199c1a85 --- .../idea/completion/impl/k2/K2CompletionRunner.kt | 14 ++++++++++++-- .../basic/expectedType/propertyAccessor.kt | 12 ++++++++++++ .../BasicCompletionWeigherTestGenerated.java | 5 +++++ .../wheigher/HighLevelWeigherTestGenerated.java | 5 +++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 plugins/kotlin/completion/testData/weighers/basic/expectedType/propertyAccessor.kt 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");