From b30f57813eda35e42a3056c218cee20ae216469b Mon Sep 17 00:00:00 2001 From: Jiaxiang Chen Date: Wed, 1 Nov 2023 10:49:11 -0700 Subject: [PATCH] fix classifier reference toString (cherry picked from commit bc16a1887581edff62448b54b56fdb26a4f9cba4) --- .../symbol/kotlin/KSClassifierReferenceImpl.kt | 15 +++++++++------ .../impl/symbol/kotlin/KSTypeReferenceImpl.kt | 2 +- kotlin-analysis-api/testData/parent.kt | 16 ++++++++-------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSClassifierReferenceImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSClassifierReferenceImpl.kt index 818fd60272..655cbb6755 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSClassifierReferenceImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSClassifierReferenceImpl.kt @@ -22,19 +22,18 @@ import com.google.devtools.ksp.symbol.KSClassifierReference import com.google.devtools.ksp.symbol.KSNode import com.google.devtools.ksp.symbol.KSTypeArgument import com.google.devtools.ksp.symbol.Location -import com.google.devtools.ksp.symbol.Origin import org.jetbrains.kotlin.psi.KtUserType class KSClassifierReferenceImpl private constructor( val ktUserType: KtUserType, - override val parent: KSNode? + override val parent: KSNode ) : KSClassifierReference { companion object : KSObjectCache, KSClassifierReferenceImpl>() { - fun getCached(ktUserType: KtUserType, parent: KSNode? = null) = + fun getCached(ktUserType: KtUserType, parent: KSNode) = cache.getOrPut(IdKeyPair(ktUserType, parent)) { KSClassifierReferenceImpl(ktUserType, parent) } } - override val origin = Origin.KOTLIN + override val origin = parent.origin override val location: Location by lazy { ktUserType.toLocation() @@ -53,9 +52,13 @@ class KSClassifierReferenceImpl private constructor( if (ktUserType.qualifier == null) { null } else { - KSClassifierReferenceImpl.getCached(ktUserType.qualifier!!, parent) + getCached(ktUserType.qualifier!!, parent) } } - override fun toString() = referencedName() + override fun toString(): String { + return ktUserType.referencedName + if (typeArguments.isNotEmpty()) "<${ + typeArguments.map { it.toString() }.joinToString(", ") + }>" else "" + } } diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSTypeReferenceImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSTypeReferenceImpl.kt index 57b5bb6255..f63287b159 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSTypeReferenceImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSTypeReferenceImpl.kt @@ -71,7 +71,7 @@ class KSTypeReferenceImpl( ktType.annotations.map { KSAnnotationImpl.getCached(it) }.asSequence() } - override val origin: Origin = Origin.KOTLIN + override val origin: Origin = parent?.origin ?: Origin.SYNTHETIC override val location: Location by lazy { ktTypeReference.toLocation() diff --git a/kotlin-analysis-api/testData/parent.kt b/kotlin-analysis-api/testData/parent.kt index 7a8c2d4f23..3e469057e9 100644 --- a/kotlin-analysis-api/testData/parent.kt +++ b/kotlin-analysis-api/testData/parent.kt @@ -72,16 +72,17 @@ // parent of File: a.kt: null // parent of Int: Int // parent of Int: INVARIANT Int -// parent of INVARIANT Int: List -// parent of List: List? -// parent of List?: topProp -// parent of List?: topProp.getter() +// parent of INVARIANT Int: List +// parent of List: List +// parent of List: topProp +// parent of List: List +// parent of List: topProp.getter() // parent of topProp.getter(): topProp // parent of Anno: null // parent of @Anno: topProp // parent of topProp: File: a.kt -// parent of T: T? -// parent of T?: topFun +// parent of T: T +// parent of T: topFun // parent of Any?: T // parent of T: topFun // parent of Anno: null @@ -103,7 +104,6 @@ // parent of Any?: T // parent of T: File: a.kt // parent of Alias: File: a.kt -// parent of Any: Any // parent of Any: ITF // parent of ITF: File: a.kt // parent of ITF: ITF @@ -119,6 +119,7 @@ // parent of memberFun: topClass // parent of Int: Int // parent of Int: a +// parent of Int: Int // parent of Int: a.getter() // parent of a.getter(): a // parent of a: topClass @@ -131,7 +132,6 @@ // parent of : b.setter() // parent of b.setter(): b // parent of b: topClass -// parent of Any: Any // parent of Any: InnerClass // parent of Any?: P // parent of P: InnerClass