Skip to content

Commit

Permalink
Simplify checks of descriptor and psi correspondance.
Browse files Browse the repository at this point in the history
  • Loading branch information
ting-yuan committed Mar 29, 2024
1 parent ffb13a6 commit d73953f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Expand Up @@ -536,15 +536,15 @@ class ResolverImpl(
kindFilter = DescriptorKindFilter.VARIABLES,
name = propName
) {
synthesizedPropPrefix[prefix]!!(it as? PropertyDescriptor)?.findPsi() == psi
synthesizedPropPrefix[prefix]!!(it as? PropertyDescriptor)?.correspondsTo(psi) == true
}
}
property ?: moduleClassResolver
.resolveContainingClass(psi)?.let { containingClass ->
val filter = if (psi is SyntheticElement) {
{ declaration: DeclarationDescriptor -> declaration.name.asString() == psi.name }
} else {
{ declaration: DeclarationDescriptor -> declaration.findPsi() == psi }
{ declaration: DeclarationDescriptor -> declaration.correspondsTo(psi) }
}
containingClass.findEnclosedDescriptor(
kindFilter = DescriptorKindFilter.FUNCTIONS,
Expand All @@ -559,7 +559,7 @@ class ResolverImpl(
?.findEnclosedDescriptor(
kindFilter = DescriptorKindFilter.VARIABLES,
name = psi.name,
filter = { it.findPsi() == psi }
filter = { it.correspondsTo(psi) }
)
}
else -> throw IllegalStateException("unhandled psi element kind: ${psi.javaClass}")
Expand Down Expand Up @@ -756,7 +756,7 @@ class ResolverImpl(
?.findEnclosedDescriptor(
kindFilter = DescriptorKindFilter.FUNCTIONS,
name = owner.name,
filter = { it.findPsi() == owner }
filter = { it.correspondsTo(owner) }
) as FunctionDescriptor
} as DeclarationDescriptor
val typeParameterDescriptor = LazyJavaTypeParameterDescriptor(
Expand Down
Expand Up @@ -559,6 +559,11 @@ fun DeclarationDescriptor.findPsi(): PsiElement? {
return leaf.parentsWithSelf.firstOrNull { psi.manager.areElementsEquivalent(it, psi) }
}

fun DeclarationDescriptor.correspondsTo(candidate: PsiElement): Boolean {
val psi = (this as? DeclarationDescriptorWithSource)?.source?.getPsi() ?: return false
return psi.manager.areElementsEquivalent(psi, candidate)
}

internal fun KSFile.getPackageAnnotations() = (this as? KSFileJavaImpl)?.psi?.packageStatement
?.annotationList?.annotations?.map { KSAnnotationJavaImpl.getCached(it) } ?: emptyList<KSAnnotation>()

Expand Down

0 comments on commit d73953f

Please sign in to comment.