Reload script definitions on compiler settings changes

Provide API to track compiler settings changes
This commit is contained in:
Pavel V. Talanov
2017-10-27 20:30:18 +03:00
parent ef539bd89d
commit 4906c850af
6 changed files with 31 additions and 6 deletions
@@ -18,6 +18,8 @@ package org.jetbrains.kotlin.idea.compiler.configuration
import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.StoragePathMacros.PROJECT_CONFIG_DIR
import com.intellij.openapi.project.Project
import com.intellij.util.messages.Topic
import com.intellij.util.ReflectionUtil
import com.intellij.util.xmlb.SkipDefaultValuesSerializationFilters
import com.intellij.util.xmlb.XmlSerializer
@@ -26,7 +28,7 @@ import org.jetbrains.kotlin.cli.common.arguments.*
import org.jetbrains.kotlin.config.SettingConstants
import kotlin.reflect.KClass
abstract class BaseKotlinCompilerSettings<T : Freezable> protected constructor() : PersistentStateComponent<Element>, Cloneable {
abstract class BaseKotlinCompilerSettings<T : Freezable> protected constructor(private val project: Project) : PersistentStateComponent<Element>, Cloneable {
@Suppress("LeakingThis", "UNCHECKED_CAST")
private var _settings: T = createSettings().frozen() as T
private set(value) {
@@ -37,8 +39,8 @@ abstract class BaseKotlinCompilerSettings<T : Freezable> protected constructor()
get() = _settings
set(value) {
validateNewSettings(value)
@Suppress("UNCHECKED_CAST")
_settings = value
project.messageBus.syncPublisher(KotlinCompilerSettingsListener.TOPIC).settingsChanged(value)
}
fun update(changer: T.() -> Unit) {
@@ -71,6 +73,7 @@ abstract class BaseKotlinCompilerSettings<T : Freezable> protected constructor()
}
XmlSerializer.deserializeInto(this, state)
}
project.messageBus.syncPublisher(KotlinCompilerSettingsListener.TOPIC).settingsChanged(settings)
}
public override fun clone(): Any = super.clone()
@@ -88,3 +91,11 @@ abstract class BaseKotlinCompilerSettings<T : Freezable> protected constructor()
)
}
}
interface KotlinCompilerSettingsListener {
fun <T> settingsChanged(newSettings: T)
companion object {
val TOPIC = Topic.create("KotlinCompilerSettingsListener", KotlinCompilerSettingsListener::class.java)
}
}
@@ -25,7 +25,7 @@ import org.jetbrains.kotlin.idea.compiler.configuration.BaseKotlinCompilerSettin
@State(name = KOTLIN_TO_JS_COMPILER_ARGUMENTS_SECTION,
storages = arrayOf(Storage(file = StoragePathMacros.PROJECT_FILE),
Storage(file = KOTLIN_COMPILER_SETTINGS_PATH, scheme = StorageScheme.DIRECTORY_BASED)))
class Kotlin2JsCompilerArgumentsHolder : BaseKotlinCompilerSettings<K2JSCompilerArguments>() {
class Kotlin2JsCompilerArgumentsHolder(project: Project) : BaseKotlinCompilerSettings<K2JSCompilerArguments>(project) {
override fun createSettings() = K2JSCompilerArguments()
override fun validateNewSettings(settings: K2JSCompilerArguments) {
@@ -25,7 +25,7 @@ import org.jetbrains.kotlin.idea.compiler.configuration.BaseKotlinCompilerSettin
@State(name = KOTLIN_TO_JVM_COMPILER_ARGUMENTS_SECTION,
storages = arrayOf(Storage(file = StoragePathMacros.PROJECT_FILE),
Storage(file = KOTLIN_COMPILER_SETTINGS_PATH, scheme = StorageScheme.DIRECTORY_BASED)))
class Kotlin2JvmCompilerArgumentsHolder : BaseKotlinCompilerSettings<K2JVMCompilerArguments>() {
class Kotlin2JvmCompilerArgumentsHolder(project: Project) : BaseKotlinCompilerSettings<K2JVMCompilerArguments>(project) {
override fun createSettings() = K2JVMCompilerArguments()
override fun validateNewSettings(settings: K2JVMCompilerArguments) {
@@ -29,7 +29,7 @@ import org.jetbrains.kotlin.config.getOption
storages = arrayOf(Storage(file = StoragePathMacros.PROJECT_FILE),
Storage(file = BaseKotlinCompilerSettings.KOTLIN_COMPILER_SETTINGS_PATH,
scheme = StorageScheme.DIRECTORY_BASED)))
class KotlinCommonCompilerArgumentsHolder : BaseKotlinCompilerSettings<CommonCompilerArguments>() {
class KotlinCommonCompilerArgumentsHolder(project: Project) : BaseKotlinCompilerSettings<CommonCompilerArguments>(project) {
private fun Element.dropElementIfDefault() {
if (DEFAULT_LANGUAGE_VERSION == getAttribute("value")?.value) {
detach()
@@ -24,7 +24,7 @@ import org.jetbrains.kotlin.config.SettingConstants.KOTLIN_COMPILER_SETTINGS_SEC
@State(name = KOTLIN_COMPILER_SETTINGS_SECTION,
storages = arrayOf(Storage(file = StoragePathMacros.PROJECT_FILE),
Storage(file = BaseKotlinCompilerSettings.KOTLIN_COMPILER_SETTINGS_PATH, scheme = StorageScheme.DIRECTORY_BASED)))
class KotlinCompilerSettings : BaseKotlinCompilerSettings<CompilerSettings>() {
class KotlinCompilerSettings(project: Project) : BaseKotlinCompilerSettings<CompilerSettings>(project) {
override fun createSettings() = CompilerSettings()
@@ -16,14 +16,28 @@
package org.jetbrains.kotlin.idea.script
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.config.CompilerSettings
import org.jetbrains.kotlin.idea.compiler.configuration.KotlinCompilerSettings
import org.jetbrains.kotlin.idea.compiler.configuration.KotlinCompilerSettingsListener
import org.jetbrains.kotlin.idea.core.script.ScriptDefinitionContributor
import org.jetbrains.kotlin.idea.core.script.ScriptDefinitionsManager
import org.jetbrains.kotlin.idea.core.script.loadDefinitionsFromTemplates
import org.jetbrains.kotlin.script.KotlinScriptDefinition
import java.io.File
class ScriptTemplatesFromCompilerSettingsProvider(private val project: Project) : ScriptDefinitionContributor {
init {
project.messageBus.connect().subscribe(KotlinCompilerSettingsListener.TOPIC, object: KotlinCompilerSettingsListener {
override fun <T> settingsChanged(newSettings: T) {
if (newSettings !is CompilerSettings) return
project.service<ScriptDefinitionsManager>().reloadDefinitionsBy(this@ScriptTemplatesFromCompilerSettingsProvider)
}
})
}
override fun getDefinitions(): List<KotlinScriptDefinition> {
val kotlinSettings = KotlinCompilerSettings.getInstance(project).settings
return loadDefinitionsFromTemplates(