UAST: unify implementation/creation of type reference expression

This commit is contained in:
Jinseong Jeon
2021-06-01 08:26:08 -07:00
committed by teamcityserver
parent fa36098214
commit 517fd24f14
8 changed files with 17 additions and 25 deletions
@@ -95,7 +95,7 @@ internal object KotlinConverter : BaseKotlinConverter {
is KtWhenCondition -> convertWhenCondition(element, givenParent, expectedTypes) is KtWhenCondition -> convertWhenCondition(element, givenParent, expectedTypes)
is KtTypeReference -> is KtTypeReference ->
expectedTypes.accommodate( expectedTypes.accommodate(
alternative { LazyKotlinUTypeReferenceExpression(element, givenParent) }, alternative { KotlinUTypeReferenceExpression(element, givenParent) },
alternative { convertReceiverParameter(element) } alternative { convertReceiverParameter(element) }
).firstOrNull() ).firstOrNull()
is KtConstructorDelegationCall -> is KtConstructorDelegationCall ->
@@ -289,7 +289,7 @@ internal object KotlinConverter : BaseKotlinConverter {
} }
val typeRef = condition.typeReference val typeRef = condition.typeReference
typeReference = typeRef?.let { typeReference = typeRef?.let {
LazyKotlinUTypeReferenceExpression(it, this) { typeRef.toPsiType(this, boxed = true) } KotlinUTypeReferenceExpression(it, this) { typeRef.toPsiType(this, boxed = true) }
} }
} }
} }
@@ -49,7 +49,7 @@ abstract class AbstractKotlinUClass(givenParent: UElement?) : KotlinAbstractUEle
override val uastSuperTypes: List<UTypeReferenceExpression> override val uastSuperTypes: List<UTypeReferenceExpression>
get() = ktClass?.superTypeListEntries.orEmpty().mapNotNull { it.typeReference }.map { get() = ktClass?.superTypeListEntries.orEmpty().mapNotNull { it.typeReference }.map {
LazyKotlinUTypeReferenceExpression(it, this) KotlinUTypeReferenceExpression(it, this)
} }
val delegateExpressions: List<UExpression> val delegateExpressions: List<UExpression>
@@ -80,7 +80,7 @@ class KotlinSupertypeDelegationUExpression(override val sourcePsi: KtDelegatedSu
override val psi: PsiElement? get() = sourcePsi override val psi: PsiElement? get() = sourcePsi
val typeReference: UTypeReferenceExpression? by lazy { val typeReference: UTypeReferenceExpression? by lazy {
sourcePsi.typeReference?.let { KotlinUTypeReferenceExpression(it.toPsiType(this), it, this) } sourcePsi.typeReference?.let { KotlinUTypeReferenceExpression(it, this) { it.toPsiType(this) } }
} }
val delegateExpression: UExpression? by lazy { val delegateExpression: UExpression? by lazy {
@@ -119,7 +119,7 @@ open class KotlinUMethod(
override val returnTypeReference: UTypeReferenceExpression? by lz { override val returnTypeReference: UTypeReferenceExpression? by lz {
(sourcePsi as? KtCallableDeclaration)?.typeReference?.let { (sourcePsi as? KtCallableDeclaration)?.typeReference?.let {
LazyKotlinUTypeReferenceExpression(it, this) { javaPsi.returnType ?: UastErrorType } KotlinUTypeReferenceExpression(it, this) { javaPsi.returnType ?: UastErrorType }
} }
} }
@@ -95,7 +95,7 @@ abstract class AbstractKotlinUVariable(givenParent: UElement?) : KotlinAbstractU
protected abstract fun acceptsAnnotationTarget(target: AnnotationUseSiteTarget?): Boolean protected abstract fun acceptsAnnotationTarget(target: AnnotationUseSiteTarget?): Boolean
override val typeReference: UTypeReferenceExpression? by lz { override val typeReference: UTypeReferenceExpression? by lz {
KotlinUTypeReferenceExpression(type, (sourcePsi as? KtCallableDeclaration)?.typeReference, this) KotlinUTypeReferenceExpression((sourcePsi as? KtCallableDeclaration)?.typeReference, this) { type }
} }
override val uastAnchor: UIdentifier? override val uastAnchor: UIdentifier?
@@ -32,7 +32,7 @@ class KotlinUBinaryExpressionWithType(
override val type by lz { sourcePsi.right.toPsiType(this) } override val type by lz { sourcePsi.right.toPsiType(this) }
override val typeReference by lz { override val typeReference by lz {
sourcePsi.right?.let { LazyKotlinUTypeReferenceExpression(it, this) } sourcePsi.right?.let { KotlinUTypeReferenceExpression(it, this) }
} }
override val operationKind = when (sourcePsi.operationReference.getReferencedNameElementType()) { override val operationKind = when (sourcePsi.operationReference.getReferencedNameElementType()) {
@@ -56,4 +56,4 @@ class KotlinCustomUBinaryExpressionWithType(
override var typeReference: UTypeReferenceExpression? = null override var typeReference: UTypeReferenceExpression? = null
internal set internal set
} }
@@ -45,7 +45,7 @@ class KotlinUCatchClause(
override val typeReferences by lz { override val typeReferences by lz {
val parameter = sourcePsi.catchParameter ?: return@lz emptyList<UTypeReferenceExpression>() val parameter = sourcePsi.catchParameter ?: return@lz emptyList<UTypeReferenceExpression>()
val typeReference = parameter.typeReference ?: return@lz emptyList<UTypeReferenceExpression>() val typeReference = parameter.typeReference ?: return@lz emptyList<UTypeReferenceExpression>()
listOf(LazyKotlinUTypeReferenceExpression(typeReference, this) { typeReference.toPsiType(this, boxed = true) }) listOf(KotlinUTypeReferenceExpression(typeReference, this) { typeReference.toPsiType(this, boxed = true) })
} }
// equal to IDEA 202 implementation // equal to IDEA 202 implementation
@@ -59,4 +59,4 @@ class KotlinUCatchClause(
// equal to IDEA 202 implementation // equal to IDEA 202 implementation
override fun asRenderString(): String = "catch (${parameters.joinToString { it.asRenderString() }}) ${body.asRenderString()}" override fun asRenderString(): String = "catch (${parameters.joinToString { it.asRenderString() }}) ${body.asRenderString()}"
} }
@@ -30,7 +30,7 @@ class KotlinUTypeCheckExpression(
override val type by lz { sourcePsi.typeReference.toPsiType(this) } override val type by lz { sourcePsi.typeReference.toPsiType(this) }
override val typeReference = sourcePsi.typeReference?.let { override val typeReference = sourcePsi.typeReference?.let {
LazyKotlinUTypeReferenceExpression(it, this) { it.toPsiType(this) } KotlinUTypeReferenceExpression(it, this) { it.toPsiType(this) }
} }
override val operationKind = override val operationKind =
@@ -38,4 +38,4 @@ class KotlinUTypeCheckExpression(
KotlinBinaryExpressionWithTypeKinds.NEGATED_INSTANCE_CHECK KotlinBinaryExpressionWithTypeKinds.NEGATED_INSTANCE_CHECK
else else
UastBinaryExpressionWithTypeKind.INSTANCE_CHECK UastBinaryExpressionWithTypeKind.INSTANCE_CHECK
} }
@@ -1,23 +1,15 @@
package org.jetbrains.uast.kotlin package org.jetbrains.uast.kotlin
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiType import com.intellij.psi.PsiType
import org.jetbrains.kotlin.psi.KtTypeReference import org.jetbrains.kotlin.psi.KtTypeReference
import org.jetbrains.uast.UElement import org.jetbrains.uast.UElement
import org.jetbrains.uast.UTypeReferenceExpression import org.jetbrains.uast.UTypeReferenceExpression
open class KotlinUTypeReferenceExpression( class KotlinUTypeReferenceExpression(
override val type: PsiType, override val sourcePsi: KtTypeReference?,
override val psi: PsiElement?, givenParent: UElement?,
givenParent: UElement? private val typeSupplier: (() -> PsiType)? = null
) : KotlinAbstractUExpression(givenParent), UTypeReferenceExpression, KotlinUElementWithType ) : KotlinAbstractUExpression(givenParent), UTypeReferenceExpression, KotlinUElementWithType {
class LazyKotlinUTypeReferenceExpression(
override val sourcePsi: KtTypeReference,
givenParent: UElement?,
private val typeSupplier: (() -> PsiType)? = null
) : KotlinAbstractUExpression(givenParent), UTypeReferenceExpression {
override val type: PsiType by lz { override val type: PsiType by lz {
typeSupplier?.invoke() ?: sourcePsi.toPsiType(uastParent ?: this) typeSupplier?.invoke() ?: sourcePsi.toPsiType(uastParent ?: this)
} }