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
@@ -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