diff --git a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/script/ScriptsCompilationConfigurationCache.kt b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/script/ScriptsCompilationConfigurationCache.kt index a8864b1070e..bc4f8e72a33 100644 --- a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/script/ScriptsCompilationConfigurationCache.kt +++ b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/script/ScriptsCompilationConfigurationCache.kt @@ -1,26 +1,18 @@ /* - * Copyright 2010-2017 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. + * 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 +import com.intellij.ProjectTopics import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer import com.intellij.openapi.extensions.Extensions import com.intellij.openapi.module.ModuleManager 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.roots.ModuleRootManager import com.intellij.openapi.roots.OrderRootType import com.intellij.openapi.vfs.VirtualFile @@ -48,6 +40,15 @@ class ScriptsCompilationConfigurationCache(private val project: Project) { const val MAX_SCRIPTS_CACHED = 50 } + init { + val connection = project.messageBus.connect() + connection.subscribe(ProjectTopics.PROJECT_ROOTS, object : ModuleRootListener { + override fun rootsChanged(event: ModuleRootEvent) { + clearCaches() + } + }) + } + private val cacheLock = ReentrantReadWriteLock() private val scriptDependenciesCache = SLRUCacheWithLock() @@ -126,6 +127,11 @@ class ScriptsCompilationConfigurationCache(private val project: Project) { } private fun onChange(files: List) { + clearCaches() + updateHighlighting(files) + } + + private fun clearCaches() { this::allSdks.clearValue() this::allNonIndexedSdks.clearValue() @@ -143,7 +149,6 @@ class ScriptsCompilationConfigurationCache(private val project: Project) { .single() kotlinScriptDependenciesClassFinder.clearCache() - updateHighlighting(files) } private fun updateHighlighting(files: List) {