CompositeScriptConfigurationManager: unified caching
This commit is contained in:
+7
-20
@@ -27,8 +27,6 @@ import com.intellij.psi.search.GlobalSearchScope
|
||||
import com.intellij.util.io.URLUtil
|
||||
import org.jetbrains.annotations.TestOnly
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.CompositeScriptConfigurationManager
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.listener.ScriptConfigurationUpdater
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.loader.ScriptConfigurationLoader
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.UserDataProperty
|
||||
import org.jetbrains.kotlin.scripting.definitions.ScriptDependenciesProvider
|
||||
@@ -69,12 +67,6 @@ interface ScriptConfigurationManager {
|
||||
*/
|
||||
fun getConfiguration(file: KtFile): ScriptCompilationConfigurationWrapper?
|
||||
|
||||
/**
|
||||
* Reload the configuration for [file] even it is already loaded.
|
||||
* [loader] is used to load configuration. Other loaders aren't taken into account.
|
||||
*/
|
||||
fun forceReloadConfiguration(file: VirtualFile, loader: ScriptConfigurationLoader): ScriptCompilationConfigurationWrapper?
|
||||
|
||||
@Deprecated("Use getScriptClasspath(KtFile) instead")
|
||||
fun getScriptClasspath(file: VirtualFile): List<VirtualFile>
|
||||
|
||||
@@ -97,15 +89,10 @@ interface ScriptConfigurationManager {
|
||||
*/
|
||||
fun isConfigurationLoadingInProgress(file: KtFile): Boolean
|
||||
|
||||
/**
|
||||
* See [ScriptConfigurationUpdater].
|
||||
*/
|
||||
val updater: ScriptConfigurationUpdater
|
||||
|
||||
/**
|
||||
* Clear all caches and re-highlighting opened scripts
|
||||
*/
|
||||
fun clearConfigurationCachesAndRehighlight()
|
||||
fun updateScriptDefinitions()
|
||||
|
||||
///////////////
|
||||
// classpath roots info:
|
||||
@@ -126,14 +113,14 @@ interface ScriptConfigurationManager {
|
||||
ServiceManager.getService(project, ScriptConfigurationManager::class.java)
|
||||
|
||||
fun toVfsRoots(roots: Iterable<File>): List<VirtualFile> {
|
||||
return roots.mapNotNull { it.classpathEntryToVfs() }
|
||||
return roots.mapNotNull { classpathEntryToVfs(it) }
|
||||
}
|
||||
|
||||
private fun File.classpathEntryToVfs(): VirtualFile? {
|
||||
fun classpathEntryToVfs(file: File): VirtualFile? {
|
||||
val res = when {
|
||||
!exists() -> null
|
||||
isDirectory -> StandardFileSystems.local()?.findFileByPath(this.canonicalPath)
|
||||
isFile -> StandardFileSystems.jar()?.findFileByPath(this.canonicalPath + URLUtil.JAR_SEPARATOR)
|
||||
!file.exists() -> null
|
||||
file.isDirectory -> StandardFileSystems.local()?.findFileByPath(file.canonicalPath)
|
||||
file.isFile -> StandardFileSystems.jar()?.findFileByPath(file.canonicalPath + URLUtil.JAR_SEPARATOR)
|
||||
else -> null
|
||||
}
|
||||
// TODO: report this somewhere, but do not throw: assert(res != null, { "Invalid classpath entry '$this': exists: ${exists()}, is directory: $isDirectory, is file: $isFile" })
|
||||
@@ -150,7 +137,7 @@ interface ScriptConfigurationManager {
|
||||
@TestOnly
|
||||
fun clearCaches(project: Project) {
|
||||
(getInstance(project) as CompositeScriptConfigurationManager).default
|
||||
.clearCaches()
|
||||
.updateScriptDefinitions()
|
||||
}
|
||||
|
||||
fun clearManualConfigurationLoadingIfNeeded(file: VirtualFile) {
|
||||
|
||||
+1
-1
@@ -190,7 +190,7 @@ class ScriptDefinitionsManager(private val project: Project) : LazyScriptDefinit
|
||||
scriptDefinitionsCacheLock.withLock { scriptDefinitionsCache.clear() }
|
||||
|
||||
// TODO: clear by script type/definition
|
||||
ScriptConfigurationManager.getInstance(project).clearConfigurationCachesAndRehighlight()
|
||||
ScriptConfigurationManager.getInstance(project).updateScriptDefinitions()
|
||||
}
|
||||
|
||||
private fun ScriptDefinitionsSource.safeGetDefinitions(): List<ScriptDefinition> {
|
||||
|
||||
+62
-73
@@ -15,69 +15,87 @@ import com.intellij.psi.search.GlobalSearchScope
|
||||
import org.jetbrains.kotlin.idea.core.script.ScriptConfigurationManager
|
||||
import org.jetbrains.kotlin.idea.core.script.ScriptDependenciesModificationTracker
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.listener.ScriptChangesNotifier
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.listener.ScriptConfigurationUpdater
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.loader.ScriptConfigurationLoader
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.utils.ScriptClassRootsCache
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.utils.ScriptClassRootsUpdater
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.utils.getKtFile
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.scripting.definitions.isNonScript
|
||||
import org.jetbrains.kotlin.scripting.resolve.ScriptCompilationConfigurationWrapper
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult
|
||||
|
||||
class CompositeScriptConfigurationManager(val project: Project) : ScriptConfigurationManager {
|
||||
@Suppress("unused")
|
||||
private val notifier = ScriptChangesNotifier(project, updater)
|
||||
private val notifier = ScriptChangesNotifier(project)
|
||||
|
||||
private val providers = ScriptingSupport.Provider.EPN.getPoint(project).extensionList
|
||||
val updater = ScriptClassRootsUpdater(project, this)
|
||||
|
||||
val default = DefaultScriptingSupport(project)
|
||||
private val plugins = ScriptingSupport.Provider.EPN.getPoint(project).extensionList
|
||||
|
||||
private val managers
|
||||
get() = mutableListOf<ScriptingSupport>().also { managers ->
|
||||
managers.add(default)
|
||||
providers.forEach { managers.addAll(it.all) }
|
||||
}
|
||||
|
||||
private fun getRelatedManager(file: VirtualFile): ScriptingSupport =
|
||||
providers.firstNotNullResult { it.getSupport(file) } ?: default
|
||||
|
||||
private fun getRelatedManager(file: KtFile): ScriptingSupport =
|
||||
getRelatedManager(file.originalFile.virtualFile)
|
||||
val default = DefaultScriptingSupport(this)
|
||||
|
||||
private fun getOrLoadConfiguration(
|
||||
virtualFile: VirtualFile,
|
||||
preloadedKtFile: KtFile? = null
|
||||
): ScriptCompilationConfigurationWrapper? =
|
||||
getRelatedManager(virtualFile).getOrLoadConfiguration(virtualFile, preloadedKtFile)
|
||||
): ScriptCompilationConfigurationWrapper? {
|
||||
val scriptConfiguration = classpathRoots.getScriptConfiguration(virtualFile)
|
||||
if (scriptConfiguration != null) return scriptConfiguration
|
||||
|
||||
override fun getConfiguration(file: KtFile) = getOrLoadConfiguration(file.originalFile.virtualFile, file)
|
||||
// check that this script should be loaded later in special way (e.g. gradle project import)
|
||||
if (plugins.any { it.isApplicable(virtualFile) }) return null
|
||||
|
||||
return default.getOrLoadConfiguration(virtualFile, preloadedKtFile)
|
||||
}
|
||||
|
||||
override fun getConfiguration(file: KtFile) =
|
||||
getOrLoadConfiguration(file.originalFile.virtualFile, file)
|
||||
|
||||
override fun hasConfiguration(file: KtFile): Boolean =
|
||||
getRelatedManager(file).hasCachedConfiguration(file)
|
||||
classpathRoots.contains(file.originalFile.virtualFile)
|
||||
|
||||
override fun isConfigurationLoadingInProgress(file: KtFile): Boolean =
|
||||
getRelatedManager(file).isConfigurationLoadingInProgress(file)
|
||||
plugins.firstOrNull { it.isApplicable(file.originalFile.virtualFile) }?.isConfigurationLoadingInProgress(file)
|
||||
?: default.isConfigurationLoadingInProgress(file)
|
||||
|
||||
override val updater: ScriptConfigurationUpdater
|
||||
get() = object : ScriptConfigurationUpdater {
|
||||
override fun ensureUpToDatedConfigurationSuggested(file: KtFile) =
|
||||
getRelatedManager(file).updater.ensureUpToDatedConfigurationSuggested(file)
|
||||
@Volatile
|
||||
private var classpathRoots: ScriptClassRootsCache = recreateRootsCache()
|
||||
|
||||
override fun ensureConfigurationUpToDate(files: List<KtFile>): Boolean =
|
||||
files.groupBy { getRelatedManager(it) }.all { (manager, files) ->
|
||||
manager.updater.ensureConfigurationUpToDate(files)
|
||||
}
|
||||
fun getLightScriptInfo(file: String): ScriptClassRootsCache.LightScriptInfo? =
|
||||
classpathRoots.getLightScriptInfo(file)
|
||||
|
||||
override fun suggestToUpdateConfigurationIfOutOfDate(file: KtFile) =
|
||||
getRelatedManager(file).updater.suggestToUpdateConfigurationIfOutOfDate(file)
|
||||
private fun recreateRootsCache(): ScriptClassRootsCache {
|
||||
val builder = ScriptClassRootsCache.Builder(project)
|
||||
default.collectConfigurations(builder)
|
||||
plugins.forEach { it.collectConfigurations(builder) }
|
||||
return builder.build()
|
||||
}
|
||||
|
||||
fun collectRootsAndCheckNew(): ScriptClassRootsCache.Updates {
|
||||
val old = classpathRoots
|
||||
val new = recreateRootsCache()
|
||||
classpathRoots = new
|
||||
return new.diff(old)
|
||||
}
|
||||
|
||||
override fun updateScriptDefinitions() {
|
||||
ScriptDependenciesModificationTracker.getInstance(project).incModificationCount()
|
||||
|
||||
default.updateScriptDefinitions()
|
||||
|
||||
if (classpathRoots.customDefinitionsUsed) {
|
||||
plugins.forEach {
|
||||
it.updateScriptDefinitions()
|
||||
}
|
||||
|
||||
updater.ensureUpdateScheduled()
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
val connection = project.messageBus.connect(project)
|
||||
connection.subscribe(ProjectTopics.PROJECT_ROOTS, object : ModuleRootListener {
|
||||
override fun rootsChanged(event: ModuleRootEvent) {
|
||||
managers.forEach {
|
||||
it.clearClassRootsCaches(project)
|
||||
if (event.isCausedByFileTypesChange) return
|
||||
|
||||
if (classpathRoots.hasInvalidSdk(project)) {
|
||||
updater.ensureUpdateScheduled()
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -88,49 +106,36 @@ class CompositeScriptConfigurationManager(val project: Project) : ScriptConfigur
|
||||
* Loads script configuration if classpath roots don't contain [file] yet
|
||||
*/
|
||||
private fun getActualClasspathRoots(file: VirtualFile): ScriptClassRootsCache {
|
||||
val manager = getRelatedManager(file)
|
||||
|
||||
val classpathRoots = manager.classpathRoots
|
||||
val classpathRoots = classpathRoots
|
||||
if (classpathRoots.contains(file)) {
|
||||
return classpathRoots
|
||||
}
|
||||
|
||||
getOrLoadConfiguration(file)
|
||||
|
||||
return manager.classpathRoots
|
||||
return this.classpathRoots
|
||||
}
|
||||
|
||||
override fun getScriptSdk(file: VirtualFile): Sdk? =
|
||||
getActualClasspathRoots(file).getScriptSdk(file)
|
||||
|
||||
override fun getFirstScriptsSdk(): Sdk? {
|
||||
managers.forEach {
|
||||
it.classpathRoots.firstScriptSdk?.let { sdk -> return sdk }
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
override fun getFirstScriptsSdk(): Sdk? =
|
||||
classpathRoots.firstScriptSdk
|
||||
|
||||
override fun getScriptDependenciesClassFilesScope(file: VirtualFile): GlobalSearchScope =
|
||||
getActualClasspathRoots(file).getScriptDependenciesClassFilesScope(file)
|
||||
classpathRoots.getScriptDependenciesClassFilesScope(file)
|
||||
|
||||
override fun getAllScriptsDependenciesClassFilesScope(): GlobalSearchScope =
|
||||
GlobalSearchScope.union(managers.map { it.classpathRoots.allDependenciesClassFilesScope })
|
||||
classpathRoots.allDependenciesClassFilesScope
|
||||
|
||||
override fun getAllScriptDependenciesSourcesScope(): GlobalSearchScope =
|
||||
GlobalSearchScope.union(managers.map { it.classpathRoots.allDependenciesSourcesScope })
|
||||
classpathRoots.allDependenciesSourcesScope
|
||||
|
||||
override fun getAllScriptsDependenciesClassFiles(): List<VirtualFile> =
|
||||
managers.flatMap { it.classpathRoots.allDependenciesClassFiles }
|
||||
classpathRoots.allDependenciesClassFiles
|
||||
|
||||
override fun getAllScriptDependenciesSources(): List<VirtualFile> =
|
||||
managers.flatMap { it.classpathRoots.allDependenciesSources }
|
||||
|
||||
override fun forceReloadConfiguration(file: VirtualFile, loader: ScriptConfigurationLoader): ScriptCompilationConfigurationWrapper? {
|
||||
val ktFile = project.getKtFile(file, null) ?: return null
|
||||
|
||||
return default.forceReloadConfiguration(ktFile, loader)
|
||||
}
|
||||
classpathRoots.allDependenciesSources
|
||||
|
||||
///////////////////
|
||||
// Adapters for deprecated API
|
||||
@@ -144,20 +149,4 @@ class CompositeScriptConfigurationManager(val project: Project) : ScriptConfigur
|
||||
|
||||
override fun getScriptClasspath(file: KtFile): List<VirtualFile> =
|
||||
ScriptConfigurationManager.toVfsRoots(getConfiguration(file)?.dependenciesClassPath.orEmpty())
|
||||
|
||||
private fun clearCaches() {
|
||||
managers.forEach {
|
||||
it.clearCaches()
|
||||
}
|
||||
}
|
||||
|
||||
override fun clearConfigurationCachesAndRehighlight() {
|
||||
ScriptDependenciesModificationTracker.getInstance(project).incModificationCount()
|
||||
|
||||
clearCaches()
|
||||
|
||||
ScriptingSupportHelper.updateHighlighting(project) {
|
||||
!it.isNonScript()
|
||||
}
|
||||
}
|
||||
}
|
||||
+50
-52
@@ -20,7 +20,6 @@ import org.jetbrains.annotations.TestOnly
|
||||
import org.jetbrains.kotlin.idea.core.script.*
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.DefaultScriptConfigurationManagerExtensions.LOADER
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.cache.*
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.listener.ScriptConfigurationUpdater
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.loader.DefaultScriptConfigurationLoader
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.loader.ScriptConfigurationLoader
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.loader.ScriptConfigurationLoadingContext
|
||||
@@ -37,21 +36,21 @@ import java.util.concurrent.locks.ReentrantLock
|
||||
import kotlin.concurrent.withLock
|
||||
import kotlin.script.experimental.api.ScriptDiagnostic
|
||||
|
||||
class DefaultScriptingSupport(project: Project) : DefaultScriptingSupportBase(project) {
|
||||
class DefaultScriptingSupport(manager: CompositeScriptConfigurationManager) : DefaultScriptingSupportBase(manager) {
|
||||
// TODO public for tests
|
||||
val backgroundExecutor: BackgroundExecutor =
|
||||
if (ApplicationManager.getApplication().isUnitTestMode) TestingBackgroundExecutor(rootsIndexer)
|
||||
else DefaultBackgroundExecutor(project, rootsIndexer)
|
||||
if (ApplicationManager.getApplication().isUnitTestMode) TestingBackgroundExecutor(manager.updater)
|
||||
else DefaultBackgroundExecutor(project, manager.updater)
|
||||
|
||||
private val outsiderLoader = ScriptOutsiderFileConfigurationLoader(project)
|
||||
private val fileAttributeCache = ScriptConfigurationFileAttributeCache(project)
|
||||
private val defaultLoader = DefaultScriptConfigurationLoader(project)
|
||||
private val loaders: Sequence<ScriptConfigurationLoader>
|
||||
get() = sequence {
|
||||
yield(outsiderLoader)
|
||||
yield(fileAttributeCache)
|
||||
yieldAll(LOADER.getPoint(project).extensionList)
|
||||
yield(defaultLoader)
|
||||
private val loaders: List<ScriptConfigurationLoader>
|
||||
get() = mutableListOf<ScriptConfigurationLoader>().apply {
|
||||
add(outsiderLoader)
|
||||
add(fileAttributeCache)
|
||||
addAll(LOADER.getPoint(project).extensionList)
|
||||
add(defaultLoader)
|
||||
}
|
||||
|
||||
private val saveLock = ReentrantLock()
|
||||
@@ -166,7 +165,7 @@ class DefaultScriptingSupport(project: Project) : DefaultScriptingSupportBase(pr
|
||||
if (!ScriptDefinitionsManager.getInstance(project).isReady()) return null
|
||||
val scriptDefinition = file.findScriptDefinition() ?: return null
|
||||
|
||||
rootsIndexer.transaction {
|
||||
manager.updater.update {
|
||||
if (!loader.shouldRunInBackground(scriptDefinition)) {
|
||||
loader.loadDependencies(false, file, scriptDefinition, loadingContext)
|
||||
} else {
|
||||
@@ -240,7 +239,7 @@ class DefaultScriptingSupport(project: Project) : DefaultScriptingSupportBase(pr
|
||||
onClick = {
|
||||
saveReports(file, newResult.reports)
|
||||
file.removeScriptDependenciesNotificationPanel(project)
|
||||
rootsIndexer.transaction {
|
||||
manager.updater.update {
|
||||
setAppliedConfiguration(file, newResult)
|
||||
}
|
||||
}
|
||||
@@ -272,10 +271,16 @@ class DefaultScriptingSupport(project: Project) : DefaultScriptingSupportBase(pr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun getInstance(project: Project) =
|
||||
(ScriptConfigurationManager.getInstance(project) as CompositeScriptConfigurationManager).default
|
||||
}
|
||||
}
|
||||
|
||||
abstract class DefaultScriptingSupportBase(val project: Project) : ScriptingSupport() {
|
||||
protected val rootsIndexer = ScriptClassRootsIndexer(project)
|
||||
abstract class DefaultScriptingSupportBase(val manager: CompositeScriptConfigurationManager) : ScriptingSupport() {
|
||||
val project: Project
|
||||
get() = manager.project
|
||||
|
||||
@Suppress("LeakingThis")
|
||||
protected val cache: ScriptConfigurationCache = createCache()
|
||||
@@ -311,14 +316,14 @@ abstract class DefaultScriptingSupportBase(val project: Project) : ScriptingSupp
|
||||
fun getAppliedConfiguration(file: VirtualFile?): ScriptConfigurationSnapshot? =
|
||||
getCachedConfigurationState(file)?.applied
|
||||
|
||||
override fun hasCachedConfiguration(file: KtFile): Boolean =
|
||||
fun hasCachedConfiguration(file: KtFile): Boolean =
|
||||
getAppliedConfiguration(file.originalFile.virtualFile) != null
|
||||
|
||||
override fun isConfigurationLoadingInProgress(file: KtFile): Boolean {
|
||||
fun isConfigurationLoadingInProgress(file: KtFile): Boolean {
|
||||
return !hasCachedConfiguration(file) && !ScriptConfigurationManager.isManualConfigurationLoading(file.originalFile.virtualFile)
|
||||
}
|
||||
|
||||
override fun getOrLoadConfiguration(
|
||||
fun getOrLoadConfiguration(
|
||||
virtualFile: VirtualFile,
|
||||
preloadedKtFile: KtFile?
|
||||
): ScriptCompilationConfigurationWrapper? {
|
||||
@@ -326,32 +331,30 @@ abstract class DefaultScriptingSupportBase(val project: Project) : ScriptingSupp
|
||||
if (cached != null) return cached.configuration
|
||||
|
||||
val ktFile = project.getKtFile(virtualFile, preloadedKtFile) ?: return null
|
||||
rootsIndexer.transaction {
|
||||
manager.updater.update {
|
||||
reloadOutOfDateConfiguration(ktFile, isFirstLoad = true)
|
||||
}
|
||||
|
||||
return getAppliedConfiguration(virtualFile)?.configuration
|
||||
}
|
||||
|
||||
override val updater: ScriptConfigurationUpdater = object : ScriptConfigurationUpdater {
|
||||
override fun ensureUpToDatedConfigurationSuggested(file: KtFile) {
|
||||
reloadIfOutOfDate(listOf(file), loadEvenWillNotBeApplied = true, isPostponedLoad = false)
|
||||
}
|
||||
fun ensureUpToDatedConfigurationSuggested(file: KtFile) {
|
||||
reloadIfOutOfDate(listOf(file), loadEvenWillNotBeApplied = true, isPostponedLoad = false)
|
||||
}
|
||||
|
||||
override fun ensureConfigurationUpToDate(files: List<KtFile>): Boolean {
|
||||
return reloadIfOutOfDate(files, loadEvenWillNotBeApplied = false, isPostponedLoad = false)
|
||||
}
|
||||
fun ensureConfigurationUpToDate(files: List<KtFile>): Boolean {
|
||||
return reloadIfOutOfDate(files, loadEvenWillNotBeApplied = false, isPostponedLoad = false)
|
||||
}
|
||||
|
||||
override fun suggestToUpdateConfigurationIfOutOfDate(file: KtFile) {
|
||||
reloadIfOutOfDate(listOf(file), loadEvenWillNotBeApplied = true, isPostponedLoad = true)
|
||||
}
|
||||
fun suggestToUpdateConfigurationIfOutOfDate(file: KtFile) {
|
||||
reloadIfOutOfDate(listOf(file), loadEvenWillNotBeApplied = true, isPostponedLoad = true)
|
||||
}
|
||||
|
||||
private fun reloadIfOutOfDate(files: List<KtFile>, loadEvenWillNotBeApplied: Boolean, isPostponedLoad: Boolean): Boolean {
|
||||
if (!ScriptDefinitionsManager.getInstance(project).isReady()) return false
|
||||
|
||||
var upToDate = true
|
||||
rootsIndexer.transaction {
|
||||
manager.updater.update {
|
||||
files.forEach { file ->
|
||||
val virtualFile = file.originalFile.virtualFile
|
||||
if (virtualFile != null) {
|
||||
@@ -376,23 +379,13 @@ abstract class DefaultScriptingSupportBase(val project: Project) : ScriptingSupp
|
||||
file: VirtualFile,
|
||||
newConfigurationSnapshot: ScriptConfigurationSnapshot?
|
||||
) {
|
||||
rootsIndexer.checkInTransaction()
|
||||
manager.updater.checkInTransaction()
|
||||
val newConfiguration = newConfigurationSnapshot?.configuration
|
||||
debug(file) { "configuration changed = $newConfiguration" }
|
||||
|
||||
if (newConfiguration != null) {
|
||||
if (hasNotCachedRoots(newConfiguration)) {
|
||||
debug(file) { "new class roots found: $newConfiguration" }
|
||||
rootsIndexer.markNewRoot()
|
||||
}
|
||||
|
||||
manager.updater.invalidate(file)
|
||||
cache.setApplied(file, newConfigurationSnapshot)
|
||||
|
||||
clearClassRootsCaches(project)
|
||||
}
|
||||
|
||||
ScriptingSupportHelper.updateHighlighting(project) {
|
||||
it == file
|
||||
}
|
||||
}
|
||||
|
||||
@@ -403,10 +396,6 @@ abstract class DefaultScriptingSupportBase(val project: Project) : ScriptingSupp
|
||||
cache.setLoaded(file, configurationSnapshot)
|
||||
}
|
||||
|
||||
private fun hasNotCachedRoots(configuration: ScriptCompilationConfigurationWrapper): Boolean {
|
||||
return classpathRoots.hasNotCachedRoots(DefaultClassRootsCache.extractRoots(project, configuration))
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
internal fun updateScriptDependenciesSynchronously(file: PsiFile) {
|
||||
file.findScriptDefinition() ?: return
|
||||
@@ -416,20 +405,29 @@ abstract class DefaultScriptingSupportBase(val project: Project) : ScriptingSupp
|
||||
val virtualFile = file.virtualFile
|
||||
if (cache[virtualFile]?.isUpToDate(project, virtualFile, file) == true) return
|
||||
|
||||
rootsIndexer.transaction {
|
||||
manager.updater.update {
|
||||
reloadOutOfDateConfiguration(file, forceSync = true, loadEvenWillNotBeApplied = true)
|
||||
}
|
||||
}
|
||||
|
||||
override fun clearCaches() {
|
||||
fun updateScriptDefinitions() {
|
||||
cache.clear()
|
||||
}
|
||||
|
||||
override fun recreateRootsCache(): ScriptClassRootsCache {
|
||||
return DefaultClassRootsCache(
|
||||
project,
|
||||
cache.allApplied()
|
||||
)
|
||||
fun collectConfigurations(builder: ScriptClassRootsCache.Builder) {
|
||||
// todo: drop the hell below
|
||||
// keep this one only:
|
||||
// cache.allApplied().forEach { (vFile, configuration) -> builder.add(vFile, configuration) }
|
||||
|
||||
// own builder for saving to storage
|
||||
val ownBuilder = ScriptClassRootsCache.Builder(project)
|
||||
val rootsStorage = ScriptClassRootsStorage.getInstance(project)
|
||||
|
||||
rootsStorage.load(ownBuilder)
|
||||
cache.allApplied().forEach { (vFile, configuration) -> ownBuilder.add(vFile, configuration) }
|
||||
rootsStorage.save(ownBuilder)
|
||||
|
||||
builder.add(ownBuilder)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-60
@@ -6,76 +6,20 @@
|
||||
package org.jetbrains.kotlin.idea.core.script.configuration
|
||||
|
||||
import com.intellij.openapi.extensions.ExtensionPointName
|
||||
import com.intellij.openapi.extensions.Extensions
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.psi.PsiElementFinder
|
||||
import org.jetbrains.kotlin.idea.core.script.KotlinScriptDependenciesClassFinder
|
||||
import org.jetbrains.kotlin.idea.core.script.ScriptDependenciesModificationTracker
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.listener.ScriptConfigurationUpdater
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.utils.ScriptClassRootsCache
|
||||
import org.jetbrains.kotlin.idea.core.script.debug
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.scripting.resolve.ScriptCompilationConfigurationWrapper
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
import kotlin.concurrent.withLock
|
||||
|
||||
abstract class ScriptingSupport {
|
||||
abstract class Provider {
|
||||
abstract val all: Collection<ScriptingSupport>
|
||||
|
||||
abstract fun getSupport(file: VirtualFile): ScriptingSupport?
|
||||
abstract fun updateScriptDefinitions()
|
||||
abstract fun isApplicable(file: VirtualFile): Boolean
|
||||
abstract fun isConfigurationLoadingInProgress(file: KtFile): Boolean
|
||||
abstract fun collectConfigurations(builder: ScriptClassRootsCache.Builder)
|
||||
|
||||
companion object {
|
||||
val EPN: ExtensionPointName<Provider> =
|
||||
ExtensionPointName.create("org.jetbrains.kotlin.scripting.idea.scriptingSupportProvider")
|
||||
}
|
||||
}
|
||||
|
||||
abstract fun clearCaches()
|
||||
abstract fun hasCachedConfiguration(file: KtFile): Boolean
|
||||
abstract fun isConfigurationLoadingInProgress(file: KtFile): Boolean
|
||||
abstract fun getOrLoadConfiguration(virtualFile: VirtualFile, preloadedKtFile: KtFile? = null): ScriptCompilationConfigurationWrapper?
|
||||
|
||||
abstract val updater: ScriptConfigurationUpdater
|
||||
|
||||
private val classpathRootsLock = ReentrantLock()
|
||||
|
||||
@Volatile
|
||||
private var _classpathRoots: ScriptClassRootsCache? = null
|
||||
val classpathRoots: ScriptClassRootsCache
|
||||
get() {
|
||||
val value1 = _classpathRoots
|
||||
if (value1 != null) return value1
|
||||
|
||||
classpathRootsLock.withLock {
|
||||
val value2 = _classpathRoots
|
||||
if (value2 != null) return value2
|
||||
|
||||
val value3 = recreateRootsCache()
|
||||
value3.saveClassRootsToStorage()
|
||||
_classpathRoots = value3
|
||||
return value3
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract fun recreateRootsCache(): ScriptClassRootsCache
|
||||
|
||||
fun clearClassRootsCaches(project: Project) {
|
||||
debug { "class roots caches cleared" }
|
||||
|
||||
classpathRootsLock.withLock {
|
||||
_classpathRoots = null
|
||||
}
|
||||
|
||||
val kotlinScriptDependenciesClassFinder =
|
||||
Extensions.getArea(project)
|
||||
.getExtensionPoint(PsiElementFinder.EP_NAME).extensions
|
||||
.filterIsInstance<KotlinScriptDependenciesClassFinder>()
|
||||
.single()
|
||||
|
||||
kotlinScriptDependenciesClassFinder.clearCache()
|
||||
|
||||
ScriptDependenciesModificationTracker.getInstance(project).incModificationCount()
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -31,7 +31,7 @@ interface ScriptConfigurationCache {
|
||||
fun setApplied(file: VirtualFile, configurationSnapshot: ScriptConfigurationSnapshot)
|
||||
fun setLoaded(file: VirtualFile, configurationSnapshot: ScriptConfigurationSnapshot)
|
||||
|
||||
fun allApplied(): Map<VirtualFile, ScriptCompilationConfigurationWrapper>
|
||||
fun allApplied(): List<Pair<VirtualFile, ScriptCompilationConfigurationWrapper>>
|
||||
fun clear()
|
||||
|
||||
fun getAnyLoadedScript(): ScriptCompilationConfigurationWrapper?
|
||||
|
||||
+3
-3
@@ -41,11 +41,11 @@ open class ScriptConfigurationMemoryCache(
|
||||
|
||||
@Synchronized
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun allApplied(): Map<VirtualFile, ScriptCompilationConfigurationWrapper> {
|
||||
val result = hashMapOf<VirtualFile, ScriptCompilationConfigurationWrapper>()
|
||||
override fun allApplied(): List<Pair<VirtualFile, ScriptCompilationConfigurationWrapper>> {
|
||||
val result = mutableListOf<Pair<VirtualFile, ScriptCompilationConfigurationWrapper>>()
|
||||
for ((file, configuration) in memoryCache.entrySet()) {
|
||||
if (configuration.applied?.configuration != null) {
|
||||
result[file] = configuration.applied.configuration
|
||||
result.add(Pair(file, configuration.applied.configuration))
|
||||
}
|
||||
}
|
||||
return result
|
||||
|
||||
+4
-4
@@ -9,14 +9,14 @@ import com.intellij.openapi.vfs.VirtualFile
|
||||
import org.jetbrains.kotlin.scripting.definitions.isNonScript
|
||||
|
||||
open class DefaultScriptChangeListener(project: Project) : ScriptChangeListener(project) {
|
||||
override fun editorActivated(vFile: VirtualFile, updater: ScriptConfigurationUpdater) {
|
||||
override fun editorActivated(vFile: VirtualFile) {
|
||||
val file = getAnalyzableKtFileForScript(vFile) ?: return
|
||||
updater.ensureUpToDatedConfigurationSuggested(file)
|
||||
default.ensureUpToDatedConfigurationSuggested(file)
|
||||
}
|
||||
|
||||
override fun documentChanged(vFile: VirtualFile, updater: ScriptConfigurationUpdater) {
|
||||
override fun documentChanged(vFile: VirtualFile) {
|
||||
val file = getAnalyzableKtFileForScript(vFile) ?: return
|
||||
updater.ensureUpToDatedConfigurationSuggested(file)
|
||||
default.ensureUpToDatedConfigurationSuggested(file)
|
||||
}
|
||||
|
||||
override fun isApplicable(vFile: VirtualFile): Boolean {
|
||||
|
||||
+6
-2
@@ -9,6 +9,7 @@ import com.intellij.openapi.extensions.ExtensionPointName
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.psi.PsiManager
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.DefaultScriptingSupport
|
||||
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil
|
||||
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
@@ -21,8 +22,11 @@ import org.jetbrains.kotlin.psi.KtFile
|
||||
* @see DefaultScriptConfigurationManager for more details.
|
||||
*/
|
||||
abstract class ScriptChangeListener(protected val project: Project) {
|
||||
abstract fun editorActivated(vFile: VirtualFile, updater: ScriptConfigurationUpdater)
|
||||
abstract fun documentChanged(vFile: VirtualFile, updater: ScriptConfigurationUpdater)
|
||||
val default: DefaultScriptingSupport
|
||||
get() = DefaultScriptingSupport.getInstance(project)
|
||||
|
||||
abstract fun editorActivated(vFile: VirtualFile)
|
||||
abstract fun documentChanged(vFile: VirtualFile)
|
||||
|
||||
abstract fun isApplicable(vFile: VirtualFile): Boolean
|
||||
|
||||
|
||||
+9
-11
@@ -22,8 +22,7 @@ import org.jetbrains.kotlin.idea.core.script.configuration.listener.ScriptChange
|
||||
import org.jetbrains.kotlin.idea.core.script.isScriptChangesNotifierDisabled
|
||||
|
||||
internal class ScriptChangesNotifier(
|
||||
private val project: Project,
|
||||
private val updater: ScriptConfigurationUpdater,
|
||||
private val project: Project
|
||||
) {
|
||||
private val scriptsQueue = Alarm(Alarm.ThreadToUse.POOLED_THREAD, project)
|
||||
private val scriptChangesListenerDelay = 1400
|
||||
@@ -46,10 +45,10 @@ internal class ScriptChangesNotifier(
|
||||
|
||||
private fun runScriptDependenciesUpdateIfNeeded(file: VirtualFile) {
|
||||
if (ApplicationManager.getApplication().isUnitTestMode) {
|
||||
getListener(project, file)?.editorActivated(file, updater)
|
||||
getListener(project, file)?.editorActivated(file)
|
||||
} else {
|
||||
AppExecutorUtil.getAppExecutorService().submit {
|
||||
getListener(project, file)?.editorActivated(file, updater)
|
||||
getListener(project, file)?.editorActivated(file)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,11 +67,11 @@ internal class ScriptChangesNotifier(
|
||||
}
|
||||
|
||||
if (ApplicationManager.getApplication().isUnitTestMode) {
|
||||
getListener(project, file)?.documentChanged(file, updater)
|
||||
getListener(project, file)?.documentChanged(file)
|
||||
} else {
|
||||
scriptsQueue.cancelAllRequests()
|
||||
scriptsQueue.addRequest(
|
||||
{ getListener(project, file)?.documentChanged(file, updater) },
|
||||
{ getListener(project, file)?.documentChanged(file) },
|
||||
scriptChangesListenerDelay,
|
||||
true,
|
||||
)
|
||||
@@ -84,11 +83,10 @@ internal class ScriptChangesNotifier(
|
||||
}
|
||||
|
||||
private val defaultListener = DefaultScriptChangeListener(project)
|
||||
private val listeners: Sequence<ScriptChangeListener>
|
||||
get() = sequence {
|
||||
yieldAll(LISTENER.getPoint(project).extensionList)
|
||||
yield(defaultListener)
|
||||
}
|
||||
private val listeners: Collection<ScriptChangeListener> = mutableListOf<ScriptChangeListener>().apply {
|
||||
addAll(LISTENER.getPoint(project).extensionList)
|
||||
add(defaultListener)
|
||||
}
|
||||
|
||||
private fun getListener(project: Project, file: VirtualFile): ScriptChangeListener? {
|
||||
if (project.isDisposed || areListenersDisabled()) return null
|
||||
|
||||
+2
-2
@@ -32,7 +32,7 @@ import java.util.*
|
||||
*/
|
||||
internal class DefaultBackgroundExecutor(
|
||||
val project: Project,
|
||||
val rootsManager: ScriptClassRootsIndexer
|
||||
val rootsManager: ScriptClassRootsUpdater
|
||||
) : BackgroundExecutor {
|
||||
companion object {
|
||||
const val PROGRESS_INDICATOR_DELAY = 1000
|
||||
@@ -137,7 +137,7 @@ internal class DefaultBackgroundExecutor(
|
||||
private fun ensureInTransaction() {
|
||||
if (inTransaction) return
|
||||
inTransaction = true
|
||||
rootsManager.startTransaction()
|
||||
rootsManager.beginUpdating()
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
|
||||
-86
@@ -1,86 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2020 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.core.script.configuration.utils
|
||||
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.projectRoots.Sdk
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.util.containers.ConcurrentFactoryMap
|
||||
import org.jetbrains.kotlin.idea.core.script.ScriptConfigurationManager
|
||||
import org.jetbrains.kotlin.scripting.resolve.ScriptCompilationConfigurationWrapper
|
||||
import java.io.File
|
||||
|
||||
internal class DefaultClassRootsCache(
|
||||
project: Project,
|
||||
private val all: Map<VirtualFile, ScriptCompilationConfigurationWrapper>
|
||||
) : ScriptClassRootsCache(
|
||||
project,
|
||||
ScriptClassRootsStorage.Companion.Key("default"),
|
||||
extractRoots(all, project)
|
||||
) {
|
||||
|
||||
override fun getConfiguration(file: VirtualFile) = all[file]
|
||||
|
||||
override fun contains(file: VirtualFile): Boolean = file in all
|
||||
|
||||
private val scriptsSdksCache: Map<VirtualFile, Sdk?> =
|
||||
ConcurrentFactoryMap.createWeakMap { file ->
|
||||
return@createWeakMap getScriptSdkOrDefault(all[file]?.javaHome, project)
|
||||
}
|
||||
|
||||
override fun getScriptSdk(file: VirtualFile): Sdk? = scriptsSdksCache[file]
|
||||
|
||||
override val firstScriptSdk: Sdk? by lazy {
|
||||
getScriptSdkOrDefault(all.values.firstOrNull()?.javaHome, project)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun extractRoots(
|
||||
project: Project,
|
||||
configuration: ScriptCompilationConfigurationWrapper
|
||||
): ScriptClassRootsStorage.Companion.ScriptClassRoots {
|
||||
val scriptSdk = getScriptSdkOrDefault(configuration.javaHome, project)
|
||||
if (scriptSdk != null && !scriptSdk.isAlreadyIndexed(project)) {
|
||||
return ScriptClassRootsStorage.Companion.ScriptClassRoots(
|
||||
toStringValues(configuration.dependenciesClassPath),
|
||||
toStringValues(configuration.dependenciesSources),
|
||||
setOf(scriptSdk)
|
||||
)
|
||||
}
|
||||
|
||||
return ScriptClassRootsStorage.Companion.ScriptClassRoots(
|
||||
toStringValues(configuration.dependenciesClassPath),
|
||||
toStringValues(configuration.dependenciesSources),
|
||||
emptySet()
|
||||
)
|
||||
}
|
||||
|
||||
fun extractRoots(
|
||||
all: Map<VirtualFile, ScriptCompilationConfigurationWrapper>,
|
||||
project: Project
|
||||
): ScriptClassRootsStorage.Companion.ScriptClassRoots {
|
||||
val classpath = mutableSetOf<File>()
|
||||
val sources = mutableSetOf<File>()
|
||||
val sdks = mutableSetOf<Sdk>()
|
||||
|
||||
for ((_, configuration) in all) {
|
||||
val scriptSdk = getScriptSdkOrDefault(configuration.javaHome, project)
|
||||
if (scriptSdk != null && !scriptSdk.isAlreadyIndexed(project)) {
|
||||
sdks.add(scriptSdk)
|
||||
}
|
||||
|
||||
classpath.addAll(configuration.dependenciesClassPath)
|
||||
sources.addAll(configuration.dependenciesSources)
|
||||
}
|
||||
|
||||
return ScriptClassRootsStorage.Companion.ScriptClassRoots(
|
||||
toStringValues(classpath),
|
||||
toStringValues(sources),
|
||||
sdks
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
+181
-90
@@ -12,100 +12,99 @@ import com.intellij.openapi.projectRoots.Sdk
|
||||
import com.intellij.openapi.roots.ModuleRootManager
|
||||
import com.intellij.openapi.roots.OrderRootType
|
||||
import com.intellij.openapi.roots.ProjectRootManager
|
||||
import com.intellij.openapi.vfs.*
|
||||
import com.intellij.openapi.vfs.VfsUtil
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import com.intellij.psi.search.NonClasspathDirectoriesScope
|
||||
import com.intellij.util.containers.ConcurrentFactoryMap
|
||||
import com.intellij.psi.search.NonClasspathDirectoriesScope.compose
|
||||
import org.jetbrains.kotlin.idea.caches.project.getAllProjectSdks
|
||||
import org.jetbrains.kotlin.idea.core.script.LOG
|
||||
import org.jetbrains.kotlin.idea.core.script.ScriptConfigurationManager
|
||||
import org.jetbrains.kotlin.idea.core.script.ScriptConfigurationManager.Companion.classpathEntryToVfs
|
||||
import org.jetbrains.kotlin.idea.core.script.ScriptConfigurationManager.Companion.toVfsRoots
|
||||
import org.jetbrains.kotlin.idea.util.getProjectJdkTableSafe
|
||||
import org.jetbrains.kotlin.scripting.resolve.ScriptCompilationConfigurationWrapper
|
||||
import java.io.File
|
||||
import java.lang.ref.Reference
|
||||
import java.lang.ref.SoftReference
|
||||
|
||||
abstract class ScriptClassRootsCache(
|
||||
private val project: Project,
|
||||
private val rootsCacheKey: ScriptClassRootsStorage.Companion.Key,
|
||||
private val roots: ScriptClassRootsStorage.Companion.ScriptClassRoots
|
||||
class ScriptClassRootsCache(
|
||||
val scripts: Map<String, LightScriptInfo>,
|
||||
val classes: Set<String>,
|
||||
val sources: Set<String>,
|
||||
val sdks: Map<String?, Sdk?>,
|
||||
private val nonModulesSdks: Collection<Sdk>,
|
||||
val customDefinitionsUsed: Boolean
|
||||
) {
|
||||
protected abstract fun getConfiguration(file: VirtualFile): ScriptCompilationConfigurationWrapper?
|
||||
abstract class LightScriptInfo {
|
||||
@Volatile
|
||||
var heavyCache: Reference<HeavyScriptInfo>? = null
|
||||
|
||||
abstract val firstScriptSdk: Sdk?
|
||||
abstract fun buildConfiguration(): ScriptCompilationConfigurationWrapper?
|
||||
}
|
||||
|
||||
abstract fun getScriptSdk(file: VirtualFile): Sdk?
|
||||
class DirectScriptInfo(val result: ScriptCompilationConfigurationWrapper) : LightScriptInfo() {
|
||||
override fun buildConfiguration(): ScriptCompilationConfigurationWrapper = result
|
||||
}
|
||||
|
||||
abstract fun contains(file: VirtualFile): Boolean
|
||||
|
||||
private class Fat(
|
||||
class HeavyScriptInfo(
|
||||
val scriptConfiguration: ScriptCompilationConfigurationWrapper,
|
||||
val classFilesScope: GlobalSearchScope
|
||||
val classFilesScope: GlobalSearchScope,
|
||||
val sdk: Sdk?
|
||||
)
|
||||
|
||||
val allDependenciesClassFiles by lazy {
|
||||
ScriptClassRootsStorage.getInstance(project).loadClasspathRoots(rootsCacheKey)
|
||||
}
|
||||
class Builder(val project: Project) {
|
||||
val scripts = mutableMapOf<String, LightScriptInfo>()
|
||||
|
||||
val allDependenciesSources by lazy {
|
||||
ScriptClassRootsStorage.getInstance(project).loadSourcesRoots(rootsCacheKey)
|
||||
}
|
||||
private val defaultSdk = getScriptDefaultSdk()
|
||||
val sdks = mutableMapOf<String?, Sdk?>(null to defaultSdk)
|
||||
|
||||
val allDependenciesClassFilesScope by lazy {
|
||||
NonClasspathDirectoriesScope.compose(allDependenciesClassFiles)
|
||||
}
|
||||
val classes = mutableSetOf<String>()
|
||||
val sources = mutableSetOf<String>()
|
||||
private var customDefinitionsUsed: Boolean = false
|
||||
|
||||
val allDependenciesSourcesScope by lazy {
|
||||
NonClasspathDirectoriesScope.compose(allDependenciesSources)
|
||||
}
|
||||
fun build(): ScriptClassRootsCache {
|
||||
val nonIndexedSdks = collectNonIndexedSdks()
|
||||
return ScriptClassRootsCache(scripts, classes, sources, sdks, nonIndexedSdks, customDefinitionsUsed)
|
||||
}
|
||||
|
||||
private val scriptsDependenciesCache: MutableMap<VirtualFile, Fat> =
|
||||
ConcurrentFactoryMap.createWeakMap { file ->
|
||||
val configuration = getConfiguration(file) ?: return@createWeakMap null
|
||||
|
||||
val roots = configuration.dependenciesClassPath
|
||||
val sdk = getScriptSdk(file)
|
||||
|
||||
@Suppress("FoldInitializerAndIfToElvis")
|
||||
if (sdk == null) {
|
||||
return@createWeakMap Fat(
|
||||
configuration,
|
||||
NonClasspathDirectoriesScope.compose(ScriptConfigurationManager.toVfsRoots(roots))
|
||||
)
|
||||
fun collectNonIndexedSdks(): MutableSet<Sdk> {
|
||||
val nonIndexedSdks = sdks.values.filterNotNullTo(mutableSetOf())
|
||||
ModuleManager.getInstance(project).modules.map {
|
||||
nonIndexedSdks.remove(ModuleRootManager.getInstance(it).sdk)
|
||||
}
|
||||
|
||||
return@createWeakMap Fat(
|
||||
configuration,
|
||||
NonClasspathDirectoriesScope.compose(
|
||||
sdk.rootProvider.getFiles(OrderRootType.CLASSES).toList() + ScriptConfigurationManager.toVfsRoots(roots)
|
||||
)
|
||||
)
|
||||
return nonIndexedSdks
|
||||
}
|
||||
|
||||
fun getScriptDependenciesClassFilesScope(file: VirtualFile): GlobalSearchScope {
|
||||
return scriptsDependenciesCache[file]?.classFilesScope ?: GlobalSearchScope.EMPTY_SCOPE
|
||||
}
|
||||
|
||||
fun getScriptConfiguration(file: VirtualFile): ScriptCompilationConfigurationWrapper? {
|
||||
return scriptsDependenciesCache[file]?.scriptConfiguration
|
||||
}
|
||||
|
||||
fun hasNotCachedRoots(roots: ScriptClassRootsStorage.Companion.ScriptClassRoots): Boolean {
|
||||
return !ScriptClassRootsStorage.getInstance(project).containsAll(rootsCacheKey, roots)
|
||||
}
|
||||
|
||||
fun saveClassRootsToStorage() {
|
||||
val rootsStorage = ScriptClassRootsStorage.getInstance(project)
|
||||
if (roots.classpathFiles.isNotEmpty() || roots.sourcesFiles.isNotEmpty() || roots.sdks.isNotEmpty()) {
|
||||
rootsStorage.save(rootsCacheKey, roots)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun toStringValues(prop: Collection<File>): Set<String> {
|
||||
return prop.mapNotNull { it.absolutePath }.toSet()
|
||||
fun useCustomScriptDefinition() {
|
||||
customDefinitionsUsed = true
|
||||
}
|
||||
|
||||
fun getScriptSdkOrDefault(javaHome: File?, project: Project): Sdk? {
|
||||
return javaHome?.let { getScriptSdkByJavaHome(it) } ?: getScriptDefaultSdk(project)
|
||||
fun add(
|
||||
vFile: VirtualFile,
|
||||
configuration: ScriptCompilationConfigurationWrapper
|
||||
) {
|
||||
addSdk(configuration.javaHome)
|
||||
|
||||
configuration.dependenciesClassPath.forEach { classes.add(it.absolutePath) }
|
||||
configuration.dependenciesSources.forEach { sources.add(it.absolutePath) }
|
||||
|
||||
scripts[vFile.path] = DirectScriptInfo(configuration)
|
||||
|
||||
useCustomScriptDefinition()
|
||||
}
|
||||
|
||||
fun add(other: Builder) {
|
||||
classes.addAll(other.classes)
|
||||
sources.addAll(other.sources)
|
||||
sdks.putAll(other.sdks)
|
||||
scripts.putAll(other.scripts)
|
||||
}
|
||||
|
||||
fun addSdk(javaHome: File?): Sdk? {
|
||||
if (javaHome == null) return defaultSdk
|
||||
val canonicalPath = javaHome.canonicalPath
|
||||
return sdks.getOrPut(canonicalPath) {
|
||||
getScriptSdkByJavaHome(javaHome) ?: defaultSdk
|
||||
}
|
||||
}
|
||||
|
||||
private fun getScriptSdkByJavaHome(javaHome: File): Sdk? {
|
||||
@@ -119,7 +118,13 @@ abstract class ScriptClassRootsCache(
|
||||
return getProjectJdkTableSafe().allJdks.find { it.homeDirectory == javaHomeVF }
|
||||
}
|
||||
|
||||
private fun getScriptDefaultSdk(project: Project): Sdk? {
|
||||
fun addSdkByName(sdkName: String) {
|
||||
val sdk = getProjectJdkTableSafe().allJdks.find { it.name == sdkName } ?: defaultSdk ?: return
|
||||
val homePath = sdk.homePath ?: return
|
||||
sdks[homePath] = sdk
|
||||
}
|
||||
|
||||
private fun getScriptDefaultSdk(): Sdk? {
|
||||
val projectSdk = ProjectRootManager.getInstance(project).projectSdk?.takeIf { it.canBeUsedForScript() }
|
||||
if (projectSdk != null) return projectSdk
|
||||
|
||||
@@ -133,32 +138,118 @@ abstract class ScriptClassRootsCache(
|
||||
"projectSdk = ${ProjectRootManager.getInstance(project).projectSdk}, " +
|
||||
"all sdks = ${getAllProjectSdks().joinToString("\n")}"
|
||||
)
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
private fun Sdk.canBeUsedForScript() = sdkType is JavaSdkType
|
||||
}
|
||||
|
||||
fun Sdk.isAlreadyIndexed(project: Project): Boolean {
|
||||
return ModuleManager.getInstance(project).modules.any { ModuleRootManager.getInstance(it).sdk == this }
|
||||
fun getLightScriptInfo(file: String) = scripts[file]
|
||||
|
||||
fun contains(file: VirtualFile): Boolean = file.path in scripts
|
||||
|
||||
private fun getHeavyScriptInfo(file: String): HeavyScriptInfo? {
|
||||
val lightScriptInfo = getLightScriptInfo(file) ?: return null
|
||||
val heavy0 = lightScriptInfo.heavyCache?.get()
|
||||
if (heavy0 != null) return heavy0
|
||||
synchronized(lightScriptInfo) {
|
||||
val heavy1 = lightScriptInfo.heavyCache?.get()
|
||||
if (heavy1 != null) return heavy1
|
||||
val heavy2 = computeHeavy(lightScriptInfo)
|
||||
lightScriptInfo.heavyCache = SoftReference(heavy2)
|
||||
return heavy2
|
||||
}
|
||||
}
|
||||
|
||||
private fun computeHeavy(lightScriptInfo: LightScriptInfo): HeavyScriptInfo? {
|
||||
val configuration = lightScriptInfo.buildConfiguration() ?: return null
|
||||
|
||||
val roots = configuration.dependenciesClassPath
|
||||
val sdk = sdks[configuration.javaHome?.canonicalPath]
|
||||
|
||||
return if (sdk == null) {
|
||||
HeavyScriptInfo(configuration, compose(toVfsRoots(roots)), null)
|
||||
} else {
|
||||
val sdkClasses = sdk.rootProvider.getFiles(OrderRootType.CLASSES).toList()
|
||||
HeavyScriptInfo(configuration, compose(sdkClasses + toVfsRoots(roots)), sdk)
|
||||
}
|
||||
}
|
||||
|
||||
val firstScriptSdk: Sdk? = sdks.values.firstOrNull()
|
||||
|
||||
val allDependenciesClassFiles: List<VirtualFile>
|
||||
|
||||
val allDependenciesSources: List<VirtualFile>
|
||||
|
||||
init {
|
||||
allDependenciesClassFiles = mutableSetOf<VirtualFile>().also { result ->
|
||||
nonModulesSdks.forEach { result.addAll(it.rootProvider.getFiles(OrderRootType.CLASSES)) }
|
||||
classes.mapNotNullTo(result) { classpathEntryToVfs(File(it)) }
|
||||
}.toList()
|
||||
|
||||
allDependenciesSources = mutableSetOf<VirtualFile>().also { result ->
|
||||
nonModulesSdks.forEach { result.addAll(it.rootProvider.getFiles(OrderRootType.SOURCES)) }
|
||||
sources.mapNotNullTo(result) { classpathEntryToVfs(File(it)) }
|
||||
}.toList()
|
||||
}
|
||||
|
||||
val allDependenciesClassFilesScope = compose(allDependenciesClassFiles)
|
||||
|
||||
val allDependenciesSourcesScope = compose(allDependenciesSources)
|
||||
|
||||
fun getScriptConfiguration(file: VirtualFile): ScriptCompilationConfigurationWrapper? =
|
||||
getHeavyScriptInfo(file.path)?.scriptConfiguration
|
||||
|
||||
fun getScriptSdk(file: VirtualFile): Sdk? =
|
||||
getHeavyScriptInfo(file.path)?.sdk
|
||||
|
||||
fun getScriptDependenciesClassFilesScope(file: VirtualFile): GlobalSearchScope =
|
||||
getHeavyScriptInfo(file.path)?.classFilesScope ?: GlobalSearchScope.EMPTY_SCOPE
|
||||
|
||||
fun hasInvalidSdk(project: Project): Boolean {
|
||||
val builder = Builder(project)
|
||||
if (sdks.any { (home, sdk) -> builder.addSdk(home?.let(::File)) != sdk }) return true
|
||||
if (builder.collectNonIndexedSdks() != nonModulesSdks) return true
|
||||
return false
|
||||
}
|
||||
|
||||
fun diff(old: ScriptClassRootsCache): Updates =
|
||||
Updates(
|
||||
hasNewRoots(old),
|
||||
getChangedScripts(old)
|
||||
)
|
||||
|
||||
private fun hasNewRoots(old: ScriptClassRootsCache): Boolean {
|
||||
return classes.any { it !in old.classes }
|
||||
|| sources.any { it !in old.sources }
|
||||
|| sdks.any { it.key !in old.sdks }
|
||||
}
|
||||
|
||||
private fun getChangedScripts(old: ScriptClassRootsCache): Set<String> {
|
||||
val changed = mutableSetOf<String>()
|
||||
|
||||
scripts.forEach {
|
||||
if (old.scripts[it.key] != it.value) {
|
||||
changed.add(it.key)
|
||||
}
|
||||
}
|
||||
|
||||
fun empty(project: Project) = object : ScriptClassRootsCache(
|
||||
project,
|
||||
ScriptClassRootsStorage.Companion.Key("empty"),
|
||||
ScriptClassRootsStorage.Companion.ScriptClassRoots(
|
||||
setOf(),
|
||||
setOf(),
|
||||
setOf()
|
||||
)
|
||||
) {
|
||||
override fun getConfiguration(file: VirtualFile): ScriptCompilationConfigurationWrapper? = null
|
||||
|
||||
override val firstScriptSdk: Sdk? = null
|
||||
|
||||
override fun getScriptSdk(file: VirtualFile): Sdk? = null
|
||||
|
||||
override fun contains(file: VirtualFile): Boolean = true
|
||||
old.scripts.forEach {
|
||||
if (it.key !in scripts) {
|
||||
changed.add(it.key)
|
||||
}
|
||||
}
|
||||
|
||||
return changed
|
||||
}
|
||||
|
||||
class Updates(
|
||||
val hasNewRoots: Boolean,
|
||||
val updatedScripts: Set<String>
|
||||
) {
|
||||
val changed: Boolean
|
||||
get() = hasNewRoots || updatedScripts.isNotEmpty()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
-80
@@ -1,80 +0,0 @@
|
||||
/*
|
||||
* 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.idea.core.script.configuration.utils
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.openapi.application.TransactionGuard
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.roots.ex.ProjectRootManagerEx
|
||||
import com.intellij.openapi.util.EmptyRunnable
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import org.jetbrains.kotlin.idea.core.script.ScriptDependenciesModificationTracker
|
||||
import org.jetbrains.kotlin.idea.core.script.debug
|
||||
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
||||
import org.jetbrains.kotlin.scripting.resolve.ScriptCompilationConfigurationWrapper
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
/**
|
||||
* Utility for postponing indexing of new roots to the end of some bulk operation.
|
||||
*/
|
||||
class ScriptClassRootsIndexer(val project: Project) {
|
||||
private var newRootsPresent: Boolean = false
|
||||
private val concurrentTransactions = AtomicInteger()
|
||||
|
||||
@Synchronized
|
||||
fun markNewRoot() {
|
||||
checkInTransaction()
|
||||
newRootsPresent = true
|
||||
}
|
||||
|
||||
fun checkInTransaction() {
|
||||
check(concurrentTransactions.get() > 0)
|
||||
}
|
||||
|
||||
inline fun <T> transaction(body: () -> T): T {
|
||||
startTransaction()
|
||||
return try {
|
||||
body()
|
||||
} finally {
|
||||
commit()
|
||||
}
|
||||
}
|
||||
|
||||
fun startTransaction() {
|
||||
concurrentTransactions.incrementAndGet()
|
||||
}
|
||||
|
||||
fun commit() {
|
||||
concurrentTransactions.decrementAndGet()
|
||||
|
||||
// run indexing even in inner transaction
|
||||
// (outer transaction may be async, so it would be better to not wait it)
|
||||
startIndexingIfNeeded()
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
private fun startIndexingIfNeeded() {
|
||||
if (!newRootsPresent) return
|
||||
newRootsPresent = false
|
||||
|
||||
val doNotifyRootsChanged = Runnable {
|
||||
runWriteAction {
|
||||
if (project.isDisposed) return@runWriteAction
|
||||
|
||||
debug { "roots change event" }
|
||||
|
||||
ProjectRootManagerEx.getInstanceEx(project)?.makeRootsChange(EmptyRunnable.getInstance(), false, true)
|
||||
ScriptDependenciesModificationTracker.getInstance(project).incModificationCount()
|
||||
}
|
||||
}
|
||||
|
||||
if (ApplicationManager.getApplication().isUnitTestMode) {
|
||||
TransactionGuard.submitTransaction(project, doNotifyRootsChanged)
|
||||
} else {
|
||||
TransactionGuard.getInstance().submitTransactionLater(project, doNotifyRootsChanged)
|
||||
}
|
||||
}
|
||||
}
|
||||
+24
-122
@@ -7,144 +7,46 @@ package org.jetbrains.kotlin.idea.core.script.configuration.utils
|
||||
|
||||
import com.intellij.openapi.components.*
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.projectRoots.ProjectJdkTable
|
||||
import com.intellij.openapi.projectRoots.Sdk
|
||||
import com.intellij.openapi.roots.OrderRootType
|
||||
import com.intellij.openapi.vfs.JarFileSystem
|
||||
import com.intellij.openapi.vfs.StandardFileSystems
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import org.jdom.Element
|
||||
import org.jetbrains.kotlin.idea.core.script.debug
|
||||
import com.intellij.util.xmlb.XmlSerializerUtil
|
||||
|
||||
/**
|
||||
* Actually, this storage is required only for before Gradle 6.0.
|
||||
* For projects starting from Gradle 6.0, [org.jetbrains.kotlin.idea.scripting.gradle.GradleScriptingSupportProvider]
|
||||
* will also store all roots in FS, and will overwrite this one shrotly after start.
|
||||
*
|
||||
* Btw, this is still useful for projects without custom scripting support.
|
||||
*/
|
||||
@State(
|
||||
name = "ScriptClassRootsStorage",
|
||||
storages = [Storage(StoragePathMacros.CACHE_FILE)]
|
||||
)
|
||||
class ScriptClassRootsStorage : PersistentStateComponent<Element> {
|
||||
class ScriptClassRootsStorage(val project: Project) : PersistentStateComponent<ScriptClassRootsStorage> {
|
||||
private var classpath: Set<String> = hashSetOf()
|
||||
private var sources: Set<String> = hashSetOf()
|
||||
private var sdks: Set<String> = hashSetOf()
|
||||
|
||||
private var classpath: MutableMap<String, Set<String>> = hashMapOf()
|
||||
private var sources: MutableMap<String, Set<String>> = hashMapOf()
|
||||
private var sdks: MutableMap<String, Set<String>> = hashMapOf()
|
||||
|
||||
override fun getState(): Element {
|
||||
val root = Element("ScriptClassRootsStorage")
|
||||
|
||||
storeCollection(root, classpath, "classpath")
|
||||
storeCollection(root, sources, "sources")
|
||||
storeCollection(root, sdks, "sdk")
|
||||
|
||||
return root
|
||||
override fun getState(): ScriptClassRootsStorage? {
|
||||
return this
|
||||
}
|
||||
|
||||
private fun storeCollection(root: Element, col: Map<String, Set<String>>, name: String) {
|
||||
for ((key, paths) in col) {
|
||||
for (path in paths) {
|
||||
val element = Element(name)
|
||||
element.setAttribute("path", path)
|
||||
element.setAttribute("key", key)
|
||||
root.addContent(element)
|
||||
}
|
||||
}
|
||||
override fun loadState(state: ScriptClassRootsStorage) {
|
||||
XmlSerializerUtil.copyBean(state, this)
|
||||
}
|
||||
|
||||
override fun loadState(state: Element) {
|
||||
classpath = readCollection(state, "classpath")
|
||||
sources = readCollection(state, "sources")
|
||||
sdks = readCollection(state, "sdks")
|
||||
fun save(roots: ScriptClassRootsCache.Builder) {
|
||||
classpath = roots.classes
|
||||
sources = roots.sources
|
||||
sdks = roots.sdks.values.mapNotNullTo(mutableSetOf()) { it?.name }
|
||||
}
|
||||
|
||||
private fun readCollection(root: Element, name: String): MutableMap<String, Set<String>> {
|
||||
val result: MutableMap<String, HashSet<String>> = hashMapOf()
|
||||
for (it in root.getChildren(name)) {
|
||||
result.getOrPut(it.getAttributeValue("key")) {
|
||||
hashSetOf()
|
||||
}.add(it.getAttributeValue("path"))
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return result as MutableMap<String, Set<String>>
|
||||
}
|
||||
|
||||
private fun toStringNames(sdks: Collection<Sdk>): Set<String> {
|
||||
return sdks.map { it.name }.toSet()
|
||||
}
|
||||
|
||||
private fun toVirtualFiles(prop: Set<String>?, sources: Boolean): List<VirtualFile> {
|
||||
if (prop == null) return emptyList()
|
||||
|
||||
val rootType = if (sources) OrderRootType.SOURCES else OrderRootType.CLASSES
|
||||
return prop.mapNotNull { ProjectJdkTable.getInstance().findJdk(it) }
|
||||
.flatMap { it.rootProvider.getFiles(rootType).toList() }
|
||||
}
|
||||
|
||||
private fun toVirtualFiles(prop: Collection<String>?): List<VirtualFile> {
|
||||
if (prop == null) return emptyList()
|
||||
|
||||
return prop.mapNotNull {
|
||||
if (it.endsWith(JarFileSystem.PROTOCOL)) {
|
||||
StandardFileSystems.jar()?.findFileByPath(it + JarFileSystem.JAR_SEPARATOR)?.let {
|
||||
return@mapNotNull it
|
||||
}
|
||||
}
|
||||
|
||||
StandardFileSystems.local()?.findFileByPath(it)?.let {
|
||||
return@mapNotNull it
|
||||
}
|
||||
|
||||
// TODO: report this somewhere, but do not throw: assert(res != null, { "Invalid classpath entry '$this': exists: ${exists()}, is directory: $isDirectory, is file: $isFile" })
|
||||
|
||||
null
|
||||
}.distinct()
|
||||
}
|
||||
|
||||
fun containsAll(key: Key, configuration: ScriptClassRoots): Boolean {
|
||||
if (configuration.classpathFiles.isNotEmpty() && classpath[key.value]?.containsAll(configuration.classpathFiles) != true) {
|
||||
debug { "class roots were changed: old = $classpath, new = ${configuration.classpathFiles}" }
|
||||
return false
|
||||
}
|
||||
if (configuration.sourcesFiles.isNotEmpty() && sources[key.value]?.containsAll(configuration.sourcesFiles) != true) {
|
||||
debug { "source roots were changed: old = $sources, new = ${configuration.sourcesFiles}" }
|
||||
return false
|
||||
}
|
||||
if (configuration.sdks.isNotEmpty() && sdks[key.value]?.containsAll(toStringNames(configuration.sdks)) != true) {
|
||||
debug { "sdk classes were changed: old = $sdks, new = ${configuration.sdks.map { it.homePath }}" }
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fun save(key: Key, configuration: ScriptClassRoots) {
|
||||
// TODO: do not drop all storage on save: KT-34444
|
||||
classpath.getOrPut(key.value, { emptySet() })
|
||||
classpath.replace(key.value, configuration.classpathFiles)
|
||||
|
||||
sources.getOrPut(key.value, { emptySet() })
|
||||
sources.replace(key.value, configuration.sourcesFiles)
|
||||
|
||||
sdks.getOrPut(key.value, { emptySet() })
|
||||
sdks.replace(key.value, toStringNames(configuration.sdks))
|
||||
}
|
||||
|
||||
fun loadClasspathRoots(key: Key): List<VirtualFile> {
|
||||
return toVirtualFiles(sdks[key.value], false) + toVirtualFiles(classpath[key.value])
|
||||
}
|
||||
|
||||
fun loadSourcesRoots(key: Key): List<VirtualFile> {
|
||||
return toVirtualFiles(sdks[key.value], true) + toVirtualFiles(sources[key.value])
|
||||
fun load(builder: ScriptClassRootsCache.Builder) {
|
||||
builder.sources.addAll(sources)
|
||||
builder.classes.addAll(classpath)
|
||||
sdks.forEach(builder::addSdkByName)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun getInstance(project: Project): ScriptClassRootsStorage =
|
||||
ServiceManager.getService(project, ScriptClassRootsStorage::class.java)
|
||||
|
||||
data class ScriptClassRoots(
|
||||
val classpathFiles: Set<String>,
|
||||
val sourcesFiles: Set<String>,
|
||||
val sdks: Set<Sdk>
|
||||
)
|
||||
|
||||
val EMPTY = ScriptClassRoots(emptySet(), emptySet(), emptySet())
|
||||
|
||||
data class Key(val value: String)
|
||||
}
|
||||
}
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* 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.idea.core.script.configuration.utils
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.openapi.application.TransactionGuard
|
||||
import com.intellij.openapi.progress.ProgressIndicator
|
||||
import com.intellij.openapi.progress.ProgressManager
|
||||
import com.intellij.openapi.progress.util.BackgroundTaskUtil
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.roots.ex.ProjectRootManagerEx
|
||||
import com.intellij.openapi.util.EmptyRunnable
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.psi.PsiElementFinder
|
||||
import org.jetbrains.kotlin.idea.core.script.KotlinScriptDependenciesClassFinder
|
||||
import org.jetbrains.kotlin.idea.core.script.ScriptDependenciesModificationTracker
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.CompositeScriptConfigurationManager
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.ScriptingSupportHelper
|
||||
import org.jetbrains.kotlin.idea.core.script.debug
|
||||
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
/**
|
||||
* Utility for postponing indexing of new roots to the end of some bulk operation.
|
||||
*/
|
||||
class ScriptClassRootsUpdater(
|
||||
val project: Project,
|
||||
val manager: CompositeScriptConfigurationManager
|
||||
) {
|
||||
private var invalidated: Boolean = false
|
||||
private val concurrentUpdates = AtomicInteger()
|
||||
|
||||
@Synchronized
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
fun invalidate(file: VirtualFile) {
|
||||
// todo: record invalided files for some optimisations in update
|
||||
invalidate()
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun invalidate() {
|
||||
checkInTransaction()
|
||||
invalidated = true
|
||||
}
|
||||
|
||||
fun checkInTransaction() {
|
||||
check(concurrentUpdates.get() > 0)
|
||||
}
|
||||
|
||||
inline fun <T> update(body: () -> T): T {
|
||||
beginUpdating()
|
||||
return try {
|
||||
body()
|
||||
} finally {
|
||||
commit()
|
||||
}
|
||||
}
|
||||
|
||||
fun beginUpdating() {
|
||||
concurrentUpdates.incrementAndGet()
|
||||
}
|
||||
|
||||
fun commit() {
|
||||
concurrentUpdates.decrementAndGet()
|
||||
|
||||
// run update even in inner transaction
|
||||
// (outer transaction may be async, so it would be better to not wait it)
|
||||
scheduleUpdateIfInvalid()
|
||||
}
|
||||
|
||||
private fun scheduleUpdateIfInvalid() {
|
||||
if (!invalidated) return
|
||||
invalidated = false
|
||||
|
||||
ensureUpdateScheduled()
|
||||
}
|
||||
|
||||
private var scheduledUpdate: ProgressIndicator? = null
|
||||
|
||||
@Synchronized
|
||||
fun ensureUpdateScheduled() {
|
||||
scheduledUpdate?.cancel()
|
||||
scheduledUpdate = BackgroundTaskUtil.executeOnPooledThread(project) {
|
||||
doUpdate()
|
||||
}
|
||||
}
|
||||
|
||||
fun doUpdate() {
|
||||
val updates = manager.collectRootsAndCheckNew()
|
||||
|
||||
if (!updates.changed) return
|
||||
|
||||
ProgressManager.checkCanceled()
|
||||
|
||||
if (updates.hasNewRoots) {
|
||||
notifyRootsChanged()
|
||||
}
|
||||
|
||||
PsiElementFinder.EP.findExtensionOrFail(KotlinScriptDependenciesClassFinder::class.java, project)
|
||||
.clearCache()
|
||||
|
||||
ScriptDependenciesModificationTracker.getInstance(project).incModificationCount()
|
||||
|
||||
if (updates.updatedScripts.isNotEmpty()) {
|
||||
ScriptingSupportHelper.updateHighlighting(project) {
|
||||
it.path in updates.updatedScripts
|
||||
}
|
||||
}
|
||||
|
||||
synchronized(this) {
|
||||
scheduledUpdate = null
|
||||
}
|
||||
}
|
||||
|
||||
private fun notifyRootsChanged() {
|
||||
val doNotifyRootsChanged = Runnable {
|
||||
runWriteAction {
|
||||
if (project.isDisposed) return@runWriteAction
|
||||
|
||||
debug { "roots change event" }
|
||||
|
||||
ProjectRootManagerEx.getInstanceEx(project)?.makeRootsChange(EmptyRunnable.getInstance(), false, true)
|
||||
ScriptDependenciesModificationTracker.getInstance(project).incModificationCount()
|
||||
}
|
||||
}
|
||||
|
||||
if (ApplicationManager.getApplication().isUnitTestMode) {
|
||||
TransactionGuard.submitTransaction(project, doNotifyRootsChanged)
|
||||
} else {
|
||||
TransactionGuard.getInstance().submitTransactionLater(project, doNotifyRootsChanged)
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -10,7 +10,7 @@ import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.util.containers.HashSetQueue
|
||||
|
||||
class TestingBackgroundExecutor internal constructor(
|
||||
private val rootsManager: ScriptClassRootsIndexer
|
||||
private val rootsManager: ScriptClassRootsUpdater
|
||||
) : BackgroundExecutor {
|
||||
val backgroundQueue = HashSetQueue<BackgroundTask>()
|
||||
|
||||
@@ -42,7 +42,7 @@ class TestingBackgroundExecutor internal constructor(
|
||||
|
||||
actions()
|
||||
|
||||
rootsManager.transaction {
|
||||
rootsManager.update {
|
||||
copy.forEach {
|
||||
it.actions()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user