Introduce ImportingScope::computeImportedNames

This commit is contained in:
Denis Zharkov
2017-08-15 18:30:50 +07:00
parent cd3edfc5b2
commit 69f3b01e98
7 changed files with 20 additions and 7 deletions
@@ -22,15 +22,17 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.BaseImportingScope
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.ResolutionScope
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.resolve.scopes.computeAllNames
import org.jetbrains.kotlin.utils.Printer
import org.jetbrains.kotlin.utils.addToStdlib.flatMapToNullable
class AllUnderImportScope(
descriptor: DeclarationDescriptor,
excludedImportNames: Collection<FqName>
) : BaseImportingScope(null) {
private val scopes: List<ResolutionScope> = if (descriptor is ClassDescriptor) {
private val scopes: List<MemberScope> = if (descriptor is ClassDescriptor) {
listOf(descriptor.staticScope, descriptor.unsubstitutedInnerClassesScope)
}
else {
@@ -49,6 +51,8 @@ class AllUnderImportScope(
excludedImportNames.mapNotNull { if (it.parent() == fqName) it.shortName() else null }.toSet()
}
override fun computeImportedNames(): Set<Name>? = scopes.flatMapToNullable(hashSetOf(), MemberScope::computeAllNames)
override fun getContributedDescriptors(
kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean,
@@ -101,6 +101,8 @@ class LazyExplicitImportScope(
return descriptors
}
override fun computeImportedNames() = setOf(aliasName)
override fun printStructure(p: Printer) {
p.println(this::class.java.simpleName, ": ", aliasName)
}
@@ -148,4 +150,4 @@ class LazyExplicitImportScope(
private fun <D : CallableMemberDescriptor> Collection<D>.choseOnlyVisibleOrAll() =
filter { isVisible(it, packageFragmentForVisibilityCheck, position = QualifierPosition.IMPORT) }.
takeIf { it.isNotEmpty() } ?: this
}
}
@@ -27,10 +27,7 @@ import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtImportDirective
import org.jetbrains.kotlin.psi.KtImportsFactory
import org.jetbrains.kotlin.resolve.*
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.ImportingScope
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.SubpackagesImportingScope
import org.jetbrains.kotlin.resolve.scopes.*
import org.jetbrains.kotlin.resolve.source.KotlinSourceElement
import org.jetbrains.kotlin.script.getScriptExternalDependencies
import org.jetbrains.kotlin.storage.StorageManager
@@ -220,6 +217,8 @@ class FileScopeFactory(
).filter { it !is PackageViewDescriptor } // subpackages of the current package not accessible by the short name
}
override fun computeImportedNames() = packageView.memberScope.computeAllNames()
override fun toString() = "Scope for current package (${filteringKind.name})"
override fun printStructure(p: Printer) {
@@ -121,12 +121,15 @@ interface ImportingScope : HierarchicalScope {
return getContributedDescriptors(kindFilter, nameFilter, changeNamesForAliased = false)
}
fun computeImportedNames(): Set<Name>?
object Empty : BaseImportingScope(null) {
override fun printStructure(p: Printer) {
p.println("ImportingScope.Empty")
}
override fun definitelyDoesNotContainName(name: Name) = true
override fun computeImportedNames() = emptySet<Name>()
}
}
@@ -48,4 +48,5 @@ class SubpackagesImportingScope(
//TODO: kept old behavior, but it seems very strange (super call seems more applicable)
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean, changeNamesForAliased: Boolean): Collection<DeclarationDescriptor>
= emptyList()
override fun computeImportedNames() = emptySet<Name>()
}
@@ -122,6 +122,8 @@ private class MemberScopeToImportingScopeAdapter(override val parent: ImportingS
override fun toString() = "${this::class.java.simpleName} for $memberScope"
override fun computeImportedNames() = memberScope.computeAllNames()
override fun printStructure(p: Printer) {
p.println(this::class.java.simpleName)
p.pushIndent()
@@ -38,6 +38,8 @@ class ExplicitImportsScope(private val descriptors: Collection<DeclarationDescri
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean, changeNamesForAliased: Boolean)
= descriptors
override fun computeImportedNames() = descriptors.mapTo(hashSetOf()) { it.name }
override fun printStructure(p: Printer) {
p.println(this::class.java.name)
}