Kotlin Facet: Reuse JvmTarget and LanguageVersion in facet configuration

This commit is contained in:
Alexey Sedunov
2016-10-27 16:28:23 +03:00
parent b6de7d3503
commit a2948a624f
9 changed files with 35 additions and 53 deletions
@@ -34,8 +34,8 @@ open class DefaultValues(val defaultValue: String, val possibleValues: List<Stri
) )
object JvmTargetVersions : DefaultValues( object JvmTargetVersions : DefaultValues(
"\"" + JvmTarget.DEFAULT.string + "\"", "\"" + JvmTarget.DEFAULT.description + "\"",
JvmTarget.values().map { "\"${it.string}\"" } JvmTarget.values().map { "\"${it.description}\"" }
) )
object JsEcmaVersions : DefaultValues( object JsEcmaVersions : DefaultValues(
@@ -134,7 +134,7 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
} }
else { else {
val errorMessage = "Unknown JVM target version: ${arguments.jvmTarget}\n" + 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) messageCollector.report(CompilerMessageSeverity.ERROR, errorMessage, CompilerMessageLocation.NO_LOCATION)
} }
} }
@@ -16,7 +16,9 @@
package org.jetbrains.kotlin.config 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_6("1.6"),
JVM_1_8("1.8"), JVM_1_8("1.8"),
; ;
@@ -26,6 +28,6 @@ enum class JvmTarget(val string: String) {
val DEFAULT = JVM_1_6 val DEFAULT = JVM_1_6
@JvmStatic @JvmStatic
fun fromString(string: String) = values().find { it.string == string } fun fromString(string: String) = values().find { it.description == string }
} }
} }
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.config package org.jetbrains.kotlin.config
import org.jetbrains.kotlin.config.LanguageVersion.KOTLIN_1_1 import org.jetbrains.kotlin.config.LanguageVersion.KOTLIN_1_1
import org.jetbrains.kotlin.utils.DescriptionAware
enum class LanguageFeature(val sinceVersion: LanguageVersion) { enum class LanguageFeature(val sinceVersion: LanguageVersion) {
// Note: names of these entries are also used in diagnostic tests // 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_0("1.0"),
KOTLIN_1_1("1.1"); KOTLIN_1_1("1.1");
override val description: String
get() = versionString
override fun toString() = versionString override fun toString() = versionString
companion object { companion object {
@@ -14,23 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
/* package org.jetbrains.kotlin.utils;
* 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;
interface DescriptionAware { interface DescriptionAware {
val description: String val description: String
+1
View File
@@ -11,5 +11,6 @@
<orderEntry type="library" name="kotlin-reflect" level="project" /> <orderEntry type="library" name="kotlin-reflect" level="project" />
<orderEntry type="library" name="intellij-core" level="project" /> <orderEntry type="library" name="intellij-core" level="project" />
<orderEntry type="module" module-name="cli-common" /> <orderEntry type="module" module-name="cli-common" />
<orderEntry type="module" module-name="util" />
</component> </component>
</module> </module>
@@ -36,11 +36,7 @@ import com.intellij.util.xmlb.annotations.Property
import com.intellij.util.xmlb.annotations.Transient import com.intellij.util.xmlb.annotations.Transient
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.utils.DescriptionAware
enum class LanguageLevel(override val description: String) : DescriptionAware {
KOTLIN_1_0("1.0"),
KOTLIN_1_1("1.1")
}
sealed class TargetPlatformKind<out Version : DescriptionAware>( sealed class TargetPlatformKind<out Version : DescriptionAware>(
val version: Version, val version: Version,
@@ -57,24 +53,19 @@ object NoVersion : DescriptionAware {
override val description = "" override val description = ""
} }
enum class JVMVersion(override val description: String) : DescriptionAware { class JVMPlatform(version: JvmTarget) : TargetPlatformKind<JvmTarget>(version, "JVM") {
JVM_1_6("1.6"),
JVM_1_8("1.8")
}
class JVMPlatform(version: JVMVersion) : TargetPlatformKind<JVMVersion>(version, "JVM") {
companion object { 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>(NoVersion, "JavaScript") object JSPlatform : TargetPlatformKind<NoVersion>(NoVersion, "JavaScript")
data class KotlinVersionInfo( data class KotlinVersionInfo(
var languageLevel: LanguageLevel? = null, var languageLevel: LanguageVersion? = null,
var apiLevel: LanguageLevel? = null, var apiLevel: LanguageVersion? = null,
@get:Transient var targetPlatformKindKind: TargetPlatformKind<*>? = null @get:Transient var targetPlatformKindKind: TargetPlatformKind<*>? = null
) { ) {
// To be serialized // To be serialized
@@ -20,9 +20,9 @@ import com.intellij.facet.impl.ui.libraries.DelegatingLibrariesValidatorContext
import com.intellij.facet.ui.* import com.intellij.facet.ui.*
import com.intellij.facet.ui.libraries.FrameworkLibraryValidator import com.intellij.facet.ui.libraries.FrameworkLibraryValidator
import com.intellij.util.ui.FormBuilder import com.intellij.util.ui.FormBuilder
import org.jetbrains.kotlin.config.DescriptionAware import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.config.LanguageLevel
import org.jetbrains.kotlin.config.TargetPlatformKind import org.jetbrains.kotlin.config.TargetPlatformKind
import org.jetbrains.kotlin.utils.DescriptionAware
import java.awt.BorderLayout import java.awt.BorderLayout
import java.awt.Component import java.awt.Component
import javax.swing.* import javax.swing.*
@@ -43,8 +43,8 @@ class KotlinFacetEditorGeneralTab(
inner class VersionValidator : FacetEditorValidator() { inner class VersionValidator : FacetEditorValidator() {
override fun check(): ValidationResult { override fun check(): ValidationResult {
val apiLevel = apiVersionComboBox.selectedItem as? LanguageLevel? ?: return ValidationResult.OK val apiLevel = apiVersionComboBox.selectedItem as? LanguageVersion? ?: return ValidationResult.OK
val languageLevel = languageVersionComboBox.selectedItem as? LanguageLevel? ?: return ValidationResult.OK val languageLevel = languageVersionComboBox.selectedItem as? LanguageVersion? ?: return ValidationResult.OK
val targetPlatform = targetPlatformComboBox.selectedItem as TargetPlatformKind<*>? val targetPlatform = targetPlatformComboBox.selectedItem as TargetPlatformKind<*>?
val libraryLevel = getLibraryLanguageLevel(editorContext.module, editorContext.rootModel, targetPlatform) val libraryLevel = getLibraryLanguageLevel(editorContext.module, editorContext.rootModel, targetPlatform)
if (languageLevel < apiLevel || libraryLevel < apiLevel) { if (languageLevel < apiLevel || libraryLevel < apiLevel) {
@@ -55,12 +55,12 @@ class KotlinFacetEditorGeneralTab(
} }
private val languageVersionComboBox = private val languageVersionComboBox =
JComboBox<LanguageLevel>(LanguageLevel.values()).apply { JComboBox<LanguageVersion>(LanguageVersion.values()).apply {
setRenderer(DescriptionListCellRenderer()) setRenderer(DescriptionListCellRenderer())
} }
private val apiVersionComboBox = private val apiVersionComboBox =
JComboBox<LanguageLevel>(LanguageLevel.values()).apply { JComboBox<LanguageVersion>(LanguageVersion.values()).apply {
setRenderer(DescriptionListCellRenderer()) setRenderer(DescriptionListCellRenderer())
} }
@@ -122,9 +122,9 @@ class KotlinFacetEditorGeneralTab(
override fun apply() { override fun apply() {
with(configuration.state.versionInfo) { with(configuration.state.versionInfo) {
languageLevel = languageVersionComboBox.selectedItem as LanguageLevel? languageLevel = languageVersionComboBox.selectedItem as LanguageVersion?
targetPlatformKindKind = targetPlatformComboBox.selectedItem as TargetPlatformKind<*>? targetPlatformKindKind = targetPlatformComboBox.selectedItem as TargetPlatformKind<*>?
apiLevel = apiVersionComboBox.selectedItem as LanguageLevel? apiLevel = apiVersionComboBox.selectedItem as LanguageVersion?
} }
} }
@@ -66,23 +66,23 @@ private fun getDefaultTargetPlatform(module: Module, rootModel: ModuleRootModel?
val sdk = ((rootModel ?: ModuleRootManager.getInstance(module))).sdk val sdk = ((rootModel ?: ModuleRootManager.getInstance(module))).sdk
val sdkVersion = (sdk?.sdkType as? JavaSdk)?.getVersion(sdk!!) val sdkVersion = (sdk?.sdkType as? JavaSdk)?.getVersion(sdk!!)
return when { return when {
sdkVersion != null && sdkVersion <= JavaSdkVersion.JDK_1_6 -> JVMPlatform[JVMVersion.JVM_1_6] sdkVersion != null && sdkVersion <= JavaSdkVersion.JDK_1_6 -> JVMPlatform[JvmTarget.JVM_1_6]
else -> JVMPlatform[JVMVersion.JVM_1_8] else -> JVMPlatform[JvmTarget.JVM_1_8]
} }
} }
private fun getDefaultLanguageLevel( private fun getDefaultLanguageLevel(
module: Module, module: Module,
explicitVersion: String? = null explicitVersion: String? = null
): LanguageLevel { ): LanguageVersion {
val libVersion = explicitVersion val libVersion = explicitVersion
?: KotlinVersionInfoProvider.EP_NAME.extensions ?: KotlinVersionInfoProvider.EP_NAME.extensions
.mapNotNull { it.getCompilerVersion(module) } .mapNotNull { it.getCompilerVersion(module) }
.minWith(VersionComparatorUtil.COMPARATOR) .minWith(VersionComparatorUtil.COMPARATOR)
?: bundledRuntimeVersion() ?: bundledRuntimeVersion()
return when { return when {
libVersion.startsWith("1.0") -> LanguageLevel.KOTLIN_1_0 libVersion.startsWith("1.0") -> LanguageVersion.KOTLIN_1_0
else -> LanguageLevel.KOTLIN_1_1 else -> LanguageVersion.KOTLIN_1_1
} }
} }
@@ -90,8 +90,8 @@ internal fun getLibraryLanguageLevel(
module: Module, module: Module,
rootModel: ModuleRootModel?, rootModel: ModuleRootModel?,
targetPlatform: TargetPlatformKind<*>? targetPlatform: TargetPlatformKind<*>?
): LanguageLevel { ): LanguageVersion {
val minVersion = getRuntimeLibraryVersions(module, rootModel, targetPlatform ?: JVMPlatform[JVMVersion.JVM_1_8]) val minVersion = getRuntimeLibraryVersions(module, rootModel, targetPlatform ?: JVMPlatform[JvmTarget.JVM_1_8])
.minWith(VersionComparatorUtil.COMPARATOR) .minWith(VersionComparatorUtil.COMPARATOR)
return getDefaultLanguageLevel(module, minVersion) return getDefaultLanguageLevel(module, minVersion)
} }