diff --git a/jps/jps-common/src/org/jetbrains/kotlin/config/KotlinFacetSettings.kt b/jps/jps-common/src/org/jetbrains/kotlin/config/KotlinFacetSettings.kt index 7883fb746d5..7c71a715654 100644 --- a/jps/jps-common/src/org/jetbrains/kotlin/config/KotlinFacetSettings.kt +++ b/jps/jps-common/src/org/jetbrains/kotlin/config/KotlinFacetSettings.kt @@ -70,6 +70,48 @@ object CoroutineSupport { } } +sealed class VersionView : DescriptionAware { + abstract val version: LanguageVersion + + object LatestStable : VersionView() { + override val version: LanguageVersion = LanguageVersion.LATEST_STABLE + + override val description: String + get() = "Latest stable (${version.versionString})" + } + + class Specific(override val version: LanguageVersion) : VersionView() { + override val description: String + get() = version.description + + override fun equals(other: Any?) = other is Specific && version == other.version + + override fun hashCode() = version.hashCode() + } + + companion object { + fun deserialize(value: String?, isAutoAdvance: Boolean): VersionView { + if (isAutoAdvance) return VersionView.LatestStable + val languageVersion = LanguageVersion.fromVersionString(value) + return if (languageVersion != null) VersionView.Specific(languageVersion) else VersionView.LatestStable + } + } +} + +var CommonCompilerArguments.languageVersionView: VersionView + get() = VersionView.deserialize(languageVersion, autoAdvanceLanguageVersion) + set(value) { + languageVersion = value.version.versionString + autoAdvanceLanguageVersion = value == VersionView.LatestStable + } + +var CommonCompilerArguments.apiVersionView: VersionView + get() = VersionView.deserialize(apiVersion, autoAdvanceApiVersion) + set(value) { + apiVersion = value.version.versionString + autoAdvanceApiVersion = value == VersionView.LatestStable + } + class KotlinFacetSettings { companion object { // Increment this when making serialization-incompatible changes to configuration data diff --git a/jps/jps-common/src/org/jetbrains/kotlin/config/facetSerialization.kt b/jps/jps-common/src/org/jetbrains/kotlin/config/facetSerialization.kt index 61636b121d1..fbb209aedd3 100644 --- a/jps/jps-common/src/org/jetbrains/kotlin/config/facetSerialization.kt +++ b/jps/jps-common/src/org/jetbrains/kotlin/config/facetSerialization.kt @@ -74,6 +74,8 @@ private fun readV1Config(element: Element): KotlinFacetSettings { compilerArguments.apiVersion = apiLevel } + compilerArguments.detectVersionAutoAdvance() + if (useProjectSettings != null) { this.useProjectSettings = useProjectSettings } @@ -106,6 +108,7 @@ private fun readV2AndLaterConfig(element: Element): KotlinFacetSettings { element.getChild("compilerArguments")?.let { compilerArguments = platformKind.createCompilerArguments { freeArgs = ArrayList() } XmlSerializer.deserializeInto(compilerArguments!!, it) + compilerArguments!!.detectVersionAutoAdvance() } testOutputPath = element.getChild("testOutputPath")?.let { PathUtil.toSystemDependentName((it.content.firstOrNull() as? Text)?.textTrim) @@ -214,8 +217,8 @@ private fun Element.restoreNormalOrdering(bean: Any) { .forEachIndexed { index, element -> elementsToReorder[index] = element.clone() } } -private fun buildChildElement(element: Element, tag: String, bean: Any, filter: SerializationFilter) { - Element(tag).apply { +private fun buildChildElement(element: Element, tag: String, bean: Any, filter: SerializationFilter): Element { + return Element(tag).apply { XmlSerializer.serializeInto(bean, this, filter) restoreNormalOrdering(bean) element.addContent(this) @@ -245,7 +248,24 @@ private fun KotlinFacetSettings.writeLatestConfig(element: Element) { } compilerArguments?.let { copyBean(it) }?.let { it.convertPathsToSystemIndependent() - buildChildElement(element, "compilerArguments", it, filter) + val compilerArgumentsXml = buildChildElement(element, "compilerArguments", it, filter) + compilerArgumentsXml.dropVersionsIfNecessary(it) + } +} + +fun CommonCompilerArguments.detectVersionAutoAdvance() { + autoAdvanceLanguageVersion = languageVersion == null + autoAdvanceApiVersion = apiVersion == null +} + +fun Element.dropVersionsIfNecessary(settings: CommonCompilerArguments) { + // Do not serialize language/api version if they correspond to the default language version + if (settings.autoAdvanceLanguageVersion) { + getOption("languageVersion")?.detach() + } + + if (settings.autoAdvanceApiVersion) { + getOption("apiVersion")?.detach() } }