Skip to content

Commit

Permalink
Cache symbols for getSymbolsWithAnnotation
Browse files Browse the repository at this point in the history
Currently, all annotation types are implemented by
KSTypeReferenceResolvedImpl so the short-name-check optimization doesn't
help. The special treatment of type aliases is only required by the
short-name-check optimization and therefore isn't needed as well.
  • Loading branch information
ting-yuan committed Jan 25, 2024
1 parent a1cef8b commit 300e928
Showing 1 changed file with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -463,23 +463,37 @@ class ResolverAAImpl(
}
}

// TODO: optimization and type alias handling.
// Currently, all annotation types are imlemented by KSTypeReferenceResolvedImpl.
// The short-name-check optimization doesn't help.
override fun getSymbolsWithAnnotation(annotationName: String, inDepth: Boolean): Sequence<KSAnnotated> {
val newSymbols = if (inDepth) newAnnotatedSymbolsWithLocals else newAnnotatedSymbols
return (newSymbols + deferredSymbolsRestored).asSequence().filter {
it.annotations.any {
it.annotationType.resolve().declaration.qualifiedName?.asString() == annotationName
}
}
}

private fun collectAnnotatedSymbols(inDepth: Boolean): Collection<KSAnnotated> {
val visitor = CollectAnnotatedSymbolsVisitor(inDepth)

for (file in getNewFiles()) {
for (file in newKSFiles) {
file.accept(visitor, Unit)
}

val deferred = deferredSymbols.values.flatten().mapNotNull {
it.restore()
}.toSet()
return visitor.symbols
}

return (visitor.symbols + deferred).asSequence().filter {
it.annotations.any {
it.annotationType.resolve().declaration.qualifiedName?.asString() == annotationName
}
}
private val deferredSymbolsRestored: Set<KSAnnotated> by lazy {
deferredSymbols.values.flatten().mapNotNull { it.restore() }.toSet()
}

private val newAnnotatedSymbols: Collection<KSAnnotated> by lazy {
collectAnnotatedSymbols(false)
}

private val newAnnotatedSymbolsWithLocals: Collection<KSAnnotated> by lazy {
collectAnnotatedSymbols(true)
}

override fun getTypeArgument(typeRef: KSTypeReference, variance: Variance): KSTypeArgument {
Expand Down

0 comments on commit 300e928

Please sign in to comment.