From 03b289b899f476ac8ad85fc0013c0a802e363e1a Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Fri, 28 Sep 2018 14:53:45 +0300 Subject: [PATCH] Configuration: Do not save default values of sourceMapEmbedSources --- .../BaseKotlinCompilerSettings.kt | 51 ++++++++++++++----- .../BaseKotlinCompilerSettings.kt.181 | 49 ++++++++++++++---- .../KotlinCompilerConfigurableTab.java | 2 +- 3 files changed, 77 insertions(+), 25 deletions(-) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.kt index 2611c8e1f0c..6666c64abee 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.kt @@ -19,15 +19,51 @@ package org.jetbrains.kotlin.idea.compiler.configuration import com.intellij.openapi.components.PersistentStateComponent import com.intellij.openapi.progress.util.BackgroundTaskUtil import com.intellij.openapi.project.Project +import com.intellij.openapi.util.Comparing +import com.intellij.openapi.util.JDOMUtil import com.intellij.util.ReflectionUtil import com.intellij.util.messages.Topic -import com.intellij.util.xmlb.SkipDefaultValuesSerializationFilters +import com.intellij.util.xmlb.Accessor +import com.intellij.util.xmlb.SerializationFilterBase import com.intellij.util.xmlb.XmlSerializer +import gnu.trove.THashMap import org.jdom.Element import org.jetbrains.kotlin.cli.common.arguments.* import kotlin.reflect.KClass abstract class BaseKotlinCompilerSettings protected constructor(private val project: Project) : PersistentStateComponent, Cloneable { + // Based on com.intellij.util.xmlb.SkipDefaultValuesSerializationFilters + private object DefaultValuesFilter : SerializationFilterBase() { + private val defaultBeans = THashMap, Any>() + + private fun createDefaultBean(beanClass: Class): Any { + return ReflectionUtil.newInstance(beanClass).apply { + if (this is K2JSCompilerArguments) { + sourceMapPrefix = "" + } + } + } + + private fun getDefaultValue(accessor: Accessor, bean: Any): Any? { + if (bean is K2JSCompilerArguments && accessor.name == K2JSCompilerArguments::sourceMapEmbedSources.name) { + return if (bean.sourceMap) K2JsArgumentConstants.SOURCE_MAP_SOURCE_CONTENT_INLINING else null + } + + val beanClass = bean.javaClass + val defaultBean = defaultBeans.getOrPut(beanClass) { createDefaultBean(beanClass) } + return accessor.read(defaultBean) + } + + override fun accepts(accessor: Accessor, bean: Any, beanValue: Any?): Boolean { + val defValue = getDefaultValue(accessor, bean) + return if (defValue is Element && beanValue is Element) { + !JDOMUtil.areElementsEqual(beanValue, defValue) + } else { + !Comparing.equal(beanValue, defValue) + } + } + } + @Suppress("LeakingThis", "UNCHECKED_CAST") private var _settings: T = createSettings().frozen() as T private set(value) { @@ -64,7 +100,7 @@ abstract class BaseKotlinCompilerSettings protected constructor(p protected abstract fun createSettings(): T - override fun getState() = XmlSerializer.serialize(_settings, SKIP_DEFAULT_VALUES) + override fun getState() = XmlSerializer.serialize(_settings, DefaultValuesFilter) override fun loadState(state: Element) { _settings = ReflectionUtil.newInstance(_settings.javaClass).apply { @@ -78,17 +114,6 @@ abstract class BaseKotlinCompilerSettings protected constructor(p } public override fun clone(): Any = super.clone() - - companion object { - private val SKIP_DEFAULT_VALUES = SkipDefaultValuesSerializationFilters( - CommonCompilerArguments.DummyImpl(), - K2JVMCompilerArguments(), - K2JSCompilerArguments().apply { - sourceMapPrefix = "" - sourceMapEmbedSources = K2JsArgumentConstants.SOURCE_MAP_SOURCE_CONTENT_INLINING - } - ) - } } interface KotlinCompilerSettingsListener { diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.kt.181 b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.kt.181 index 41a3f10646f..4de48ddbe6c 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.kt.181 +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.kt.181 @@ -20,16 +20,52 @@ import com.intellij.openapi.components.PersistentStateComponent import com.intellij.openapi.components.StoragePathMacros.PROJECT_CONFIG_DIR import com.intellij.openapi.progress.util.BackgroundTaskUtil import com.intellij.openapi.project.Project +import com.intellij.openapi.util.Comparing +import com.intellij.openapi.util.JDOMUtil import com.intellij.util.ReflectionUtil import com.intellij.util.messages.Topic -import com.intellij.util.xmlb.SkipDefaultValuesSerializationFilters +import com.intellij.util.xmlb.Accessor +import com.intellij.util.xmlb.SerializationFilterBase import com.intellij.util.xmlb.XmlSerializer +import gnu.trove.THashMap import org.jdom.Element import org.jetbrains.kotlin.cli.common.arguments.* import org.jetbrains.kotlin.config.SettingConstants import kotlin.reflect.KClass abstract class BaseKotlinCompilerSettings protected constructor(private val project: Project) : PersistentStateComponent, Cloneable { + // Based on com.intellij.util.xmlb.SkipDefaultValuesSerializationFilters + private object DefaultValuesFilter : SerializationFilterBase() { + private val defaultBeans = THashMap, Any>() + + private fun createDefaultBean(beanClass: Class): Any { + return ReflectionUtil.newInstance(beanClass).apply { + if (this is K2JSCompilerArguments) { + sourceMapPrefix = "" + } + } + } + + private fun getDefaultValue(accessor: Accessor, bean: Any): Any? { + if (bean is K2JSCompilerArguments && accessor.name == K2JSCompilerArguments::sourceMapEmbedSources.name) { + return if (bean.sourceMap) K2JsArgumentConstants.SOURCE_MAP_SOURCE_CONTENT_INLINING else null + } + + val beanClass = bean.javaClass + val defaultBean = defaultBeans.getOrPut(beanClass) { createDefaultBean(beanClass) } + return accessor.read(defaultBean) + } + + override fun accepts(accessor: Accessor, bean: Any, beanValue: Any?): Boolean { + val defValue = getDefaultValue(accessor, bean) + return if (defValue is Element && beanValue is Element) { + !JDOMUtil.areElementsEqual(beanValue, defValue) + } else { + !Comparing.equal(beanValue, defValue) + } + } + } + @Suppress("LeakingThis", "UNCHECKED_CAST") private var _settings: T = createSettings().frozen() as T private set(value) { @@ -66,7 +102,7 @@ abstract class BaseKotlinCompilerSettings protected constructor(p protected abstract fun createSettings(): T - override fun getState() = XmlSerializer.serialize(_settings, SKIP_DEFAULT_VALUES) + override fun getState() = XmlSerializer.serialize(_settings, DefaultValuesFilter) override fun loadState(state: Element) { _settings = ReflectionUtil.newInstance(_settings.javaClass).apply { @@ -83,15 +119,6 @@ abstract class BaseKotlinCompilerSettings protected constructor(p companion object { const val KOTLIN_COMPILER_SETTINGS_PATH = PROJECT_CONFIG_DIR + "/" + SettingConstants.KOTLIN_COMPILER_SETTINGS_FILE - - private val SKIP_DEFAULT_VALUES = SkipDefaultValuesSerializationFilters( - CommonCompilerArguments.DummyImpl(), - K2JVMCompilerArguments(), - K2JSCompilerArguments().apply { - sourceMapPrefix = "" - sourceMapEmbedSources = K2JsArgumentConstants.SOURCE_MAP_SOURCE_CONTENT_INLINING - } - ) } } diff --git a/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerConfigurableTab.java b/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerConfigurableTab.java index 35ae89e9d4a..c5db86a0203 100644 --- a/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerConfigurableTab.java +++ b/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerConfigurableTab.java @@ -563,7 +563,7 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co k2jsCompilerArguments.setModuleKind(getSelectedModuleKind()); k2jsCompilerArguments.setSourceMapPrefix(sourceMapPrefix.getText()); - k2jsCompilerArguments.setSourceMapEmbedSources(getSelectedSourceMapSourceEmbedding()); + k2jsCompilerArguments.setSourceMapEmbedSources(generateSourceMapsCheckBox.isSelected() ? getSelectedSourceMapSourceEmbedding() : null); k2jvmCompilerArguments.setJvmTarget(getSelectedJvmVersion());