Support for import aliases in code completion

#KT-8848 Fixed
This commit is contained in:
Valentin Kipyatkov
2017-07-28 17:37:56 +03:00
parent 48246e5f34
commit 9361cd895c
44 changed files with 429 additions and 76 deletions
@@ -358,8 +358,15 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l
}
}
override fun createSubstitutedCopy(newOwner: DeclarationDescriptor, newModality: Modality, newVisibility: Visibility, original: PropertyDescriptor?, kind: CallableMemberDescriptor.Kind): PropertyDescriptorImpl {
return MyPropertyDescriptor(newOwner, this, annotations, newModality, newVisibility, isVar, name, kind, source).apply {
override fun createSubstitutedCopy(
newOwner: DeclarationDescriptor,
newModality: Modality,
newVisibility: Visibility,
original: PropertyDescriptor?,
kind: CallableMemberDescriptor.Kind,
newName: Name
): PropertyDescriptorImpl {
return MyPropertyDescriptor(newOwner, this, annotations, newModality, newVisibility, isVar, newName, kind, source).apply {
getMethod = this@MyPropertyDescriptor.getMethod
setMethod = this@MyPropertyDescriptor.setMethod
}
@@ -49,7 +49,11 @@ class AllUnderImportScope(
excludedImportNames.mapNotNull { if (it.parent() == fqName) it.shortName() else null }.toSet()
}
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): List<DeclarationDescriptor> {
override fun getContributedDescriptors(
kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean,
changeNamesForAliased: Boolean
): Collection<DeclarationDescriptor> {
val nameFilterToUse = if (excludedNames.isEmpty()) { // optimization
nameFilter
}
@@ -57,12 +57,47 @@ class LazyExplicitImportScope(
return collectCallableMemberDescriptors(location, MemberScope::getContributedVariables)
}
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> {
override fun getContributedDescriptors(
kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean,
changeNamesForAliased: Boolean
): Collection<DeclarationDescriptor> {
val descriptors = SmartList<DeclarationDescriptor>()
descriptors.addIfNotNull(getContributedClassifier(aliasName, NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS))
descriptors.addAll(getContributedFunctions(aliasName, NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS))
descriptors.addAll(getContributedVariables(aliasName, NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS))
if (changeNamesForAliased && aliasName != declaredName) {
for (i in descriptors.indices) {
val descriptor = descriptors[i]
val newDescriptor: DeclarationDescriptor = when (descriptor) {
is ClassDescriptor -> {
object : ClassDescriptor by descriptor {
override fun getName() = aliasName
}
}
is TypeAliasDescriptor -> {
object : TypeAliasDescriptor by descriptor {
override fun getName() = aliasName
}
}
is CallableMemberDescriptor -> {
descriptor
.newCopyBuilder()
.setName(aliasName)
.setOriginal(descriptor)
.build()!!
}
else -> error("Unknown kind of descriptor in import alias: $descriptor")
}
descriptors[i] = newDescriptor
}
}
return descriptors
}
@@ -208,7 +208,11 @@ class FileScopeFactory(
return scope.getContributedFunctions(name, location)
}
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> {
override fun getContributedDescriptors(
kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean,
changeNamesForAliased: Boolean
): Collection<DeclarationDescriptor> {
// we do not perform any filtering by visibility here because all descriptors from both visible/invisible filter scopes are to be added anyway
if (filteringKind == FilteringKind.INVISIBLE_CLASSES) return listOf()
return scope.getContributedDescriptors(
@@ -238,7 +238,11 @@ class LazyImportScope(
return importResolver.collectFromImports(name) { scope, name -> scope.getContributedFunctions(name, location) }
}
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> {
override fun getContributedDescriptors(
kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean,
changeNamesForAliased: Boolean
): Collection<DeclarationDescriptor> {
// we do not perform any filtering by visibility here because all descriptors from both visible/invisible filter scopes are to be added anyway
if (filteringKind == FilteringKind.INVISIBLE_CLASSES) return listOf()
@@ -248,7 +252,7 @@ class LazyImportScope(
val importPath = directive.importPath ?: continue
val importedName = importPath.importedName
if (importedName == null || nameFilter(importedName)) {
descriptors.addAll(importResolver.getImportScope(directive).getContributedDescriptors(kindFilter, nameFilter))
descriptors.addAll(importResolver.getImportScope(directive).getContributedDescriptors(kindFilter, nameFilter, changeNamesForAliased))
}
}
descriptors
@@ -51,10 +51,11 @@ class JvmPropertyDescriptorImpl private constructor(
newModality: Modality,
newVisibility: Visibility,
original: PropertyDescriptor?,
kind: CallableMemberDescriptor.Kind
kind: CallableMemberDescriptor.Kind,
newName: Name
): PropertyDescriptorImpl =
JvmPropertyDescriptorImpl(
newOwner, original, annotations, newModality, newVisibility, extraFlags, isVar, name, kind,
newOwner, original, annotations, newModality, newVisibility, extraFlags, isVar, newName, kind,
SourceElement.NO_SOURCE, isLateInit, isConst, isHeader, isImpl
)
@@ -109,6 +109,16 @@ interface ImportingScope : HierarchicalScope {
fun getContributedPackage(name: Name): PackageViewDescriptor?
fun getContributedDescriptors(
kindFilter: DescriptorKindFilter = DescriptorKindFilter.ALL,
nameFilter: (Name) -> Boolean = MemberScope.ALL_NAME_FILTER,
changeNamesForAliased: Boolean
): Collection<DeclarationDescriptor>
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> {
return getContributedDescriptors(kindFilter, nameFilter, changeNamesForAliased = false)
}
object Empty : BaseImportingScope(null) {
override fun printStructure(p: Printer) {
p.println("ImportingScope.Empty")
@@ -131,4 +141,11 @@ abstract class BaseImportingScope(parent: ImportingScope?) : BaseHierarchicalSco
get() = super.parent as ImportingScope?
override fun getContributedPackage(name: Name): PackageViewDescriptor? = null
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> {
return getContributedDescriptors(kindFilter, nameFilter, changeNamesForAliased = false)
}
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean, changeNamesForAliased: Boolean): Collection<DeclarationDescriptor>
= emptyList()
}
@@ -40,12 +40,12 @@ class SubpackagesImportingScope(
override fun getContributedFunctions(name: Name, location: LookupLocation) = super.getContributedFunctions(name, location)
//TODO: kept old behavior, but it seems very strange (super call seems more applicable)
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? {
return ImportingScope.Empty.getContributedClassifier(name, location)
}
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? = null
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> =
emptyList()
//TODO: kept old behavior, but it seems very strange (super call seems more applicable)
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> {
return ImportingScope.Empty.getContributedDescriptors(kindFilter, nameFilter)
}
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean, changeNamesForAliased: Boolean): Collection<DeclarationDescriptor>
= emptyList()
}
@@ -50,14 +50,18 @@ fun LexicalScope.getDeclarationsByLabel(labelName: Name): Collection<Declaration
// Result is guaranteed to be filtered by kind and name.
fun HierarchicalScope.collectDescriptorsFiltered(
kindFilter: DescriptorKindFilter = DescriptorKindFilter.ALL,
nameFilter: (Name) -> Boolean = { true }
nameFilter: (Name) -> Boolean = { true },
changeNamesForAliased: Boolean = false
): Collection<DeclarationDescriptor> {
if (kindFilter.kindMask == 0) return listOf()
return collectAllFromMeAndParent { it.getContributedDescriptors(kindFilter, nameFilter) }
.filter { kindFilter.accepts(it) && nameFilter(it.name) }
return collectAllFromMeAndParent {
if (it is ImportingScope)
it.getContributedDescriptors(kindFilter, nameFilter, changeNamesForAliased)
else
it.getContributedDescriptors(kindFilter, nameFilter)
}.filter { kindFilter.accepts(it) && nameFilter(it.name) }
}
@Deprecated("Use getContributedProperties instead") fun LexicalScope.findLocalVariable(name: Name): VariableDescriptor? {
return findFirstFromMeAndParent {
when {
@@ -103,7 +107,7 @@ fun HierarchicalScope.takeSnapshot(): HierarchicalScope = if (this is LexicalWri
private class MemberScopeToImportingScopeAdapter(override val parent: ImportingScope?, val memberScope: MemberScope) : ImportingScope {
override fun getContributedPackage(name: Name): PackageViewDescriptor? = null
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean)
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean, changeNamesForAliased: Boolean)
= memberScope.getContributedDescriptors(kindFilter, nameFilter)
override fun getContributedClassifier(name: Name, location: LookupLocation) = memberScope.getContributedClassifier(name, location)