Kotlin Facet: Combine "General" and "Compiler" tabs

This commit is contained in:
Alexey Sedunov
2017-01-31 20:37:54 +03:00
parent 03cdc20d64
commit 8849d0e2a3
4 changed files with 71 additions and 150 deletions
@@ -120,20 +120,14 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co
this.k2jvmCompilerArguments = k2jvmCompilerArguments; this.k2jvmCompilerArguments = k2jvmCompilerArguments;
this.isProjectSettings = isProjectSettings; this.isProjectSettings = isProjectSettings;
if (isProjectSettings) { languageVersionComboBox.addActionListener(
languageVersionComboBox.addActionListener( new ActionListener() {
new ActionListener() { @Override
@Override public void actionPerformed(ActionEvent e) {
public void actionPerformed(ActionEvent e) { restrictAPIVersions();
restrictAPIVersions();
}
} }
); }
} );
else {
languageVersionPanel.setVisible(false);
apiVersionPanel.setVisible(false);
}
additionalArgsOptionsField.attachLabel(additionalArgsLabel); additionalArgsOptionsField.attachLabel(additionalArgsLabel);
@@ -298,8 +292,8 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co
@Override @Override
public boolean isModified() { public boolean isModified() {
return ComparingUtils.isModified(generateNoWarningsCheckBox, commonCompilerArguments.suppressWarnings) || return ComparingUtils.isModified(generateNoWarningsCheckBox, commonCompilerArguments.suppressWarnings) ||
(isProjectSettings && !getSelectedLanguageVersion().equals(getLanguageVersionOrDefault(commonCompilerArguments.languageVersion))) || !getSelectedLanguageVersion().equals(getLanguageVersionOrDefault(commonCompilerArguments.languageVersion)) ||
(isProjectSettings && !getSelectedAPIVersion().equals(getLanguageVersionOrDefault(commonCompilerArguments.apiVersion))) || !getSelectedAPIVersion().equals(getLanguageVersionOrDefault(commonCompilerArguments.apiVersion)) ||
!coroutineSupportComboBox.getSelectedItem().equals(CoroutineSupport.byCompilerArguments(commonCompilerArguments)) || !coroutineSupportComboBox.getSelectedItem().equals(CoroutineSupport.byCompilerArguments(commonCompilerArguments)) ||
ComparingUtils.isModified(additionalArgsOptionsField, compilerSettings.additionalArguments) || ComparingUtils.isModified(additionalArgsOptionsField, compilerSettings.additionalArguments) ||
ComparingUtils.isModified(scriptTemplatesField, compilerSettings.scriptTemplates) || ComparingUtils.isModified(scriptTemplatesField, compilerSettings.scriptTemplates) ||
@@ -340,14 +334,12 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co
@Override @Override
public void apply() throws ConfigurationException { public void apply() throws ConfigurationException {
commonCompilerArguments.suppressWarnings = generateNoWarningsCheckBox.isSelected();
if (isProjectSettings) { if (isProjectSettings) {
boolean shouldInvalidateCaches = boolean shouldInvalidateCaches =
commonCompilerArguments.languageVersion != getSelectedLanguageVersion() || commonCompilerArguments.languageVersion != getSelectedLanguageVersion() ||
commonCompilerArguments.apiVersion != getSelectedAPIVersion() || commonCompilerArguments.apiVersion != getSelectedAPIVersion() ||
!coroutineSupportComboBox.getSelectedItem().equals(CoroutineSupport.byCompilerArguments(commonCompilerArguments)); !coroutineSupportComboBox.getSelectedItem().equals(CoroutineSupport.byCompilerArguments(commonCompilerArguments));
commonCompilerArguments.languageVersion = getSelectedLanguageVersion();
commonCompilerArguments.apiVersion = getSelectedAPIVersion();
if (shouldInvalidateCaches) { if (shouldInvalidateCaches) {
ApplicationUtilsKt.runWriteAction( ApplicationUtilsKt.runWriteAction(
new Function0<Object>() { new Function0<Object>() {
@@ -360,6 +352,10 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co
); );
} }
} }
commonCompilerArguments.suppressWarnings = generateNoWarningsCheckBox.isSelected();
commonCompilerArguments.languageVersion = getSelectedLanguageVersion();
commonCompilerArguments.apiVersion = getSelectedAPIVersion();
CoroutineSupport coroutineSupport = (CoroutineSupport) coroutineSupportComboBox.getSelectedItem(); CoroutineSupport coroutineSupport = (CoroutineSupport) coroutineSupportComboBox.getSelectedItem();
commonCompilerArguments.coroutinesEnable = coroutineSupport == CoroutineSupport.ENABLED; commonCompilerArguments.coroutinesEnable = coroutineSupport == CoroutineSupport.ENABLED;
commonCompilerArguments.coroutinesWarn = coroutineSupport == CoroutineSupport.ENABLED_WITH_WARNING; commonCompilerArguments.coroutinesWarn = coroutineSupport == CoroutineSupport.ENABLED_WITH_WARNING;
@@ -395,11 +391,9 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co
@Override @Override
public void reset() { public void reset() {
generateNoWarningsCheckBox.setSelected(commonCompilerArguments.suppressWarnings); generateNoWarningsCheckBox.setSelected(commonCompilerArguments.suppressWarnings);
if (isProjectSettings) { languageVersionComboBox.setSelectedItem(getLanguageVersionOrDefault(commonCompilerArguments.languageVersion));
languageVersionComboBox.setSelectedItem(getLanguageVersionOrDefault(commonCompilerArguments.languageVersion)); apiVersionComboBox.setSelectedItem(getLanguageVersionOrDefault(commonCompilerArguments.apiVersion));
apiVersionComboBox.setSelectedItem(getLanguageVersionOrDefault(commonCompilerArguments.apiVersion)); restrictAPIVersions();
restrictAPIVersions();
}
coroutineSupportComboBox.setSelectedItem(CoroutineSupport.byCompilerArguments(commonCompilerArguments)); coroutineSupportComboBox.setSelectedItem(CoroutineSupport.byCompilerArguments(commonCompilerArguments));
additionalArgsOptionsField.setText(compilerSettings.additionalArguments); additionalArgsOptionsField.setText(compilerSettings.additionalArguments);
scriptTemplatesField.setText(compilerSettings.scriptTemplates); scriptTemplatesField.setText(compilerSettings.scriptTemplates);
@@ -442,4 +436,12 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co
public JPanel getContentPane() { public JPanel getContentPane() {
return contentPane; return contentPane;
} }
public JComboBox getLanguageVersionComboBox() {
return languageVersionComboBox;
}
public JComboBox getApiVersionComboBox() {
return apiVersionComboBox;
}
} }
@@ -60,9 +60,7 @@ class KotlinFacetConfiguration : FacetConfiguration {
): Array<FacetEditorTab> { ): Array<FacetEditorTab> {
settings.initializeIfNeeded(editorContext.module, editorContext.rootModel) settings.initializeIfNeeded(editorContext.module, editorContext.rootModel)
val compilerTab = KotlinFacetEditorCompilerTab(settings.compilerInfo, editorContext) val tabs = arrayListOf<FacetEditorTab>(KotlinFacetEditorGeneralTab(this, editorContext, validatorsManager))
val generalTab = KotlinFacetEditorGeneralTab(this, editorContext, validatorsManager, compilerTab)
val tabs = arrayListOf(generalTab, compilerTab)
KotlinFacetConfigurationExtension.EP_NAME.extensions.flatMapTo(tabs) { it.createEditorTabs(editorContext, validatorsManager) } KotlinFacetConfigurationExtension.EP_NAME.extensions.flatMapTo(tabs) { it.createEditorTabs(editorContext, validatorsManager) }
return tabs.toTypedArray() return tabs.toTypedArray()
} }
@@ -1,49 +0,0 @@
/*
* 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.facet.ui.FacetEditorContext
import com.intellij.facet.ui.FacetEditorTab
import org.jetbrains.kotlin.config.KotlinCompilerInfo
import org.jetbrains.kotlin.idea.compiler.configuration.KotlinCompilerConfigurableTab
class KotlinFacetEditorCompilerTab(
compilerInfo: KotlinCompilerInfo,
editorContext: FacetEditorContext
) : FacetEditorTab() {
val compilerConfigurable = KotlinCompilerConfigurableTab(
editorContext.project,
compilerInfo.commonCompilerArguments,
compilerInfo.k2jsCompilerArguments,
compilerInfo.compilerSettings,
null,
null,
false
)
override fun apply() = compilerConfigurable.apply()
override fun getDisplayName() = "Compiler"
override fun createComponent() = compilerConfigurable.createComponent()!!
override fun disposeUIResources() = compilerConfigurable.disposeUIResources()
override fun isModified() = compilerConfigurable.isModified
override fun reset() = compilerConfigurable.reset()
}
@@ -23,19 +23,21 @@ import com.intellij.util.ui.FormBuilder
import com.intellij.util.ui.UIUtil import com.intellij.util.ui.UIUtil
import org.jetbrains.kotlin.config.LanguageVersion import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.config.TargetPlatformKind import org.jetbrains.kotlin.config.TargetPlatformKind
import org.jetbrains.kotlin.idea.compiler.configuration.KotlinCompilerConfigurableTab
import java.awt.BorderLayout import java.awt.BorderLayout
import javax.swing.* import javax.swing.*
class KotlinFacetEditorGeneralTab( class KotlinFacetEditorGeneralTab(
private val configuration: KotlinFacetConfiguration, private val configuration: KotlinFacetConfiguration,
private val editorContext: FacetEditorContext, private val editorContext: FacetEditorContext,
validatorsManager: FacetValidatorsManager, validatorsManager: FacetValidatorsManager
private val compilerTab: KotlinFacetEditorCompilerTab
) : FacetEditorTab() { ) : FacetEditorTab() {
inner class VersionValidator : FacetEditorValidator() { inner class VersionValidator : FacetEditorValidator() {
override fun check(): ValidationResult { override fun check(): ValidationResult {
val apiLevel = apiVersionComboBox.selectedItem as? LanguageVersion? ?: return ValidationResult.OK val apiLevel = compilerConfigurable.apiVersionComboBox.selectedItem as? LanguageVersion?
val languageLevel = languageVersionComboBox.selectedItem as? LanguageVersion? ?: return ValidationResult.OK ?: return ValidationResult.OK
val languageLevel = compilerConfigurable.languageVersionComboBox.selectedItem as? LanguageVersion?
?: return ValidationResult.OK
val targetPlatform = targetPlatformComboBox.selectedItem as TargetPlatformKind<*>? val targetPlatform = targetPlatformComboBox.selectedItem as TargetPlatformKind<*>?
val libraryLevel = getLibraryLanguageLevel(editorContext.module, editorContext.rootModel, targetPlatform) val libraryLevel = getLibraryLanguageLevel(editorContext.module, editorContext.rootModel, targetPlatform)
if (languageLevel < apiLevel || libraryLevel < apiLevel) { if (languageLevel < apiLevel || libraryLevel < apiLevel) {
@@ -45,18 +47,20 @@ class KotlinFacetEditorGeneralTab(
} }
} }
private val compilerConfigurable = with(configuration.settings.compilerInfo) {
KotlinCompilerConfigurableTab(
editorContext.project,
commonCompilerArguments,
k2jsCompilerArguments,
compilerSettings,
null,
null,
false
)
}
private val useProjectSettingsCheckBox = JCheckBox("Use project settings") private val useProjectSettingsCheckBox = JCheckBox("Use project settings")
private val languageVersionComboBox =
JComboBox<LanguageVersion>(LanguageVersion.values()).apply {
setRenderer(DescriptionListCellRenderer())
}
private val apiVersionComboBox =
JComboBox<LanguageVersion>(LanguageVersion.values()).apply {
setRenderer(DescriptionListCellRenderer())
}
private val targetPlatformComboBox = private val targetPlatformComboBox =
JComboBox<TargetPlatformKind<*>>(TargetPlatformKind.ALL_PLATFORMS.toTypedArray()).apply { JComboBox<TargetPlatformKind<*>>(TargetPlatformKind.ALL_PLATFORMS.toTypedArray()).apply {
setRenderer(DescriptionListCellRenderer()) setRenderer(DescriptionListCellRenderer())
@@ -76,105 +80,71 @@ class KotlinFacetEditorGeneralTab(
validatorsManager.registerValidator(versionValidator) validatorsManager.registerValidator(versionValidator)
useProjectSettingsCheckBox.addActionListener { useProjectSettingsCheckBox.addActionListener {
useProjectSettingsChanged() updateCompilerConfigurable()
} }
languageVersionComboBox.addActionListener { compilerConfigurable.languageVersionComboBox.addActionListener {
validatorsManager.validate() validatorsManager.validate()
restrictAPIVersions()
} }
apiVersionComboBox.addActionListener { compilerConfigurable.apiVersionComboBox.addActionListener {
validatorsManager.validate() validatorsManager.validate()
} }
targetPlatformComboBox.addActionListener { targetPlatformComboBox.addActionListener {
validatorsManager.validate() validatorsManager.validate()
updateCompilerTab() updateCompilerConfigurable()
} }
updateCompilerTab() updateCompilerConfigurable()
reset() reset()
} }
private fun restrictAPIVersions() { private fun updateCompilerConfigurable() {
val selectedLanguageVersion = languageVersionComboBox.selectedItem as LanguageVersion? ?: return compilerConfigurable.setTargetPlatform(chosenPlatform)
val selectedAPIVersion = apiVersionComboBox.selectedItem as LanguageVersion? UIUtil.setEnabled(compilerConfigurable.contentPane, !useProjectSettingsCheckBox.isSelected, true)
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
apiVersionComboBox.isEnabled = useModuleSpecific
updateCompilerTab()
}
private fun updateCompilerTab() {
compilerTab.compilerConfigurable.setTargetPlatform(chosenPlatform)
UIUtil.setEnabled(compilerTab.compilerConfigurable.contentPane, !useProjectSettingsCheckBox.isSelected, true)
} }
override fun isModified(): Boolean { override fun isModified(): Boolean {
if (useProjectSettingsCheckBox.isSelected != configuration.settings.useProjectSettings) return true if (useProjectSettingsCheckBox.isSelected != configuration.settings.useProjectSettings) return true
if (targetPlatformComboBox.selectedItem != configuration.settings.versionInfo.targetPlatformKind) return true
return with(configuration.settings.versionInfo) { return !useProjectSettingsCheckBox.isSelected && compilerConfigurable.isModified
if (useProjectSettingsCheckBox.isSelected) return targetPlatformComboBox.selectedItem != targetPlatformKind
languageVersionComboBox.selectedItem != languageLevel
|| targetPlatformComboBox.selectedItem != targetPlatformKind
|| apiVersionComboBox.selectedItem != apiLevel
}
} }
override fun reset() { override fun reset() {
useProjectSettingsCheckBox.isSelected = configuration.settings.useProjectSettings useProjectSettingsCheckBox.isSelected = configuration.settings.useProjectSettings
with(configuration.settings.versionInfo) { targetPlatformComboBox.selectedItem = configuration.settings.versionInfo.targetPlatformKind
languageVersionComboBox.selectedItem = languageLevel compilerConfigurable.reset()
apiVersionComboBox.selectedItem = apiLevel updateCompilerConfigurable()
targetPlatformComboBox.selectedItem = targetPlatformKind
restrictAPIVersions()
}
useProjectSettingsChanged()
} }
override fun apply() { override fun apply() {
configuration.settings.useProjectSettings = useProjectSettingsCheckBox.isSelected compilerConfigurable.apply()
with(configuration.settings.versionInfo) { with(configuration.settings) {
if (!useProjectSettingsCheckBox.isSelected) { useProjectSettings = useProjectSettingsCheckBox.isSelected
languageLevel = languageVersionComboBox.selectedItem as LanguageVersion? versionInfo.targetPlatformKind = targetPlatformComboBox.selectedItem as TargetPlatformKind<*>?
apiLevel = apiVersionComboBox.selectedItem as LanguageVersion? versionInfo.languageLevel = LanguageVersion.fromVersionString(compilerInfo.commonCompilerArguments?.languageVersion)
} versionInfo.apiLevel = LanguageVersion.fromVersionString(compilerInfo.commonCompilerArguments?.apiVersion)
targetPlatformKind = targetPlatformComboBox.selectedItem as TargetPlatformKind<*>?
} }
} }
override fun getDisplayName() = "General" override fun getDisplayName() = "General"
override fun createComponent(): JComponent { override fun createComponent(): JComponent {
val mainPanel = JPanel(BorderLayout()) val mainPanel = JPanel(BorderLayout())
val contentPanel = FormBuilder val contentPanel = FormBuilder
.createFormBuilder() .createFormBuilder()
.addComponent(useProjectSettingsCheckBox) .addComponent(useProjectSettingsCheckBox)
.addLabeledComponent("&Language version: ", languageVersionComboBox) .addLabeledComponent("&Target platform: ", targetPlatformComboBox)
.addLabeledComponent("&Standard library API version: ", apiVersionComboBox) .addComponent(compilerConfigurable.createComponent()!!)
.addLabeledComponent("&Target platform: ", targetPlatformComboBox) .panel
.panel
mainPanel.add(contentPanel, BorderLayout.NORTH) mainPanel.add(contentPanel, BorderLayout.NORTH)
return mainPanel return mainPanel
} }
override fun disposeUIResources() { override fun disposeUIResources() {
compilerConfigurable.disposeUIResources()
} }
val chosenPlatform: TargetPlatformKind<*>? val chosenPlatform: TargetPlatformKind<*>?