Move import exclusion logic from ModuleDescriptorImpl to frontend
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.resolve.lazy
|
||||
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.isChildOf
|
||||
import org.jetbrains.kotlin.name.isSubpackageOf
|
||||
import org.jetbrains.kotlin.resolve.ImportPath
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import org.jetbrains.kotlin.storage.getValue
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.check
|
||||
|
||||
class DefaultImportProvider(
|
||||
storageManager: StorageManager,
|
||||
private val moduleDescriptor: ModuleDescriptor
|
||||
) {
|
||||
// TODO: get rid of ModuleDescriptor#defaultImports
|
||||
val defaultImports: List<ImportPath>
|
||||
get() = moduleDescriptor.defaultImports
|
||||
|
||||
val excludedImports: List<FqName> by storageManager.createLazyValue {
|
||||
val packagesWithAliases = listOf(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME, KotlinBuiltIns.TEXT_PACKAGE_FQ_NAME)
|
||||
val builtinTypeAliases = moduleDescriptor.allDependentModules.flatMap { dependentModule ->
|
||||
packagesWithAliases.map(dependentModule::getPackage).flatMap {
|
||||
it.memberScope.getContributedDescriptors(DescriptorKindFilter.TYPE_ALIASES).filterIsInstance<TypeAliasDescriptor>()
|
||||
}
|
||||
}
|
||||
|
||||
val nonKotlinDefaultImportedPackages =
|
||||
defaultImports
|
||||
.filter { it.isAllUnder }
|
||||
.mapNotNull {
|
||||
it.fqnPart().check { !it.isSubpackageOf(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME) }
|
||||
}
|
||||
val nonKotlinAliasedTypeFqNames =
|
||||
builtinTypeAliases
|
||||
.mapNotNull { it.expandedType.constructor.declarationDescriptor?.fqNameSafe }
|
||||
.filter { nonKotlinDefaultImportedPackages.any(it::isChildOf) }
|
||||
|
||||
nonKotlinAliasedTypeFqNames
|
||||
}
|
||||
}
|
||||
@@ -52,10 +52,11 @@ class FileScopeFactory(
|
||||
private val bindingTrace: BindingTrace,
|
||||
private val ktImportsFactory: KtImportsFactory,
|
||||
private val platformToKotlinClassMap: PlatformToKotlinClassMap,
|
||||
private val defaultImportProvider: DefaultImportProvider,
|
||||
private val languageVersionSettings: LanguageVersionSettings
|
||||
) {
|
||||
private val defaultImports by storageManager.createLazyValue {
|
||||
ktImportsFactory.createImportDirectives(moduleDescriptor.defaultImports)
|
||||
ktImportsFactory.createImportDirectives(defaultImportProvider.defaultImports)
|
||||
}
|
||||
|
||||
fun createScopesForFile(file: KtFile, existingImports: ImportingScope? = null): FileScopes {
|
||||
@@ -97,7 +98,7 @@ class FileScopeFactory(
|
||||
val allUnderImportResolver = createImportResolver(AllUnderImportsIndexed(imports), bindingTrace) // TODO: should we count excludedImports here also?
|
||||
|
||||
val defaultExplicitImportResolver = createImportResolver(ExplicitImportsIndexed(defaultImportsFiltered), tempTrace)
|
||||
val defaultAllUnderImportResolver = createImportResolver(AllUnderImportsIndexed(defaultImportsFiltered), tempTrace, moduleDescriptor.effectivelyExcludedImports)
|
||||
val defaultAllUnderImportResolver = createImportResolver(AllUnderImportsIndexed(defaultImportsFiltered), tempTrace, defaultImportProvider.excludedImports)
|
||||
|
||||
val dummyContainerDescriptor = DummyContainerDescriptor(file, packageFragment)
|
||||
|
||||
|
||||
@@ -43,7 +43,10 @@ interface ModuleDescriptor : DeclarationDescriptor {
|
||||
|
||||
val defaultImports: List<ImportPath>
|
||||
|
||||
val effectivelyExcludedImports: List<FqName>
|
||||
/**
|
||||
* @return dependent modules in the same order in which this module depends on them. Does not include `this`
|
||||
*/
|
||||
val allDependentModules: List<ModuleDescriptor>
|
||||
|
||||
fun <T> getCapability(capability: Capability<T>): T?
|
||||
|
||||
|
||||
@@ -20,18 +20,11 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentProvider
|
||||
import org.jetbrains.kotlin.descriptors.PackageViewDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.name.isChildOf
|
||||
import org.jetbrains.kotlin.name.isSubpackageOf
|
||||
import org.jetbrains.kotlin.resolve.ImportPath
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import org.jetbrains.kotlin.storage.getValue
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.check
|
||||
import org.jetbrains.kotlin.utils.sure
|
||||
import java.lang.IllegalArgumentException
|
||||
|
||||
@@ -55,28 +48,8 @@ class ModuleDescriptorImpl @JvmOverloads constructor(
|
||||
fqName: FqName -> LazyPackageViewDescriptorImpl(this, fqName, storageManager)
|
||||
}
|
||||
|
||||
override val effectivelyExcludedImports: List<FqName> by storageManager.createLazyValue {
|
||||
val packagesWithAliases = listOf(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME, KotlinBuiltIns.TEXT_PACKAGE_FQ_NAME)
|
||||
val dependencies = this.dependencies.sure { "Dependencies of module $id were not set" }
|
||||
val builtinTypeAliases = dependencies.allDependencies.filter { it != this }.flatMap {
|
||||
packagesWithAliases.map(it::getPackage).flatMap {
|
||||
it.memberScope.getContributedDescriptors(DescriptorKindFilter.TYPE_ALIASES).filterIsInstance<TypeAliasDescriptor>()
|
||||
}
|
||||
}
|
||||
|
||||
val nonKotlinDefaultImportedPackages =
|
||||
defaultImports
|
||||
.filter { it.isAllUnder }
|
||||
.mapNotNull {
|
||||
it.fqnPart().check { !it.isSubpackageOf(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME) }
|
||||
}
|
||||
val nonKotlinAliasedTypeFqNames =
|
||||
builtinTypeAliases
|
||||
.mapNotNull { it.expandedType.constructor.declarationDescriptor?.fqNameSafe }
|
||||
.filter { nonKotlinDefaultImportedPackages.any(it::isChildOf) }
|
||||
|
||||
nonKotlinAliasedTypeFqNames
|
||||
}
|
||||
override val allDependentModules: List<ModuleDescriptor>
|
||||
get() = this.dependencies.sure { "Dependencies of module $id were not set" }.allDependencies.filter { it != this }
|
||||
|
||||
override fun getPackage(fqName: FqName): PackageViewDescriptor = packages(fqName)
|
||||
|
||||
|
||||
@@ -62,12 +62,6 @@ public class ErrorUtils {
|
||||
return emptyList();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<FqName> getEffectivelyExcludedImports() {
|
||||
return emptyList();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Annotations getAnnotations() {
|
||||
@@ -94,6 +88,12 @@ public class ErrorUtils {
|
||||
throw new IllegalStateException("Should not be called!");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<ModuleDescriptor> getAllDependentModules() {
|
||||
return emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R, D> R accept(@NotNull DeclarationDescriptorVisitor<R, D> visitor, D data) {
|
||||
return null;
|
||||
|
||||
+3
-1
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
|
||||
import org.jetbrains.kotlin.idea.references.canBeResolvedViaImport
|
||||
import org.jetbrains.kotlin.idea.references.mainReference
|
||||
import org.jetbrains.kotlin.idea.resolve.ResolutionFacade
|
||||
import org.jetbrains.kotlin.idea.resolve.frontendService
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.FqNameUnsafe
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
@@ -41,6 +42,7 @@ import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.module
|
||||
import org.jetbrains.kotlin.resolve.lazy.DefaultImportProvider
|
||||
import org.jetbrains.kotlin.resolve.lazy.descriptors.ClassResolutionScopesSupport
|
||||
import org.jetbrains.kotlin.resolve.scopes.*
|
||||
import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitReceiver
|
||||
@@ -205,7 +207,7 @@ object ReplaceWithAnnotationAnalyzer {
|
||||
}
|
||||
|
||||
private fun buildDefaultImportsScopes(resolutionFacade: ResolutionFacade, module: ModuleDescriptor): List<ImportingScope> {
|
||||
val (allUnderImports, aliasImports) = module.defaultImports.partition { it.isAllUnder }
|
||||
val (allUnderImports, aliasImports) = resolutionFacade.frontendService<DefaultImportProvider>().defaultImports.partition { it.isAllUnder }
|
||||
// this solution doesn't support aliased default imports with a different alias
|
||||
// TODO: Create import directives from ImportPath, create ImportResolver, create LazyResolverScope, see FileScopeProviderImpl
|
||||
|
||||
|
||||
@@ -19,15 +19,14 @@ package org.jetbrains.kotlin.util
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.findModuleDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||
import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings
|
||||
import org.jetbrains.kotlin.idea.core.targetDescriptors
|
||||
import org.jetbrains.kotlin.idea.imports.ImportPathComparator
|
||||
import org.jetbrains.kotlin.idea.imports.getImportableTargets
|
||||
import org.jetbrains.kotlin.idea.imports.importableFqName
|
||||
import org.jetbrains.kotlin.idea.project.platform
|
||||
import org.jetbrains.kotlin.idea.refactoring.fqName.isImported
|
||||
import org.jetbrains.kotlin.idea.resolve.frontendService
|
||||
import org.jetbrains.kotlin.idea.util.ImportDescriptorResult
|
||||
import org.jetbrains.kotlin.idea.util.ImportInsertHelper
|
||||
import org.jetbrains.kotlin.idea.util.getFileResolutionScope
|
||||
@@ -38,6 +37,7 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.ImportPath
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.getImportableDescriptor
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.resolve.lazy.DefaultImportProvider
|
||||
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
|
||||
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
@@ -58,8 +58,8 @@ class ImportInsertHelperImpl(private val project: Project) : ImportInsertHelper(
|
||||
get() = ImportPathComparator
|
||||
|
||||
override fun isImportedWithDefault(importPath: ImportPath, contextFile: KtFile): Boolean {
|
||||
val moduleDescriptor = contextFile.findModuleDescriptor()
|
||||
return importPath.isImported(moduleDescriptor.defaultImports, moduleDescriptor.effectivelyExcludedImports)
|
||||
val defaultImportProvider = contextFile.getResolutionFacade().frontendService<DefaultImportProvider>()
|
||||
return importPath.isImported(defaultImportProvider.defaultImports, defaultImportProvider.excludedImports)
|
||||
}
|
||||
|
||||
override fun mayImportOnShortenReferences(descriptor: DeclarationDescriptor): Boolean {
|
||||
|
||||
Reference in New Issue
Block a user