diff --git a/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetConfiguration.kt b/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetConfiguration.kt index eebb4906013..6d2f8ead0e8 100644 --- a/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetConfiguration.kt +++ b/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetConfiguration.kt @@ -21,10 +21,8 @@ import com.intellij.facet.ui.FacetEditorContext import com.intellij.facet.ui.FacetEditorTab import com.intellij.facet.ui.FacetValidatorsManager import com.intellij.openapi.components.PersistentStateComponent -import com.intellij.openapi.util.Key import org.jdom.Element import org.jetbrains.kotlin.idea.util.DescriptionAware -import java.util.* class KotlinFacetConfiguration : FacetConfiguration, PersistentStateComponent { enum class LanguageLevel(override val description: String) : DescriptionAware { @@ -38,10 +36,14 @@ class KotlinFacetConfiguration : FacetConfiguration, PersistentStateComponent): List { - return editorContext - .rootModel - .orderEntries - .asSequence() - .filterIsInstance() - .mapNotNull { it.library?.let { getLibraryProperties(presentationProvider, it) } ?.versionString } - .toList() - } - - private fun getDefaultTargetPlatform(): KotlinFacetConfiguration.TargetPlatform { - getRuntimeLibraryVersions(JSLibraryStdPresentationProvider.getInstance()).firstOrNull()?.let { - return KotlinFacetConfiguration.TargetPlatform.JS - } - - val sdk = editorContext.rootModel.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 - } - } - - private fun getDefaultLanguageLevel(libraryVersion: String? = null): KotlinFacetConfiguration.LanguageLevel { - val libVersion = libraryVersion ?: bundledRuntimeVersion() - return when { - libVersion.startsWith("1.0") -> KotlinFacetConfiguration.LanguageLevel.KOTLIN_1_0 - else -> KotlinFacetConfiguration.LanguageLevel.KOTLIN_1_1 - } - } - - private fun getLibraryLanguageLevel(): KotlinFacetConfiguration.LanguageLevel { - val presentationProvider = when (targetPlatformComboBox.selectedItem as KotlinFacetConfiguration.TargetPlatform?) { - KotlinFacetConfiguration.TargetPlatform.JS -> - JSLibraryStdPresentationProvider.getInstance() - KotlinFacetConfiguration.TargetPlatform.JVM_1_6, - KotlinFacetConfiguration.TargetPlatform.JVM_1_8, - null -> - JavaRuntimePresentationProvider.getInstance() - } - val minVersion = getRuntimeLibraryVersions(presentationProvider).minWith(VersionComparatorUtil.COMPARATOR) - return getDefaultLanguageLevel(minVersion) - } - init { libraryValidator = FrameworkLibraryValidatorWithDynamicDescription( DelegatingLibrariesValidatorContext(editorContext), @@ -145,39 +96,33 @@ class KotlinFacetEditorTab( validatorsManager.validate() } - with(configuration.state) { - if (targetPlatformKind == null) { - targetPlatformKind = getDefaultTargetPlatform() - } - - if (languageLevel == null) { - languageLevel = getDefaultLanguageLevel() - } - - if (apiLevel == null) { - apiLevel = languageLevel!!.coerceAtMost(getLibraryLanguageLevel()) - } - } + configuration.state.versionInfo.initializeIfNeeded(editorContext.module, editorContext.rootModel) reset() } override fun isModified(): Boolean { - return languageVersionComboBox.selectedItem != configuration.state.languageLevel - || targetPlatformComboBox.selectedItem != configuration.state.targetPlatformKind - || apiVersionComboBox.selectedItem != configuration.state.apiLevel + return with(configuration.state.versionInfo) { + languageVersionComboBox.selectedItem != languageLevel + || targetPlatformComboBox.selectedItem != targetPlatformKind + || apiVersionComboBox.selectedItem != apiLevel + } } override fun reset() { - languageVersionComboBox.selectedItem = configuration.state.languageLevel - targetPlatformComboBox.selectedItem = configuration.state.targetPlatformKind - apiVersionComboBox.selectedItem = configuration.state.apiLevel + with(configuration.state.versionInfo) { + languageVersionComboBox.selectedItem = languageLevel + targetPlatformComboBox.selectedItem = targetPlatformKind + apiVersionComboBox.selectedItem = apiLevel + } } override fun apply() { - configuration.state.languageLevel = languageVersionComboBox.selectedItem as KotlinFacetConfiguration.LanguageLevel? - configuration.state.targetPlatformKind = targetPlatformComboBox.selectedItem as KotlinFacetConfiguration.TargetPlatform? - configuration.state.apiLevel = apiVersionComboBox.selectedItem as KotlinFacetConfiguration.LanguageLevel? + with(configuration.state.versionInfo) { + languageLevel = languageVersionComboBox.selectedItem as KotlinFacetConfiguration.LanguageLevel? + targetPlatformKind = targetPlatformComboBox.selectedItem as KotlinFacetConfiguration.TargetPlatform? + apiLevel = apiVersionComboBox.selectedItem as KotlinFacetConfiguration.LanguageLevel? + } } override fun getDisplayName() = "General" diff --git a/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt b/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt new file mode 100644 index 00000000000..48829540a00 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt @@ -0,0 +1,102 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.facet + +import com.intellij.framework.library.LibraryVersionProperties +import com.intellij.openapi.module.Module +import com.intellij.openapi.projectRoots.JavaSdk +import com.intellij.openapi.projectRoots.JavaSdkVersion +import com.intellij.openapi.roots.LibraryOrderEntry +import com.intellij.openapi.roots.ModuleRootManager +import com.intellij.openapi.roots.ModuleRootModel +import com.intellij.openapi.roots.libraries.LibraryPresentationProvider +import com.intellij.util.text.VersionComparatorUtil +import org.jetbrains.kotlin.idea.framework.JSLibraryStdPresentationProvider +import org.jetbrains.kotlin.idea.framework.JavaRuntimePresentationProvider +import org.jetbrains.kotlin.idea.framework.getLibraryProperties +import org.jetbrains.kotlin.idea.versions.bundledRuntimeVersion + +private fun getRuntimeLibraryVersions( + module: Module, + rootModel: ModuleRootModel?, + presentationProvider: LibraryPresentationProvider +): List { + return (rootModel ?: ModuleRootManager.getInstance(module)) + .orderEntries + .asSequence() + .filterIsInstance() + .mapNotNull { it.library?.let { getLibraryProperties(presentationProvider, it) }?.versionString } + .toList() +} + +private fun getDefaultTargetPlatform(module: Module, rootModel: ModuleRootModel?): KotlinFacetConfiguration.TargetPlatform { + getRuntimeLibraryVersions(module, rootModel, JSLibraryStdPresentationProvider.getInstance()) + .firstOrNull() + ?.let { return KotlinFacetConfiguration.TargetPlatform.JS } + + 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 + } +} + +private fun getDefaultLanguageLevel(explicitVersion: String? = null): KotlinFacetConfiguration.LanguageLevel { + val libVersion = explicitVersion ?: bundledRuntimeVersion() + return when { + libVersion.startsWith("1.0") -> KotlinFacetConfiguration.LanguageLevel.KOTLIN_1_0 + else -> KotlinFacetConfiguration.LanguageLevel.KOTLIN_1_1 + } +} + +internal fun getLibraryLanguageLevel( + module: Module, + rootModel: ModuleRootModel?, + targetPlatform: KotlinFacetConfiguration.TargetPlatform? +): KotlinFacetConfiguration.LanguageLevel { + val presentationProvider = when (targetPlatform) { + KotlinFacetConfiguration.TargetPlatform.JS -> + JSLibraryStdPresentationProvider.getInstance() + KotlinFacetConfiguration.TargetPlatform.JVM_1_6, + KotlinFacetConfiguration.TargetPlatform.JVM_1_8, + null -> + JavaRuntimePresentationProvider.getInstance() + } + val minVersion = getRuntimeLibraryVersions(module, rootModel, presentationProvider).minWith(VersionComparatorUtil.COMPARATOR) + return getDefaultLanguageLevel(minVersion) +} + +internal fun KotlinFacetConfiguration.VersionInfo.initializeIfNeeded(module: Module, rootModel: ModuleRootModel?) { + if (targetPlatformKind == null) { + targetPlatformKind = getDefaultTargetPlatform(module, rootModel) + } + + if (languageLevel == null) { + languageLevel = getDefaultLanguageLevel() + } + + if (apiLevel == null) { + apiLevel = languageLevel!!.coerceAtMost(getLibraryLanguageLevel(module, rootModel, targetPlatformKind!!)) + } +} + +internal fun Module.getKotlinVersionInfo(rootModel: ModuleRootModel? = null): KotlinFacetConfiguration.VersionInfo { + val versionInfo = KotlinFacet.get(this)?.configuration?.state?.versionInfo ?: KotlinFacetConfiguration.VersionInfo() + versionInfo.initializeIfNeeded(this, rootModel) + return versionInfo +}