[kotlin] KTIJ-36482 Fixed completion not having an expected type for property accessor without body

^KTIJ-36482 fixed

GitOrigin-RevId: 9f4af6ae6025bf27202775eab253a181199c1a85
This commit is contained in:
Frederik Haselmeier
2025-11-19 18:32:24 +00:00
committed by intellij-monorepo-bot
parent 75b04f3698
commit 0ef2dad8a3
4 changed files with 34 additions and 2 deletions
@@ -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
@@ -0,0 +1,12 @@
class Foo
val somePrefixA: Int = 5
val somePrefixB: Foo = Foo()
val somePrefixC: Int = 5
val testing: Foo
get() {
return somePrefix<caret>
}
// ORDER: somePrefixB, somePrefixA, somePrefixC
@@ -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");
@@ -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");