Use nameFilter if all descriptors have not calculated

#KT-49821 Fixed
This commit is contained in:
Vladimir Dolzhenko
2021-11-21 00:03:26 +01:00
committed by teamcityserver
parent bfa3f89aeb
commit 42837415f0
2 changed files with 31 additions and 7 deletions
@@ -38,6 +38,7 @@ import org.jetbrains.kotlin.resolve.lazy.declarations.ClassMemberDeclarationProv
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.resolve.scopes.MemberScope.Companion.ALL_NAME_FILTER
import org.jetbrains.kotlin.storage.NotNullLazyValue
import org.jetbrains.kotlin.storage.NullableLazyValue
import org.jetbrains.kotlin.storage.getValue
@@ -60,36 +61,58 @@ open class LazyClassMemberScope(
) {
private val allDescriptors = storageManager.createLazyValue {
doDescriptors(ALL_NAME_FILTER)
}
private fun doDescriptors(nameFilter: (Name) -> Boolean): List<DeclarationDescriptor> {
val result = LinkedHashSet(
computeDescriptorsFromDeclaredElements(
DescriptorKindFilter.ALL,
MemberScope.ALL_NAME_FILTER,
nameFilter,
NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS
)
)
result.addAll(computeExtraDescriptors(NoLookupLocation.FOR_ALREADY_TRACKED))
result.toList()
return result.toList()
}
private val allClassifierDescriptors = storageManager.createLazyValue {
doClassifierDescriptors(ALL_NAME_FILTER)
}
private fun doClassifierDescriptors(nameFilter: (Name) -> Boolean): List<DeclarationDescriptor> {
val result = LinkedHashSet(
computeDescriptorsFromDeclaredElements(
DescriptorKindFilter.CLASSIFIERS,
MemberScope.ALL_NAME_FILTER,
nameFilter,
NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS
)
)
addSyntheticCompanionObject(result, NoLookupLocation.FOR_ALREADY_TRACKED)
addSyntheticNestedClasses(result, NoLookupLocation.FOR_ALREADY_TRACKED)
result.toList()
return result.toList()
}
override fun getContributedDescriptors(
kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean
): Collection<DeclarationDescriptor> = when (kindFilter) {
DescriptorKindFilter.CLASSIFIERS -> allClassifierDescriptors()
else -> allDescriptors()
DescriptorKindFilter.CLASSIFIERS ->
if (nameFilter == ALL_NAME_FILTER || allClassifierDescriptors.isComputed() || allClassifierDescriptors.isComputing()) {
allClassifierDescriptors()
} else {
storageManager.compute {
doClassifierDescriptors(nameFilter)
}
}
else ->
if (nameFilter == ALL_NAME_FILTER || allDescriptors.isComputed() || allDescriptors.isComputing()) {
allDescriptors()
} else {
storageManager.compute {
doDescriptors(nameFilter)
}
}
}
protected open fun computeExtraDescriptors(location: LookupLocation): Collection<DeclarationDescriptor> {
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.resolve.scopes
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.MemberScope.Companion.ALL_NAME_FILTER
import org.jetbrains.kotlin.utils.Printer
import org.jetbrains.kotlin.utils.addToStdlib.flatMapToNullable
import java.lang.reflect.Modifier
@@ -73,7 +74,7 @@ fun Iterable<MemberScope>.flatMapClassifierNamesOrNull(): MutableSet<Name>? =
*/
fun MemberScope.getDescriptorsFiltered(
kindFilter: DescriptorKindFilter = DescriptorKindFilter.ALL,
nameFilter: (Name) -> Boolean = { true }
nameFilter: (Name) -> Boolean = ALL_NAME_FILTER
): Collection<DeclarationDescriptor> {
if (kindFilter.kindMask == 0) return listOf()
return getContributedDescriptors(kindFilter, nameFilter).filter { kindFilter.accepts(it) && nameFilter(it.name) }