Added resolve scope enlarger to module infos in Kotlin

From now it's possible to enlarge resolve scope for analysis in IDE,
the only difference is that it's based on module file.

#KT-26313 Fixed
This commit is contained in:
Alexander Podkhalyuzin
2018-08-24 12:31:48 +03:00
parent a9a8925409
commit c0e92ba350
@@ -16,6 +16,7 @@ import com.intellij.openapi.roots.impl.libraries.LibraryEx
import com.intellij.openapi.roots.libraries.Library import com.intellij.openapi.roots.libraries.Library
import com.intellij.openapi.util.ModificationTracker import com.intellij.openapi.util.ModificationTracker
import com.intellij.openapi.vfs.VirtualFile import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.ResolveScopeEnlarger
import com.intellij.psi.search.GlobalSearchScope import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.util.CachedValueProvider import com.intellij.psi.util.CachedValueProvider
import com.intellij.util.PathUtil import com.intellij.util.PathUtil
@@ -58,6 +59,14 @@ interface IdeaModuleInfo : org.jetbrains.kotlin.idea.caches.resolve.IdeaModuleIn
override fun dependencies(): List<IdeaModuleInfo> override fun dependencies(): List<IdeaModuleInfo>
} }
private fun enlargedSearchScope(searchScope: GlobalSearchScope, moduleFile: VirtualFile?): GlobalSearchScope {
if (moduleFile == null) return searchScope
return ResolveScopeEnlarger.EP_NAME.extensions.fold(searchScope) { scope, enlarger ->
val extra = enlarger.getAdditionalResolveScope(moduleFile, scope.project)
if (extra != null) scope.union(extra) else scope
}
}
private fun orderEntryToModuleInfo(project: Project, orderEntry: OrderEntry, forProduction: Boolean): List<IdeaModuleInfo> { private fun orderEntryToModuleInfo(project: Project, orderEntry: OrderEntry, forProduction: Boolean): List<IdeaModuleInfo> {
fun Module.toInfos() = correspondingModuleInfos().filter { !forProduction || it is ModuleProductionSourceInfo } fun Module.toInfos() = correspondingModuleInfos().filter { !forProduction || it is ModuleProductionSourceInfo }
@@ -161,7 +170,7 @@ data class ModuleProductionSourceInfo internal constructor(
override val stableName: Name = module.getStableName() override val stableName: Name = module.getStableName()
override fun contentScope(): GlobalSearchScope = ModuleProductionSourceScope(module) override fun contentScope(): GlobalSearchScope = enlargedSearchScope(ModuleProductionSourceScope(module), module.moduleFile)
override fun <T> createCachedValueProvider(f: () -> CachedValueProvider.Result<T>) = CachedValueProvider { f() } override fun <T> createCachedValueProvider(f: () -> CachedValueProvider.Result<T>) = CachedValueProvider { f() }
} }
@@ -177,7 +186,7 @@ data class ModuleTestSourceInfo internal constructor(override val module: Module
override val displayedName get() = module.name + " (test)" override val displayedName get() = module.name + " (test)"
override fun contentScope(): GlobalSearchScope = ModuleTestSourceScope(module) override fun contentScope(): GlobalSearchScope = enlargedSearchScope(ModuleTestSourceScope(module), module.moduleFile)
override fun modulesWhoseInternalsAreVisible() = module.cached(CachedValueProvider { override fun modulesWhoseInternalsAreVisible() = module.cached(CachedValueProvider {
val list = SmartList<ModuleInfo>() val list = SmartList<ModuleInfo>()
@@ -433,4 +442,4 @@ data class PlatformModuleInfo(
} }
fun IdeaModuleInfo.projectSourceModules(): List<ModuleSourceInfo>? = fun IdeaModuleInfo.projectSourceModules(): List<ModuleSourceInfo>? =
(this as? ModuleSourceInfo)?.let(::listOf) ?: (this as? PlatformModuleInfo)?.containedModules (this as? ModuleSourceInfo)?.let(::listOf) ?: (this as? PlatformModuleInfo)?.containedModules