Refactoring: extract separate manager to update script configurations of gradle scripts
The main goal is to extract logic that is related to Gradle scripts only to separate place. Configurations for gradle scripts are updated during import simultaneously, there is no need to manage script configuration update for one file Note that for Gradle less than 6.0 The DefaultScriptingSupport is used
This commit is contained in:
+3
-15
@@ -6,7 +6,6 @@
|
||||
package org.jetbrains.kotlin.idea.core.script
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.openapi.extensions.ExtensionPointName
|
||||
import com.intellij.openapi.fileEditor.FileEditor
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager
|
||||
import com.intellij.openapi.project.Project
|
||||
@@ -15,28 +14,17 @@ import com.intellij.openapi.util.Ref
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.ui.EditorNotificationPanel
|
||||
import com.intellij.ui.HyperlinkLabel
|
||||
import org.jetbrains.annotations.TestOnly
|
||||
import org.jetbrains.kotlin.idea.core.script.settings.KotlinScriptingSettings
|
||||
import org.jetbrains.kotlin.idea.core.util.KotlinIdeaCoreBundle
|
||||
import org.jetbrains.kotlin.psi.UserDataProperty
|
||||
|
||||
interface ScriptConfigurationNotificationFactory {
|
||||
fun showNotification(file: VirtualFile, project: Project, onClick: () -> Unit): Boolean
|
||||
fun hideNotification(file: VirtualFile, project: Project): Boolean
|
||||
|
||||
companion object {
|
||||
val NOTIFICATION_FACTORY: ExtensionPointName<ScriptConfigurationNotificationFactory> =
|
||||
ExtensionPointName.create("org.jetbrains.kotlin.scripting.idea.notificationFactory")
|
||||
}
|
||||
}
|
||||
|
||||
class DefaultScriptConfigurationNotificationFactory : ScriptConfigurationNotificationFactory {
|
||||
override fun showNotification(file: VirtualFile, project: Project, onClick: () -> Unit): Boolean {
|
||||
object LoadScriptConfigurationNotificationFactory {
|
||||
fun showNotification(file: VirtualFile, project: Project, onClick: () -> Unit): Boolean {
|
||||
file.addLoadConfigurationNotificationPanel(project, onClick)
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hideNotification(file: VirtualFile, project: Project): Boolean {
|
||||
fun hideNotification(file: VirtualFile, project: Project): Boolean {
|
||||
file.removeLoadConfigurationNotificationPanel(project)
|
||||
return true
|
||||
}
|
||||
+9
-11
@@ -29,8 +29,8 @@ import com.intellij.psi.search.GlobalSearchScope
|
||||
import com.intellij.util.io.URLUtil
|
||||
import org.jetbrains.annotations.TestOnly
|
||||
import org.jetbrains.kotlin.idea.caches.project.getAllProjectSdks
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.AbstractScriptConfigurationManager
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.cache.ScriptConfigurationSnapshot
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.CompositeManager
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.DefaultScriptingSupport
|
||||
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
|
||||
@@ -38,6 +38,7 @@ import org.jetbrains.kotlin.psi.UserDataProperty
|
||||
import org.jetbrains.kotlin.scripting.definitions.ScriptDependenciesProvider
|
||||
import org.jetbrains.kotlin.scripting.resolve.ScriptCompilationConfigurationResult
|
||||
import org.jetbrains.kotlin.scripting.resolve.ScriptCompilationConfigurationWrapper
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
|
||||
import java.io.File
|
||||
import kotlin.script.experimental.api.asSuccess
|
||||
import kotlin.script.experimental.api.makeFailureResult
|
||||
@@ -106,13 +107,6 @@ interface ScriptConfigurationManager {
|
||||
*/
|
||||
fun clearConfigurationCachesAndRehighlight()
|
||||
|
||||
/**
|
||||
* Save configurations into cache.
|
||||
* Start indexing for new class/source roots.
|
||||
* Re-highlight opened scripts with changed configuration.
|
||||
*/
|
||||
fun saveCompilationConfigurationAfterImport(files: List<Pair<VirtualFile, ScriptConfigurationSnapshot>>)
|
||||
|
||||
///////////////
|
||||
// classpath roots info:
|
||||
|
||||
@@ -168,12 +162,16 @@ interface ScriptConfigurationManager {
|
||||
@TestOnly
|
||||
fun updateScriptDependenciesSynchronously(file: PsiFile) {
|
||||
// TODO: review the usages of this method
|
||||
(getInstance(file.project) as AbstractScriptConfigurationManager).updateScriptDependenciesSynchronously(file)
|
||||
(getInstance(file.project) as CompositeManager).managers
|
||||
.firstIsInstance<DefaultScriptingSupport>()
|
||||
.updateScriptDependenciesSynchronously(file)
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
fun clearCaches(project: Project) {
|
||||
(getInstance(project) as AbstractScriptConfigurationManager).clearCaches()
|
||||
(getInstance(project) as CompositeManager).managers
|
||||
.firstIsInstance<DefaultScriptingSupport>()
|
||||
.clearCaches()
|
||||
}
|
||||
|
||||
fun clearManualConfigurationLoadingIfNeeded(file: VirtualFile) {
|
||||
|
||||
+130
-273
@@ -6,293 +6,45 @@
|
||||
package org.jetbrains.kotlin.idea.core.script.configuration
|
||||
|
||||
import com.intellij.ProjectTopics
|
||||
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer
|
||||
import com.intellij.openapi.extensions.ExtensionPointName
|
||||
import com.intellij.openapi.extensions.Extensions
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.projectRoots.Sdk
|
||||
import com.intellij.openapi.roots.ModuleRootEvent
|
||||
import com.intellij.openapi.roots.ModuleRootListener
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.psi.PsiElementFinder
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.PsiManager
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.jetbrains.annotations.TestOnly
|
||||
import org.jetbrains.kotlin.idea.core.script.*
|
||||
import org.jetbrains.kotlin.idea.core.script.ScriptConfigurationManager.Companion.toVfsRoots
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.cache.ScriptConfigurationCache
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.cache.ScriptConfigurationCacheScope
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.cache.ScriptConfigurationSnapshot
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.cache.ScriptConfigurationState
|
||||
import org.jetbrains.kotlin.idea.core.script.KotlinScriptDependenciesClassFinder
|
||||
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.ScriptClassRootsIndexer
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.utils.getKtFile
|
||||
import org.jetbrains.kotlin.idea.core.util.EDT
|
||||
import org.jetbrains.kotlin.idea.core.script.debug
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.scripting.definitions.findScriptDefinition
|
||||
import org.jetbrains.kotlin.scripting.definitions.isNonScript
|
||||
import org.jetbrains.kotlin.scripting.resolve.ScriptCompilationConfigurationWrapper
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
import kotlin.concurrent.withLock
|
||||
|
||||
/**
|
||||
* Abstract [ScriptConfigurationManager] implementation based on [cache] and [reloadOutOfDateConfiguration].
|
||||
* Among this two methods concrete implementation should provide script changes listening (by calling [updater] on some event).
|
||||
*
|
||||
* Basically all requests routed to [cache]. If there is no entry in [cache] or it is considered out-of-date,
|
||||
* then [reloadOutOfDateConfiguration] will be called, which, in turn, should call [setAppliedConfiguration]
|
||||
* immediately or in some future (e.g. after user will click "apply context" or/and configuration will
|
||||
* be calculated by some background thread).
|
||||
*
|
||||
* [classpathRoots] will be calculated lazily based on [cache]d configurations.
|
||||
* Every change in [cache] will invalidate [classpathRoots] cache.
|
||||
* Some internal state changes in [cache] may also invalidate [classpathRoots] by calling [clearClassRootsCaches]
|
||||
* (for example, when cache loaded from FS to memory)
|
||||
*/
|
||||
internal abstract class AbstractScriptConfigurationManager(
|
||||
protected val project: Project
|
||||
) : ScriptConfigurationManager {
|
||||
protected val rootsIndexer = ScriptClassRootsIndexer(project)
|
||||
abstract class ScriptingSupport {
|
||||
abstract fun isRelated(file: VirtualFile): Boolean
|
||||
|
||||
@Suppress("LeakingThis")
|
||||
protected val cache: ScriptConfigurationCache = createCache()
|
||||
abstract fun clearCaches()
|
||||
abstract fun hasCachedConfiguration(file: KtFile): Boolean
|
||||
abstract fun getOrLoadConfiguration(virtualFile: VirtualFile, preloadedKtFile: KtFile? = null): ScriptCompilationConfigurationWrapper?
|
||||
|
||||
protected abstract fun createCache(): ScriptConfigurationCache
|
||||
|
||||
/**
|
||||
* Will be called on [cache] miss or when [file] is changed.
|
||||
* Implementation should initiate loading of [file]'s script configuration and call [setAppliedConfiguration]
|
||||
* immediately or in some future
|
||||
* (e.g. after user will click "apply context" or/and configuration will be calculated by some background thread).
|
||||
*
|
||||
* @param isFirstLoad may be set explicitly for optimization reasons (to avoid expensive fs cache access)
|
||||
* @param loadEvenWillNotBeApplied may should be set to false only on requests from particular editor, when
|
||||
* user can see potential notification and accept new configuration. In other cases this should be `false` since
|
||||
* loaded configuration will be just leaved in hidden user notification and cannot be used in any way.
|
||||
* @param forceSync should be used in tests only
|
||||
* @param isPostponedLoad is used to postspone loading: show a notification for out of date script and start loading when user request
|
||||
*/
|
||||
protected abstract fun reloadOutOfDateConfiguration(
|
||||
file: KtFile,
|
||||
isFirstLoad: Boolean = getAppliedConfiguration(file.originalFile.virtualFile) == null,
|
||||
loadEvenWillNotBeApplied: Boolean = false,
|
||||
forceSync: Boolean = false,
|
||||
isPostponedLoad: Boolean = false
|
||||
)
|
||||
|
||||
/**
|
||||
* Will be called on user action
|
||||
* Load configuration event it is already cached or inputs are up-to-date
|
||||
*
|
||||
* @param loader is used to load configuration. Other loaders aren't taken into account.
|
||||
*/
|
||||
protected abstract fun forceReloadConfiguration(
|
||||
file: KtFile,
|
||||
loader: ScriptConfigurationLoader
|
||||
)
|
||||
|
||||
@Deprecated("Use getScriptClasspath(KtFile) instead")
|
||||
override fun getScriptClasspath(file: VirtualFile): List<VirtualFile> {
|
||||
val ktFile = project.getKtFile(file) ?: return emptyList()
|
||||
return getScriptClasspath(ktFile)
|
||||
}
|
||||
|
||||
override fun getScriptClasspath(file: KtFile): List<VirtualFile> =
|
||||
toVfsRoots(getConfiguration(file)?.dependenciesClassPath.orEmpty())
|
||||
|
||||
fun getCachedConfigurationState(file: VirtualFile?): ScriptConfigurationState? {
|
||||
if (file == null) return null
|
||||
return cache[file]
|
||||
}
|
||||
|
||||
fun getAppliedConfiguration(file: VirtualFile?): ScriptConfigurationSnapshot? =
|
||||
getCachedConfigurationState(file)?.applied
|
||||
|
||||
override fun hasConfiguration(file: KtFile): Boolean {
|
||||
return getAppliedConfiguration(file.originalFile.virtualFile) != null
|
||||
}
|
||||
|
||||
override fun getConfiguration(file: KtFile): ScriptCompilationConfigurationWrapper? {
|
||||
return getConfiguration(file.originalFile.virtualFile, file)
|
||||
}
|
||||
|
||||
fun getConfiguration(
|
||||
virtualFile: VirtualFile,
|
||||
preloadedKtFile: KtFile? = null
|
||||
): ScriptCompilationConfigurationWrapper? {
|
||||
val cached = getAppliedConfiguration(virtualFile)
|
||||
if (cached != null) return cached.configuration
|
||||
|
||||
val ktFile = project.getKtFile(virtualFile, preloadedKtFile) ?: return null
|
||||
rootsIndexer.transaction {
|
||||
reloadOutOfDateConfiguration(ktFile, isFirstLoad = true)
|
||||
}
|
||||
|
||||
return getAppliedConfiguration(virtualFile)?.configuration
|
||||
}
|
||||
|
||||
override fun forceReloadConfiguration(
|
||||
file: VirtualFile,
|
||||
loader: ScriptConfigurationLoader
|
||||
): ScriptCompilationConfigurationWrapper? {
|
||||
val ktFile = project.getKtFile(file, null) ?: return null
|
||||
|
||||
rootsIndexer.transaction {
|
||||
forceReloadConfiguration(ktFile, loader)
|
||||
}
|
||||
|
||||
return getAppliedConfiguration(file)?.configuration
|
||||
}
|
||||
|
||||
override val updater: ScriptConfigurationUpdater = object : ScriptConfigurationUpdater {
|
||||
override 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)
|
||||
}
|
||||
|
||||
override fun postponeConfigurationReload(scope: ScriptConfigurationCacheScope) {
|
||||
cache.markOutOfDate(scope)
|
||||
}
|
||||
|
||||
override 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 {
|
||||
files.forEach { file ->
|
||||
val virtualFile = file.originalFile.virtualFile
|
||||
if (virtualFile != null) {
|
||||
val state = cache[virtualFile]
|
||||
if (state == null || !state.isUpToDate(project, virtualFile, file)) {
|
||||
upToDate = false
|
||||
reloadOutOfDateConfiguration(
|
||||
file,
|
||||
isFirstLoad = state == null,
|
||||
loadEvenWillNotBeApplied = loadEvenWillNotBeApplied,
|
||||
isPostponedLoad = isPostponedLoad
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return upToDate
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
internal fun updateScriptDependenciesSynchronously(file: PsiFile) {
|
||||
file.findScriptDefinition() ?: return
|
||||
|
||||
file as? KtFile ?: error("PsiFile $file should be a KtFile, otherwise script dependencies cannot be loaded")
|
||||
|
||||
val virtualFile = file.virtualFile
|
||||
if (cache[virtualFile]?.isUpToDate(project, virtualFile, file) == true) return
|
||||
|
||||
rootsIndexer.transaction {
|
||||
reloadOutOfDateConfiguration(file, forceSync = true, loadEvenWillNotBeApplied = true)
|
||||
}
|
||||
}
|
||||
|
||||
protected open fun setAppliedConfiguration(
|
||||
file: VirtualFile,
|
||||
newConfigurationSnapshot: ScriptConfigurationSnapshot?
|
||||
) {
|
||||
rootsIndexer.checkInTransaction()
|
||||
val newConfiguration = newConfigurationSnapshot?.configuration
|
||||
debug(file) { "configuration changed = $newConfiguration" }
|
||||
|
||||
if (newConfiguration != null) {
|
||||
if (hasNotCachedRoots(newConfiguration)) {
|
||||
rootsIndexer.markNewRoot(file, newConfiguration)
|
||||
}
|
||||
|
||||
cache.setApplied(file, newConfigurationSnapshot)
|
||||
|
||||
clearClassRootsCaches()
|
||||
}
|
||||
|
||||
updateHighlighting(listOf(file))
|
||||
}
|
||||
|
||||
protected fun setLoadedConfiguration(
|
||||
file: VirtualFile,
|
||||
configurationSnapshot: ScriptConfigurationSnapshot
|
||||
) {
|
||||
cache.setLoaded(file, configurationSnapshot)
|
||||
}
|
||||
|
||||
private fun hasNotCachedRoots(configuration: ScriptCompilationConfigurationWrapper): Boolean {
|
||||
return classpathRoots.hasNotCachedRoots(configuration)
|
||||
}
|
||||
|
||||
init {
|
||||
val connection = project.messageBus.connect(project)
|
||||
connection.subscribe(ProjectTopics.PROJECT_ROOTS, object : ModuleRootListener {
|
||||
override fun rootsChanged(event: ModuleRootEvent) {
|
||||
clearClassRootsCaches()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear configuration caches
|
||||
* Start re-highlighting for opened scripts
|
||||
*/
|
||||
override fun clearConfigurationCachesAndRehighlight() {
|
||||
ScriptDependenciesModificationTracker.getInstance(project).incModificationCount()
|
||||
|
||||
// todo: cache.clear()
|
||||
clearClassRootsCaches()
|
||||
|
||||
if (project.isOpen) {
|
||||
val openedScripts = FileEditorManager.getInstance(project).openFiles.filterNot { it.isNonScript() }
|
||||
updateHighlighting(openedScripts)
|
||||
}
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
internal fun clearCaches() {
|
||||
cache.clear()
|
||||
}
|
||||
|
||||
private fun updateHighlighting(files: List<VirtualFile>) {
|
||||
if (files.isEmpty()) return
|
||||
|
||||
GlobalScope.launch(EDT(project)) {
|
||||
if (project.isDisposed) return@launch
|
||||
|
||||
val openFiles = FileEditorManager.getInstance(project).openFiles
|
||||
val openScripts = files.filter { it.isValid && openFiles.contains(it) }
|
||||
|
||||
openScripts.forEach {
|
||||
PsiManager.getInstance(project).findFile(it)?.let { psiFile ->
|
||||
DaemonCodeAnalyzer.getInstance(project).restart(psiFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////
|
||||
// ScriptRootsCache
|
||||
abstract val updater: ScriptConfigurationUpdater
|
||||
|
||||
private val classpathRootsLock = ReentrantLock()
|
||||
|
||||
@Volatile
|
||||
private var _classpathRoots: ScriptClassRootsCache? = null
|
||||
private val classpathRoots: ScriptClassRootsCache
|
||||
val classpathRoots: ScriptClassRootsCache
|
||||
get() {
|
||||
val value1 = _classpathRoots
|
||||
if (value1 != null) return value1
|
||||
@@ -301,13 +53,16 @@ internal abstract class AbstractScriptConfigurationManager(
|
||||
val value2 = _classpathRoots
|
||||
if (value2 != null) return value2
|
||||
|
||||
val value3 = ScriptClassRootsCache(project, cache.allApplied())
|
||||
val value3 = recreateRootsCache()
|
||||
value3.saveClassRootsToStorage()
|
||||
_classpathRoots = value3
|
||||
return value3
|
||||
}
|
||||
}
|
||||
|
||||
private fun clearClassRootsCaches() {
|
||||
protected abstract fun recreateRootsCache(): ScriptClassRootsCache
|
||||
|
||||
fun clearClassRootsCaches(project: Project) {
|
||||
debug { "class roots caches cleared" }
|
||||
|
||||
classpathRootsLock.withLock {
|
||||
@@ -324,31 +79,133 @@ internal abstract class AbstractScriptConfigurationManager(
|
||||
ScriptDependenciesModificationTracker.getInstance(project).incModificationCount()
|
||||
}
|
||||
|
||||
companion object {
|
||||
val SCRIPTING_SUPPORT: ExtensionPointName<ScriptingSupport> =
|
||||
ExtensionPointName.create("org.jetbrains.kotlin.scripting.idea.scriptingSupport")
|
||||
}
|
||||
}
|
||||
|
||||
class CompositeManager(val project: Project) : ScriptConfigurationManager {
|
||||
@Suppress("unused")
|
||||
private val notifier = ScriptChangesNotifier(project, updater)
|
||||
|
||||
// todo public for tests
|
||||
val managers = ScriptingSupport.SCRIPTING_SUPPORT.getPoint(project).extensionList
|
||||
|
||||
private fun getRelatedManager(file: VirtualFile): ScriptingSupport = managers.first { it.isRelated(file) }
|
||||
private fun getRelatedManager(file: KtFile): ScriptingSupport =
|
||||
getRelatedManager(file.originalFile.virtualFile)
|
||||
|
||||
private fun getOrLoadConfiguration(
|
||||
virtualFile: VirtualFile,
|
||||
preloadedKtFile: KtFile? = null
|
||||
): ScriptCompilationConfigurationWrapper? =
|
||||
getRelatedManager(virtualFile).getOrLoadConfiguration(virtualFile, preloadedKtFile)
|
||||
|
||||
override fun getConfiguration(file: KtFile) = getOrLoadConfiguration(file.originalFile.virtualFile, file)
|
||||
|
||||
override fun hasConfiguration(file: KtFile): Boolean =
|
||||
getRelatedManager(file).hasCachedConfiguration(file)
|
||||
|
||||
override val updater: ScriptConfigurationUpdater
|
||||
get() = object : ScriptConfigurationUpdater {
|
||||
override fun ensureUpToDatedConfigurationSuggested(file: KtFile) =
|
||||
getRelatedManager(file).updater.ensureUpToDatedConfigurationSuggested(file)
|
||||
|
||||
override fun ensureConfigurationUpToDate(files: List<KtFile>): Boolean =
|
||||
files.groupBy { getRelatedManager(it) }.all { (manager, files) ->
|
||||
manager.updater.ensureConfigurationUpToDate(files)
|
||||
}
|
||||
|
||||
override fun suggestToUpdateConfigurationIfOutOfDate(file: KtFile) =
|
||||
getRelatedManager(file).updater.suggestToUpdateConfigurationIfOutOfDate(file)
|
||||
}
|
||||
|
||||
init {
|
||||
val connection = project.messageBus.connect(project)
|
||||
connection.subscribe(ProjectTopics.PROJECT_ROOTS, object : ModuleRootListener {
|
||||
override fun rootsChanged(event: ModuleRootEvent) {
|
||||
managers.forEach {
|
||||
it.clearClassRootsCaches(project)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns script classpath roots
|
||||
* Loads script configuration if classpath roots don't contain [file] yet
|
||||
*/
|
||||
private fun getActualClasspathRoots(file: VirtualFile): ScriptClassRootsCache {
|
||||
val classpathRoots = getRelatedManager(file).classpathRoots
|
||||
if (classpathRoots.contains(file)) {
|
||||
return classpathRoots
|
||||
}
|
||||
|
||||
getConfiguration(file)
|
||||
return classpathRoots
|
||||
getOrLoadConfiguration(file)
|
||||
|
||||
return getRelatedManager(file).classpathRoots
|
||||
}
|
||||
|
||||
override fun getScriptSdk(file: VirtualFile): Sdk? = getActualClasspathRoots(file).getScriptSdk(file)
|
||||
override fun getScriptSdk(file: VirtualFile): Sdk? =
|
||||
getActualClasspathRoots(file).getScriptSdk(file)
|
||||
|
||||
override fun getFirstScriptsSdk(): Sdk? = classpathRoots.firstScriptSdk
|
||||
override fun getFirstScriptsSdk(): Sdk? {
|
||||
managers.forEach {
|
||||
it.classpathRoots.firstScriptSdk?.let { sdk -> return sdk }
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
override fun getScriptDependenciesClassFilesScope(file: VirtualFile): GlobalSearchScope =
|
||||
getActualClasspathRoots(file).getScriptDependenciesClassFilesScope(file)
|
||||
|
||||
override fun getAllScriptsDependenciesClassFilesScope(): GlobalSearchScope = classpathRoots.allDependenciesClassFilesScope
|
||||
override fun getAllScriptsDependenciesClassFilesScope(): GlobalSearchScope =
|
||||
GlobalSearchScope.union(managers.map { it.classpathRoots.allDependenciesClassFilesScope })
|
||||
|
||||
override fun getAllScriptDependenciesSourcesScope(): GlobalSearchScope = classpathRoots.allDependenciesSourcesScope
|
||||
override fun getAllScriptDependenciesSourcesScope(): GlobalSearchScope =
|
||||
GlobalSearchScope.union(managers.map { it.classpathRoots.allDependenciesSourcesScope })
|
||||
|
||||
override fun getAllScriptsDependenciesClassFiles(): List<VirtualFile> = classpathRoots.allDependenciesClassFiles
|
||||
override fun getAllScriptsDependenciesClassFiles(): List<VirtualFile> =
|
||||
managers.flatMap { it.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 managers.firstIsInstanceOrNull<DefaultScriptingSupport>()?.forceReloadConfiguration(ktFile, loader)
|
||||
}
|
||||
|
||||
///////////////////
|
||||
// Adapters for deprecated API
|
||||
//
|
||||
|
||||
@Deprecated("Use getScriptClasspath(KtFile) instead")
|
||||
override fun getScriptClasspath(file: VirtualFile): List<VirtualFile> {
|
||||
val ktFile = project.getKtFile(file) ?: return emptyList()
|
||||
return getScriptClasspath(ktFile)
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getAllScriptDependenciesSources(): List<VirtualFile> = classpathRoots.allDependenciesSources
|
||||
}
|
||||
+179
-96
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.
|
||||
*/
|
||||
|
||||
@@ -11,95 +11,36 @@ import com.intellij.openapi.components.ServiceManager
|
||||
import com.intellij.openapi.extensions.ExtensionPointName
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.PsiManager
|
||||
import com.intellij.ui.EditorNotifications
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
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.ScriptConfigurationFileAttributeCache
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.cache.ScriptConfigurationMemoryCache
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.cache.ScriptConfigurationSnapshot
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.listener.ScriptChangesNotifier
|
||||
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
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.loader.ScriptOutsiderFileConfigurationLoader
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.utils.*
|
||||
import org.jetbrains.kotlin.idea.core.script.configuration.utils.DefaultBackgroundExecutor
|
||||
import org.jetbrains.kotlin.idea.core.script.settings.KotlinScriptingSettings
|
||||
import org.jetbrains.kotlin.idea.core.util.EDT
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.scripting.definitions.ScriptDefinition
|
||||
import org.jetbrains.kotlin.scripting.definitions.findScriptDefinition
|
||||
import org.jetbrains.kotlin.scripting.resolve.ScriptCompilationConfigurationWrapper
|
||||
import org.jetbrains.kotlin.scripting.resolve.ScriptReportSink
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
import kotlin.concurrent.withLock
|
||||
import kotlin.script.experimental.api.*
|
||||
import kotlin.script.experimental.api.ScriptDiagnostic
|
||||
|
||||
/**
|
||||
* Standard implementation of scripts configuration loading and caching
|
||||
* (we have plans to extract separate implementation for Gradle scripts).
|
||||
*
|
||||
* ## Loading initiation
|
||||
*
|
||||
* [getConfiguration] will be called when we need to show or analyze some script file.
|
||||
*
|
||||
* As described in [AbstractScriptConfigurationManager], configuration may be loaded from [cache]
|
||||
* or [reloadOutOfDateConfiguration] will be called on [cache] miss.
|
||||
*
|
||||
* There are 2 tiers [cache]: memory and FS. For now FS cache implemented by [ScriptConfigurationLoader]
|
||||
* because we are not storing classpath roots yet. As a workaround cache.all() will return only memory
|
||||
* cached configurations. So, for now we are indexing roots that loaded from FS with
|
||||
* default [reloadOutOfDateConfiguration] mechanics. todo(KT-34444): implement fs classpath roots cache
|
||||
*
|
||||
* [notifier] will call first applicable [listeners] when editor is activated or document changed.
|
||||
* Listener may call [updater] to invalidate configuration and schedule reloading.
|
||||
*
|
||||
* Also, [ScriptConfigurationUpdater.ensureConfigurationUpToDate] may be called from [UnusedSymbolInspection]
|
||||
* to ensure that configuration of all scripts containing some symbol are up-to-date or try load it in sync.
|
||||
* Note: it makes sense only in case of "auto apply" mode and sync loader, in other cases all symbols just
|
||||
* will be treated as used.
|
||||
*
|
||||
* ## Loading
|
||||
*
|
||||
* When requested, configuration will be loaded using first applicable [loaders].
|
||||
* It can work synchronously or asynchronously.
|
||||
*
|
||||
* Synchronous loader will be called just immediately. Despite this, its result may not be applied immediately,
|
||||
* see next section for details.
|
||||
*
|
||||
* Asynchronous loader will be called in background thread (by [BackgroundExecutor]).
|
||||
*
|
||||
* ## Applying
|
||||
*
|
||||
* By default loaded configuration will *not* be applied immediately. Instead, we show in editor notification
|
||||
* that suggests user to apply changed configuration. This was done to avoid sporadically starting indexing of new roots,
|
||||
* which may happens regularly for large Gradle projects.
|
||||
*
|
||||
* Notification will be displayed when configuration is going to be updated. First configuration will be loaded
|
||||
* without notification.
|
||||
*
|
||||
* This behavior may be disabled by enabling "auto reload" in project settings.
|
||||
* When enabled, all loaded configurations will be applied immediately, without any notification.
|
||||
*
|
||||
* ## Concurrency
|
||||
*
|
||||
* Each files may be in on of this state:
|
||||
* - scriptDefinition is not ready
|
||||
* - not loaded
|
||||
* - up-to-date
|
||||
* - invalid, in queue (in [BackgroundExecutor] queue)
|
||||
* - invalid, loading
|
||||
* - invalid, waiting for apply
|
||||
*
|
||||
* [reloadOutOfDateConfiguration] guard this states. See it's docs for more details.
|
||||
*/
|
||||
internal class DefaultScriptConfigurationManager(project: Project) :
|
||||
AbstractScriptConfigurationManager(project) {
|
||||
|
||||
internal val backgroundExecutor: BackgroundExecutor =
|
||||
class DefaultScriptingSupport(project: Project) : DefaultScriptingSupportBase(project) {
|
||||
// TODO public for tests
|
||||
val backgroundExecutor: BackgroundExecutor =
|
||||
if (ApplicationManager.getApplication().isUnitTestMode) TestingBackgroundExecutor(rootsIndexer)
|
||||
else DefaultBackgroundExecutor(project, rootsIndexer)
|
||||
|
||||
@@ -114,8 +55,6 @@ internal class DefaultScriptConfigurationManager(project: Project) :
|
||||
yield(defaultLoader)
|
||||
}
|
||||
|
||||
private val notifier = ScriptChangesNotifier(project, updater)
|
||||
|
||||
private val saveLock = ReentrantLock()
|
||||
|
||||
override fun createCache() = object : ScriptConfigurationMemoryCache(project) {
|
||||
@@ -189,10 +128,8 @@ internal class DefaultScriptConfigurationManager(project: Project) :
|
||||
loaders.firstOrNull { it.loadDependencies(isFirstLoad, file, scriptDefinition, loadingContext) }
|
||||
} else {
|
||||
if (postponeLoading) {
|
||||
ScriptConfigurationNotificationFactory.NOTIFICATION_FACTORY.getPoint(project).extensionList.firstOrNull {
|
||||
it.showNotification(virtualFile, project) {
|
||||
runAsyncLoaders(file, virtualFile, scriptDefinition, async, isLoadingPostponed = true)
|
||||
}
|
||||
LoadScriptConfigurationNotificationFactory.showNotification(virtualFile, project) {
|
||||
runAsyncLoaders(file, virtualFile, scriptDefinition, async, isLoadingPostponed = true)
|
||||
}
|
||||
} else {
|
||||
runAsyncLoaders(file, virtualFile, scriptDefinition, async, isLoadingPostponed = false)
|
||||
@@ -224,11 +161,11 @@ internal class DefaultScriptConfigurationManager(project: Project) :
|
||||
}
|
||||
}
|
||||
|
||||
override fun forceReloadConfiguration(file: KtFile, loader: ScriptConfigurationLoader) {
|
||||
val virtualFile = file.originalFile.virtualFile ?: return
|
||||
fun forceReloadConfiguration(file: KtFile, loader: ScriptConfigurationLoader): ScriptCompilationConfigurationWrapper? {
|
||||
val virtualFile = file.originalFile.virtualFile ?: return null
|
||||
|
||||
if (!ScriptDefinitionsManager.getInstance(project).isReady()) return
|
||||
val scriptDefinition = file.findScriptDefinition() ?: return
|
||||
if (!ScriptDefinitionsManager.getInstance(project).isReady()) return null
|
||||
val scriptDefinition = file.findScriptDefinition() ?: return null
|
||||
|
||||
if (!loader.shouldRunInBackground(scriptDefinition)) {
|
||||
loader.loadDependencies(false, file, scriptDefinition, loadingContext)
|
||||
@@ -237,24 +174,13 @@ internal class DefaultScriptConfigurationManager(project: Project) :
|
||||
loader.loadDependencies(false, file, scriptDefinition, loadingContext)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save configurations into cache.
|
||||
* Start indexing for new class/source roots.
|
||||
* Re-highlight opened scripts with changed configuration.
|
||||
*/
|
||||
override fun saveCompilationConfigurationAfterImport(files: List<Pair<VirtualFile, ScriptConfigurationSnapshot>>) {
|
||||
rootsIndexer.transaction {
|
||||
for ((file, result) in files) {
|
||||
loadingContext.saveNewConfiguration(file, result)
|
||||
}
|
||||
}
|
||||
return getAppliedConfiguration(virtualFile)?.configuration
|
||||
}
|
||||
|
||||
private val loadingContext = object : ScriptConfigurationLoadingContext {
|
||||
override fun getCachedConfiguration(file: VirtualFile): ScriptConfigurationSnapshot? =
|
||||
this@DefaultScriptConfigurationManager.getAppliedConfiguration(file)
|
||||
getAppliedConfiguration(file)
|
||||
|
||||
override fun suggestNewConfiguration(file: VirtualFile, newResult: ScriptConfigurationSnapshot) {
|
||||
suggestOrSaveConfiguration(file, newResult, false)
|
||||
@@ -275,9 +201,7 @@ internal class DefaultScriptConfigurationManager(project: Project) :
|
||||
|
||||
setLoadedConfiguration(file, newResult)
|
||||
|
||||
ScriptConfigurationNotificationFactory.NOTIFICATION_FACTORY.getPoint(project).extensionList.firstOrNull {
|
||||
it.hideNotification(file, project)
|
||||
}
|
||||
LoadScriptConfigurationNotificationFactory.hideNotification(file, project)
|
||||
|
||||
val newConfiguration = newResult.configuration
|
||||
if (newConfiguration == null) {
|
||||
@@ -349,10 +273,169 @@ internal class DefaultScriptConfigurationManager(project: Project) :
|
||||
}
|
||||
}
|
||||
|
||||
abstract class DefaultScriptingSupportBase(val project: Project) : ScriptingSupport() {
|
||||
protected val rootsIndexer = ScriptClassRootsIndexer(project)
|
||||
|
||||
@Suppress("LeakingThis")
|
||||
protected val cache: ScriptConfigurationCache = createCache()
|
||||
|
||||
protected abstract fun createCache(): ScriptConfigurationCache
|
||||
|
||||
/**
|
||||
* Will be called on [cache] miss or when [file] is changed.
|
||||
* Implementation should initiate loading of [file]'s script configuration and call [setAppliedConfiguration]
|
||||
* immediately or in some future
|
||||
* (e.g. after user will click "apply context" or/and configuration will be calculated by some background thread).
|
||||
*
|
||||
* @param isFirstLoad may be set explicitly for optimization reasons (to avoid expensive fs cache access)
|
||||
* @param loadEvenWillNotBeApplied may should be set to false only on requests from particular editor, when
|
||||
* user can see potential notification and accept new configuration. In other cases this should be `false` since
|
||||
* loaded configuration will be just leaved in hidden user notification and cannot be used in any way.
|
||||
* @param forceSync should be used in tests only
|
||||
* @param isPostponedLoad is used to postspone loading: show a notification for out of date script and start loading when user request
|
||||
*/
|
||||
protected abstract fun reloadOutOfDateConfiguration(
|
||||
file: KtFile,
|
||||
isFirstLoad: Boolean = getAppliedConfiguration(file.originalFile.virtualFile) == null,
|
||||
loadEvenWillNotBeApplied: Boolean = false,
|
||||
forceSync: Boolean = false,
|
||||
isPostponedLoad: Boolean = false
|
||||
)
|
||||
|
||||
fun getCachedConfigurationState(file: VirtualFile?): ScriptConfigurationState? {
|
||||
if (file == null) return null
|
||||
return cache[file]
|
||||
}
|
||||
|
||||
fun getAppliedConfiguration(file: VirtualFile?): ScriptConfigurationSnapshot? =
|
||||
getCachedConfigurationState(file)?.applied
|
||||
|
||||
override fun isRelated(file: VirtualFile): Boolean = true
|
||||
|
||||
override fun hasCachedConfiguration(file: KtFile): Boolean =
|
||||
getAppliedConfiguration(file.originalFile.virtualFile) != null
|
||||
|
||||
override fun getOrLoadConfiguration(
|
||||
virtualFile: VirtualFile,
|
||||
preloadedKtFile: KtFile?
|
||||
): ScriptCompilationConfigurationWrapper? {
|
||||
val cached = getAppliedConfiguration(virtualFile)
|
||||
if (cached != null) return cached.configuration
|
||||
|
||||
val ktFile = project.getKtFile(virtualFile, preloadedKtFile) ?: return null
|
||||
rootsIndexer.transaction {
|
||||
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)
|
||||
}
|
||||
|
||||
override 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)
|
||||
}
|
||||
}
|
||||
|
||||
private fun reloadIfOutOfDate(files: List<KtFile>, loadEvenWillNotBeApplied: Boolean, isPostponedLoad: Boolean): Boolean {
|
||||
if (!ScriptDefinitionsManager.getInstance(project).isReady()) return false
|
||||
|
||||
var upToDate = true
|
||||
rootsIndexer.transaction {
|
||||
files.forEach { file ->
|
||||
val virtualFile = file.originalFile.virtualFile
|
||||
if (virtualFile != null) {
|
||||
val state = cache[virtualFile]
|
||||
if (state == null || !state.isUpToDate(project, virtualFile, file)) {
|
||||
upToDate = false
|
||||
reloadOutOfDateConfiguration(
|
||||
file,
|
||||
isFirstLoad = state == null,
|
||||
loadEvenWillNotBeApplied = loadEvenWillNotBeApplied,
|
||||
isPostponedLoad = isPostponedLoad
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return upToDate
|
||||
}
|
||||
|
||||
protected open fun setAppliedConfiguration(
|
||||
file: VirtualFile,
|
||||
newConfigurationSnapshot: ScriptConfigurationSnapshot?
|
||||
) {
|
||||
rootsIndexer.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()
|
||||
}
|
||||
|
||||
cache.setApplied(file, newConfigurationSnapshot)
|
||||
|
||||
clearClassRootsCaches(project)
|
||||
}
|
||||
|
||||
ScriptingSupportHelper.updateHighlighting(project) {
|
||||
it == file
|
||||
}
|
||||
}
|
||||
|
||||
protected fun setLoadedConfiguration(
|
||||
file: VirtualFile,
|
||||
configurationSnapshot: ScriptConfigurationSnapshot
|
||||
) {
|
||||
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
|
||||
|
||||
file as? KtFile ?: error("PsiFile $file should be a KtFile, otherwise script dependencies cannot be loaded")
|
||||
|
||||
val virtualFile = file.virtualFile
|
||||
if (cache[virtualFile]?.isUpToDate(project, virtualFile, file) == true) return
|
||||
|
||||
rootsIndexer.transaction {
|
||||
reloadOutOfDateConfiguration(file, forceSync = true, loadEvenWillNotBeApplied = true)
|
||||
}
|
||||
}
|
||||
|
||||
override fun clearCaches() {
|
||||
cache.clear()
|
||||
}
|
||||
|
||||
override fun recreateRootsCache(): ScriptClassRootsCache {
|
||||
return DefaultClassRootsCache(
|
||||
project,
|
||||
cache.allApplied()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
object DefaultScriptConfigurationManagerExtensions {
|
||||
val LOADER: ExtensionPointName<ScriptConfigurationLoader> =
|
||||
ExtensionPointName.create("org.jetbrains.kotlin.scripting.idea.loader")
|
||||
}
|
||||
|
||||
val ScriptConfigurationManager.testingBackgroundExecutor
|
||||
get() = (this as DefaultScriptConfigurationManager).backgroundExecutor as TestingBackgroundExecutor
|
||||
get() = (this as CompositeManager).managers
|
||||
.firstIsInstance<DefaultScriptingSupport>()
|
||||
.backgroundExecutor as TestingBackgroundExecutor
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.psi.PsiManager
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.jetbrains.kotlin.idea.core.util.EDT
|
||||
|
||||
object ScriptingSupportHelper {
|
||||
fun updateHighlighting(project: Project, filter: (VirtualFile) -> Boolean) {
|
||||
if (!project.isOpen) return
|
||||
|
||||
val openFiles = FileEditorManager.getInstance(project).openFiles
|
||||
val openedScripts = openFiles.filter { filter(it) }
|
||||
|
||||
if (openedScripts.isEmpty()) return
|
||||
|
||||
GlobalScope.launch(EDT(project)) {
|
||||
if (project.isDisposed) return@launch
|
||||
|
||||
openedScripts.forEach {
|
||||
PsiManager.getInstance(project).findFile(it)?.let { psiFile ->
|
||||
DaemonCodeAnalyzer.getInstance(project).restart(psiFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
@@ -33,6 +33,8 @@ interface ScriptConfigurationCache {
|
||||
|
||||
fun allApplied(): Map<VirtualFile, ScriptCompilationConfigurationWrapper>
|
||||
fun clear()
|
||||
|
||||
fun getAnyLoadedScript(): ScriptCompilationConfigurationWrapper?
|
||||
}
|
||||
|
||||
sealed class ScriptConfigurationCacheScope {
|
||||
|
||||
+3
@@ -24,6 +24,9 @@ open class ScriptConfigurationMemoryCache(
|
||||
return memoryCache.get(file)
|
||||
}
|
||||
|
||||
override fun getAnyLoadedScript(): ScriptCompilationConfigurationWrapper? =
|
||||
memoryCache.entrySet().firstOrNull()?.value?.loaded?.configuration
|
||||
|
||||
@Synchronized
|
||||
override fun setApplied(file: VirtualFile, configurationSnapshot: ScriptConfigurationSnapshot) {
|
||||
val old = memoryCache[file] ?: ScriptConfigurationState()
|
||||
|
||||
-1
@@ -9,7 +9,6 @@ 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.DefaultScriptConfigurationManager
|
||||
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil
|
||||
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* 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, extractRoots(all, project)) {
|
||||
|
||||
override val fileToConfiguration: (VirtualFile) -> ScriptCompilationConfigurationWrapper?
|
||||
get() = { all[it] }
|
||||
|
||||
override fun contains(file: VirtualFile): Boolean = file in all
|
||||
|
||||
override val rootsCacheKey = ScriptClassRootsStorage.Companion.Key("default")
|
||||
|
||||
private val scriptsSdksCache: Map<VirtualFile, Sdk?> =
|
||||
ConcurrentFactoryMap.createWeakMap { file ->
|
||||
return@createWeakMap getScriptSdk(
|
||||
all[file]?.javaHome
|
||||
) ?: ScriptConfigurationManager.getScriptDefaultSdk(project)
|
||||
}
|
||||
|
||||
override fun getScriptSdk(file: VirtualFile): Sdk? = scriptsSdksCache[file]
|
||||
|
||||
override val firstScriptSdk: Sdk? by lazy {
|
||||
val firstCachedScript = all.keys.firstOrNull() ?: return@lazy null
|
||||
return@lazy getScriptSdk(firstCachedScript)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun extractRoots(
|
||||
project: Project,
|
||||
configuration: ScriptCompilationConfigurationWrapper
|
||||
): ScriptClassRootsStorage.Companion.ScriptClassRoots {
|
||||
val scriptSdk =
|
||||
getScriptSdkOfDefault(
|
||||
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 =
|
||||
getScriptSdk(configuration.javaHome)
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
+62
-88
@@ -10,53 +10,41 @@ import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.projectRoots.Sdk
|
||||
import com.intellij.openapi.roots.ModuleRootManager
|
||||
import com.intellij.openapi.roots.OrderRootType
|
||||
import com.intellij.openapi.vfs.VfsUtil
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.openapi.vfs.*
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import com.intellij.psi.search.NonClasspathDirectoriesScope
|
||||
import com.intellij.util.containers.ConcurrentFactoryMap
|
||||
import org.jetbrains.kotlin.idea.caches.project.getAllProjectSdks
|
||||
import org.jetbrains.kotlin.idea.core.script.ScriptConfigurationManager
|
||||
import org.jetbrains.kotlin.idea.util.getProjectJdkTableSafe
|
||||
import org.jetbrains.kotlin.scripting.resolve.ScriptCompilationConfigurationWrapper
|
||||
import java.io.File
|
||||
import java.nio.file.FileSystems
|
||||
|
||||
internal class ScriptClassRootsCache(
|
||||
abstract class ScriptClassRootsCache(
|
||||
private val project: Project,
|
||||
private val all: Map<VirtualFile, ScriptCompilationConfigurationWrapper>
|
||||
private val roots: ScriptClassRootsStorage.Companion.ScriptClassRoots
|
||||
) {
|
||||
private val scriptsSdksCache: Map<VirtualFile, Sdk?> =
|
||||
ConcurrentFactoryMap.createWeakMap { file ->
|
||||
return@createWeakMap getScriptSdk(all[file]) ?: ScriptConfigurationManager.getScriptDefaultSdk(project)
|
||||
}
|
||||
protected abstract val fileToConfiguration: (VirtualFile) -> ScriptCompilationConfigurationWrapper?
|
||||
|
||||
fun getScriptSdk(file: VirtualFile): Sdk? = scriptsSdksCache[file]
|
||||
protected abstract val rootsCacheKey: ScriptClassRootsStorage.Companion.Key
|
||||
|
||||
private fun getScriptSdk(compilationConfiguration: ScriptCompilationConfigurationWrapper?): Sdk? {
|
||||
// workaround for mismatched gradle wrapper and plugin version
|
||||
val javaHome = try {
|
||||
compilationConfiguration?.javaHome?.let { VfsUtil.findFileByIoFile(it, true) }
|
||||
} catch (e: Throwable) {
|
||||
null
|
||||
} ?: return null
|
||||
abstract val firstScriptSdk: Sdk?
|
||||
|
||||
return getAllProjectSdks().find { it.homeDirectory == javaHome }
|
||||
}
|
||||
abstract fun getScriptSdk(file: VirtualFile): Sdk?
|
||||
|
||||
val firstScriptSdk: Sdk? by lazy {
|
||||
val firstCachedScript = all.keys.firstOrNull() ?: return@lazy null
|
||||
return@lazy getScriptSdk(firstCachedScript)
|
||||
}
|
||||
abstract fun contains(file: VirtualFile): Boolean
|
||||
|
||||
private fun Sdk.isAlreadyIndexed(): Boolean {
|
||||
return ModuleManager.getInstance(project).modules.any { ModuleRootManager.getInstance(it).sdk == this }
|
||||
}
|
||||
class Fat(
|
||||
val scriptConfiguration: ScriptCompilationConfigurationWrapper,
|
||||
val classFilesScope: GlobalSearchScope
|
||||
)
|
||||
|
||||
val allDependenciesClassFiles by lazy {
|
||||
ScriptClassRootsStorage.getInstance(project).loadClasspathRoots()
|
||||
ScriptClassRootsStorage.getInstance(project).loadClasspathRoots(rootsCacheKey)
|
||||
}
|
||||
|
||||
val allDependenciesSources by lazy {
|
||||
ScriptClassRootsStorage.getInstance(project).loadSourcesRoots()
|
||||
ScriptClassRootsStorage.getInstance(project).loadSourcesRoots(rootsCacheKey)
|
||||
}
|
||||
|
||||
val allDependenciesClassFilesScope by lazy {
|
||||
@@ -67,85 +55,71 @@ internal class ScriptClassRootsCache(
|
||||
NonClasspathDirectoriesScope.compose(allDependenciesSources)
|
||||
}
|
||||
|
||||
private val scriptsDependenciesClasspathScopeCache: MutableMap<VirtualFile, GlobalSearchScope> =
|
||||
private val scriptsDependenciesCache: MutableMap<VirtualFile, Fat> =
|
||||
ConcurrentFactoryMap.createWeakMap { file ->
|
||||
val compilationConfiguration = all[file]
|
||||
?: return@createWeakMap GlobalSearchScope.EMPTY_SCOPE
|
||||
val configuration = fileToConfiguration(file) ?: return@createWeakMap null
|
||||
|
||||
val roots = compilationConfiguration.dependenciesClassPath
|
||||
val sdk = scriptsSdksCache[file]
|
||||
val roots = configuration.dependenciesClassPath
|
||||
val sdk = getScriptSdk(file)
|
||||
|
||||
@Suppress("FoldInitializerAndIfToElvis")
|
||||
if (sdk == null) {
|
||||
return@createWeakMap NonClasspathDirectoriesScope.compose(
|
||||
ScriptConfigurationManager.toVfsRoots(
|
||||
roots
|
||||
)
|
||||
return@createWeakMap Fat(
|
||||
configuration,
|
||||
NonClasspathDirectoriesScope.compose(ScriptConfigurationManager.toVfsRoots(roots))
|
||||
)
|
||||
}
|
||||
|
||||
return@createWeakMap NonClasspathDirectoriesScope.compose(
|
||||
sdk.rootProvider.getFiles(OrderRootType.CLASSES).toList() + ScriptConfigurationManager.toVfsRoots(
|
||||
roots
|
||||
return@createWeakMap Fat(
|
||||
configuration,
|
||||
NonClasspathDirectoriesScope.compose(
|
||||
sdk.rootProvider.getFiles(OrderRootType.CLASSES).toList() + ScriptConfigurationManager.toVfsRoots(roots)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fun getScriptDependenciesClassFilesScope(file: VirtualFile): GlobalSearchScope {
|
||||
return scriptsDependenciesClasspathScopeCache[file] ?: GlobalSearchScope.EMPTY_SCOPE
|
||||
return scriptsDependenciesCache[file]?.classFilesScope ?: GlobalSearchScope.EMPTY_SCOPE
|
||||
}
|
||||
|
||||
fun hasNotCachedRoots(compilationConfiguration: ScriptCompilationConfigurationWrapper): Boolean {
|
||||
return !ScriptClassRootsStorage.getInstance(project).containsAll(extractRoots(compilationConfiguration))
|
||||
fun getScriptConfiguration(file: VirtualFile): ScriptCompilationConfigurationWrapper? {
|
||||
return scriptsDependenciesCache[file]?.scriptConfiguration
|
||||
}
|
||||
|
||||
private fun extractRoots(configuration: ScriptCompilationConfigurationWrapper): ScriptClassRootsStorage.Companion.ScriptClassRoots {
|
||||
val scriptSdk = getScriptSdk(configuration) ?: ScriptConfigurationManager.getScriptDefaultSdk(project)
|
||||
if (scriptSdk != null && !scriptSdk.isAlreadyIndexed()) {
|
||||
return ScriptClassRootsStorage.Companion.ScriptClassRoots(
|
||||
configuration.dependenciesClassPath,
|
||||
configuration.dependenciesSources,
|
||||
listOf(scriptSdk)
|
||||
)
|
||||
}
|
||||
|
||||
return ScriptClassRootsStorage.Companion.ScriptClassRoots(
|
||||
configuration.dependenciesClassPath,
|
||||
configuration.dependenciesSources,
|
||||
emptyList()
|
||||
)
|
||||
fun hasNotCachedRoots(roots: ScriptClassRootsStorage.Companion.ScriptClassRoots): Boolean {
|
||||
return !ScriptClassRootsStorage.getInstance(project).containsAll(rootsCacheKey, roots)
|
||||
}
|
||||
|
||||
init {
|
||||
saveClassRootsToStorage()
|
||||
}
|
||||
|
||||
private fun saveClassRootsToStorage() {
|
||||
if (all.isEmpty()) return
|
||||
|
||||
val classpath = mutableSetOf<File>()
|
||||
val sources = mutableSetOf<File>()
|
||||
val sdks = mutableSetOf<Sdk>()
|
||||
|
||||
for ((file, configuration) in all) {
|
||||
val scriptSdk = getScriptSdk(file)
|
||||
if (scriptSdk != null && !scriptSdk.isAlreadyIndexed()) {
|
||||
sdks.add(scriptSdk)
|
||||
}
|
||||
|
||||
classpath.addAll(configuration.dependenciesClassPath)
|
||||
sources.addAll(configuration.dependenciesSources)
|
||||
}
|
||||
|
||||
fun saveClassRootsToStorage() {
|
||||
val rootsStorage = ScriptClassRootsStorage.getInstance(project)
|
||||
rootsStorage.save(
|
||||
ScriptClassRootsStorage.Companion.ScriptClassRoots(
|
||||
classpath.toList(),
|
||||
sources.toList(),
|
||||
sdks.toList()
|
||||
)
|
||||
)
|
||||
if (roots.classpathFiles.isNotEmpty() || roots.sourcesFiles.isNotEmpty() || roots.sdks.isNotEmpty()) {
|
||||
rootsStorage.save(rootsCacheKey, roots)
|
||||
}
|
||||
}
|
||||
|
||||
fun contains(file: VirtualFile) = all.containsKey(file)
|
||||
}
|
||||
companion object {
|
||||
fun toStringValues(prop: Collection<File>): Set<String> {
|
||||
return prop.mapNotNull { it.absolutePath }.toSet()
|
||||
}
|
||||
|
||||
fun getScriptSdkOfDefault(javaHomeStr: File?, project: Project): Sdk? {
|
||||
return getScriptSdk(javaHomeStr) ?: ScriptConfigurationManager.getScriptDefaultSdk(project)
|
||||
}
|
||||
|
||||
fun getScriptSdk(javaHomeStr: File?): Sdk? {
|
||||
// workaround for mismatched gradle wrapper and plugin version
|
||||
val javaHome = try {
|
||||
javaHomeStr?.let { VfsUtil.findFileByIoFile(it, true) }
|
||||
} catch (e: Throwable) {
|
||||
null
|
||||
} ?: return null
|
||||
|
||||
return getProjectJdkTableSafe().allJdks.find { it.homeDirectory == javaHome }
|
||||
}
|
||||
|
||||
fun Sdk.isAlreadyIndexed(project: Project): Boolean {
|
||||
return ModuleManager.getInstance(project).modules.any { ModuleRootManager.getInstance(it).sdk == this }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-3
@@ -20,13 +20,12 @@ import java.util.concurrent.atomic.AtomicInteger
|
||||
/**
|
||||
* Utility for postponing indexing of new roots to the end of some bulk operation.
|
||||
*/
|
||||
internal class ScriptClassRootsIndexer(val project: Project) {
|
||||
class ScriptClassRootsIndexer(val project: Project) {
|
||||
private var newRootsPresent: Boolean = false
|
||||
private val concurrentTransactions = AtomicInteger()
|
||||
|
||||
@Synchronized
|
||||
fun markNewRoot(file: VirtualFile, configuration: ScriptCompilationConfigurationWrapper) {
|
||||
debug(file) { "new class roots found: $configuration" }
|
||||
fun markNewRoot() {
|
||||
checkInTransaction()
|
||||
newRootsPresent = true
|
||||
}
|
||||
|
||||
+75
-40
@@ -10,57 +10,84 @@ 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 com.intellij.util.io.URLUtil
|
||||
import com.intellij.util.xmlb.XmlSerializerUtil
|
||||
import org.jdom.Element
|
||||
import org.jetbrains.kotlin.idea.core.script.debug
|
||||
import java.io.File
|
||||
|
||||
@State(
|
||||
name = "ScriptClassRootsStorage",
|
||||
storages = [Storage(StoragePathMacros.CACHE_FILE)]
|
||||
)
|
||||
class ScriptClassRootsStorage : PersistentStateComponent<ScriptClassRootsStorage> {
|
||||
private var classpath: Set<String> = hashSetOf()
|
||||
private var sources: Set<String> = hashSetOf()
|
||||
private var sdks: Set<String> = hashSetOf()
|
||||
class ScriptClassRootsStorage : PersistentStateComponent<Element> {
|
||||
|
||||
override fun getState(): ScriptClassRootsStorage? {
|
||||
return this
|
||||
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 loadState(state: ScriptClassRootsStorage) {
|
||||
XmlSerializerUtil.copyBean(state, 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: Element) {
|
||||
classpath = readCollection(state, "classpath")
|
||||
sources = readCollection(state, "sources")
|
||||
sdks = readCollection(state, "sdks")
|
||||
}
|
||||
|
||||
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 toStringValues(prop: Collection<File>): Set<String> {
|
||||
return prop.mapNotNull {
|
||||
when {
|
||||
it.isDirectory -> it.absolutePath
|
||||
it.isFile -> it.absolutePath + URLUtil.JAR_SEPARATOR
|
||||
else -> null
|
||||
}
|
||||
}.toSet()
|
||||
}
|
||||
private fun toVirtualFiles(prop: Set<String>?, sources: Boolean): List<VirtualFile> {
|
||||
if (prop == null) return emptyList()
|
||||
|
||||
private fun toVirtualFiles(prop: Set<String>, sources: Boolean): List<VirtualFile> {
|
||||
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> {
|
||||
private fun toVirtualFiles(prop: Collection<String>?): List<VirtualFile> {
|
||||
if (prop == null) return emptyList()
|
||||
|
||||
return prop.mapNotNull {
|
||||
StandardFileSystems.local()?.findFileByPath(it)?.let {
|
||||
return@mapNotNull it
|
||||
if (it.endsWith(JarFileSystem.PROTOCOL)) {
|
||||
StandardFileSystems.jar()?.findFileByPath(it + JarFileSystem.JAR_SEPARATOR)?.let {
|
||||
return@mapNotNull it
|
||||
}
|
||||
}
|
||||
|
||||
StandardFileSystems.jar()?.findFileByPath(it)?.let {
|
||||
StandardFileSystems.local()?.findFileByPath(it)?.let {
|
||||
return@mapNotNull it
|
||||
}
|
||||
|
||||
@@ -70,36 +97,40 @@ class ScriptClassRootsStorage : PersistentStateComponent<ScriptClassRootsStorage
|
||||
}.distinct()
|
||||
}
|
||||
|
||||
fun containsAll(configuration: ScriptClassRoots): Boolean {
|
||||
if (!classpath.containsAll(toStringValues(configuration.classpathFiles))) {
|
||||
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 (!sources.containsAll(toStringValues(configuration.sourcesFiles))) {
|
||||
if (configuration.sourcesFiles.isNotEmpty() && sources[key.value]?.containsAll(configuration.sourcesFiles) != true) {
|
||||
debug { "source roots were changed: old = $sources, new = ${configuration.sourcesFiles}" }
|
||||
return false
|
||||
}
|
||||
if (!sdks.containsAll(toStringNames(configuration.sdks))) {
|
||||
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(configuration: ScriptClassRoots) {
|
||||
fun save(key: Key, configuration: ScriptClassRoots) {
|
||||
// TODO: do not drop all storage on save: KT-34444
|
||||
classpath = toStringValues(configuration.classpathFiles)
|
||||
sources = toStringValues(configuration.sourcesFiles)
|
||||
classpath.getOrPut(key.value, { emptySet() })
|
||||
classpath.replace(key.value, configuration.classpathFiles)
|
||||
|
||||
sdks = toStringNames(configuration.sdks)
|
||||
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(): List<VirtualFile> {
|
||||
return toVirtualFiles(sdks, false) + toVirtualFiles(classpath)
|
||||
fun loadClasspathRoots(key: Key): List<VirtualFile> {
|
||||
return toVirtualFiles(sdks[key.value], false) + toVirtualFiles(classpath[key.value])
|
||||
}
|
||||
|
||||
fun loadSourcesRoots(): List<VirtualFile> {
|
||||
return toVirtualFiles(sdks, true) + toVirtualFiles(sources)
|
||||
fun loadSourcesRoots(key: Key): List<VirtualFile> {
|
||||
return toVirtualFiles(sdks[key.value], true) + toVirtualFiles(sources[key.value])
|
||||
}
|
||||
|
||||
companion object {
|
||||
@@ -107,9 +138,13 @@ class ScriptClassRootsStorage : PersistentStateComponent<ScriptClassRootsStorage
|
||||
ServiceManager.getService(project, ScriptClassRootsStorage::class.java)
|
||||
|
||||
data class ScriptClassRoots(
|
||||
val classpathFiles: List<File>,
|
||||
val sourcesFiles: List<File>,
|
||||
val sdks: List<Sdk>
|
||||
val classpathFiles: Set<String>,
|
||||
val sourcesFiles: Set<String>,
|
||||
val sdks: Set<Sdk>
|
||||
)
|
||||
|
||||
val EMPTY = ScriptClassRoots(emptySet(), emptySet(), emptySet())
|
||||
|
||||
data class Key(val value: String)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user