KotlinScriptConfigurationManager: perform initial caching using StartupManager
This commit is contained in:
+36
-22
@@ -21,6 +21,7 @@ import com.intellij.openapi.components.ServiceManager
|
|||||||
import com.intellij.openapi.project.DumbService
|
import com.intellij.openapi.project.DumbService
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.roots.ex.ProjectRootManagerEx
|
import com.intellij.openapi.roots.ex.ProjectRootManagerEx
|
||||||
|
import com.intellij.openapi.startup.StartupManager
|
||||||
import com.intellij.openapi.util.EmptyRunnable
|
import com.intellij.openapi.util.EmptyRunnable
|
||||||
import com.intellij.openapi.vfs.StandardFileSystems
|
import com.intellij.openapi.vfs.StandardFileSystems
|
||||||
import com.intellij.openapi.vfs.VirtualFile
|
import com.intellij.openapi.vfs.VirtualFile
|
||||||
@@ -55,24 +56,20 @@ class KotlinScriptConfigurationManager(
|
|||||||
init {
|
init {
|
||||||
reloadScriptDefinitions()
|
reloadScriptDefinitions()
|
||||||
|
|
||||||
dumbService.runWhenSmart {
|
StartupManager.getInstance(project).registerPostStartupActivity {
|
||||||
// TODO: sort out read/write action business and if possible make it lazy (e.g. move to getAllScriptsClasspath)
|
cacheAllScriptsExtraImports()
|
||||||
runReadAction { cacheAllScriptsExtraImports() }
|
invalidateLocalCaches()
|
||||||
|
notifyRootsChanged()
|
||||||
project.messageBus.connect().subscribe(VirtualFileManager.VFS_CHANGES, object : BulkFileListener.Adapter() {
|
|
||||||
override fun after(events: List<VFileEvent>) {
|
|
||||||
val isChanged = scriptExternalImportsProvider?.updateExternalImportsCache(events.mapNotNull { it.file })?.any() ?: false
|
|
||||||
if (isChanged) {
|
|
||||||
// TODO: consider more fine-grained update
|
|
||||||
cacheLock.write {
|
|
||||||
allScriptsClasspathCache = null
|
|
||||||
allLibrarySourcesCache = null
|
|
||||||
}
|
|
||||||
notifyRootsChanged()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
project.messageBus.connect().subscribe(VirtualFileManager.VFS_CHANGES, object : BulkFileListener.Adapter() {
|
||||||
|
override fun after(events: List<VFileEvent>) {
|
||||||
|
updateExternalImportsCache(events.mapNotNull { it.file }) {
|
||||||
|
invalidateLocalCaches()
|
||||||
|
notifyRootsChanged()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private var allScriptsClasspathCache: List<VirtualFile>? = null
|
private var allScriptsClasspathCache: List<VirtualFile>? = null
|
||||||
@@ -152,14 +149,31 @@ class KotlinScriptConfigurationManager(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun cacheAllScriptsExtraImports() {
|
private fun cacheAllScriptsExtraImports() {
|
||||||
scriptExternalImportsProvider?.apply {
|
runReadAction {
|
||||||
invalidateCaches()
|
scriptExternalImportsProvider?.apply {
|
||||||
cacheExternalImports(
|
invalidateCaches()
|
||||||
scriptDefinitionProvider.getAllKnownFileTypes()
|
cacheExternalImports(
|
||||||
.flatMap { FileTypeIndex.getFiles(it, GlobalSearchScope.allScope(project)) })
|
scriptDefinitionProvider.getAllKnownFileTypes()
|
||||||
|
.flatMap { FileTypeIndex.getFiles(it, GlobalSearchScope.allScope(project)) })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun updateExternalImportsCache(files: Iterable<VirtualFile>, onChange: () -> Unit) {
|
||||||
|
val isChanged = scriptExternalImportsProvider?.updateExternalImportsCache(files)?.any() ?: false
|
||||||
|
if (isChanged) {
|
||||||
|
onChange()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun invalidateLocalCaches() {
|
||||||
|
cacheLock.write {
|
||||||
|
allScriptsClasspathCache = null
|
||||||
|
allLibrarySourcesCache = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun getInstance(project: Project): KotlinScriptConfigurationManager =
|
fun getInstance(project: Project): KotlinScriptConfigurationManager =
|
||||||
|
|||||||
Reference in New Issue
Block a user