Add script dependencies modification tracker, some optimizations

This commit is contained in:
Ilya Chernikov
2016-10-26 15:29:27 +02:00
parent 3382ab3d9c
commit 2e03a6f1fd
7 changed files with 95 additions and 44 deletions
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea.core.script
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.DumbService
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ex.ProjectRootManagerEx
import com.intellij.openapi.startup.StartupManager
@@ -47,6 +48,7 @@ import kotlin.concurrent.write
@Suppress("SimplifyAssertNotNull")
class KotlinScriptConfigurationManager(
private val project: Project,
private val dumbService: DumbService,
private val scriptDefinitionProvider: KotlinScriptDefinitionProvider,
private val scriptExternalImportsProvider: KotlinScriptExternalImportsProvider
) {
@@ -76,14 +78,27 @@ class KotlinScriptConfigurationManager(
toVfsRoots(scriptExternalImportsProvider.getKnownCombinedClasspath().distinct())
}
private val allScriptsClasspathScope = ClearableLazyValue(cacheLock) {
NonClasspathDirectoriesScope(getAllScriptsClasspath())
}
private val allLibrarySourcesCache = ClearableLazyValue(cacheLock) {
toVfsRoots(scriptExternalImportsProvider.getKnownSourceRoots().distinct())
}
private val allLibrarySourcesScope = ClearableLazyValue(cacheLock) {
NonClasspathDirectoriesScope(getAllLibrarySources())
}
private fun notifyRootsChanged() {
// TODO: it seems invokeLater leads to inconsistent behaviour (at least in tests)
ApplicationManager.getApplication().invokeLater {
runWriteAction { ProjectRootManagerEx.getInstanceEx(project)?.makeRootsChange(EmptyRunnable.getInstance(), false, true) }
runWriteAction {
ProjectRootManagerEx.getInstanceEx(project)?.makeRootsChange(EmptyRunnable.getInstance(), false, true)
dumbService.runWhenSmart {
ScriptDependenciesModificationTracker.getInstance(project).incModificationCount()
}
}
}
}
@@ -95,9 +110,9 @@ class KotlinScriptConfigurationManager(
fun getAllLibrarySources(): List<VirtualFile> = allLibrarySourcesCache.get()
fun getAllScriptsClasspathScope() = NonClasspathDirectoriesScope(getAllScriptsClasspath())
fun getAllScriptsClasspathScope() = allScriptsClasspathScope.get()
fun getAllLibrarySourcesScope() = NonClasspathDirectoriesScope(getAllLibrarySources())
fun getAllLibrarySourcesScope() = allLibrarySourcesScope.get()
private fun reloadScriptDefinitions() {
makeScriptDefsFromTemplatesProviderExtensions(project, { ep, ex -> log.warn("[kts] Error loading definition from ${ep.id}", ex) }).let {
@@ -127,7 +142,9 @@ class KotlinScriptConfigurationManager(
private fun invalidateLocalCaches() {
allScriptsClasspathCache.clear()
allScriptsClasspathScope.clear()
allLibrarySourcesCache.clear()
allLibrarySourcesScope.clear()
}
@@ -26,7 +26,6 @@ import com.intellij.psi.search.EverythingGlobalScope
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.util.containers.ConcurrentFactoryMap
import org.jetbrains.kotlin.idea.caches.resolve.ScriptModuleSearchScope
import org.jetbrains.kotlin.idea.core.script.KotlinScriptConfigurationManager
import org.jetbrains.kotlin.load.java.JavaClassFinderImpl
import org.jetbrains.kotlin.resolve.jvm.KotlinSafeClassFinder
@@ -34,22 +33,23 @@ class KotlinScriptDependenciesClassFinder(project: Project,
private val kotlinScriptConfigurationManager: KotlinScriptConfigurationManager
) : NonClasspathClassFinder(project), KotlinSafeClassFinder {
private val myCaches = object : ConcurrentFactoryMap<VirtualFile, PackageDirectoryCache>() {
override fun create(file: VirtualFile): PackageDirectoryCache? {
val scriptClasspath = kotlinScriptConfigurationManager.getScriptClasspath(file)
val v = createCache(scriptClasspath)
return v
private val myCaches by lazy {
object : ConcurrentFactoryMap<VirtualFile, PackageDirectoryCache>() {
override fun create(file: VirtualFile): PackageDirectoryCache? {
val scriptClasspath = kotlinScriptConfigurationManager.getScriptClasspath(file)
val v = createCache(scriptClasspath)
return v
}
}
}
override fun calcClassRoots(): List<VirtualFile> = kotlinScriptConfigurationManager.getAllScriptsClasspath()
override fun calcClassRoots(): List<VirtualFile> = kotlinScriptConfigurationManager.getAllScriptsClasspath().toList()
override fun getCache(scope: GlobalSearchScope?): PackageDirectoryCache =
(scope as? ScriptModuleSearchScope ?:
(scope as? JavaClassFinderImpl.FilterOutKotlinSourceFilesScope)?.base as? ScriptModuleSearchScope
)?.let {
myCaches.get(it.scriptFile)
myCaches[it.scriptFile]
} ?: super.getCache(scope)
override fun clearCache() {
@@ -0,0 +1,30 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.core.script
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.SimpleModificationTracker
class ScriptDependenciesModificationTracker(): SimpleModificationTracker() {
companion object {
@JvmStatic
fun getInstance(project: Project): ScriptDependenciesModificationTracker =
ServiceManager.getService(project, ScriptDependenciesModificationTracker::class.java)!!
}
}