diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.kt.182 b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.kt.182 new file mode 100644 index 00000000000..d84e67cd1a3 --- /dev/null +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.kt.182 @@ -0,0 +1,97 @@ +/* + * 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.compiler.configuration + +import com.intellij.openapi.components.PersistentStateComponent +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 +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 { + @Suppress("LeakingThis", "UNCHECKED_CAST") + private var _settings: T = createSettings().frozen() as T + private set(value) { + field = value.frozen() as T + } + + var settings: T + get() = _settings + set(value) { + validateNewSettings(value) + _settings = value + project.messageBus.syncPublisher(KotlinCompilerSettingsListener.TOPIC).settingsChanged(value) + } + + fun update(changer: T.() -> Unit) { + @Suppress("UNCHECKED_CAST") + settings = (settings.unfrozen() as T).apply { changer() } + } + + protected fun validateInheritedFieldsUnchanged(settings: T) { + @Suppress("UNCHECKED_CAST") + val inheritedProperties = collectProperties(settings::class as KClass, true) + val defaultInstance = createSettings() + val invalidFields = inheritedProperties.filter { it.get(settings) != it.get(defaultInstance) } + if (invalidFields.isNotEmpty()) { + throw IllegalArgumentException("Following fields are expected to be left unchanged in ${settings.javaClass}: ${invalidFields.joinToString { it.name }}") + } + } + + protected open fun validateNewSettings(settings: T) { + + } + + protected abstract fun createSettings(): T + + override fun getState() = XmlSerializer.serialize(_settings, SKIP_DEFAULT_VALUES) + + override fun loadState(state: Element) { + _settings = ReflectionUtil.newInstance(_settings.javaClass).apply { + if (this is CommonCompilerArguments) { + freeArgs = ArrayList() + } + XmlSerializer.deserializeInto(this, state) + } + project.messageBus.syncPublisher(KotlinCompilerSettingsListener.TOPIC).settingsChanged(settings) + } + + 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 { + fun settingsChanged(newSettings: T) + + companion object { + val TOPIC = Topic.create("KotlinCompilerSettingsListener", KotlinCompilerSettingsListener::class.java) + } +} \ No newline at end of file diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JsCompilerArgumentsHolder.kt.182 b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JsCompilerArgumentsHolder.kt.182 new file mode 100644 index 00000000000..bb8eea34f92 --- /dev/null +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JsCompilerArgumentsHolder.kt.182 @@ -0,0 +1,37 @@ +/* + * Copyright 2010-2015 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.compiler.configuration + +import com.intellij.openapi.components.* +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments +import org.jetbrains.kotlin.config.SettingConstants +import org.jetbrains.kotlin.config.SettingConstants.KOTLIN_TO_JS_COMPILER_ARGUMENTS_SECTION + +@State(name = KOTLIN_TO_JS_COMPILER_ARGUMENTS_SECTION, storages = [(Storage(SettingConstants.KOTLIN_COMPILER_SETTINGS_FILE))]) +class Kotlin2JsCompilerArgumentsHolder(project: Project) : BaseKotlinCompilerSettings(project) { + override fun createSettings() = K2JSCompilerArguments() + + override fun validateNewSettings(settings: K2JSCompilerArguments) { + validateInheritedFieldsUnchanged(settings) + } + + companion object { + fun getInstance(project: Project) = ServiceManager.getService(project, Kotlin2JsCompilerArgumentsHolder::class.java)!! + + } +} diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JvmCompilerArgumentsHolder.kt.182 b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JvmCompilerArgumentsHolder.kt.182 new file mode 100644 index 00000000000..d130447ec6f --- /dev/null +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JvmCompilerArgumentsHolder.kt.182 @@ -0,0 +1,36 @@ +/* + * Copyright 2010-2015 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.compiler.configuration + +import com.intellij.openapi.components.* +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments +import org.jetbrains.kotlin.config.SettingConstants +import org.jetbrains.kotlin.config.SettingConstants.KOTLIN_TO_JVM_COMPILER_ARGUMENTS_SECTION + +@State(name = KOTLIN_TO_JVM_COMPILER_ARGUMENTS_SECTION, storages = [(Storage(SettingConstants.KOTLIN_COMPILER_SETTINGS_FILE))]) +class Kotlin2JvmCompilerArgumentsHolder(project: Project) : BaseKotlinCompilerSettings(project) { + override fun createSettings() = K2JVMCompilerArguments() + + override fun validateNewSettings(settings: K2JVMCompilerArguments) { + validateInheritedFieldsUnchanged(settings) + } + + companion object { + fun getInstance(project: Project) = ServiceManager.getService(project, Kotlin2JvmCompilerArgumentsHolder::class.java)!! + } +} diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCommonCompilerArgumentsHolder.kt.182 b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCommonCompilerArgumentsHolder.kt.182 new file mode 100644 index 00000000000..3cdeb705111 --- /dev/null +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCommonCompilerArgumentsHolder.kt.182 @@ -0,0 +1,54 @@ +/* + * 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. + */ + +package org.jetbrains.kotlin.idea.compiler.configuration + +import com.intellij.openapi.components.* +import com.intellij.openapi.project.Project +import com.intellij.util.text.VersionComparatorUtil +import org.jdom.Element +import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments +import org.jetbrains.kotlin.cli.common.arguments.setApiVersionToLanguageVersionIfNeeded +import org.jetbrains.kotlin.config.SettingConstants +import org.jetbrains.kotlin.config.SettingConstants.KOTLIN_COMMON_COMPILER_ARGUMENTS_SECTION +import org.jetbrains.kotlin.config.detectVersionAutoAdvance +import org.jetbrains.kotlin.config.dropVersionsIfNecessary + +@State(name = KOTLIN_COMMON_COMPILER_ARGUMENTS_SECTION, storages = [(Storage(SettingConstants.KOTLIN_COMPILER_SETTINGS_FILE))]) +class KotlinCommonCompilerArgumentsHolder(project: Project) : BaseKotlinCompilerSettings(project) { + override fun getState(): Element { + return super.getState().apply { + dropVersionsIfNecessary(settings) + } + } + + override fun loadState(state: Element) { + super.loadState(state) + + update { + // To fix earlier configurations with incorrect combination of language and API version + setApiVersionToLanguageVersionIfNeeded() + detectVersionAutoAdvance() + } + } + + override fun createSettings() = CommonCompilerArguments.DummyImpl() + + companion object { + fun getInstance(project: Project) = + ServiceManager.getService(project, KotlinCommonCompilerArgumentsHolder::class.java)!! + } +} diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerSettings.kt.182 b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerSettings.kt.182 new file mode 100644 index 00000000000..b836ef4c5bc --- /dev/null +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerSettings.kt.182 @@ -0,0 +1,34 @@ +/* + * 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.compiler.configuration + +import com.intellij.openapi.components.* +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.config.CompilerSettings +import org.jetbrains.kotlin.config.SettingConstants +import org.jetbrains.kotlin.config.SettingConstants.KOTLIN_COMPILER_SETTINGS_SECTION + +@State(name = KOTLIN_COMPILER_SETTINGS_SECTION, storages = [(Storage(SettingConstants.KOTLIN_COMPILER_SETTINGS_FILE))]) +class KotlinCompilerSettings(project: Project) : BaseKotlinCompilerSettings(project) { + override fun createSettings() = CompilerSettings() + + + + companion object { + fun getInstance(project: Project) = ServiceManager.getService(project, KotlinCompilerSettings::class.java)!! + } +}