Skip to content

Commit 641a9a7

Browse files
committed
Kotlin Facet: Get rid of duplicating data in facet configuration
1 parent 19ea18a commit 641a9a7

File tree

36 files changed

+856
-407
lines changed

36 files changed

+856
-407
lines changed

idea/idea-analysis/src/org/jetbrains/kotlin/idea/facet/KotlinVersionInfoProvider.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fun getLibraryLanguageLevel(
5454
rootModel: ModuleRootModel?,
5555
targetPlatform: TargetPlatformKind<*>?
5656
): LanguageVersion {
57-
val minVersion = getRuntimeLibraryVersions(module, rootModel, targetPlatform ?: TargetPlatformKind.Jvm[JvmTarget.JVM_1_8])
57+
val minVersion = getRuntimeLibraryVersions(module, rootModel, targetPlatform ?: TargetPlatformKind.DEFAULT_PLATFORM)
5858
.minWith(VersionComparatorUtil.COMPARATOR)
5959
return getDefaultLanguageLevel(module, minVersion)
6060
}
@@ -75,7 +75,7 @@ fun getDefaultLanguageLevel(
7575
}
7676

7777
fun getRuntimeLibraryVersion(module: Module): String? {
78-
val targetPlatform = KotlinFacetSettingsProvider.getInstance(module.project).getSettings(module).versionInfo.targetPlatformKind
79-
val versions = getRuntimeLibraryVersions(module, null, targetPlatform ?: TargetPlatformKind.Jvm[JvmTarget.JVM_1_8])
78+
val targetPlatform = KotlinFacetSettingsProvider.getInstance(module.project).getSettings(module).targetPlatformKind
79+
val versions = getRuntimeLibraryVersions(module, null, targetPlatform ?: TargetPlatformKind.DEFAULT_PLATFORM)
8080
return versions.toSet().singleOrNull()
8181
}

idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/Platform.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fun Module.getAndCacheLanguageLevelByDependencies(): LanguageVersion {
4747
val languageLevel = getLibraryLanguageLevel(
4848
this,
4949
null,
50-
KotlinFacetSettingsProvider.getInstance(project).getSettings(this).versionInfo.targetPlatformKind
50+
KotlinFacetSettingsProvider.getInstance(project).getSettings(this).targetPlatformKind
5151
)
5252

5353
// Preserve inferred version in facet/project settings
@@ -63,7 +63,7 @@ fun Module.getAndCacheLanguageLevelByDependencies(): LanguageVersion {
6363
}
6464
}
6565
else {
66-
with(facetSettings.versionInfo) {
66+
with(facetSettings) {
6767
if (this.languageLevel == null) {
6868
this.languageLevel = languageLevel
6969
}
@@ -101,22 +101,21 @@ val Module.languageVersionSettings: LanguageVersionSettings
101101
get() {
102102
val facetSettings = KotlinFacetSettingsProvider.getInstance(project).getSettings(this)
103103
if (facetSettings.useProjectSettings) return project.getLanguageVersionSettings(this)
104-
val versionInfo = facetSettings.versionInfo
105-
val languageVersion = versionInfo.languageLevel ?: getAndCacheLanguageLevelByDependencies()
106-
val apiVersion = versionInfo.apiLevel ?: languageVersion
104+
val languageVersion = facetSettings.languageLevel ?: getAndCacheLanguageLevelByDependencies()
105+
val apiVersion = facetSettings.apiLevel ?: languageVersion
107106

108107
val extraLanguageFeatures = getExtraLanguageFeatures(
109-
versionInfo.targetPlatformKind ?: TargetPlatformKind.Common,
110-
facetSettings.compilerInfo.coroutineSupport,
111-
facetSettings.compilerInfo.compilerSettings,
108+
facetSettings.targetPlatformKind ?: TargetPlatformKind.Common,
109+
facetSettings.coroutineSupport,
110+
facetSettings.compilerSettings,
112111
this
113112
)
114113

115114
return LanguageVersionSettingsImpl(languageVersion, ApiVersion.createByLanguageVersion(apiVersion), extraLanguageFeatures)
116115
}
117116

118117
val Module.targetPlatform: TargetPlatformKind<*>?
119-
get() = KotlinFacetSettingsProvider.getInstance(project).getSettings(this).versionInfo.targetPlatformKind
118+
get() = KotlinFacetSettingsProvider.getInstance(project).getSettings(this).targetPlatformKind
120119

121120
private val Module.implementsCommonModule: Boolean
122121
get() = targetPlatform != TargetPlatformKind.Common

idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/ProjectStructureUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public Result<TargetPlatform> compute() {
6868
@Nullable
6969
private static TargetPlatform getPlatformConfiguredInFacet(@NotNull Module module) {
7070
KotlinFacetSettings settings = KotlinFacetSettingsProvider.Companion.getInstance(module.getProject()).getSettings(module);
71-
TargetPlatformKind<?> kind = settings.getVersionInfo().getTargetPlatformKind();
71+
TargetPlatformKind<?> kind = settings.getTargetPlatformKind();
7272
if (kind instanceof TargetPlatformKind.Jvm) {
7373
return JvmPlatform.INSTANCE;
7474
}

idea/idea-jps-common/src/org/jetbrains/kotlin/config/KotlinFacetSettings.kt

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ package org.jetbrains.kotlin.config
1919
import com.intellij.openapi.components.ServiceManager
2020
import com.intellij.openapi.module.Module
2121
import com.intellij.openapi.project.Project
22-
import com.intellij.util.xmlb.annotations.Property
23-
import com.intellij.util.xmlb.annotations.Transient
2422
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
2523
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
2624
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
25+
import org.jetbrains.kotlin.cli.common.arguments.K2MetadataCompilerArguments
2726
import org.jetbrains.kotlin.utils.DescriptionAware
2827

2928
sealed class TargetPlatformKind<out Version : DescriptionAware>(
@@ -45,24 +44,12 @@ sealed class TargetPlatformKind<out Version : DescriptionAware>(
4544
object Common : TargetPlatformKind<DescriptionAware.NoVersion>(DescriptionAware.NoVersion, "Common (experimental)")
4645

4746
companion object {
48-
4947
val ALL_PLATFORMS: List<TargetPlatformKind<*>> by lazy { Jvm.JVM_PLATFORMS + JavaScript + Common }
48+
val DEFAULT_PLATFORM: TargetPlatformKind<*>
49+
get() = Jvm[JvmTarget.DEFAULT]
5050
}
5151
}
5252

53-
data class KotlinVersionInfo(
54-
var languageLevel: LanguageVersion? = null,
55-
var apiLevel: LanguageVersion? = null,
56-
@get:Transient var targetPlatformKind: TargetPlatformKind<*>? = null
57-
) {
58-
// To be serialized
59-
var targetPlatformName: String
60-
get() = targetPlatformKind?.description ?: ""
61-
set(value) {
62-
targetPlatformKind = TargetPlatformKind.ALL_PLATFORMS.firstOrNull { it.description == value }
63-
}
64-
}
65-
6653
enum class CoroutineSupport(
6754
override val description: String,
6855
val compilerArgument: String
@@ -90,38 +77,62 @@ enum class CoroutineSupport(
9077
}
9178
}
9279

93-
class KotlinCompilerInfo {
94-
// To be serialized
95-
@Property private var _commonCompilerArguments: CommonCompilerArguments.DummyImpl? = null
96-
@get:Transient var commonCompilerArguments: CommonCompilerArguments?
97-
get() = _commonCompilerArguments
98-
set(value) {
99-
_commonCompilerArguments = value as? CommonCompilerArguments.DummyImpl
100-
}
101-
var k2jsCompilerArguments: K2JSCompilerArguments? = null
102-
var k2jvmCompilerArguments: K2JVMCompilerArguments? = null
103-
var compilerSettings: CompilerSettings? = null
104-
105-
@get:Transient var coroutineSupport: CoroutineSupport
106-
get() = CoroutineSupport.byCompilerArguments(commonCompilerArguments)
107-
set(value) {
108-
commonCompilerArguments?.coroutinesEnable = value == CoroutineSupport.ENABLED
109-
commonCompilerArguments?.coroutinesWarn = value == CoroutineSupport.ENABLED_WITH_WARNING
110-
commonCompilerArguments?.coroutinesError = value == CoroutineSupport.DISABLED
111-
}
112-
}
113-
11480
class KotlinFacetSettings {
11581
companion object {
11682
// Increment this when making serialization-incompatible changes to configuration data
117-
val CURRENT_VERSION = 1
83+
val CURRENT_VERSION = 2
11884
val DEFAULT_VERSION = 0
11985
}
12086

12187
var useProjectSettings: Boolean = true
12288

123-
var versionInfo = KotlinVersionInfo()
124-
var compilerInfo = KotlinCompilerInfo()
89+
var compilerArguments: CommonCompilerArguments? = null
90+
var compilerSettings: CompilerSettings? = null
91+
92+
var languageLevel: LanguageVersion?
93+
get() = compilerArguments?.languageVersion?.let { LanguageVersion.fromFullVersionString(it) }
94+
set(value) {
95+
compilerArguments!!.languageVersion = value?.versionString
96+
}
97+
98+
var apiLevel: LanguageVersion?
99+
get() = compilerArguments?.apiVersion?.let { LanguageVersion.fromFullVersionString(it) }
100+
set(value) {
101+
compilerArguments!!.apiVersion = value?.versionString
102+
}
103+
104+
val targetPlatformKind: TargetPlatformKind<*>?
105+
get() = compilerArguments?.let {
106+
when (it) {
107+
is K2JVMCompilerArguments -> {
108+
val jvmTarget = it.jvmTarget ?: JvmTarget.DEFAULT.description
109+
TargetPlatformKind.Jvm.JVM_PLATFORMS.firstOrNull { it.version.description >= jvmTarget }
110+
}
111+
is K2JSCompilerArguments -> TargetPlatformKind.JavaScript
112+
is K2MetadataCompilerArguments -> TargetPlatformKind.Common
113+
else -> null
114+
}
115+
}
116+
117+
var coroutineSupport: CoroutineSupport
118+
get() = CoroutineSupport.byCompilerArguments(compilerArguments)
119+
set(value) {
120+
with(compilerArguments!!) {
121+
coroutinesEnable = value == CoroutineSupport.ENABLED
122+
coroutinesWarn = value == CoroutineSupport.ENABLED_WITH_WARNING
123+
coroutinesError = value == CoroutineSupport.DISABLED
124+
}
125+
}
126+
}
127+
128+
fun TargetPlatformKind<*>.createCompilerArguments(): CommonCompilerArguments {
129+
return when (this) {
130+
is TargetPlatformKind.Jvm -> {
131+
K2JVMCompilerArguments().apply { jvmTarget = this@createCompilerArguments.version.description }
132+
}
133+
is TargetPlatformKind.JavaScript -> K2JSCompilerArguments()
134+
is TargetPlatformKind.Common -> K2MetadataCompilerArguments()
135+
}
125136
}
126137

127138
interface KotlinFacetSettingsProvider {

idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/KotlinMavenImporter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class KotlinMavenImporter : MavenImporter(KOTLIN_PLUGIN_GROUP_ID, KOTLIN_PLUGIN_
150150
val platform = detectPlatformByExecutions(mavenProject) ?: detectPlatformByLibraries(mavenProject)
151151

152152
kotlinFacet.configureFacet(compilerVersion, CoroutineSupport.DEFAULT, platform, modifiableModelsProvider)
153-
val configuredPlatform = kotlinFacet.configuration.settings.versionInfo.targetPlatformKind!!
153+
val configuredPlatform = kotlinFacet.configuration.settings.targetPlatformKind!!
154154
val configuration = mavenPlugin.configurationElement
155155
val sharedArguments = configuration?.let { getCompilerArgumentsByConfigurationElement(it, configuredPlatform) } ?: emptyList()
156156
val executionArguments = mavenPlugin.executions?.filter { it.goals.any { it in compilationGoals } }

idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenImporterTest.kt

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.jetbrains.kotlin.idea.maven
1818

19+
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
20+
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
1921
import org.jetbrains.kotlin.config.KotlinFacetSettings
2022
import org.jetbrains.kotlin.config.TargetPlatformKind
2123
import org.jetbrains.kotlin.idea.facet.KotlinFacet
@@ -442,16 +444,16 @@ class KotlinMavenImporterTest : MavenImportingTestCase() {
442444
assertImporterStatePresent()
443445

444446
with (facetSettings) {
445-
Assert.assertEquals("1.1", versionInfo.languageLevel!!.versionString)
446-
Assert.assertEquals("1.1", compilerInfo.commonCompilerArguments!!.languageVersion)
447-
Assert.assertEquals("1.0", versionInfo.apiLevel!!.versionString)
448-
Assert.assertEquals("1.0", compilerInfo.commonCompilerArguments!!.apiVersion)
449-
Assert.assertEquals(true, compilerInfo.commonCompilerArguments!!.suppressWarnings)
450-
Assert.assertEquals("enable", compilerInfo.coroutineSupport.compilerArgument)
451-
Assert.assertEquals("JVM 1.8", versionInfo.targetPlatformKind!!.description)
452-
Assert.assertEquals("1.8", compilerInfo.k2jvmCompilerArguments!!.jvmTarget)
447+
Assert.assertEquals("1.1", languageLevel!!.versionString)
448+
Assert.assertEquals("1.1", compilerArguments!!.languageVersion)
449+
Assert.assertEquals("1.0", apiLevel!!.versionString)
450+
Assert.assertEquals("1.0", compilerArguments!!.apiVersion)
451+
Assert.assertEquals(true, compilerArguments!!.suppressWarnings)
452+
Assert.assertEquals("enable", coroutineSupport.compilerArgument)
453+
Assert.assertEquals("JVM 1.8", targetPlatformKind!!.description)
454+
Assert.assertEquals("1.8", (compilerArguments as K2JVMCompilerArguments).jvmTarget)
453455
Assert.assertEquals("-cp foobar.jar -jdk-home JDK_HOME -Xmulti-platform",
454-
compilerInfo.compilerSettings!!.additionalArguments)
456+
compilerSettings!!.additionalArguments)
455457
}
456458
}
457459

@@ -510,17 +512,19 @@ class KotlinMavenImporterTest : MavenImportingTestCase() {
510512
assertImporterStatePresent()
511513

512514
with (facetSettings) {
513-
Assert.assertEquals("1.1", versionInfo.languageLevel!!.versionString)
514-
Assert.assertEquals("1.1", compilerInfo.commonCompilerArguments!!.languageVersion)
515-
Assert.assertEquals("1.0", versionInfo.apiLevel!!.versionString)
516-
Assert.assertEquals("1.0", compilerInfo.commonCompilerArguments!!.apiVersion)
517-
Assert.assertEquals(true, compilerInfo.commonCompilerArguments!!.suppressWarnings)
518-
Assert.assertEquals("enable", compilerInfo.coroutineSupport.compilerArgument)
519-
Assert.assertTrue(versionInfo.targetPlatformKind is TargetPlatformKind.JavaScript)
520-
Assert.assertEquals(true, compilerInfo.k2jsCompilerArguments!!.sourceMap)
521-
Assert.assertEquals("commonjs", compilerInfo.k2jsCompilerArguments!!.moduleKind)
515+
Assert.assertEquals("1.1", languageLevel!!.versionString)
516+
Assert.assertEquals("1.1", compilerArguments!!.languageVersion)
517+
Assert.assertEquals("1.0", apiLevel!!.versionString)
518+
Assert.assertEquals("1.0", compilerArguments!!.apiVersion)
519+
Assert.assertEquals(true, compilerArguments!!.suppressWarnings)
520+
Assert.assertEquals("enable", coroutineSupport.compilerArgument)
521+
Assert.assertTrue(targetPlatformKind is TargetPlatformKind.JavaScript)
522+
with(compilerArguments as K2JSCompilerArguments) {
523+
Assert.assertEquals(true, sourceMap)
524+
Assert.assertEquals("commonjs", moduleKind)
525+
}
522526
Assert.assertEquals("-output test.js -meta-info -Xmulti-platform",
523-
compilerInfo.compilerSettings!!.additionalArguments)
527+
compilerSettings!!.additionalArguments)
524528
}
525529
}
526530

@@ -580,16 +584,16 @@ class KotlinMavenImporterTest : MavenImportingTestCase() {
580584
assertImporterStatePresent()
581585

582586
with (facetSettings) {
583-
Assert.assertEquals("1.1", versionInfo.languageLevel!!.versionString)
584-
Assert.assertEquals("1.1", compilerInfo.commonCompilerArguments!!.languageVersion)
585-
Assert.assertEquals("1.0", versionInfo.apiLevel!!.versionString)
586-
Assert.assertEquals("1.0", compilerInfo.commonCompilerArguments!!.apiVersion)
587-
Assert.assertEquals(true, compilerInfo.commonCompilerArguments!!.suppressWarnings)
588-
Assert.assertEquals("enable", compilerInfo.coroutineSupport.compilerArgument)
589-
Assert.assertEquals("JVM 1.8", versionInfo.targetPlatformKind!!.description)
590-
Assert.assertEquals("1.8", compilerInfo.k2jvmCompilerArguments!!.jvmTarget)
587+
Assert.assertEquals("1.1", languageLevel!!.versionString)
588+
Assert.assertEquals("1.1", compilerArguments!!.languageVersion)
589+
Assert.assertEquals("1.0", apiLevel!!.versionString)
590+
Assert.assertEquals("1.0", compilerArguments!!.apiVersion)
591+
Assert.assertEquals(true, compilerArguments!!.suppressWarnings)
592+
Assert.assertEquals("enable", coroutineSupport.compilerArgument)
593+
Assert.assertEquals("JVM 1.8", targetPlatformKind!!.description)
594+
Assert.assertEquals("1.8", (compilerArguments as K2JVMCompilerArguments).jvmTarget)
591595
Assert.assertEquals("-cp foobar.jar -jdk-home JDK_HOME -Xmulti-platform",
592-
compilerInfo.compilerSettings!!.additionalArguments)
596+
compilerSettings!!.additionalArguments)
593597
}
594598
}
595599

@@ -642,9 +646,9 @@ class KotlinMavenImporterTest : MavenImportingTestCase() {
642646
assertImporterStatePresent()
643647

644648
with (facetSettings) {
645-
Assert.assertEquals("JVM 1.8", versionInfo.targetPlatformKind!!.description)
646-
Assert.assertEquals("1.8", compilerInfo.k2jvmCompilerArguments!!.jvmTarget)
647-
Assert.assertEquals("enable", compilerInfo.coroutineSupport.compilerArgument)
649+
Assert.assertEquals("JVM 1.8", targetPlatformKind!!.description)
650+
Assert.assertEquals("1.8", (compilerArguments as K2JVMCompilerArguments).jvmTarget)
651+
Assert.assertEquals("enable", coroutineSupport.compilerArgument)
648652
}
649653
}
650654

idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/KotlinProjectDescriptorWithFacet.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ import org.jetbrains.kotlin.config.LanguageVersion
2424
import org.jetbrains.kotlin.idea.facet.KotlinFacet
2525
import org.jetbrains.kotlin.idea.facet.KotlinFacetConfiguration
2626
import org.jetbrains.kotlin.idea.facet.KotlinFacetType
27+
import org.jetbrains.kotlin.idea.facet.initializeIfNeeded
2728
import org.jetbrains.kotlin.test.testFramework.runInEdtAndWait
2829
import org.jetbrains.kotlin.test.testFramework.runWriteAction
2930

3031
class KotlinProjectDescriptorWithFacet(val languageVersion: LanguageVersion) : KotlinLightProjectDescriptor() {
3132
override fun configureModule(module: Module, model: ModifiableRootModel, contentEntry: ContentEntry) {
3233
configureKotlinFacet(module) { ->
33-
settings.versionInfo.languageLevel = languageVersion
34+
settings.languageLevel = languageVersion
3435
}
3536
}
3637

@@ -43,6 +44,7 @@ fun configureKotlinFacet(module: Module, configureCallback: KotlinFacetConfigura
4344
val facetManager = FacetManager.getInstance(module)
4445
val facetModel = facetManager.createModifiableModel()
4546
val configuration = KotlinFacetConfiguration()
47+
configuration.settings.initializeIfNeeded(module, null)
4648
configuration.settings.useProjectSettings = false
4749
configuration.configureCallback()
4850
val facet = facetManager.createFacet(KotlinFacetType.INSTANCE, "Kotlin", configuration, null)

0 commit comments

Comments
 (0)