Kotlin Facet: Get rid of duplicating data in facet configuration
Original commit: 641a9a7153
This commit is contained in:
@@ -19,11 +19,10 @@ package org.jetbrains.kotlin.config
|
||||
import com.intellij.openapi.components.ServiceManager
|
||||
import com.intellij.openapi.module.Module
|
||||
import com.intellij.openapi.project.Project
|
||||
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
|
||||
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
|
||||
import org.jetbrains.kotlin.cli.common.arguments.K2MetadataCompilerArguments
|
||||
import org.jetbrains.kotlin.utils.DescriptionAware
|
||||
|
||||
sealed class TargetPlatformKind<out Version : DescriptionAware>(
|
||||
@@ -45,24 +44,12 @@ sealed class TargetPlatformKind<out Version : DescriptionAware>(
|
||||
object Common : TargetPlatformKind<DescriptionAware.NoVersion>(DescriptionAware.NoVersion, "Common (experimental)")
|
||||
|
||||
companion object {
|
||||
|
||||
val ALL_PLATFORMS: List<TargetPlatformKind<*>> by lazy { Jvm.JVM_PLATFORMS + JavaScript + Common }
|
||||
val DEFAULT_PLATFORM: TargetPlatformKind<*>
|
||||
get() = Jvm[JvmTarget.DEFAULT]
|
||||
}
|
||||
}
|
||||
|
||||
data class KotlinVersionInfo(
|
||||
var languageLevel: LanguageVersion? = null,
|
||||
var apiLevel: LanguageVersion? = null,
|
||||
@get:Transient var targetPlatformKind: TargetPlatformKind<*>? = null
|
||||
) {
|
||||
// To be serialized
|
||||
var targetPlatformName: String
|
||||
get() = targetPlatformKind?.description ?: ""
|
||||
set(value) {
|
||||
targetPlatformKind = TargetPlatformKind.ALL_PLATFORMS.firstOrNull { it.description == value }
|
||||
}
|
||||
}
|
||||
|
||||
enum class CoroutineSupport(
|
||||
override val description: String,
|
||||
val compilerArgument: String
|
||||
@@ -90,38 +77,62 @@ enum class CoroutineSupport(
|
||||
}
|
||||
}
|
||||
|
||||
class KotlinCompilerInfo {
|
||||
// To be serialized
|
||||
@Property private var _commonCompilerArguments: CommonCompilerArguments.DummyImpl? = null
|
||||
@get:Transient var commonCompilerArguments: CommonCompilerArguments?
|
||||
get() = _commonCompilerArguments
|
||||
set(value) {
|
||||
_commonCompilerArguments = value as? CommonCompilerArguments.DummyImpl
|
||||
}
|
||||
var k2jsCompilerArguments: K2JSCompilerArguments? = null
|
||||
var k2jvmCompilerArguments: K2JVMCompilerArguments? = null
|
||||
var compilerSettings: CompilerSettings? = null
|
||||
|
||||
@get:Transient var coroutineSupport: CoroutineSupport
|
||||
get() = CoroutineSupport.byCompilerArguments(commonCompilerArguments)
|
||||
set(value) {
|
||||
commonCompilerArguments?.coroutinesEnable = value == CoroutineSupport.ENABLED
|
||||
commonCompilerArguments?.coroutinesWarn = value == CoroutineSupport.ENABLED_WITH_WARNING
|
||||
commonCompilerArguments?.coroutinesError = value == CoroutineSupport.DISABLED
|
||||
}
|
||||
}
|
||||
|
||||
class KotlinFacetSettings {
|
||||
companion object {
|
||||
// Increment this when making serialization-incompatible changes to configuration data
|
||||
val CURRENT_VERSION = 1
|
||||
val CURRENT_VERSION = 2
|
||||
val DEFAULT_VERSION = 0
|
||||
}
|
||||
|
||||
var useProjectSettings: Boolean = true
|
||||
|
||||
var versionInfo = KotlinVersionInfo()
|
||||
var compilerInfo = KotlinCompilerInfo()
|
||||
var compilerArguments: CommonCompilerArguments? = null
|
||||
var compilerSettings: CompilerSettings? = null
|
||||
|
||||
var languageLevel: LanguageVersion?
|
||||
get() = compilerArguments?.languageVersion?.let { LanguageVersion.fromFullVersionString(it) }
|
||||
set(value) {
|
||||
compilerArguments!!.languageVersion = value?.versionString
|
||||
}
|
||||
|
||||
var apiLevel: LanguageVersion?
|
||||
get() = compilerArguments?.apiVersion?.let { LanguageVersion.fromFullVersionString(it) }
|
||||
set(value) {
|
||||
compilerArguments!!.apiVersion = value?.versionString
|
||||
}
|
||||
|
||||
val targetPlatformKind: TargetPlatformKind<*>?
|
||||
get() = compilerArguments?.let {
|
||||
when (it) {
|
||||
is K2JVMCompilerArguments -> {
|
||||
val jvmTarget = it.jvmTarget ?: JvmTarget.DEFAULT.description
|
||||
TargetPlatformKind.Jvm.JVM_PLATFORMS.firstOrNull { it.version.description >= jvmTarget }
|
||||
}
|
||||
is K2JSCompilerArguments -> TargetPlatformKind.JavaScript
|
||||
is K2MetadataCompilerArguments -> TargetPlatformKind.Common
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
var coroutineSupport: CoroutineSupport
|
||||
get() = CoroutineSupport.byCompilerArguments(compilerArguments)
|
||||
set(value) {
|
||||
with(compilerArguments!!) {
|
||||
coroutinesEnable = value == CoroutineSupport.ENABLED
|
||||
coroutinesWarn = value == CoroutineSupport.ENABLED_WITH_WARNING
|
||||
coroutinesError = value == CoroutineSupport.DISABLED
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun TargetPlatformKind<*>.createCompilerArguments(): CommonCompilerArguments {
|
||||
return when (this) {
|
||||
is TargetPlatformKind.Jvm -> {
|
||||
K2JVMCompilerArguments().apply { jvmTarget = this@createCompilerArguments.version.description }
|
||||
}
|
||||
is TargetPlatformKind.JavaScript -> K2JSCompilerArguments()
|
||||
is TargetPlatformKind.Common -> K2MetadataCompilerArguments()
|
||||
}
|
||||
}
|
||||
|
||||
interface KotlinFacetSettingsProvider {
|
||||
|
||||
@@ -66,11 +66,8 @@ class JpsKotlinCompilerSettings : JpsElementBase<JpsKotlinCompilerSettings>() {
|
||||
val defaultArguments = getSettings(module.project).commonCompilerArguments
|
||||
val facetSettings = module.kotlinFacetExtension?.settings ?: return defaultArguments
|
||||
if (facetSettings.useProjectSettings) return defaultArguments
|
||||
val (languageLevel, apiLevel) = facetSettings.versionInfo
|
||||
val facetArguments = facetSettings.compilerInfo.commonCompilerArguments ?: return defaultArguments
|
||||
val facetArguments = facetSettings.compilerArguments ?: return defaultArguments
|
||||
return copyBean(facetArguments).apply {
|
||||
languageVersion = languageLevel?.description
|
||||
apiVersion = apiLevel?.description
|
||||
multiPlatform = module
|
||||
.dependenciesList
|
||||
.dependencies
|
||||
@@ -86,11 +83,7 @@ class JpsKotlinCompilerSettings : JpsElementBase<JpsKotlinCompilerSettings>() {
|
||||
val defaultArguments = getSettings(module.project).k2JvmCompilerArguments
|
||||
val facetSettings = module.kotlinFacetExtension?.settings ?: return defaultArguments
|
||||
if (facetSettings.useProjectSettings) return defaultArguments
|
||||
val targetPlatform = facetSettings.versionInfo.targetPlatformKind as? TargetPlatformKind.Jvm ?: return defaultArguments
|
||||
val arguments = facetSettings.compilerInfo.k2jvmCompilerArguments ?: defaultArguments
|
||||
return copyBean(arguments).apply {
|
||||
jvmTarget = targetPlatform.version.description
|
||||
}
|
||||
return facetSettings.compilerArguments as? K2JVMCompilerArguments ?: defaultArguments
|
||||
}
|
||||
|
||||
fun setK2JvmCompilerArguments(project: JpsProject, k2JvmCompilerArguments: K2JVMCompilerArguments) {
|
||||
@@ -101,7 +94,7 @@ class JpsKotlinCompilerSettings : JpsElementBase<JpsKotlinCompilerSettings>() {
|
||||
val defaultArguments = getSettings(module.project).k2JsCompilerArguments
|
||||
val facetSettings = module.kotlinFacetExtension?.settings ?: return defaultArguments
|
||||
if (facetSettings.useProjectSettings) return defaultArguments
|
||||
return facetSettings.compilerInfo.k2jsCompilerArguments ?: defaultArguments
|
||||
return facetSettings.compilerArguments as? K2JSCompilerArguments ?: defaultArguments
|
||||
}
|
||||
|
||||
fun setK2JsCompilerArguments(project: JpsProject, k2JsCompilerArguments: K2JSCompilerArguments) {
|
||||
@@ -112,7 +105,7 @@ class JpsKotlinCompilerSettings : JpsElementBase<JpsKotlinCompilerSettings>() {
|
||||
val defaultSettings = getSettings(module.project).compilerSettings
|
||||
val facetSettings = module.kotlinFacetExtension?.settings ?: return defaultSettings
|
||||
if (facetSettings.useProjectSettings) return defaultSettings
|
||||
return facetSettings.compilerInfo.compilerSettings ?: defaultSettings
|
||||
return facetSettings.compilerSettings ?: defaultSettings
|
||||
}
|
||||
|
||||
fun setCompilerSettings(project: JpsProject, compilerSettings: CompilerSettings) {
|
||||
@@ -122,4 +115,4 @@ class JpsKotlinCompilerSettings : JpsElementBase<JpsKotlinCompilerSettings>() {
|
||||
}
|
||||
|
||||
val JpsModule.targetPlatform: TargetPlatformKind<*>?
|
||||
get() = kotlinFacetExtension?.settings?.versionInfo?.targetPlatformKind
|
||||
get() = kotlinFacetExtension?.settings?.targetPlatformKind
|
||||
Reference in New Issue
Block a user