Add script dependencies modification tracker, some optimizations
This commit is contained in:
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.parsing.KotlinParserDefinition
|
||||
import org.jetbrains.kotlin.psi.KtScript
|
||||
import java.io.File
|
||||
import kotlin.comparisons.compareValues
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.script.templates.standard.ScriptTemplateWithArgs
|
||||
|
||||
@@ -42,13 +43,33 @@ open class KotlinScriptDefinition(val template: KClass<out Any>) {
|
||||
open fun <TF> getDependenciesFor(file: TF, project: Project, previousDependencies: KotlinScriptExternalDependencies?): KotlinScriptExternalDependencies? = null
|
||||
}
|
||||
|
||||
interface KotlinScriptExternalDependencies {
|
||||
interface KotlinScriptExternalDependencies : Comparable<KotlinScriptExternalDependencies> {
|
||||
val javaHome: String? get() = null
|
||||
val classpath: Iterable<File> get() = emptyList()
|
||||
val imports: Iterable<String> get() = emptyList()
|
||||
val sources: Iterable<File> get() = emptyList()
|
||||
val scripts: Iterable<File> get() = emptyList()
|
||||
|
||||
override fun compareTo(other: KotlinScriptExternalDependencies): Int =
|
||||
compareValues(javaHome, other.javaHome)
|
||||
.chainCompare { compareIterables(classpath, other.classpath) }
|
||||
.chainCompare { compareIterables(imports, other.imports) }
|
||||
.chainCompare { compareIterables(sources, other.sources) }
|
||||
.chainCompare { compareIterables(scripts, other.scripts) }
|
||||
}
|
||||
|
||||
object StandardScriptDefinition : KotlinScriptDefinition(ScriptTemplateWithArgs::class)
|
||||
|
||||
private fun<T: Comparable<T>> compareIterables(a: Iterable<T>, b: Iterable<T>): Int {
|
||||
val ia = a.iterator()
|
||||
val ib = b.iterator()
|
||||
while (true) {
|
||||
if (ia.hasNext() && !ib.hasNext()) return 1
|
||||
if (!ia.hasNext() && !ib.hasNext()) return 0
|
||||
if (!ia.hasNext()) return -1
|
||||
val compRes = compareValues(ia.next(), ib.next())
|
||||
if (compRes != 0) return compRes
|
||||
}
|
||||
}
|
||||
|
||||
private inline fun Int.chainCompare(compFn: () -> Int ): Int = if (this != 0) this else compFn()
|
||||
+8
-5
@@ -39,6 +39,7 @@ import org.jetbrains.kotlin.container.getService
|
||||
import org.jetbrains.kotlin.context.GlobalContext
|
||||
import org.jetbrains.kotlin.context.GlobalContextImpl
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
|
||||
import org.jetbrains.kotlin.idea.core.script.ScriptDependenciesModificationTracker
|
||||
import org.jetbrains.kotlin.idea.project.AnalyzerFacadeProvider
|
||||
import org.jetbrains.kotlin.idea.project.TargetPlatformDetector
|
||||
import org.jetbrains.kotlin.idea.project.outOfBlockModificationCount
|
||||
@@ -52,6 +53,7 @@ import org.jetbrains.kotlin.resolve.TargetPlatform
|
||||
import org.jetbrains.kotlin.resolve.diagnostics.KotlinSuppressCache
|
||||
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.script.KotlinScriptExternalDependencies
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.sumByLong
|
||||
import java.lang.AssertionError
|
||||
@@ -74,14 +76,14 @@ class KotlinCacheServiceImpl(val project: Project) : KotlinCacheService {
|
||||
}
|
||||
|
||||
|
||||
private val facadesForScriptDependencies: SLRUCache<ComparableScriptDependencies, ProjectResolutionFacade> =
|
||||
object : SLRUCache<ComparableScriptDependencies, ProjectResolutionFacade>(2, 3) {
|
||||
override fun createValue(key: ComparableScriptDependencies?): ProjectResolutionFacade {
|
||||
private val facadesForScriptDependencies: SLRUCache<KotlinScriptExternalDependencies, ProjectResolutionFacade> =
|
||||
object : SLRUCache<KotlinScriptExternalDependencies, ProjectResolutionFacade>(2, 3) {
|
||||
override fun createValue(key: KotlinScriptExternalDependencies?): ProjectResolutionFacade {
|
||||
return createFacadeForScriptDependencies(ScriptDependenciesModuleInfo(project, key))
|
||||
}
|
||||
}
|
||||
|
||||
private fun getFacadeForScriptDependencies(dependencies: ComparableScriptDependencies?) = synchronized(facadesForScriptDependencies) {
|
||||
private fun getFacadeForScriptDependencies(dependencies: KotlinScriptExternalDependencies?) = synchronized(facadesForScriptDependencies) {
|
||||
facadesForScriptDependencies.get(dependencies)
|
||||
}
|
||||
|
||||
@@ -105,7 +107,8 @@ class KotlinCacheServiceImpl(val project: Project) : KotlinCacheService {
|
||||
//TODO: provide correct trackers
|
||||
dependencies = listOf(
|
||||
LibraryModificationTracker.getInstance(project),
|
||||
ProjectRootModificationTracker.getInstance(project)
|
||||
ProjectRootModificationTracker.getInstance(project),
|
||||
ScriptDependenciesModificationTracker.getInstance(project)
|
||||
),
|
||||
moduleFilter = { it == dependenciesModuleInfo },
|
||||
syntheticFiles = syntheticFiles
|
||||
|
||||
+3
-26
@@ -17,7 +17,6 @@
|
||||
package org.jetbrains.kotlin.idea.caches.resolve
|
||||
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.projectRoots.ProjectJdkTable
|
||||
import com.intellij.openapi.projectRoots.Sdk
|
||||
import com.intellij.openapi.roots.ProjectRootManager
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
@@ -31,7 +30,6 @@ import org.jetbrains.kotlin.script.KotlinScriptDefinition
|
||||
import org.jetbrains.kotlin.script.KotlinScriptExternalDependencies
|
||||
import org.jetbrains.kotlin.script.KotlinScriptExternalImportsProvider
|
||||
import org.jetbrains.kotlin.utils.singletonOrEmptyList
|
||||
import java.io.File
|
||||
|
||||
private val SCRIPT_NAME_PREFIX: String = "script "
|
||||
|
||||
@@ -47,7 +45,7 @@ data class ScriptModuleInfo(val project: Project, val scriptFile: VirtualFile,
|
||||
get() = ModuleOrigin.OTHER
|
||||
|
||||
val externalDependencies by lazy {
|
||||
KotlinScriptExternalImportsProvider.getInstance(project)?.getExternalImports(scriptFile)?.makeComparable()
|
||||
KotlinScriptExternalImportsProvider.getInstance(project)?.getExternalImports(scriptFile)
|
||||
}
|
||||
|
||||
override val name: Name = Name.special("<$SCRIPT_NAME_PREFIX${scriptDefinition.name}>")
|
||||
@@ -75,29 +73,8 @@ fun findJdk(dependencies: KotlinScriptExternalDependencies?, project: Project):
|
||||
allJdks.firstOrNull()
|
||||
}
|
||||
|
||||
private inline fun <T> tryGet(body: () -> T): T? {
|
||||
return try {
|
||||
body()
|
||||
}
|
||||
catch (e: Throwable) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
data class ComparableScriptDependencies(
|
||||
override val javaHome: String?,
|
||||
override val classpath: Iterable<File>,
|
||||
override val imports: Iterable<String>,
|
||||
override val sources: Iterable<File>,
|
||||
override val scripts: Iterable<File>
|
||||
): KotlinScriptExternalDependencies
|
||||
|
||||
fun KotlinScriptExternalDependencies.makeComparable() = ComparableScriptDependencies(
|
||||
tryGet { javaHome }, classpath, imports, sources, tryGet { scripts } ?: listOf()
|
||||
)
|
||||
|
||||
class ScriptDependenciesModuleInfo(val project: Project, val dependencies: ComparableScriptDependencies?): IdeaModuleInfo {
|
||||
override fun dependencies() = listOf(this) + sdkDependencies(dependencies, project)
|
||||
class ScriptDependenciesModuleInfo(val project: Project, val dependencies: KotlinScriptExternalDependencies?): IdeaModuleInfo {
|
||||
override fun dependencies() = (listOf(this) + sdkDependencies(dependencies, project))
|
||||
|
||||
override val name = Name.special("<Script dependencies>")
|
||||
|
||||
|
||||
+20
-3
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
|
||||
+9
-9
@@ -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() {
|
||||
|
||||
+30
@@ -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)!!
|
||||
}
|
||||
}
|
||||
@@ -282,6 +282,9 @@
|
||||
<projectService serviceInterface="org.jetbrains.kotlin.idea.caches.resolve.LibraryModificationTracker"
|
||||
serviceImplementation="org.jetbrains.kotlin.idea.caches.resolve.LibraryModificationTracker"/>
|
||||
|
||||
<projectService serviceInterface="org.jetbrains.kotlin.idea.core.script.ScriptDependenciesModificationTracker"
|
||||
serviceImplementation="org.jetbrains.kotlin.idea.core.script.ScriptDependenciesModificationTracker"/>
|
||||
|
||||
<projectService serviceInterface="org.jetbrains.kotlin.resolve.jvm.KotlinJavaPsiFacade"
|
||||
serviceImplementation="org.jetbrains.kotlin.resolve.jvm.KotlinJavaPsiFacade"/>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user