mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-03-23 15:51:59 +00:00
Kotlin Facet: Refactor TargetPlatform to sealed class. Move settings-related classes to top level
This commit is contained in:
@@ -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<String> {
|
||||
override fun getLibraryVersions(module: Module, targetPlatform: TargetPlatformKind<*>): Collection<String> {
|
||||
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()
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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<String> {
|
||||
override fun getLibraryVersions(module: Module, targetPlatform: TargetPlatformKind<*>): Collection<String> {
|
||||
return getGradleFile(module)?.let {
|
||||
DifferentStdlibGradleVersionInspection.getKotlinStdlibVersions(it, targetPlatform.mavenLibraryId)
|
||||
} ?: emptyList()
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<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> {
|
||||
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()
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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>(KotlinFacetConfiguration.LanguageLevel.values()).apply {
|
||||
JComboBox<LanguageLevel>(LanguageLevel.values()).apply {
|
||||
setRenderer(DescriptionListCellRenderer())
|
||||
}
|
||||
|
||||
private val apiVersionComboBox =
|
||||
JComboBox<KotlinFacetConfiguration.LanguageLevel>(KotlinFacetConfiguration.LanguageLevel.values()).apply {
|
||||
JComboBox<LanguageLevel>(LanguageLevel.values()).apply {
|
||||
setRenderer(DescriptionListCellRenderer())
|
||||
}
|
||||
|
||||
private val targetPlatformComboBox =
|
||||
JComboBox<KotlinFacetConfiguration.TargetPlatform>(KotlinFacetConfiguration.TargetPlatform.values()).apply {
|
||||
JComboBox<TargetPlatformKind<*>>(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<*>?
|
||||
}
|
||||
|
||||
@@ -25,5 +25,5 @@ interface KotlinVersionInfoProvider {
|
||||
}
|
||||
|
||||
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(
|
||||
module: Module,
|
||||
rootModel: ModuleRootModel?,
|
||||
targetPlatform: KotlinFacetConfiguration.TargetPlatform
|
||||
targetPlatform: TargetPlatformKind<*>
|
||||
): Collection<String> {
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user