1
+ // Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
2
+ package com.jetbrains.ls.imports.jps
3
+
4
+ import com.intellij.util.xmlb.XmlSerializer
5
+ import org.jdom.Element
6
+ import org.jetbrains.jps.model.JpsElement
7
+ import org.jetbrains.jps.model.JpsProject
8
+ import org.jetbrains.jps.model.ex.JpsElementBase
9
+ import org.jetbrains.jps.model.ex.JpsElementChildRoleBase
10
+ import org.jetbrains.jps.model.module.JpsModule
11
+ import org.jetbrains.jps.model.serialization.JpsProjectExtensionSerializer
12
+ import org.jetbrains.jps.model.serialization.facet.JpsFacetConfigurationSerializer
13
+ import org.jetbrains.kotlin.cli.common.arguments.*
14
+ import org.jetbrains.kotlin.config.*
15
+
16
+ // A copy-paste from the obsolete and incompatible
17
+ // kotlin-jps-plugin-classpath-2.1.21.jar library
18
+ internal class KotlinModelSerializerService : KotlinCommonJpsModelSerializerExtension () {
19
+ override fun getProjectExtensionSerializers () = listOf (
20
+ KotlinCommonCompilerArgumentsSerializer (),
21
+ Kotlin2JvmCompilerArgumentsSerializer (),
22
+ Kotlin2JsCompilerArgumentsSerializer (),
23
+ KotlinCompilerSettingsSerializer (),
24
+ KotlinJpsPluginSettingsSerializer ()
25
+ )
26
+
27
+ override fun getFacetConfigurationSerializers () = listOf (JpsKotlinFacetConfigurationSerializer )
28
+ }
29
+
30
+ internal object JpsKotlinFacetConfigurationSerializer : JpsFacetConfigurationSerializer<JpsKotlinFacetModuleExtension>(
31
+ JpsKotlinFacetModuleExtension .KIND ,
32
+ JpsKotlinFacetModuleExtension .FACET_TYPE_ID ,
33
+ JpsKotlinFacetModuleExtension .FACET_NAME
34
+ ) {
35
+ override fun loadExtension (
36
+ facetConfigurationElement : Element ,
37
+ name : String ,
38
+ parent : JpsElement ? ,
39
+ module : JpsModule
40
+ ): JpsKotlinFacetModuleExtension {
41
+ return JpsKotlinFacetModuleExtension (deserializeFacetSettings(facetConfigurationElement))
42
+ }
43
+ }
44
+
45
+ internal abstract class BaseJpsCompilerSettingsSerializer <in T : Any >(
46
+ componentName : String ,
47
+ private val settingsFactory : () -> T
48
+ ) : JpsProjectExtensionSerializer(SettingConstants .KOTLIN_COMPILER_SETTINGS_FILE , componentName) {
49
+ protected abstract fun onLoad (project : JpsProject , settings : T )
50
+
51
+ override fun loadExtension (project : JpsProject , componentTag : Element ) {
52
+ val settings = settingsFactory().apply {
53
+ if (this is CommonCompilerArguments ) {
54
+ freeArgs = ArrayList ()
55
+ }
56
+ }
57
+ XmlSerializer .deserializeInto(settings, componentTag)
58
+ onLoad(project, settings)
59
+ }
60
+ }
61
+
62
+ internal class KotlinCompilerSettingsSerializer : BaseJpsCompilerSettingsSerializer <CompilerSettings >(
63
+ SettingConstants .KOTLIN_COMPILER_SETTINGS_SECTION , ::CompilerSettings
64
+ ) {
65
+ override fun onLoad (project : JpsProject , settings : CompilerSettings ) {
66
+ project.kotlinCompilerSettings = settings
67
+ }
68
+ }
69
+
70
+ internal class KotlinJpsPluginSettingsSerializer : BaseJpsCompilerSettingsSerializer <JpsPluginSettings >(
71
+ SettingConstants .KOTLIN_JPS_PLUGIN_SETTINGS_SECTION , ::JpsPluginSettings
72
+ ) {
73
+ override fun onLoad (project : JpsProject , settings : JpsPluginSettings ) {
74
+ project.kotlinJpsPluginSettings = settings
75
+ }
76
+ }
77
+
78
+ internal class KotlinCommonCompilerArgumentsSerializer : BaseJpsCompilerSettingsSerializer <CommonCompilerArguments .DummyImpl >(
79
+ SettingConstants .KOTLIN_COMMON_COMPILER_ARGUMENTS_SECTION , CommonCompilerArguments : :DummyImpl
80
+ ) {
81
+ override fun onLoad (project : JpsProject , settings : CommonCompilerArguments .DummyImpl ) {
82
+ settings.setApiVersionToLanguageVersionIfNeeded()
83
+ project.kotlinCommonCompilerArguments = settings
84
+ }
85
+ }
86
+
87
+ internal class Kotlin2JsCompilerArgumentsSerializer : BaseJpsCompilerSettingsSerializer <K2JSCompilerArguments >(
88
+ SettingConstants .KOTLIN_TO_JS_COMPILER_ARGUMENTS_SECTION , ::K2JSCompilerArguments
89
+ ) {
90
+ override fun onLoad (project : JpsProject , settings : K2JSCompilerArguments ) {
91
+ project.k2JsCompilerArguments = settings
92
+ }
93
+ }
94
+
95
+ internal class Kotlin2JvmCompilerArgumentsSerializer : BaseJpsCompilerSettingsSerializer <K2JVMCompilerArguments >(
96
+ SettingConstants .KOTLIN_TO_JVM_COMPILER_ARGUMENTS_SECTION , ::K2JVMCompilerArguments
97
+ ) {
98
+ override fun onLoad (project : JpsProject , settings : K2JVMCompilerArguments ) {
99
+ project.k2JvmCompilerArguments = settings
100
+ }
101
+ }
102
+
103
+ internal class JpsKotlinFacetModuleExtension (settings : KotlinFacetSettings ) : JpsElementBase<JpsKotlinFacetModuleExtension>() {
104
+ var settings = settings
105
+ private set
106
+
107
+ companion object {
108
+ val KIND : JpsElementChildRoleBase <JpsKotlinFacetModuleExtension > = JpsElementChildRoleBase .create(" kotlin facet extension" )
109
+
110
+ // These must be changed in sync with KotlinFacetType.TYPE_ID and KotlinFacetType.NAME
111
+ const val FACET_TYPE_ID = " kotlin-language"
112
+ const val FACET_NAME = " Kotlin"
113
+ }
114
+ }
115
+
116
+ internal var JpsProject .kotlinCompilerSettings
117
+ get() = kotlinCompilerSettingsContainer.compilerSettings
118
+ set(value) {
119
+ getOrCreateSettings().compilerSettings = value
120
+ }
121
+
122
+ internal var JpsProject .kotlinJpsPluginSettings
123
+ get() = kotlinCompilerSettingsContainer.jpsPluginSettings
124
+ set(value) {
125
+ getOrCreateSettings().jpsPluginSettings = value
126
+ }
127
+
128
+ internal var JpsProject .kotlinCommonCompilerArguments
129
+ get() = kotlinCompilerSettingsContainer.commonCompilerArguments
130
+ set(value) {
131
+ getOrCreateSettings().commonCompilerArguments = value
132
+ }
133
+
134
+ internal var JpsProject .k2MetadataCompilerArguments
135
+ get() = kotlinCompilerSettingsContainer.k2MetadataCompilerArguments
136
+ set(value) {
137
+ getOrCreateSettings().k2MetadataCompilerArguments = value
138
+ }
139
+
140
+ internal var JpsProject .k2JsCompilerArguments
141
+ get() = kotlinCompilerSettingsContainer.k2JsCompilerArguments
142
+ set(value) {
143
+ getOrCreateSettings().k2JsCompilerArguments = value
144
+ }
145
+
146
+ internal var JpsProject .k2JvmCompilerArguments
147
+ get() = kotlinCompilerSettingsContainer.k2JvmCompilerArguments
148
+ set(value) {
149
+ getOrCreateSettings().k2JvmCompilerArguments = value
150
+ }
151
+
152
+ internal val JpsProject .kotlinCompilerSettingsContainer
153
+ get() = container.getChild(JpsKotlinCompilerSettings .ROLE ) ? : JpsKotlinCompilerSettings ()
154
+
155
+ private fun JpsProject.getOrCreateSettings (): JpsKotlinCompilerSettings {
156
+ var settings = container.getChild(JpsKotlinCompilerSettings .ROLE )
157
+ if (settings == null ) {
158
+ settings = JpsKotlinCompilerSettings ()
159
+ container.setChild(JpsKotlinCompilerSettings .ROLE , settings)
160
+ }
161
+ return settings
162
+ }
163
+
164
+ internal class JpsKotlinCompilerSettings : JpsElementBase <JpsKotlinCompilerSettings >() {
165
+ internal var commonCompilerArguments: CommonCompilerArguments = CommonCompilerArguments .DummyImpl ()
166
+ internal var k2MetadataCompilerArguments = K2MetadataCompilerArguments ()
167
+ internal var k2JvmCompilerArguments = K2JVMCompilerArguments ()
168
+ internal var k2JsCompilerArguments = K2JSCompilerArguments ()
169
+ internal var compilerSettings = CompilerSettings ()
170
+ internal var jpsPluginSettings = JpsPluginSettings ()
171
+
172
+ @Suppress(" UNCHECKED_CAST" )
173
+ internal operator fun <T : CommonCompilerArguments > get (compilerArgumentsClass : Class <T >): T = when (compilerArgumentsClass) {
174
+ K2MetadataCompilerArguments ::class .java -> k2MetadataCompilerArguments as T
175
+ K2JVMCompilerArguments ::class .java -> k2JvmCompilerArguments as T
176
+ K2JSCompilerArguments ::class .java -> k2JsCompilerArguments as T
177
+ else -> commonCompilerArguments as T
178
+ }
179
+
180
+ companion object {
181
+ val ROLE : JpsElementChildRoleBase <JpsKotlinCompilerSettings > = JpsElementChildRoleBase .create(" Kotlin Compiler Settings" )
182
+ }
183
+ }
0 commit comments