From f16f9a13fdc713b1ed1378f1929bdd6cfc4ca345 Mon Sep 17 00:00:00 2001 From: Jinseong Jeon Date: Tue, 18 May 2021 16:09:48 -0700 Subject: [PATCH] FIR/UAST: enforce the existence of BaseKotlinUastResolveProviderService --- .../jetbrains/uast/kotlin/KotlinAbstractUElement.kt | 11 +++++++---- .../uast/kotlin/KotlinAbstractUExpression.kt | 5 +++-- .../kotlin/declarations/KotlinUImportStatement.kt | 2 +- .../kotlin/internal/baseKotlinInternalUastUtils.kt | 3 +++ .../jetbrains/uast/kotlin/KotlinUastLanguagePlugin.kt | 3 ++- .../expressions/KotlinUCallableReferenceExpression.kt | 2 +- .../expressions/KotlinUDeclarationsExpression.kt | 10 ++++++---- .../expressions/KotlinUFunctionCallExpression.kt | 2 +- .../KotlinUQualifiedReferenceExpression.kt | 2 +- .../expressions/KotlinUSafeQualifiedExpression.kt | 2 +- .../expressions/KotlinUSimpleReferenceExpression.kt | 2 +- 11 files changed, 27 insertions(+), 17 deletions(-) diff --git a/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/KotlinAbstractUElement.kt b/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/KotlinAbstractUElement.kt index f4ef4159e88..6c7bd1004f2 100644 --- a/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/KotlinAbstractUElement.kt +++ b/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/KotlinAbstractUElement.kt @@ -12,15 +12,18 @@ import org.jetbrains.uast.UastLanguagePlugin import org.jetbrains.uast.kotlin.internal.KotlinUElementWithComments abstract class KotlinAbstractUElement( - givenParent: UElement? + givenParent: UElement?, + baseResolveProviderServiceSupplier: BaseResolveProviderServiceSupplier? = null, ) : KotlinUElementWithComments { protected val languagePlugin: UastLanguagePlugin? by lz { psi?.let { UastFacade.findPlugin(it) } } - protected val baseResolveProviderService: BaseKotlinUastResolveProviderService? by lz { - psi?.project?.let { ServiceManager.getService(it, BaseKotlinUastResolveProviderService::class.java) } + protected val baseResolveProviderService: BaseKotlinUastResolveProviderService by lz { + baseResolveProviderServiceSupplier?.get() + ?: psi?.project?.let { ServiceManager.getService(it, BaseKotlinUastResolveProviderService::class.java) } + ?: error("${BaseKotlinUastResolveProviderService::class.java.name} is not available") } final override val uastParent: UElement? by lz { @@ -28,7 +31,7 @@ abstract class KotlinAbstractUElement( } protected open fun convertParent(): UElement? { - return baseResolveProviderService?.convertParent(this) + return baseResolveProviderService.convertParent(this) } override fun equals(other: Any?): Boolean { diff --git a/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/KotlinAbstractUExpression.kt b/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/KotlinAbstractUExpression.kt index e9f6f958843..70654754d08 100644 --- a/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/KotlinAbstractUExpression.kt +++ b/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/KotlinAbstractUExpression.kt @@ -13,8 +13,9 @@ import org.jetbrains.uast.UExpression import org.jetbrains.uast.convertOpt abstract class KotlinAbstractUExpression( - givenParent: UElement? -) : KotlinAbstractUElement(givenParent), UExpression { + givenParent: UElement?, + baseResolveProviderServiceSupplier: BaseResolveProviderServiceSupplier? = null, +) : KotlinAbstractUElement(givenParent, baseResolveProviderServiceSupplier), UExpression { override val javaPsi: PsiElement? = null diff --git a/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/declarations/KotlinUImportStatement.kt b/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/declarations/KotlinUImportStatement.kt index 183927c2664..20cb8d8925f 100644 --- a/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/declarations/KotlinUImportStatement.kt +++ b/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/declarations/KotlinUImportStatement.kt @@ -50,7 +50,7 @@ class KotlinUImportStatement( override fun resolve(): PsiElement? { val reference = sourcePsi.getQualifiedElementSelector() as? KtReferenceExpression ?: return null - return baseResolveProviderService?.resolveToDeclaration(reference) + return baseResolveProviderService.resolveToDeclaration(reference) } } } diff --git a/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/internal/baseKotlinInternalUastUtils.kt b/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/internal/baseKotlinInternalUastUtils.kt index 314a7bd41b8..10bbb5bdef7 100644 --- a/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/internal/baseKotlinInternalUastUtils.kt +++ b/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/internal/baseKotlinInternalUastUtils.kt @@ -13,6 +13,9 @@ import org.jetbrains.kotlin.asJava.elements.KtLightMember import org.jetbrains.kotlin.psi.KtDeclaration import org.jetbrains.uast.UDeclaration import org.jetbrains.uast.UElement +import java.util.function.Supplier + +typealias BaseResolveProviderServiceSupplier = Supplier fun lz(initializer: () -> T) = lazy(LazyThreadSafetyMode.SYNCHRONIZED, initializer) diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinUastLanguagePlugin.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinUastLanguagePlugin.kt index 601f8ff8fb8..10999bcfc3d 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinUastLanguagePlugin.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinUastLanguagePlugin.kt @@ -211,7 +211,8 @@ internal object KotlinConverter { return with (expectedTypes) { when (element) { is KtParameterList -> el { - val declarationsExpression = KotlinUDeclarationsExpression(givenParent) + val resolveProviderService = ServiceManager.getService(element.project, KotlinUastResolveProviderService::class.java) + val declarationsExpression = KotlinUDeclarationsExpression(null, givenParent, null) { resolveProviderService } declarationsExpression.apply { declarations = element.parameters.mapIndexed { i, p -> KotlinUParameter(UastKotlinPsiParameter.create(p, element, declarationsExpression, i), p, this) diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUCallableReferenceExpression.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUCallableReferenceExpression.kt index 4c3227d20a4..9fe7deb097c 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUCallableReferenceExpression.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUCallableReferenceExpression.kt @@ -46,7 +46,7 @@ class KotlinUCallableReferenceExpression( override val resolvedName: String? get() = (resolve() as? PsiNamedElement)?.name - override fun resolve(): PsiElement? = baseResolveProviderService?.resolveToDeclaration(sourcePsi.callableReference) + override fun resolve(): PsiElement? = baseResolveProviderService.resolveToDeclaration(sourcePsi.callableReference) override fun multiResolve(): Iterable = getResolveResultVariants(sourcePsi.callableReference) diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUDeclarationsExpression.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUDeclarationsExpression.kt index 05081627cea..fac16a8158a 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUDeclarationsExpression.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUDeclarationsExpression.kt @@ -16,14 +16,16 @@ package org.jetbrains.uast import com.intellij.psi.PsiElement +import org.jetbrains.uast.kotlin.BaseResolveProviderServiceSupplier import org.jetbrains.uast.kotlin.KotlinAbstractUExpression import org.jetbrains.uast.kotlin.doConvertParent open class KotlinUDeclarationsExpression( - override val psi: PsiElement?, - givenParent: UElement?, - val psiAnchor: PsiElement? = null -) : KotlinAbstractUExpression(givenParent), UDeclarationsExpression { + override val psi: PsiElement?, + givenParent: UElement?, + val psiAnchor: PsiElement? = null, + baseResolveProviderServiceSupplier: BaseResolveProviderServiceSupplier? = null, +) : KotlinAbstractUExpression(givenParent, baseResolveProviderServiceSupplier), UDeclarationsExpression { override val sourcePsi: PsiElement? get() = psiAnchor 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 76b5cc6edd3..5f3649c5c57 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 @@ -145,7 +145,7 @@ class KotlinUFunctionCallExpression( val ktNameReferenceExpression = sourcePsi.calleeExpression as? KtNameReferenceExpression ?: return null val localCallableDeclaration = - baseResolveProviderService?.resolveToDeclaration(ktNameReferenceExpression) as? PsiVariable ?: return null + baseResolveProviderService.resolveToDeclaration(ktNameReferenceExpression) as? PsiVariable ?: return null if (localCallableDeclaration !is PsiLocalVariable && localCallableDeclaration !is PsiParameter) return null // an implicit receiver for variables calls (KT-25524) diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUQualifiedReferenceExpression.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUQualifiedReferenceExpression.kt index e6f199b5063..dc1ef51a262 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUQualifiedReferenceExpression.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUQualifiedReferenceExpression.kt @@ -34,7 +34,7 @@ class KotlinUQualifiedReferenceExpression( override val selector by lz { KotlinConverter.convertOrEmpty(sourcePsi.selectorExpression, this) } override val accessType = UastQualifiedExpressionAccessType.SIMPLE - override fun resolve(): PsiElement? = sourcePsi.selectorExpression?.let { baseResolveProviderService?.resolveToDeclaration(it) } + override fun resolve(): PsiElement? = sourcePsi.selectorExpression?.let { baseResolveProviderService.resolveToDeclaration(it) } override val resolvedName: String? get() = (resolve() as? PsiNamedElement)?.name diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUSafeQualifiedExpression.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUSafeQualifiedExpression.kt index 3226ef316e7..0361e799ed6 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUSafeQualifiedExpression.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUSafeQualifiedExpression.kt @@ -37,6 +37,6 @@ class KotlinUSafeQualifiedExpression( override val resolvedName: String? get() = (resolve() as? PsiNamedElement)?.name - override fun resolve(): PsiElement? = sourcePsi.selectorExpression?.let { baseResolveProviderService?.resolveToDeclaration(it) } + override fun resolve(): PsiElement? = sourcePsi.selectorExpression?.let { baseResolveProviderService.resolveToDeclaration(it) } override fun multiResolve(): Iterable = getResolveResultVariants(sourcePsi.selectorExpression) } 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 f9d04897ef8..2fa9d39292c 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 @@ -38,7 +38,7 @@ open class KotlinUSimpleReferenceExpression( override val sourcePsi: KtSimpleNameExpression, givenParent: UElement? ) : KotlinAbstractUExpression(givenParent), USimpleNameReferenceExpression, KotlinUElementWithType, KotlinEvaluatableUElement { - private val resolvedDeclaration: PsiElement? by lz { baseResolveProviderService?.resolveToDeclaration(sourcePsi) } + private val resolvedDeclaration: PsiElement? by lz { baseResolveProviderService.resolveToDeclaration(sourcePsi) } override val identifier get() = sourcePsi.getReferencedName()