From c3977d7336d1f7592302679d600b0e1b56877a14 Mon Sep 17 00:00:00 2001 From: Nicolay Mitropolsky Date: Wed, 1 Apr 2020 12:54:32 +0300 Subject: [PATCH] Uast: replacing most usages of `resolveCallToDeclaration` with `resolveToPsiMethod` --- .../uast/kotlin/expressions/KotlinUBinaryExpression.kt | 2 +- .../kotlin/expressions/KotlinUFunctionCallExpression.kt | 5 ++--- .../kotlin/expressions/KotlinUObjectLiteralExpression.kt | 4 ++-- .../uast/kotlin/expressions/KotlinUPostfixExpression.kt | 2 +- .../uast/kotlin/expressions/KotlinUPrefixExpression.kt | 2 +- .../expressions/KotlinUSimpleReferenceExpression.kt | 5 +---- .../uast/kotlin/internal/kotlinInternalUastUtils.kt | 9 ++++++++- 7 files changed, 16 insertions(+), 13 deletions(-) diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUBinaryExpression.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUBinaryExpression.kt index 2cceacb2fb5..09e5c9ffbcd 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUBinaryExpression.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUBinaryExpression.kt @@ -48,7 +48,7 @@ class KotlinUBinaryExpression( override val operatorIdentifier: UIdentifier? get() = KotlinUIdentifier(sourcePsi.operationReference.getReferencedNameElement(), this) - override fun resolveOperator() = sourcePsi.operationReference.resolveCallToDeclaration() as? PsiMethod + override fun resolveOperator(): PsiMethod? = resolveToPsiMethod(sourcePsi) override val operator = when (sourcePsi.operationToken) { KtTokens.EQ -> UastBinaryOperator.ASSIGN diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUFunctionCallExpression.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUFunctionCallExpression.kt index 4b328da46f9..eedc2ffbeb9 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUFunctionCallExpression.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUFunctionCallExpression.kt @@ -179,7 +179,7 @@ class KotlinUFunctionCallExpression( return variants.flatMap { when (val source = it.toSource()) { is KtClass -> source.toLightClass()?.constructors?.asSequence().orEmpty() - else -> resolveSource(sourcePsi, it, source)?.let { sequenceOf(it) }.orEmpty() + else -> resolveToPsiMethod(sourcePsi, it, source)?.let { sequenceOf(it) }.orEmpty() } }.map { TypedResolveResult(it) }.asIterable() }) @@ -189,8 +189,7 @@ class KotlinUFunctionCallExpression( override fun resolve(): PsiMethod? { val descriptor = resolvedCall?.resultingDescriptor ?: return null - val source = descriptor.toSource() - return resolveSource(sourcePsi, descriptor, source) + return resolveToPsiMethod(sourcePsi, descriptor) } override fun accept(visitor: UastVisitor) { diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUObjectLiteralExpression.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUObjectLiteralExpression.kt index f63b869d661..ea18439e99f 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUObjectLiteralExpression.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUObjectLiteralExpression.kt @@ -62,7 +62,7 @@ class KotlinUObjectLiteralExpression( psi.typeArguments.map { it.typeReference.toPsiType(this, boxed = true) } } - override fun resolve() = superClassConstructorCall?.resolveCallToDeclaration() as? PsiMethod + override fun resolve() = superClassConstructorCall?.let { resolveToPsiMethod(it) } override fun getArgumentForParameter(i: Int): UExpression? = superClassConstructorCall?.let { it.getResolvedCall(it.analyze()) }?.let { getArgumentExpressionByIndex(i, it, this) } @@ -75,7 +75,7 @@ class KotlinUObjectLiteralExpression( override val javaPsi: PsiElement? get() = null override val psi: KtSuperTypeCallEntry get() = sourcePsi - override fun resolve() = (sourcePsi.resolveCallToDeclaration() as? PsiMethod)?.containingClass + override fun resolve() = resolveToPsiMethod(sourcePsi)?.containingClass override val annotations: List get() = emptyList() diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUPostfixExpression.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUPostfixExpression.kt index 1583473d703..1a56f317d6e 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUPostfixExpression.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUPostfixExpression.kt @@ -40,7 +40,7 @@ class KotlinUPostfixExpression( override val operatorIdentifier: UIdentifier? get() = KotlinUIdentifier(sourcePsi.operationReference, this) - override fun resolveOperator() = sourcePsi.operationReference.resolveCallToDeclaration() as? PsiMethod + override fun resolveOperator(): PsiMethod? = resolveToPsiMethod(sourcePsi) override fun resolve(): PsiMethod? = when (sourcePsi.operationToken) { KtTokens.EXCLEXCL -> operand.tryResolve() as? PsiMethod diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUPrefixExpression.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUPrefixExpression.kt index 4b22f71870a..059cf7f9a5d 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUPrefixExpression.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUPrefixExpression.kt @@ -34,7 +34,7 @@ class KotlinUPrefixExpression( override val operatorIdentifier: UIdentifier? get() = KotlinUIdentifier(sourcePsi.operationReference, this) - override fun resolveOperator() = sourcePsi.operationReference.resolveCallToDeclaration() as? PsiMethod + override fun resolveOperator(): PsiMethod? = resolveToPsiMethod(sourcePsi) override val operator = when (sourcePsi.operationToken) { KtTokens.EXCL -> UastPrefixOperator.LOGICAL_NOT diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUSimpleReferenceExpression.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUSimpleReferenceExpression.kt index 96375430285..fd43faad630 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUSimpleReferenceExpression.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUSimpleReferenceExpression.kt @@ -187,10 +187,7 @@ open class KotlinUSimpleReferenceExpression( override val kind: UastCallKind get() = UastCallKind.METHOD_CALL - override fun resolve(): PsiMethod? { - val source = accessorDescriptor.toSource() - return resolveSource(sourcePsi, accessorDescriptor, source) - } + override fun resolve(): PsiMethod? = resolveToPsiMethod(sourcePsi, accessorDescriptor) } private fun KtExpression.readWriteAccess(): ReferenceAccess { diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/internal/kotlinInternalUastUtils.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/internal/kotlinInternalUastUtils.kt index 07fc53c2f81..e6f7e6e6df0 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/internal/kotlinInternalUastUtils.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/internal/kotlinInternalUastUtils.kt @@ -96,7 +96,14 @@ internal fun DeclarationDescriptor.toSource(): PsiElement? { } } -internal fun resolveSource(context: KtElement, descriptor: DeclarationDescriptor, source: PsiElement?): PsiMethod? { +internal fun resolveToPsiMethod(context: KtElement): PsiMethod? = + context.getResolvedCall(context.analyze())?.resultingDescriptor?.let { resolveToPsiMethod(context, it) } + +internal fun resolveToPsiMethod( + context: KtElement, + descriptor: DeclarationDescriptor, + source: PsiElement? = descriptor.toSource() +): PsiMethod? { if (descriptor is ConstructorDescriptor && descriptor.isPrimary && source is KtClassOrObject && source.primaryConstructor == null