From 3fcdda2805e4aa0d69941687ca91808eb5127972 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Fri, 21 Oct 2016 15:47:23 +0300 Subject: [PATCH] Kotlin Facet: Refactor TargetPlatform to sealed class. Move settings-related classes to top level --- .../facet/MavenKotlinVersionInfoProvider.kt | 3 +- .../KotlinCompilerConfigurableTab.java | 7 +- .../GradleKotlinVersionInfoProvider.kt | 4 +- ...kLibraryValidatorWithDynamicDescription.kt | 10 +-- .../idea/facet/KotlinFacetConfiguration.kt | 74 ++++++++++++------- .../facet/KotlinFacetEditorCompilerTab.kt | 2 +- .../idea/facet/KotlinFacetEditorGeneralTab.kt | 28 +++---- .../idea/facet/KotlinVersionInfoProvider.kt | 2 +- .../jetbrains/kotlin/idea/facet/facetUtils.kt | 46 +++++------- 9 files changed, 97 insertions(+), 79 deletions(-) diff --git a/idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/facet/MavenKotlinVersionInfoProvider.kt b/idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/facet/MavenKotlinVersionInfoProvider.kt index 5d678c49b9a..1d73151be9e 100644 --- a/idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/facet/MavenKotlinVersionInfoProvider.kt +++ b/idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/facet/MavenKotlinVersionInfoProvider.kt @@ -20,6 +20,7 @@ import com.intellij.openapi.module.Module import org.jetbrains.idea.maven.project.MavenProjectsManager import org.jetbrains.kotlin.idea.facet.KotlinFacetConfiguration import org.jetbrains.kotlin.idea.facet.KotlinVersionInfoProvider +import org.jetbrains.kotlin.idea.facet.TargetPlatformKind import org.jetbrains.kotlin.idea.facet.mavenLibraryId import org.jetbrains.kotlin.idea.maven.configuration.KotlinMavenConfigurator @@ -30,7 +31,7 @@ class MavenKotlinVersionInfoProvider : KotlinVersionInfoProvider { return mavenProject.findPlugin(KotlinMavenConfigurator.GROUP_ID, KotlinMavenConfigurator.MAVEN_PLUGIN_ID)?.version } - override fun getLibraryVersions(module: Module, targetPlatform: KotlinFacetConfiguration.TargetPlatform): Collection { + override fun getLibraryVersions(module: Module, targetPlatform: TargetPlatformKind<*>): Collection { val projectsManager = MavenProjectsManager.getInstance(module.project) val mavenProject = projectsManager.findProject(module) ?: return emptyList() return mavenProject.findDependencies(KotlinMavenConfigurator.GROUP_ID, targetPlatform.mavenLibraryId).map { it.version }.distinct() 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 f63c1034b4a..3c7cebcd702 100644 --- a/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerConfigurableTab.java +++ b/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerConfigurableTab.java @@ -38,7 +38,8 @@ import org.jetbrains.kotlin.cli.common.arguments.K2JsArgumentConstants; import org.jetbrains.kotlin.config.CompilerSettings; import org.jetbrains.kotlin.idea.KotlinBundle; import org.jetbrains.kotlin.idea.PluginStartupComponent; -import org.jetbrains.kotlin.idea.facet.KotlinFacetConfiguration; +import org.jetbrains.kotlin.idea.facet.JSPlatform; +import org.jetbrains.kotlin.idea.facet.TargetPlatformKind; import javax.swing.*; import javax.swing.event.ChangeEvent; @@ -120,8 +121,8 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co } } - public void setTargetPlatform(@Nullable KotlinFacetConfiguration.TargetPlatform targetPlatform) { - k2jsPanel.setVisible(targetPlatform == KotlinFacetConfiguration.TargetPlatform.JS); + public void setTargetPlatform(@Nullable TargetPlatformKind targetPlatform) { + k2jsPanel.setVisible(JSPlatform.INSTANCE.equals(targetPlatform)); } @SuppressWarnings("unused") diff --git a/idea/src/org/jetbrains/kotlin/idea/configuration/GradleKotlinVersionInfoProvider.kt b/idea/src/org/jetbrains/kotlin/idea/configuration/GradleKotlinVersionInfoProvider.kt index 0c0e0ca4742..b96406b9a94 100644 --- a/idea/src/org/jetbrains/kotlin/idea/configuration/GradleKotlinVersionInfoProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/configuration/GradleKotlinVersionInfoProvider.kt @@ -18,8 +18,8 @@ package org.jetbrains.kotlin.idea.configuration import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil import com.intellij.openapi.module.Module -import org.jetbrains.kotlin.idea.facet.KotlinFacetConfiguration import org.jetbrains.kotlin.idea.facet.KotlinVersionInfoProvider +import org.jetbrains.kotlin.idea.facet.TargetPlatformKind import org.jetbrains.kotlin.idea.facet.mavenLibraryId import org.jetbrains.kotlin.idea.inspections.gradle.DifferentKotlinGradleVersionInspection import org.jetbrains.kotlin.idea.inspections.gradle.DifferentStdlibGradleVersionInspection @@ -43,7 +43,7 @@ class GradleKotlinVersionInfoProvider : KotlinVersionInfoProvider { return getGradleFile(module)?.let { DifferentKotlinGradleVersionInspection.getKotlinPluginVersion(it) } } - override fun getLibraryVersions(module: Module, targetPlatform: KotlinFacetConfiguration.TargetPlatform): Collection { + override fun getLibraryVersions(module: Module, targetPlatform: TargetPlatformKind<*>): Collection { return getGradleFile(module)?.let { DifferentStdlibGradleVersionInspection.getKotlinStdlibVersions(it, targetPlatform.mavenLibraryId) } ?: emptyList() diff --git a/idea/src/org/jetbrains/kotlin/idea/facet/FrameworkLibraryValidatorWithDynamicDescription.kt b/idea/src/org/jetbrains/kotlin/idea/facet/FrameworkLibraryValidatorWithDynamicDescription.kt index 02ecf95a964..a92a5394939 100644 --- a/idea/src/org/jetbrains/kotlin/idea/facet/FrameworkLibraryValidatorWithDynamicDescription.kt +++ b/idea/src/org/jetbrains/kotlin/idea/facet/FrameworkLibraryValidatorWithDynamicDescription.kt @@ -34,16 +34,14 @@ class FrameworkLibraryValidatorWithDynamicDescription( private val context: LibrariesValidatorContext, private val validatorsManager: FacetValidatorsManager, private val libraryCategoryName: String, - private val getTargetPlatform: () -> KotlinFacetConfiguration.TargetPlatform + private val getTargetPlatform: () -> TargetPlatformKind<*> ) : FrameworkLibraryValidator() { - private val KotlinFacetConfiguration.TargetPlatform.libraryDescription: CustomLibraryDescription + private val TargetPlatformKind<*>.libraryDescription: CustomLibraryDescription get() { val project = context.module.project return when (this) { - KotlinFacetConfiguration.TargetPlatform.JVM_1_6, KotlinFacetConfiguration.TargetPlatform.JVM_1_8 -> - JavaRuntimeLibraryDescription(project) - KotlinFacetConfiguration.TargetPlatform.JS -> - JSLibraryStdDescription(project) + is JVMPlatform -> JavaRuntimeLibraryDescription(project) + is JSPlatform -> JSLibraryStdDescription(project) } } diff --git a/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetConfiguration.kt b/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetConfiguration.kt index 233c42046e5..52aad6c882b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetConfiguration.kt +++ b/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetConfiguration.kt @@ -27,33 +27,57 @@ import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments import org.jetbrains.kotlin.config.CompilerSettings import org.jetbrains.kotlin.idea.util.DescriptionAware +enum class LanguageLevel(override val description: String) : DescriptionAware { + KOTLIN_1_0("1.0"), + KOTLIN_1_1("1.1") +} + +sealed class TargetPlatformKind( + val version: Version, + val name: String +) : DescriptionAware { + override val description = "$name ${version.description}" + + companion object { + val ALL_PLATFORMS: List> by lazy { JVMPlatform.JVM_PLATFORMS + JSPlatform } + } +} + +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") { + companion object { + val JVM_PLATFORMS by lazy { JVMVersion.values().map(::JVMPlatform) } + + operator fun get(version: JVMVersion) = JVM_PLATFORMS[version.ordinal] + } +} + +object JSPlatform : TargetPlatformKind(NoVersion, "JavaScript") + +data class KotlinVersionInfo( + var languageLevel: LanguageLevel? = null, + var apiLevel: LanguageLevel? = null, + var targetPlatformKindKind: TargetPlatformKind<*>? = null +) + +class KotlinCompilerInfo { + var commonCompilerArguments: CommonCompilerArguments? = null + var k2jsCompilerArguments: K2JSCompilerArguments? = null + var compilerSettings: CompilerSettings? = null +} + class KotlinFacetConfiguration : FacetConfiguration, PersistentStateComponent { - enum class LanguageLevel(override val description: String) : DescriptionAware { - KOTLIN_1_0("1.0"), - KOTLIN_1_1("1.1") - } - - enum class TargetPlatform(override val description: String) : DescriptionAware { - JVM_1_6("JVM 1.6"), - JVM_1_8("JVM 1.8"), - JS("JavaScript") - } - - data class VersionInfo( - var languageLevel: LanguageLevel? = null, - var apiLevel: LanguageLevel? = null, - var targetPlatformKind: TargetPlatform? = null - ) - - class CompilerInfo { - var commonCompilerArguments: CommonCompilerArguments? = null - var k2jsCompilerArguments: K2JSCompilerArguments? = null - var compilerSettings: CompilerSettings? = null - } - class Settings { - val versionInfo = VersionInfo() - val compilerInfo = CompilerInfo() + val versionInfo = KotlinVersionInfo() + val compilerInfo = KotlinCompilerInfo() } private var settings = Settings() diff --git a/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetEditorCompilerTab.kt b/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetEditorCompilerTab.kt index 3b7e2a0d952..3dc9a18b753 100644 --- a/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetEditorCompilerTab.kt +++ b/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetEditorCompilerTab.kt @@ -21,7 +21,7 @@ import com.intellij.facet.ui.FacetEditorTab import org.jetbrains.kotlin.idea.compiler.configuration.KotlinCompilerConfigurableTab class KotlinFacetEditorCompilerTab( - compilerInfo: KotlinFacetConfiguration.CompilerInfo, + compilerInfo: KotlinCompilerInfo, editorContext: FacetEditorContext ) : FacetEditorTab() { val compilerConfigurable = KotlinCompilerConfigurableTab( diff --git a/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetEditorGeneralTab.kt b/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetEditorGeneralTab.kt index 8106316e43c..baddc2c6c7e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetEditorGeneralTab.kt +++ b/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetEditorGeneralTab.kt @@ -41,9 +41,9 @@ class KotlinFacetEditorGeneralTab( inner class VersionValidator : FacetEditorValidator() { override fun check(): ValidationResult { - val apiLevel = apiVersionComboBox.selectedItem as? KotlinFacetConfiguration.LanguageLevel? ?: return ValidationResult.OK - val languageLevel = languageVersionComboBox.selectedItem as? KotlinFacetConfiguration.LanguageLevel? ?: return ValidationResult.OK - val targetPlatform = targetPlatformComboBox.selectedItem as KotlinFacetConfiguration.TargetPlatform? + val apiLevel = apiVersionComboBox.selectedItem as? LanguageLevel? ?: return ValidationResult.OK + val languageLevel = languageVersionComboBox.selectedItem as? LanguageLevel? ?: return ValidationResult.OK + val targetPlatform = targetPlatformComboBox.selectedItem as TargetPlatformKind<*>? val libraryLevel = getLibraryLanguageLevel(editorContext.module, editorContext.rootModel, targetPlatform) if (languageLevel < apiLevel || libraryLevel < apiLevel) { return ValidationResult("Language version/Runtime version may not be less than API version", null) @@ -53,17 +53,17 @@ class KotlinFacetEditorGeneralTab( } private val languageVersionComboBox = - JComboBox(KotlinFacetConfiguration.LanguageLevel.values()).apply { + JComboBox(LanguageLevel.values()).apply { setRenderer(DescriptionListCellRenderer()) } private val apiVersionComboBox = - JComboBox(KotlinFacetConfiguration.LanguageLevel.values()).apply { + JComboBox(LanguageLevel.values()).apply { setRenderer(DescriptionListCellRenderer()) } private val targetPlatformComboBox = - JComboBox(KotlinFacetConfiguration.TargetPlatform.values()).apply { + JComboBox>(TargetPlatformKind.ALL_PLATFORMS.toTypedArray()).apply { setRenderer(DescriptionListCellRenderer()) } @@ -75,7 +75,7 @@ class KotlinFacetEditorGeneralTab( DelegatingLibrariesValidatorContext(editorContext), validatorsManager, "kotlin" - ) { targetPlatformComboBox.selectedItem as KotlinFacetConfiguration.TargetPlatform } + ) { targetPlatformComboBox.selectedItem as TargetPlatformKind<*> } validatorsManager.registerValidator(libraryValidator) validatorsManager.registerValidator(versionValidator) @@ -97,7 +97,7 @@ class KotlinFacetEditorGeneralTab( override fun isModified(): Boolean { return with(configuration.state.versionInfo) { languageVersionComboBox.selectedItem != languageLevel - || targetPlatformComboBox.selectedItem != targetPlatformKind + || targetPlatformComboBox.selectedItem != targetPlatformKindKind || apiVersionComboBox.selectedItem != apiLevel } } @@ -105,16 +105,16 @@ class KotlinFacetEditorGeneralTab( override fun reset() { with(configuration.state.versionInfo) { languageVersionComboBox.selectedItem = languageLevel - targetPlatformComboBox.selectedItem = targetPlatformKind + targetPlatformComboBox.selectedItem = targetPlatformKindKind apiVersionComboBox.selectedItem = apiLevel } } override fun apply() { with(configuration.state.versionInfo) { - languageLevel = languageVersionComboBox.selectedItem as KotlinFacetConfiguration.LanguageLevel? - targetPlatformKind = targetPlatformComboBox.selectedItem as KotlinFacetConfiguration.TargetPlatform? - apiLevel = apiVersionComboBox.selectedItem as KotlinFacetConfiguration.LanguageLevel? + languageLevel = languageVersionComboBox.selectedItem as LanguageLevel? + targetPlatformKindKind = targetPlatformComboBox.selectedItem as TargetPlatformKind<*>? + apiLevel = apiVersionComboBox.selectedItem as LanguageLevel? } } @@ -136,6 +136,6 @@ class KotlinFacetEditorGeneralTab( } - val chosenPlatform: KotlinFacetConfiguration.TargetPlatform? - get() = targetPlatformComboBox.selectedItem as KotlinFacetConfiguration.TargetPlatform? + val chosenPlatform: TargetPlatformKind<*>? + get() = targetPlatformComboBox.selectedItem as TargetPlatformKind<*>? } diff --git a/idea/src/org/jetbrains/kotlin/idea/facet/KotlinVersionInfoProvider.kt b/idea/src/org/jetbrains/kotlin/idea/facet/KotlinVersionInfoProvider.kt index 0814158426a..2445a6c0a11 100644 --- a/idea/src/org/jetbrains/kotlin/idea/facet/KotlinVersionInfoProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/facet/KotlinVersionInfoProvider.kt @@ -25,5 +25,5 @@ interface KotlinVersionInfoProvider { } fun getCompilerVersion(module: Module): String? - fun getLibraryVersions(module: Module, targetPlatform: KotlinFacetConfiguration.TargetPlatform): Collection + fun getLibraryVersions(module: Module, targetPlatform: TargetPlatformKind<*>): Collection } \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt b/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt index 3df71543065..5ac18cf48c7 100644 --- a/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt @@ -39,14 +39,11 @@ import org.jetbrains.kotlin.idea.versions.bundledRuntimeVersion private fun getRuntimeLibraryVersions( module: Module, rootModel: ModuleRootModel?, - targetPlatform: KotlinFacetConfiguration.TargetPlatform + targetPlatform: TargetPlatformKind<*> ): Collection { val presentationProvider = when (targetPlatform) { - KotlinFacetConfiguration.TargetPlatform.JS -> - JSLibraryStdPresentationProvider.getInstance() - KotlinFacetConfiguration.TargetPlatform.JVM_1_6, - KotlinFacetConfiguration.TargetPlatform.JVM_1_8 -> - JavaRuntimePresentationProvider.getInstance() + is JSPlatform -> JSLibraryStdPresentationProvider.getInstance() + is JVMPlatform -> JavaRuntimePresentationProvider.getInstance() } KotlinVersionInfoProvider.EP_NAME @@ -63,40 +60,40 @@ private fun getRuntimeLibraryVersions( .toList() } -private fun getDefaultTargetPlatform(module: Module, rootModel: ModuleRootModel?): KotlinFacetConfiguration.TargetPlatform { - if (getRuntimeLibraryVersions(module, rootModel, KotlinFacetConfiguration.TargetPlatform.JS).any()) { - return KotlinFacetConfiguration.TargetPlatform.JS +private fun getDefaultTargetPlatform(module: Module, rootModel: ModuleRootModel?): TargetPlatformKind<*> { + if (getRuntimeLibraryVersions(module, rootModel, JSPlatform).any()) { + return JSPlatform } val sdk = ((rootModel ?: ModuleRootManager.getInstance(module))).sdk val sdkVersion = (sdk?.sdkType as? JavaSdk)?.getVersion(sdk!!) return when { - sdkVersion != null && sdkVersion <= JavaSdkVersion.JDK_1_6 -> KotlinFacetConfiguration.TargetPlatform.JVM_1_6 - else -> KotlinFacetConfiguration.TargetPlatform.JVM_1_8 + sdkVersion != null && sdkVersion <= JavaSdkVersion.JDK_1_6 -> JVMPlatform[JVMVersion.JVM_1_6] + else -> JVMPlatform[JVMVersion.JVM_1_8] } } private fun getDefaultLanguageLevel( module: Module, explicitVersion: String? = null -): KotlinFacetConfiguration.LanguageLevel { +): LanguageLevel { val libVersion = explicitVersion ?: KotlinVersionInfoProvider.EP_NAME.extensions .mapNotNull { it.getCompilerVersion(module) } .minWith(VersionComparatorUtil.COMPARATOR) ?: bundledRuntimeVersion() return when { - libVersion.startsWith("1.0") -> KotlinFacetConfiguration.LanguageLevel.KOTLIN_1_0 - else -> KotlinFacetConfiguration.LanguageLevel.KOTLIN_1_1 + libVersion.startsWith("1.0") -> LanguageLevel.KOTLIN_1_0 + else -> LanguageLevel.KOTLIN_1_1 } } internal fun getLibraryLanguageLevel( module: Module, rootModel: ModuleRootModel?, - targetPlatform: KotlinFacetConfiguration.TargetPlatform? -): KotlinFacetConfiguration.LanguageLevel { - val minVersion = getRuntimeLibraryVersions(module, rootModel, targetPlatform ?: KotlinFacetConfiguration.TargetPlatform.JVM_1_8) + targetPlatform: TargetPlatformKind<*>? +): LanguageLevel { + val minVersion = getRuntimeLibraryVersions(module, rootModel, targetPlatform ?: JVMPlatform[JVMVersion.JVM_1_8]) .minWith(VersionComparatorUtil.COMPARATOR) return getDefaultLanguageLevel(module, minVersion) } @@ -105,8 +102,8 @@ internal fun KotlinFacetConfiguration.Settings.initializeIfNeeded(module: Module val project = module.project with(versionInfo) { - if (targetPlatformKind == null) { - targetPlatformKind = getDefaultTargetPlatform(module, rootModel) + if (targetPlatformKindKind == null) { + targetPlatformKindKind = getDefaultTargetPlatform(module, rootModel) } if (languageLevel == null) { @@ -114,7 +111,7 @@ internal fun KotlinFacetConfiguration.Settings.initializeIfNeeded(module: Module } if (apiLevel == null) { - apiLevel = languageLevel!!.coerceAtMost(getLibraryLanguageLevel(module, rootModel, targetPlatformKind!!)) + apiLevel = languageLevel!!.coerceAtMost(getLibraryLanguageLevel(module, rootModel, targetPlatformKindKind!!)) } } @@ -139,13 +136,10 @@ internal fun Module.getKotlinSettings(rootModel: ModuleRootModel? = null): Kotli return settings } -val KotlinFacetConfiguration.TargetPlatform.mavenLibraryId: String +val TargetPlatformKind<*>.mavenLibraryId: String get() { return when (this) { - KotlinFacetConfiguration.TargetPlatform.JVM_1_6, - KotlinFacetConfiguration.TargetPlatform.JVM_1_8 -> - KotlinJavaMavenConfigurator.STD_LIB_ID - KotlinFacetConfiguration.TargetPlatform.JS -> - KotlinJavascriptMavenConfigurator.STD_LIB_ID + is JVMPlatform -> KotlinJavaMavenConfigurator.STD_LIB_ID + is JSPlatform -> KotlinJavascriptMavenConfigurator.STD_LIB_ID } } \ No newline at end of file