Configuration: Prohibit api-version > language-version in Facet and Project Settings

#KT-16015 Fixed
This commit is contained in:
Alexey Sedunov
2017-01-27 17:31:39 +03:00
parent 1420bd1b33
commit 34b0e175ca
3 changed files with 72 additions and 6 deletions
@@ -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<CommonCompilerArguments> {
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() {
@@ -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<String> permittedAPIVersions = ArraysKt.mapNotNull(
LanguageVersion.values(),
new Function1<LanguageVersion, String>() {
@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);
@@ -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<LanguageVersion>(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()
}