From 34b0e175ca225605fb7abb725c9d62d59d921c23 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Fri, 27 Jan 2017 17:31:39 +0300 Subject: [PATCH] Configuration: Prohibit api-version > language-version in Facet and Project Settings #KT-16015 Fixed --- .../KotlinCommonCompilerArgumentsHolder.java | 14 +++++- .../KotlinCompilerConfigurableTab.java | 44 ++++++++++++++++++- .../idea/facet/KotlinFacetEditorGeneralTab.kt | 20 +++++++-- 3 files changed, 72 insertions(+), 6 deletions(-) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCommonCompilerArgumentsHolder.java b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCommonCompilerArgumentsHolder.java index 84e198bb7cf..1591b923d3e 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCommonCompilerArgumentsHolder.java +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCommonCompilerArgumentsHolder.java @@ -18,6 +18,8 @@ package org.jetbrains.kotlin.idea.compiler.configuration; import com.intellij.openapi.components.*; import com.intellij.openapi.project.Project; +import com.intellij.util.text.VersionComparatorUtil; +import org.jdom.Element; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments; @@ -31,11 +33,21 @@ import static org.jetbrains.kotlin.config.SettingConstants.KOTLIN_COMMON_COMPILE } ) public class KotlinCommonCompilerArgumentsHolder extends BaseKotlinCompilerSettings { - public static KotlinCommonCompilerArgumentsHolder getInstance(Project project) { return ServiceManager.getService(project, KotlinCommonCompilerArgumentsHolder.class); } + @Override + public void loadState(Element state) { + super.loadState(state); + + // To fix earlier configurations with incorrect combination of language and API version + CommonCompilerArguments settings = getSettings(); + if (VersionComparatorUtil.compare(settings.languageVersion, settings.apiVersion) < 0) { + settings.apiVersion = settings.languageVersion; + } + } + @NotNull @Override protected CommonCompilerArguments createSettings() { diff --git a/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerConfigurableTab.java b/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerConfigurableTab.java index 18e08343ac3..52e869ca2aa 100644 --- a/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerConfigurableTab.java +++ b/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerConfigurableTab.java @@ -31,7 +31,10 @@ import com.intellij.openapi.util.EmptyRunnable; import com.intellij.openapi.util.text.StringUtil; import com.intellij.ui.ListCellRendererWrapper; import com.intellij.ui.RawCommandLineEditor; +import com.intellij.util.text.VersionComparatorUtil; +import kotlin.collections.ArraysKt; import kotlin.jvm.functions.Function0; +import kotlin.jvm.functions.Function1; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -48,7 +51,10 @@ import org.jetbrains.kotlin.idea.util.application.ApplicationUtilsKt; import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Configurable.NoScroll{ @@ -114,7 +120,17 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co this.k2jvmCompilerArguments = k2jvmCompilerArguments; this.isProjectSettings = isProjectSettings; - if (!isProjectSettings) { + if (isProjectSettings) { + languageVersionComboBox.addActionListener( + new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + restrictAPIVersions(); + } + } + ); + } + else { languageVersionPanel.setVisible(false); apiVersionPanel.setVisible(false); } @@ -196,6 +212,31 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co return !StringUtil.equals(StringUtil.nullize(chooser.getText(), true), currentValue); } + @SuppressWarnings("unchecked") + private void restrictAPIVersions() { + String selectedAPIVersion = getSelectedAPIVersion(); + final String selectedLanguageVersion = getSelectedLanguageVersion(); + List permittedAPIVersions = ArraysKt.mapNotNull( + LanguageVersion.values(), + new Function1() { + @Override + public String invoke(LanguageVersion version) { + return VersionComparatorUtil.compare(version.getVersionString(), selectedLanguageVersion) <= 0 + ? version.getVersionString() + : null; + } + } + ); + apiVersionComboBox.setModel( + new DefaultComboBoxModel(permittedAPIVersions.toArray()) + ); + apiVersionComboBox.setSelectedItem( + VersionComparatorUtil.compare(selectedAPIVersion, selectedLanguageVersion) <= 0 + ? selectedAPIVersion + : selectedLanguageVersion + ); + } + @SuppressWarnings("unchecked") private void fillJvmVersionList() { for (TargetPlatformKind.Jvm jvm : TargetPlatformKind.Jvm.Companion.getJVM_PLATFORMS()) { @@ -357,6 +398,7 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co if (isProjectSettings) { languageVersionComboBox.setSelectedItem(getLanguageVersionOrDefault(commonCompilerArguments.languageVersion)); apiVersionComboBox.setSelectedItem(getLanguageVersionOrDefault(commonCompilerArguments.apiVersion)); + restrictAPIVersions(); } coroutineSupportComboBox.setSelectedItem(CoroutineSupport.byCompilerArguments(commonCompilerArguments)); additionalArgsOptionsField.setText(compilerSettings.additionalArguments); diff --git a/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetEditorGeneralTab.kt b/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetEditorGeneralTab.kt index d95262c85cc..a05fdab95b8 100644 --- a/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetEditorGeneralTab.kt +++ b/idea/src/org/jetbrains/kotlin/idea/facet/KotlinFacetEditorGeneralTab.kt @@ -24,10 +24,7 @@ import com.intellij.util.ui.UIUtil import org.jetbrains.kotlin.config.LanguageVersion import org.jetbrains.kotlin.config.TargetPlatformKind import java.awt.BorderLayout -import javax.swing.JCheckBox -import javax.swing.JComboBox -import javax.swing.JComponent -import javax.swing.JPanel +import javax.swing.* class KotlinFacetEditorGeneralTab( private val configuration: KotlinFacetConfiguration, @@ -84,6 +81,7 @@ class KotlinFacetEditorGeneralTab( languageVersionComboBox.addActionListener { validatorsManager.validate() + restrictAPIVersions() } apiVersionComboBox.addActionListener { @@ -100,6 +98,19 @@ class KotlinFacetEditorGeneralTab( reset() } + private fun restrictAPIVersions() { + val selectedLanguageVersion = languageVersionComboBox.selectedItem as LanguageVersion? ?: return + val selectedAPIVersion = apiVersionComboBox.selectedItem as LanguageVersion? + val permittedAPIVersions = LanguageVersion.values().filter { it <= selectedLanguageVersion } + apiVersionComboBox.model = DefaultComboBoxModel(permittedAPIVersions.toTypedArray()) + apiVersionComboBox.selectedItem = if (selectedAPIVersion != null && selectedAPIVersion > selectedLanguageVersion) { + selectedLanguageVersion + } + else { + selectedAPIVersion + } + } + private fun useProjectSettingsChanged() { val useModuleSpecific = !useProjectSettingsCheckBox.isSelected languageVersionComboBox.isEnabled = useModuleSpecific @@ -131,6 +142,7 @@ class KotlinFacetEditorGeneralTab( languageVersionComboBox.selectedItem = languageLevel apiVersionComboBox.selectedItem = apiLevel targetPlatformComboBox.selectedItem = targetPlatformKind + restrictAPIVersions() } useProjectSettingsChanged() }