From a2948a624f440ddbe1157239a1bdc6eb80d48e32 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Thu, 27 Oct 2016 16:28:23 +0300 Subject: [PATCH] Kotlin Facet: Reuse JvmTarget and LanguageVersion in facet configuration --- .../cli/common/arguments/DefaultValues.kt | 4 ++-- .../jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt | 2 +- .../org/jetbrains/kotlin/config/JvmTarget.kt | 6 ++++-- .../kotlin/config/LanguageVersionSettings.kt | 6 +++++- .../kotlin/utils}/DescriptionAware.kt | 18 +--------------- idea/idea-jps-common/idea-jps-common.iml | 1 + .../kotlin/config/KotlinFacetSettings.kt | 21 ++++++------------- .../idea/facet/KotlinFacetEditorGeneralTab.kt | 16 +++++++------- .../jetbrains/kotlin/idea/facet/facetUtils.kt | 14 ++++++------- 9 files changed, 35 insertions(+), 53 deletions(-) rename {idea/idea-jps-common/src/org/jetbrains/kotlin/config => compiler/util/src/org/jetbrains/kotlin/utils}/DescriptionAware.kt (50%) diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/DefaultValues.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/DefaultValues.kt index 24ac74659fe..0a6cd9ca203 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/DefaultValues.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/DefaultValues.kt @@ -34,8 +34,8 @@ open class DefaultValues(val defaultValue: String, val possibleValues: List() { } else { val errorMessage = "Unknown JVM target version: ${arguments.jvmTarget}\n" + - "Supported versions: ${JvmTarget.values().joinToString { it.string }}" + "Supported versions: ${JvmTarget.values().joinToString { it.description }}" messageCollector.report(CompilerMessageSeverity.ERROR, errorMessage, CompilerMessageLocation.NO_LOCATION) } } diff --git a/compiler/util/src/org/jetbrains/kotlin/config/JvmTarget.kt b/compiler/util/src/org/jetbrains/kotlin/config/JvmTarget.kt index 64f538c92ad..cfe456ad18d 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/JvmTarget.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/JvmTarget.kt @@ -16,7 +16,9 @@ package org.jetbrains.kotlin.config -enum class JvmTarget(val string: String) { +import org.jetbrains.kotlin.utils.DescriptionAware + +enum class JvmTarget(override val description: String) : DescriptionAware { JVM_1_6("1.6"), JVM_1_8("1.8"), ; @@ -26,6 +28,6 @@ enum class JvmTarget(val string: String) { val DEFAULT = JVM_1_6 @JvmStatic - fun fromString(string: String) = values().find { it.string == string } + fun fromString(string: String) = values().find { it.description == string } } } diff --git a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt index 456007811c1..c24d9c8bc2c 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt @@ -17,6 +17,7 @@ package org.jetbrains.kotlin.config import org.jetbrains.kotlin.config.LanguageVersion.KOTLIN_1_1 +import org.jetbrains.kotlin.utils.DescriptionAware enum class LanguageFeature(val sinceVersion: LanguageVersion) { // Note: names of these entries are also used in diagnostic tests @@ -43,10 +44,13 @@ enum class LanguageFeature(val sinceVersion: LanguageVersion) { } } -enum class LanguageVersion(val versionString: String) { +enum class LanguageVersion(val versionString: String) : DescriptionAware { KOTLIN_1_0("1.0"), KOTLIN_1_1("1.1"); + override val description: String + get() = versionString + override fun toString() = versionString companion object { diff --git a/idea/idea-jps-common/src/org/jetbrains/kotlin/config/DescriptionAware.kt b/compiler/util/src/org/jetbrains/kotlin/utils/DescriptionAware.kt similarity index 50% rename from idea/idea-jps-common/src/org/jetbrains/kotlin/config/DescriptionAware.kt rename to compiler/util/src/org/jetbrains/kotlin/utils/DescriptionAware.kt index 346a984832e..b444c9cba54 100644 --- a/idea/idea-jps-common/src/org/jetbrains/kotlin/config/DescriptionAware.kt +++ b/compiler/util/src/org/jetbrains/kotlin/utils/DescriptionAware.kt @@ -14,23 +14,7 @@ * limitations under the License. */ -/* - * 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.config; +package org.jetbrains.kotlin.utils; interface DescriptionAware { val description: String diff --git a/idea/idea-jps-common/idea-jps-common.iml b/idea/idea-jps-common/idea-jps-common.iml index 914a19b4ad5..e38866647e8 100644 --- a/idea/idea-jps-common/idea-jps-common.iml +++ b/idea/idea-jps-common/idea-jps-common.iml @@ -11,5 +11,6 @@ + \ No newline at end of file diff --git a/idea/idea-jps-common/src/org/jetbrains/kotlin/config/KotlinFacetSettings.kt b/idea/idea-jps-common/src/org/jetbrains/kotlin/config/KotlinFacetSettings.kt index a2db9376e59..1f5a591f091 100644 --- a/idea/idea-jps-common/src/org/jetbrains/kotlin/config/KotlinFacetSettings.kt +++ b/idea/idea-jps-common/src/org/jetbrains/kotlin/config/KotlinFacetSettings.kt @@ -36,11 +36,7 @@ import com.intellij.util.xmlb.annotations.Property import com.intellij.util.xmlb.annotations.Transient import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments - -enum class LanguageLevel(override val description: String) : DescriptionAware { - KOTLIN_1_0("1.0"), - KOTLIN_1_1("1.1") -} +import org.jetbrains.kotlin.utils.DescriptionAware sealed class TargetPlatformKind( val version: Version, @@ -57,24 +53,19 @@ object NoVersion : DescriptionAware { override val description = "" } -enum class JVMVersion(override val description: String) : DescriptionAware { - JVM_1_6("1.6"), - JVM_1_8("1.8") -} - -class JVMPlatform(version: JVMVersion) : TargetPlatformKind(version, "JVM") { +class JVMPlatform(version: JvmTarget) : TargetPlatformKind(version, "JVM") { companion object { - val JVM_PLATFORMS by lazy { JVMVersion.values().map(::JVMPlatform) } + val JVM_PLATFORMS by lazy { JvmTarget.values().map(::JVMPlatform) } - operator fun get(version: JVMVersion) = JVM_PLATFORMS[version.ordinal] + operator fun get(version: JvmTarget) = JVM_PLATFORMS[version.ordinal] } } object JSPlatform : TargetPlatformKind(NoVersion, "JavaScript") data class KotlinVersionInfo( - var languageLevel: LanguageLevel? = null, - var apiLevel: LanguageLevel? = null, + var languageLevel: LanguageVersion? = null, + var apiLevel: LanguageVersion? = null, @get:Transient var targetPlatformKindKind: TargetPlatformKind<*>? = null ) { // To be serialized diff --git a/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetEditorGeneralTab.kt b/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetEditorGeneralTab.kt index c8379522399..d747e6e2916 100644 --- a/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetEditorGeneralTab.kt +++ b/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetEditorGeneralTab.kt @@ -20,9 +20,9 @@ import com.intellij.facet.impl.ui.libraries.DelegatingLibrariesValidatorContext import com.intellij.facet.ui.* import com.intellij.facet.ui.libraries.FrameworkLibraryValidator import com.intellij.util.ui.FormBuilder -import org.jetbrains.kotlin.config.DescriptionAware -import org.jetbrains.kotlin.config.LanguageLevel +import org.jetbrains.kotlin.config.LanguageVersion import org.jetbrains.kotlin.config.TargetPlatformKind +import org.jetbrains.kotlin.utils.DescriptionAware import java.awt.BorderLayout import java.awt.Component import javax.swing.* @@ -43,8 +43,8 @@ class KotlinFacetEditorGeneralTab( inner class VersionValidator : FacetEditorValidator() { override fun check(): ValidationResult { - val apiLevel = apiVersionComboBox.selectedItem as? LanguageLevel? ?: return ValidationResult.OK - val languageLevel = languageVersionComboBox.selectedItem as? LanguageLevel? ?: return ValidationResult.OK + val apiLevel = apiVersionComboBox.selectedItem as? LanguageVersion? ?: return ValidationResult.OK + val languageLevel = languageVersionComboBox.selectedItem as? LanguageVersion? ?: return ValidationResult.OK val targetPlatform = targetPlatformComboBox.selectedItem as TargetPlatformKind<*>? val libraryLevel = getLibraryLanguageLevel(editorContext.module, editorContext.rootModel, targetPlatform) if (languageLevel < apiLevel || libraryLevel < apiLevel) { @@ -55,12 +55,12 @@ class KotlinFacetEditorGeneralTab( } private val languageVersionComboBox = - JComboBox(LanguageLevel.values()).apply { + JComboBox(LanguageVersion.values()).apply { setRenderer(DescriptionListCellRenderer()) } private val apiVersionComboBox = - JComboBox(LanguageLevel.values()).apply { + JComboBox(LanguageVersion.values()).apply { setRenderer(DescriptionListCellRenderer()) } @@ -122,9 +122,9 @@ class KotlinFacetEditorGeneralTab( override fun apply() { with(configuration.state.versionInfo) { - languageLevel = languageVersionComboBox.selectedItem as LanguageLevel? + languageLevel = languageVersionComboBox.selectedItem as LanguageVersion? targetPlatformKindKind = targetPlatformComboBox.selectedItem as TargetPlatformKind<*>? - apiLevel = apiVersionComboBox.selectedItem as LanguageLevel? + apiLevel = apiVersionComboBox.selectedItem as LanguageVersion? } } diff --git a/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt b/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt index 0c94178291f..36084c4cd0d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt @@ -66,23 +66,23 @@ private fun getDefaultTargetPlatform(module: Module, rootModel: ModuleRootModel? val sdk = ((rootModel ?: ModuleRootManager.getInstance(module))).sdk val sdkVersion = (sdk?.sdkType as? JavaSdk)?.getVersion(sdk!!) return when { - sdkVersion != null && sdkVersion <= JavaSdkVersion.JDK_1_6 -> JVMPlatform[JVMVersion.JVM_1_6] - else -> JVMPlatform[JVMVersion.JVM_1_8] + sdkVersion != null && sdkVersion <= JavaSdkVersion.JDK_1_6 -> JVMPlatform[JvmTarget.JVM_1_6] + else -> JVMPlatform[JvmTarget.JVM_1_8] } } private fun getDefaultLanguageLevel( module: Module, explicitVersion: String? = null -): LanguageLevel { +): LanguageVersion { val libVersion = explicitVersion ?: KotlinVersionInfoProvider.EP_NAME.extensions .mapNotNull { it.getCompilerVersion(module) } .minWith(VersionComparatorUtil.COMPARATOR) ?: bundledRuntimeVersion() return when { - libVersion.startsWith("1.0") -> LanguageLevel.KOTLIN_1_0 - else -> LanguageLevel.KOTLIN_1_1 + libVersion.startsWith("1.0") -> LanguageVersion.KOTLIN_1_0 + else -> LanguageVersion.KOTLIN_1_1 } } @@ -90,8 +90,8 @@ internal fun getLibraryLanguageLevel( module: Module, rootModel: ModuleRootModel?, targetPlatform: TargetPlatformKind<*>? -): LanguageLevel { - val minVersion = getRuntimeLibraryVersions(module, rootModel, targetPlatform ?: JVMPlatform[JVMVersion.JVM_1_8]) +): LanguageVersion { + val minVersion = getRuntimeLibraryVersions(module, rootModel, targetPlatform ?: JVMPlatform[JvmTarget.JVM_1_8]) .minWith(VersionComparatorUtil.COMPARATOR) return getDefaultLanguageLevel(module, minVersion) }