Skip to content

Commit

Permalink
Implement KSValueArgument.parent for the analysis API
Browse files Browse the repository at this point in the history
This fixes #2049

(cherry picked from commit f84a7f0)
  • Loading branch information
ansman authored and KSP Auto Pick committed Sep 5, 2024
1 parent b9ec04d commit 1506cc1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ class KSAnnotationJavaImpl private constructor(private val psi: PsiAnnotation, o
KotlinAlwaysAccessibleLifetimeToken(ResolverAAImpl.ktModule.project)
)
),
this@KSAnnotationJavaImpl,
Origin.SYNTHETIC
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ class KSAnnotationImpl private constructor(
}

override val arguments: List<KSValueArgument> by lazy {
val presentArgs = annotationApplication.arguments.map { KSValueArgumentImpl.getCached(it, Origin.KOTLIN) }
val presentArgs = annotationApplication.arguments.map {
KSValueArgumentImpl.getCached(it, this, Origin.KOTLIN)
}
val presentNames = presentArgs.mapNotNull { it.name?.asString() }
val absentArgs = defaultArguments.filter {
it.name?.asString() !in presentNames
Expand Down Expand Up @@ -112,6 +114,7 @@ class KSAnnotationImpl private constructor(
KotlinAlwaysAccessibleLifetimeToken(ResolverAAImpl.ktModule.project)
)
),
this@KSAnnotationImpl,
Origin.SYNTHETIC
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ import org.jetbrains.kotlin.analysis.api.annotations.KtNamedAnnotationValue

class KSValueArgumentImpl private constructor(
private val namedAnnotationValue: KtNamedAnnotationValue,
override val parent: KSNode,
override val origin: Origin
) : KSValueArgument, Deferrable {
companion object : KSObjectCache<KtNamedAnnotationValue, KSValueArgumentImpl>() {
fun getCached(namedAnnotationValue: KtNamedAnnotationValue, origin: Origin) =
cache.getOrPut(namedAnnotationValue) { KSValueArgumentImpl(namedAnnotationValue, origin) }
fun getCached(namedAnnotationValue: KtNamedAnnotationValue, parent: KSNode, origin: Origin) =
cache.getOrPut(namedAnnotationValue) {
KSValueArgumentImpl(namedAnnotationValue, parent, origin)
}
}

override val name: KSName? by lazy {
Expand All @@ -45,9 +48,6 @@ class KSValueArgumentImpl private constructor(
namedAnnotationValue.expression.sourcePsi?.toLocation() ?: NonExistLocation
}

override val parent: KSNode?
get() = TODO("Not yet implemented")

override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
return visitor.visitValueArgument(this, data)
}
Expand All @@ -56,5 +56,5 @@ class KSValueArgumentImpl private constructor(
return "${name?.asString() ?: ""}:$value"
}

override fun defer(): Restorable = Restorable { getCached(namedAnnotationValue, origin) }
override fun defer(): Restorable = Restorable { getCached(namedAnnotationValue, parent, origin) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ class KSAnnotationResolvedImpl private constructor(
}
}
override val arguments: List<KSValueArgument> by lazy {
val presentArgs = annotationApplication.arguments.map { KSValueArgumentImpl.getCached(it, Origin.KOTLIN) }
val presentArgs = annotationApplication.arguments.map {
KSValueArgumentImpl.getCached(it, this, Origin.KOTLIN)
}
val presentNames = presentArgs.mapNotNull { it.name?.asString() }
val absentArgs = defaultArguments.filter {
it.name?.asString() !in presentNames
Expand Down Expand Up @@ -94,6 +96,7 @@ class KSAnnotationResolvedImpl private constructor(
KotlinAlwaysAccessibleLifetimeToken(ResolverAAImpl.ktModule.project)
)
),
this@KSAnnotationResolvedImpl,
Origin.SYNTHETIC
)
}
Expand Down
19 changes: 16 additions & 3 deletions kotlin-analysis-api/testData/parent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// parent of T: B
// parent of Anno: Anno
// parent of Anno: @Anno
// parent of param:: @Anno
// parent of @Anno: B
// parent of Bnno: Bnno
// parent of Bnno: @Bnno
Expand Down Expand Up @@ -78,6 +79,7 @@
// parent of topProp.getter(): topProp
// parent of Anno: Anno
// parent of Anno: @Anno
// parent of param:: @Anno
// parent of @Anno: topProp
// parent of topProp: File: a.kt
// parent of T: T
Expand All @@ -86,13 +88,23 @@
// parent of T: topFun
// parent of Anno: Anno
// parent of Anno: @Anno
// parent of param:: @Anno
// parent of @Anno: topFun
// parent of topFun: File: a.kt
// parent of Annotation: Annotation
// parent of Annotation: Anno
// parent of Anno: File: a.kt
// parent of Anno: synthetic constructor for Anno
// parent of synthetic constructor for Anno: Anno
// parent of String: String
// parent of String: param
// parent of String: param.getter()
// parent of param.getter(): param
// parent of param: Anno
// parent of String: String
// parent of String: param
// parent of param: <init>
// parent of Anno: Anno
// parent of Anno: <init>
// parent of <init>: Anno
// parent of Int: Int
// parent of Int: INVARIANT Int
// parent of INVARIANT Int: Map<INVARIANT Int, INVARIANT T>
Expand All @@ -110,6 +122,7 @@
// parent of ITF: topClass
// parent of Anno: Anno
// parent of Anno: @Anno
// parent of param:: @Anno
// parent of @Anno: topClass
// parent of topClass: File: a.kt
// parent of Int: Int
Expand Down Expand Up @@ -243,7 +256,7 @@ enum HSV {
// MODULE: main(lib)

// FILE: a.kt
annotation class Anno
annotation class Anno(val param: String = "")

typealias Alias<T> = Map<Int, T>

Expand Down
14 changes: 11 additions & 3 deletions test-utils/testData/api/parent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// parent of T: B
// parent of Anno: Anno
// parent of Anno: @Anno
// parent of param:: @Anno
// parent of @Anno: B
// parent of p: Bnno
// parent of p.Bnno: Bnno
Expand Down Expand Up @@ -74,8 +75,15 @@
// parent of File: a.kt: null
// parent of Any: Anno
// parent of Anno: File: a.kt
// parent of Anno: synthetic constructor for Anno
// parent of synthetic constructor for Anno: Anno
// parent of String: String
// parent of String: param
// parent of param: <init>
// parent of Anno: <init>
// parent of <init>: Anno
// parent of String: String
// parent of String: param.getter()
// parent of param.getter(): param
// parent of param: <init>
// parent of Int: Int
// parent of Int: INVARIANT Int
// parent of INVARIANT Int: Map
Expand Down Expand Up @@ -246,7 +254,7 @@ enum HSV {
// MODULE: main(lib)

// FILE: a.kt
annotation class Anno
annotation class Anno(val param: String = "")

typealias Alias<T> = Map<Int, T>

Expand Down

0 comments on commit 1506cc1

Please sign in to comment.