[Misc] Refactor Module.cached extension to accept lambda instead of FunctionalInterface

This commit is contained in:
Dmitry Savvinov
2019-06-05 12:57:39 +03:00
parent b477184a3c
commit 54cdd3bfef
6 changed files with 111 additions and 87 deletions
@@ -0,0 +1,62 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.caches.project
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootModificationTracker
import com.intellij.openapi.util.UserDataHolder
import com.intellij.psi.util.CachedValueProvider
import com.intellij.psi.util.CachedValuesManager
fun <T> Module.cacheByClass(classForKey: Class<*>, vararg dependencies: Any, provider: () -> T): T {
return CachedValuesManager.getManager(project).cache(this, dependencies, classForKey, provider)
}
fun <T> Module.cacheByClassInvalidatingOnRootModifications(classForKey: Class<*>, provider: () -> T): T {
return cacheByClass(classForKey, ProjectRootModificationTracker.getInstance(project), provider = provider)
}
/**
* Note that it uses lambda's class for caching (essentially, anonymous class), which means that all invocations will be cached
* by the one and the same key.
* It is encouraged to use explicit class, just for the sake of readability.
*/
fun <T> Module.cacheInvalidatingOnRootModifications(provider: () -> T): T {
return cacheByClassInvalidatingOnRootModifications(provider::class.java, provider)
}
fun <T> Project.cacheByClass(classForKey: Class<*>, vararg dependencies: Any, provider: () -> T): T {
return CachedValuesManager.getManager(this).cache(this, dependencies, classForKey, provider)
}
fun <T> Project.cacheByClassInvalidatingOnRootModifications(classForKey: Class<*>, provider: () -> T): T {
return cacheByClass(classForKey, ProjectRootModificationTracker.getInstance(this), provider = provider)
}
/**
* Note that it uses lambda's class for caching (essentially, anonymous class), which means that all invocations will be cached
* by the one and the same key.
* It is encouraged to use explicit class, just for the sake of readability.
*/
fun <T> Project.cacheInvalidatingOnRootModifications(provider: () -> T): T {
return cacheByClassInvalidatingOnRootModifications(provider::class.java, provider)
}
private fun <T> CachedValuesManager.cache(
holder: UserDataHolder,
dependencies: Array<out Any>,
classForKey: Class<*>,
provider: () -> T
): T {
return getCachedValue(
holder,
getKeyForClass(classForKey),
{ CachedValueProvider.Result.create(provider(), dependencies) },
false
)
}
@@ -17,7 +17,6 @@ import com.intellij.openapi.roots.libraries.Library
import com.intellij.openapi.util.ModificationTracker
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.util.CachedValueProvider
import com.intellij.util.PathUtil
import com.intellij.util.SmartList
import org.jetbrains.jps.model.java.JavaSourceRootType
@@ -26,6 +25,7 @@ import org.jetbrains.kotlin.analyzer.CombinedModuleInfo
import org.jetbrains.kotlin.analyzer.LibraryModuleInfo
import org.jetbrains.kotlin.analyzer.ModuleInfo
import org.jetbrains.kotlin.analyzer.TrackableModuleInfo
import org.jetbrains.kotlin.caches.project.cacheByClassInvalidatingOnRootModifications
import org.jetbrains.kotlin.caches.resolve.resolution
import org.jetbrains.kotlin.config.SourceKotlinRootType
import org.jetbrains.kotlin.config.TestSourceKotlinRootType
@@ -177,16 +177,9 @@ sealed class ModuleSourceInfoWithExpectedBy(private val forProduction: Boolean)
return expectedByModules.mapNotNull { if (forProduction) it.productionSourceInfo() else it.testSourceInfo() }
}
override fun dependencies(): List<IdeaModuleInfo> = module.cached(createCachedValueProvider {
CachedValueProvider.Result(
ideaModelDependencies(module, forProduction, platform),
ProjectRootModificationTracker.getInstance(module.project)
)
})
// NB: CachedValueProvider must exist separately in Production / Test source info,
// otherwise caching does not work properly
protected abstract fun <T> createCachedValueProvider(f: () -> CachedValueProvider.Result<T>): CachedValueProvider<T>
override fun dependencies(): List<IdeaModuleInfo> = module.cacheByClassInvalidatingOnRootModifications(this::class.java) {
ideaModelDependencies(module, forProduction, platform)
}
}
data class ModuleProductionSourceInfo internal constructor(
@@ -200,8 +193,6 @@ data class ModuleProductionSourceInfo internal constructor(
override fun contentScope(): GlobalSearchScope {
return enlargedSearchScope(ModuleProductionSourceScope(module), module, isTestScope = false)
}
override fun <T> createCachedValueProvider(f: () -> CachedValueProvider.Result<T>) = CachedValueProvider { f() }
}
//TODO: (module refactoring) do not create ModuleTestSourceInfo when there are no test roots for module
@@ -217,19 +208,20 @@ data class ModuleTestSourceInfo internal constructor(override val module: Module
override fun contentScope(): GlobalSearchScope = enlargedSearchScope(ModuleTestSourceScope(module), module, isTestScope = true)
override fun modulesWhoseInternalsAreVisible() = module.cached(CachedValueProvider {
val list = SmartList<ModuleInfo>()
override fun modulesWhoseInternalsAreVisible(): Collection<ModuleInfo> =
module.cacheByClassInvalidatingOnRootModifications(KeyForModulesWhoseInternalsAreVisible::class.java) {
val list = SmartList<ModuleInfo>()
list.addIfNotNull(module.productionSourceInfo())
list.addIfNotNull(module.productionSourceInfo())
TestModuleProperties.getInstance(module).productionModule?.let {
list.addIfNotNull(it.productionSourceInfo())
TestModuleProperties.getInstance(module).productionModule?.let {
list.addIfNotNull(it.productionSourceInfo())
}
list
}
CachedValueProvider.Result(list, ProjectRootModificationTracker.getInstance(module.project))
})
override fun <T> createCachedValueProvider(f: () -> CachedValueProvider.Result<T>) = CachedValueProvider { f() }
private object KeyForModulesWhoseInternalsAreVisible
}
fun Module.productionSourceInfo(): ModuleProductionSourceInfo? = if (hasProductionRoots()) ModuleProductionSourceInfo(this) else null
@@ -1,19 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.caches.project
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.psi.util.CachedValueProvider
import com.intellij.psi.util.CachedValuesManager
fun <T> Module.cached(provider: CachedValueProvider<T>): T {
return CachedValuesManager.getManager(project).getCachedValue(this, provider)
}
fun <T> Project.cached(provider: CachedValueProvider<T>): T {
return CachedValuesManager.getManager(this).getCachedValue(this, provider)
}
@@ -13,17 +13,17 @@ import com.intellij.openapi.projectRoots.Sdk
import com.intellij.openapi.roots.JdkOrderEntry
import com.intellij.openapi.roots.LibraryOrderEntry
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.roots.ProjectRootModificationTracker
import com.intellij.psi.util.CachedValueProvider
import org.jetbrains.kotlin.caches.project.cacheInvalidatingOnRootModifications
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.platform.isCommon
import org.jetbrains.kotlin.types.typeUtil.closure
import java.util.concurrent.ConcurrentHashMap
fun getModuleInfosFromIdeaModel(project: Project, platform: TargetPlatform): List<IdeaModuleInfo> {
val modelInfosCache = project.cached(CachedValueProvider {
CachedValueProvider.Result(collectModuleInfosFromIdeaModel(project), ProjectRootModificationTracker.getInstance(project))
})
val modelInfosCache = project.cacheInvalidatingOnRootModifications {
collectModuleInfosFromIdeaModel(project)
}
return modelInfosCache.forPlatform(platform)
}
@@ -10,10 +10,9 @@ import com.intellij.facet.FacetTypeRegistry
import com.intellij.openapi.externalSystem.service.project.IdeModelsProviderImpl
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.roots.ProjectRootModificationTracker
import com.intellij.psi.PsiElement
import com.intellij.psi.util.CachedValueProvider
import org.jetbrains.kotlin.analyzer.ModuleInfo
import org.jetbrains.kotlin.caches.project.cacheInvalidatingOnRootModifications
import org.jetbrains.kotlin.caches.resolve.KotlinCacheService
import org.jetbrains.kotlin.config.KotlinFacetSettings
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
@@ -49,37 +48,30 @@ val KotlinFacetSettings.isMPPModule: Boolean
private val Module.facetSettings get() = KotlinFacet.get(this)?.configuration?.settings
val Module.implementingModules: List<Module>
get() = cached(CachedValueProvider {
get() = cacheInvalidatingOnRootModifications {
val moduleManager = ModuleManager.getInstance(project)
CachedValueProvider.Result(
if (isNewMPPModule) {
moduleManager.getModuleDependentModules(this).filter {
it.isNewMPPModule && it.externalProjectId == externalProjectId
}
} else {
moduleManager.modules.filter { name in it.findOldFashionedImplementedModuleNames() }
},
ProjectRootModificationTracker.getInstance(project)
)
})
if (isNewMPPModule) {
moduleManager.getModuleDependentModules(this).filter {
it.isNewMPPModule && it.externalProjectId == externalProjectId
}
} else {
moduleManager.modules.filter { name in it.findOldFashionedImplementedModuleNames() }
}
}
val Module.implementedModules: List<Module>
get() = cached<List<Module>>(
CachedValueProvider {
CachedValueProvider.Result(
if (isNewMPPModule) {
rootManager.dependencies.filter {
// TODO: remove additional android check
get() = cacheInvalidatingOnRootModifications {
if (isNewMPPModule) {
rootManager.dependencies.filter {
// TODO: remove additional android check
it.isNewMPPModule && it.platform.isCommon() && it.externalProjectId == externalProjectId && (isAndroidModule() || it.isTestModule == isTestModule)
}
} else {
val modelsProvider = IdeModelsProviderImpl(project)
findOldFashionedImplementedModuleNames().mapNotNull { modelsProvider.findIdeModule(it) }
},
ProjectRootModificationTracker.getInstance(project)
)
}
} else {
val modelsProvider = IdeModelsProviderImpl(project)
findOldFashionedImplementedModuleNames().mapNotNull { modelsProvider.findIdeModule(it) }
}
)
}
private fun Module.findOldFashionedImplementedModuleNames(): List<String> {
val facet = FacetManager.getInstance(this).findFacet(
@@ -9,12 +9,10 @@ import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleUtil
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.roots.ProjectRootModificationTracker
import com.intellij.openapi.util.ModificationTracker
import com.intellij.psi.util.CachedValueProvider
import com.intellij.util.CommonProcessors
import org.jetbrains.annotations.TestOnly
import org.jetbrains.kotlin.idea.caches.project.cached
import org.jetbrains.kotlin.caches.project.cacheByClassInvalidatingOnRootModifications
import org.jetbrains.kotlin.psi.KtFile
class KotlinModuleOutOfCodeBlockModificationTracker private constructor(private val module: Module, private val updater: Updater) :
@@ -29,18 +27,17 @@ class KotlinModuleOutOfCodeBlockModificationTracker private constructor(private
// Avoid implicit capturing for this to make CachedValueStabilityChecker happy
val module = module
module.cached(CachedValueProvider {
CachedValueProvider.Result.create(
HashSet<Module>().also { resultModuleSet ->
ModuleRootManager.getInstance(module).orderEntries().recursively().forEachModule(
CommonProcessors.CollectProcessor(resultModuleSet)
)
},
ProjectRootModificationTracker.getInstance(module.project)
)
})
module.cacheByClassInvalidatingOnRootModifications(KeyForCachedDependencies::class.java) {
HashSet<Module>().also { resultModuleSet ->
ModuleRootManager.getInstance(module).orderEntries().recursively().forEachModule(
CommonProcessors.CollectProcessor(resultModuleSet)
)
}
}
}
object KeyForCachedDependencies
override fun getModificationCount(): Long {
val currentGlobalCount = kotlinOutOfCodeBlockTracker.modificationCount