Kotlin Facet: Refactor TargetPlatform to sealed class. Move settings-related classes to top level

This commit is contained in:
Alexey Sedunov
2016-10-21 15:47:23 +03:00
parent ea13456bba
commit 3fcdda2805
9 changed files with 97 additions and 79 deletions
@@ -20,6 +20,7 @@ import com.intellij.openapi.module.Module
import org.jetbrains.idea.maven.project.MavenProjectsManager import org.jetbrains.idea.maven.project.MavenProjectsManager
import org.jetbrains.kotlin.idea.facet.KotlinFacetConfiguration import org.jetbrains.kotlin.idea.facet.KotlinFacetConfiguration
import org.jetbrains.kotlin.idea.facet.KotlinVersionInfoProvider 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.facet.mavenLibraryId
import org.jetbrains.kotlin.idea.maven.configuration.KotlinMavenConfigurator 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 return mavenProject.findPlugin(KotlinMavenConfigurator.GROUP_ID, KotlinMavenConfigurator.MAVEN_PLUGIN_ID)?.version
} }
override fun getLibraryVersions(module: Module, targetPlatform: KotlinFacetConfiguration.TargetPlatform): Collection<String> { override fun getLibraryVersions(module: Module, targetPlatform: TargetPlatformKind<*>): Collection<String> {
val projectsManager = MavenProjectsManager.getInstance(module.project) val projectsManager = MavenProjectsManager.getInstance(module.project)
val mavenProject = projectsManager.findProject(module) ?: return emptyList() val mavenProject = projectsManager.findProject(module) ?: return emptyList()
return mavenProject.findDependencies(KotlinMavenConfigurator.GROUP_ID, targetPlatform.mavenLibraryId).map { it.version }.distinct() return mavenProject.findDependencies(KotlinMavenConfigurator.GROUP_ID, targetPlatform.mavenLibraryId).map { it.version }.distinct()
@@ -38,7 +38,8 @@ import org.jetbrains.kotlin.cli.common.arguments.K2JsArgumentConstants;
import org.jetbrains.kotlin.config.CompilerSettings; import org.jetbrains.kotlin.config.CompilerSettings;
import org.jetbrains.kotlin.idea.KotlinBundle; import org.jetbrains.kotlin.idea.KotlinBundle;
import org.jetbrains.kotlin.idea.PluginStartupComponent; 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.*;
import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeEvent;
@@ -120,8 +121,8 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co
} }
} }
public void setTargetPlatform(@Nullable KotlinFacetConfiguration.TargetPlatform targetPlatform) { public void setTargetPlatform(@Nullable TargetPlatformKind<?> targetPlatform) {
k2jsPanel.setVisible(targetPlatform == KotlinFacetConfiguration.TargetPlatform.JS); k2jsPanel.setVisible(JSPlatform.INSTANCE.equals(targetPlatform));
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")
@@ -18,8 +18,8 @@ package org.jetbrains.kotlin.idea.configuration
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
import com.intellij.openapi.module.Module 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.KotlinVersionInfoProvider
import org.jetbrains.kotlin.idea.facet.TargetPlatformKind
import org.jetbrains.kotlin.idea.facet.mavenLibraryId import org.jetbrains.kotlin.idea.facet.mavenLibraryId
import org.jetbrains.kotlin.idea.inspections.gradle.DifferentKotlinGradleVersionInspection import org.jetbrains.kotlin.idea.inspections.gradle.DifferentKotlinGradleVersionInspection
import org.jetbrains.kotlin.idea.inspections.gradle.DifferentStdlibGradleVersionInspection import org.jetbrains.kotlin.idea.inspections.gradle.DifferentStdlibGradleVersionInspection
@@ -43,7 +43,7 @@ class GradleKotlinVersionInfoProvider : KotlinVersionInfoProvider {
return getGradleFile(module)?.let { DifferentKotlinGradleVersionInspection.getKotlinPluginVersion(it) } return getGradleFile(module)?.let { DifferentKotlinGradleVersionInspection.getKotlinPluginVersion(it) }
} }
override fun getLibraryVersions(module: Module, targetPlatform: KotlinFacetConfiguration.TargetPlatform): Collection<String> { override fun getLibraryVersions(module: Module, targetPlatform: TargetPlatformKind<*>): Collection<String> {
return getGradleFile(module)?.let { return getGradleFile(module)?.let {
DifferentStdlibGradleVersionInspection.getKotlinStdlibVersions(it, targetPlatform.mavenLibraryId) DifferentStdlibGradleVersionInspection.getKotlinStdlibVersions(it, targetPlatform.mavenLibraryId)
} ?: emptyList() } ?: emptyList()
@@ -34,16 +34,14 @@ class FrameworkLibraryValidatorWithDynamicDescription(
private val context: LibrariesValidatorContext, private val context: LibrariesValidatorContext,
private val validatorsManager: FacetValidatorsManager, private val validatorsManager: FacetValidatorsManager,
private val libraryCategoryName: String, private val libraryCategoryName: String,
private val getTargetPlatform: () -> KotlinFacetConfiguration.TargetPlatform private val getTargetPlatform: () -> TargetPlatformKind<*>
) : FrameworkLibraryValidator() { ) : FrameworkLibraryValidator() {
private val KotlinFacetConfiguration.TargetPlatform.libraryDescription: CustomLibraryDescription private val TargetPlatformKind<*>.libraryDescription: CustomLibraryDescription
get() { get() {
val project = context.module.project val project = context.module.project
return when (this) { return when (this) {
KotlinFacetConfiguration.TargetPlatform.JVM_1_6, KotlinFacetConfiguration.TargetPlatform.JVM_1_8 -> is JVMPlatform -> JavaRuntimeLibraryDescription(project)
JavaRuntimeLibraryDescription(project) is JSPlatform -> JSLibraryStdDescription(project)
KotlinFacetConfiguration.TargetPlatform.JS ->
JSLibraryStdDescription(project)
} }
} }
@@ -27,33 +27,57 @@ import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.config.CompilerSettings import org.jetbrains.kotlin.config.CompilerSettings
import org.jetbrains.kotlin.idea.util.DescriptionAware 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<out Version : DescriptionAware>(
val version: Version,
val name: String
) : DescriptionAware {
override val description = "$name ${version.description}"
companion object {
val ALL_PLATFORMS: List<TargetPlatformKind<*>> 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<JVMVersion>(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>(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<KotlinFacetConfiguration.Settings> { class KotlinFacetConfiguration : FacetConfiguration, PersistentStateComponent<KotlinFacetConfiguration.Settings> {
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 { class Settings {
val versionInfo = VersionInfo() val versionInfo = KotlinVersionInfo()
val compilerInfo = CompilerInfo() val compilerInfo = KotlinCompilerInfo()
} }
private var settings = Settings() private var settings = Settings()
@@ -21,7 +21,7 @@ import com.intellij.facet.ui.FacetEditorTab
import org.jetbrains.kotlin.idea.compiler.configuration.KotlinCompilerConfigurableTab import org.jetbrains.kotlin.idea.compiler.configuration.KotlinCompilerConfigurableTab
class KotlinFacetEditorCompilerTab( class KotlinFacetEditorCompilerTab(
compilerInfo: KotlinFacetConfiguration.CompilerInfo, compilerInfo: KotlinCompilerInfo,
editorContext: FacetEditorContext editorContext: FacetEditorContext
) : FacetEditorTab() { ) : FacetEditorTab() {
val compilerConfigurable = KotlinCompilerConfigurableTab( val compilerConfigurable = KotlinCompilerConfigurableTab(
@@ -41,9 +41,9 @@ class KotlinFacetEditorGeneralTab(
inner class VersionValidator : FacetEditorValidator() { inner class VersionValidator : FacetEditorValidator() {
override fun check(): ValidationResult { override fun check(): ValidationResult {
val apiLevel = apiVersionComboBox.selectedItem as? KotlinFacetConfiguration.LanguageLevel? ?: return ValidationResult.OK val apiLevel = apiVersionComboBox.selectedItem as? LanguageLevel? ?: return ValidationResult.OK
val languageLevel = languageVersionComboBox.selectedItem as? KotlinFacetConfiguration.LanguageLevel? ?: return ValidationResult.OK val languageLevel = languageVersionComboBox.selectedItem as? LanguageLevel? ?: return ValidationResult.OK
val targetPlatform = targetPlatformComboBox.selectedItem as KotlinFacetConfiguration.TargetPlatform? 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) {
return ValidationResult("Language version/Runtime version may not be less than API version", null) return ValidationResult("Language version/Runtime version may not be less than API version", null)
@@ -53,17 +53,17 @@ class KotlinFacetEditorGeneralTab(
} }
private val languageVersionComboBox = private val languageVersionComboBox =
JComboBox<KotlinFacetConfiguration.LanguageLevel>(KotlinFacetConfiguration.LanguageLevel.values()).apply { JComboBox<LanguageLevel>(LanguageLevel.values()).apply {
setRenderer(DescriptionListCellRenderer()) setRenderer(DescriptionListCellRenderer())
} }
private val apiVersionComboBox = private val apiVersionComboBox =
JComboBox<KotlinFacetConfiguration.LanguageLevel>(KotlinFacetConfiguration.LanguageLevel.values()).apply { JComboBox<LanguageLevel>(LanguageLevel.values()).apply {
setRenderer(DescriptionListCellRenderer()) setRenderer(DescriptionListCellRenderer())
} }
private val targetPlatformComboBox = private val targetPlatformComboBox =
JComboBox<KotlinFacetConfiguration.TargetPlatform>(KotlinFacetConfiguration.TargetPlatform.values()).apply { JComboBox<TargetPlatformKind<*>>(TargetPlatformKind.ALL_PLATFORMS.toTypedArray()).apply {
setRenderer(DescriptionListCellRenderer()) setRenderer(DescriptionListCellRenderer())
} }
@@ -75,7 +75,7 @@ class KotlinFacetEditorGeneralTab(
DelegatingLibrariesValidatorContext(editorContext), DelegatingLibrariesValidatorContext(editorContext),
validatorsManager, validatorsManager,
"kotlin" "kotlin"
) { targetPlatformComboBox.selectedItem as KotlinFacetConfiguration.TargetPlatform } ) { targetPlatformComboBox.selectedItem as TargetPlatformKind<*> }
validatorsManager.registerValidator(libraryValidator) validatorsManager.registerValidator(libraryValidator)
validatorsManager.registerValidator(versionValidator) validatorsManager.registerValidator(versionValidator)
@@ -97,7 +97,7 @@ class KotlinFacetEditorGeneralTab(
override fun isModified(): Boolean { override fun isModified(): Boolean {
return with(configuration.state.versionInfo) { return with(configuration.state.versionInfo) {
languageVersionComboBox.selectedItem != languageLevel languageVersionComboBox.selectedItem != languageLevel
|| targetPlatformComboBox.selectedItem != targetPlatformKind || targetPlatformComboBox.selectedItem != targetPlatformKindKind
|| apiVersionComboBox.selectedItem != apiLevel || apiVersionComboBox.selectedItem != apiLevel
} }
} }
@@ -105,16 +105,16 @@ class KotlinFacetEditorGeneralTab(
override fun reset() { override fun reset() {
with(configuration.state.versionInfo) { with(configuration.state.versionInfo) {
languageVersionComboBox.selectedItem = languageLevel languageVersionComboBox.selectedItem = languageLevel
targetPlatformComboBox.selectedItem = targetPlatformKind targetPlatformComboBox.selectedItem = targetPlatformKindKind
apiVersionComboBox.selectedItem = apiLevel apiVersionComboBox.selectedItem = apiLevel
} }
} }
override fun apply() { override fun apply() {
with(configuration.state.versionInfo) { with(configuration.state.versionInfo) {
languageLevel = languageVersionComboBox.selectedItem as KotlinFacetConfiguration.LanguageLevel? languageLevel = languageVersionComboBox.selectedItem as LanguageLevel?
targetPlatformKind = targetPlatformComboBox.selectedItem as KotlinFacetConfiguration.TargetPlatform? targetPlatformKindKind = targetPlatformComboBox.selectedItem as TargetPlatformKind<*>?
apiLevel = apiVersionComboBox.selectedItem as KotlinFacetConfiguration.LanguageLevel? apiLevel = apiVersionComboBox.selectedItem as LanguageLevel?
} }
} }
@@ -136,6 +136,6 @@ class KotlinFacetEditorGeneralTab(
} }
val chosenPlatform: KotlinFacetConfiguration.TargetPlatform? val chosenPlatform: TargetPlatformKind<*>?
get() = targetPlatformComboBox.selectedItem as KotlinFacetConfiguration.TargetPlatform? get() = targetPlatformComboBox.selectedItem as TargetPlatformKind<*>?
} }
@@ -25,5 +25,5 @@ interface KotlinVersionInfoProvider {
} }
fun getCompilerVersion(module: Module): String? fun getCompilerVersion(module: Module): String?
fun getLibraryVersions(module: Module, targetPlatform: KotlinFacetConfiguration.TargetPlatform): Collection<String> fun getLibraryVersions(module: Module, targetPlatform: TargetPlatformKind<*>): Collection<String>
} }
@@ -39,14 +39,11 @@ import org.jetbrains.kotlin.idea.versions.bundledRuntimeVersion
private fun getRuntimeLibraryVersions( private fun getRuntimeLibraryVersions(
module: Module, module: Module,
rootModel: ModuleRootModel?, rootModel: ModuleRootModel?,
targetPlatform: KotlinFacetConfiguration.TargetPlatform targetPlatform: TargetPlatformKind<*>
): Collection<String> { ): Collection<String> {
val presentationProvider = when (targetPlatform) { val presentationProvider = when (targetPlatform) {
KotlinFacetConfiguration.TargetPlatform.JS -> is JSPlatform -> JSLibraryStdPresentationProvider.getInstance()
JSLibraryStdPresentationProvider.getInstance() is JVMPlatform -> JavaRuntimePresentationProvider.getInstance()
KotlinFacetConfiguration.TargetPlatform.JVM_1_6,
KotlinFacetConfiguration.TargetPlatform.JVM_1_8 ->
JavaRuntimePresentationProvider.getInstance()
} }
KotlinVersionInfoProvider.EP_NAME KotlinVersionInfoProvider.EP_NAME
@@ -63,40 +60,40 @@ private fun getRuntimeLibraryVersions(
.toList() .toList()
} }
private fun getDefaultTargetPlatform(module: Module, rootModel: ModuleRootModel?): KotlinFacetConfiguration.TargetPlatform { private fun getDefaultTargetPlatform(module: Module, rootModel: ModuleRootModel?): TargetPlatformKind<*> {
if (getRuntimeLibraryVersions(module, rootModel, KotlinFacetConfiguration.TargetPlatform.JS).any()) { if (getRuntimeLibraryVersions(module, rootModel, JSPlatform).any()) {
return KotlinFacetConfiguration.TargetPlatform.JS return JSPlatform
} }
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 -> KotlinFacetConfiguration.TargetPlatform.JVM_1_6 sdkVersion != null && sdkVersion <= JavaSdkVersion.JDK_1_6 -> JVMPlatform[JVMVersion.JVM_1_6]
else -> KotlinFacetConfiguration.TargetPlatform.JVM_1_8 else -> JVMPlatform[JVMVersion.JVM_1_8]
} }
} }
private fun getDefaultLanguageLevel( private fun getDefaultLanguageLevel(
module: Module, module: Module,
explicitVersion: String? = null explicitVersion: String? = null
): KotlinFacetConfiguration.LanguageLevel { ): LanguageLevel {
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") -> KotlinFacetConfiguration.LanguageLevel.KOTLIN_1_0 libVersion.startsWith("1.0") -> LanguageLevel.KOTLIN_1_0
else -> KotlinFacetConfiguration.LanguageLevel.KOTLIN_1_1 else -> LanguageLevel.KOTLIN_1_1
} }
} }
internal fun getLibraryLanguageLevel( internal fun getLibraryLanguageLevel(
module: Module, module: Module,
rootModel: ModuleRootModel?, rootModel: ModuleRootModel?,
targetPlatform: KotlinFacetConfiguration.TargetPlatform? targetPlatform: TargetPlatformKind<*>?
): KotlinFacetConfiguration.LanguageLevel { ): LanguageLevel {
val minVersion = getRuntimeLibraryVersions(module, rootModel, targetPlatform ?: KotlinFacetConfiguration.TargetPlatform.JVM_1_8) val minVersion = getRuntimeLibraryVersions(module, rootModel, targetPlatform ?: JVMPlatform[JVMVersion.JVM_1_8])
.minWith(VersionComparatorUtil.COMPARATOR) .minWith(VersionComparatorUtil.COMPARATOR)
return getDefaultLanguageLevel(module, minVersion) return getDefaultLanguageLevel(module, minVersion)
} }
@@ -105,8 +102,8 @@ internal fun KotlinFacetConfiguration.Settings.initializeIfNeeded(module: Module
val project = module.project val project = module.project
with(versionInfo) { with(versionInfo) {
if (targetPlatformKind == null) { if (targetPlatformKindKind == null) {
targetPlatformKind = getDefaultTargetPlatform(module, rootModel) targetPlatformKindKind = getDefaultTargetPlatform(module, rootModel)
} }
if (languageLevel == null) { if (languageLevel == null) {
@@ -114,7 +111,7 @@ internal fun KotlinFacetConfiguration.Settings.initializeIfNeeded(module: Module
} }
if (apiLevel == null) { 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 return settings
} }
val KotlinFacetConfiguration.TargetPlatform.mavenLibraryId: String val TargetPlatformKind<*>.mavenLibraryId: String
get() { get() {
return when (this) { return when (this) {
KotlinFacetConfiguration.TargetPlatform.JVM_1_6, is JVMPlatform -> KotlinJavaMavenConfigurator.STD_LIB_ID
KotlinFacetConfiguration.TargetPlatform.JVM_1_8 -> is JSPlatform -> KotlinJavascriptMavenConfigurator.STD_LIB_ID
KotlinJavaMavenConfigurator.STD_LIB_ID
KotlinFacetConfiguration.TargetPlatform.JS ->
KotlinJavascriptMavenConfigurator.STD_LIB_ID
} }
} }