Simplify DefaultImportProvider, introduce "low priority imports"
Previously, packages `java.lang` and `kotlin.jvm` were imported on JVM by default on the same rights, causing problems when the same classifier existed both in `java.lang` and `kotlin.jvm`. Since the only known case of such conflict were type aliases to JVM classes, the corresponding classes (expansions of those type aliases) were manually excluded from default imports. This made the code in DefaultImportProvider complicated and resulted in multiple problems, regarding both correctness and performance (see82364ad3e5,a9f2f5c7d0,dd3dbda719). This change adds a new concept, a "low priority import", and treats `java.lang` as such. Since these imports are now separated from the rest of default imports in LazyImportScope via secondaryClassImportResolver, conflicts between classifiers are handled naturally: the one from `kotlin.jvm` always wins (unless the one from `java.lang` is imported explicitly, of course). This approach is simpler, safer and does not require any memory to cache anything. Skip ResolveToJava.kt test for javac-based resolve; it now fails because of a weird issue which I didn't have time to investigate (this is OK because it's a corner case of an experimental functionality)
This commit is contained in:
@@ -27,6 +27,8 @@ abstract class ImportInsertHelper {
|
||||
/*TODO: implementation is not quite correct*/
|
||||
abstract fun isImportedWithDefault(importPath: ImportPath, contextFile: KtFile): Boolean
|
||||
|
||||
abstract fun isImportedWithLowPriorityDefaultImport(importPath: ImportPath, contextFile: KtFile): Boolean
|
||||
|
||||
abstract fun mayImportOnShortenReferences(descriptor: DeclarationDescriptor): Boolean
|
||||
|
||||
abstract val importSortComparator: Comparator<ImportPath>
|
||||
|
||||
@@ -260,7 +260,7 @@ class PsiBasedClassResolver @TestOnly constructor(private val targetClassFqName:
|
||||
private fun KtFile.getDefaultImports(): List<ImportPath> {
|
||||
val moduleInfo = getNullableModuleInfo() ?: return emptyList()
|
||||
val versionSettings = IDELanguageSettingsProvider.getLanguageVersionSettings(moduleInfo, project)
|
||||
return TargetPlatformDetector.getPlatform(this).getDefaultImports(
|
||||
versionSettings.supportsFeature(LanguageFeature.DefaultImportOfPackageKotlinComparisons)
|
||||
)
|
||||
val platform = TargetPlatformDetector.getPlatform(this)
|
||||
return platform.getDefaultImports(versionSettings.supportsFeature(LanguageFeature.DefaultImportOfPackageKotlinComparisons)) +
|
||||
platform.defaultLowPriorityImports
|
||||
}
|
||||
|
||||
+2
-1
@@ -139,7 +139,8 @@ object ReplaceWithAnnotationAnalyzer {
|
||||
}
|
||||
|
||||
private fun buildDefaultImportsScopes(resolutionFacade: ResolutionFacade, module: ModuleDescriptor): List<ImportingScope> {
|
||||
val (allUnderImports, aliasImports) = resolutionFacade.frontendService<DefaultImportProvider>().defaultImports.partition { it.isAllUnder }
|
||||
val (allUnderImports, aliasImports) =
|
||||
resolutionFacade.frontendService<DefaultImportProvider>().allDefaultImports.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
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ 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.TargetPlatformDetector
|
||||
import org.jetbrains.kotlin.idea.refactoring.fqName.isImported
|
||||
import org.jetbrains.kotlin.idea.resolve.frontendService
|
||||
import org.jetbrains.kotlin.idea.util.ImportDescriptorResult
|
||||
@@ -59,7 +60,13 @@ class ImportInsertHelperImpl(private val project: Project) : ImportInsertHelper(
|
||||
|
||||
override fun isImportedWithDefault(importPath: ImportPath, contextFile: KtFile): Boolean {
|
||||
val defaultImportProvider = contextFile.getResolutionFacade().frontendService<DefaultImportProvider>()
|
||||
return importPath.isImported(defaultImportProvider.defaultImports, defaultImportProvider.excludedImports)
|
||||
val platform = TargetPlatformDetector.getPlatform(contextFile)
|
||||
return importPath.isImported(defaultImportProvider.allDefaultImports, platform.excludedImports)
|
||||
}
|
||||
|
||||
override fun isImportedWithLowPriorityDefaultImport(importPath: ImportPath, contextFile: KtFile): Boolean {
|
||||
val platform = TargetPlatformDetector.getPlatform(contextFile)
|
||||
return importPath.isImported(platform.defaultLowPriorityImports, platform.excludedImports)
|
||||
}
|
||||
|
||||
override fun mayImportOnShortenReferences(descriptor: DeclarationDescriptor): Boolean {
|
||||
|
||||
Reference in New Issue
Block a user