KT-3008 Add UI that allows to change module kind in IDEA Kotlin configurator

This commit is contained in:
Alexey Andreev
2016-04-22 14:06:16 +03:00
parent 496539d101
commit b090b79d92
6 changed files with 71 additions and 14 deletions
@@ -24,6 +24,11 @@ import static org.jetbrains.kotlin.cli.common.arguments.K2JsArgumentConstants.CA
import static org.jetbrains.kotlin.cli.common.arguments.K2JsArgumentConstants.NO_CALL;
public class K2JSCompilerArguments extends CommonCompilerArguments {
public static final String MODULE_PLAIN = "plain";
public static final String MODULE_AMD = "amd";
public static final String MODULE_COMMONJS = "commonjs";
public static final String MODULE_UMD = "umd";
@Argument(value = "output", description = "Output file path")
@ValueDescription("<path>")
public String outputFile;
@@ -278,16 +278,16 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
ModuleKind moduleKind = ModuleKind.PLAIN;
if (moduleKindName != null) {
moduleKindName = moduleKindName.toLowerCase();
if (moduleKindName.equals("plain")) {
if (moduleKindName.equals(K2JSCompilerArguments.MODULE_PLAIN)) {
moduleKind = ModuleKind.PLAIN;
}
else if (moduleKindName.equals("amd")) {
else if (moduleKindName.equals(K2JSCompilerArguments.MODULE_AMD)) {
moduleKind = ModuleKind.AMD;
}
else if (moduleKindName.equals("commonjs")) {
else if (moduleKindName.equals(K2JSCompilerArguments.MODULE_COMMONJS)) {
moduleKind = ModuleKind.COMMON_JS;
}
else if (moduleKindName.equals("umd")) {
else if (moduleKindName.equals(K2JSCompilerArguments.MODULE_UMD)) {
moduleKind = ModuleKind.UMD;
}
else {
@@ -3,7 +3,7 @@
<grid id="27dc6" binding="contentPane" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="594" height="357"/>
<xy x="20" y="20" width="828" height="622"/>
</constraints>
<properties/>
<border type="none"/>
@@ -81,7 +81,7 @@
</component>
</children>
</grid>
<grid id="98e8a" layout-manager="GridLayoutManager" row-count="5" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="98e8a" layout-manager="GridLayoutManager" row-count="6" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
@@ -160,6 +160,23 @@
<text resource-bundle="org/jetbrains/kotlin/idea/KotlinBundle" key="kotlin.compiler.js.option.output.copy.files"/>
</properties>
</component>
<component id="915d" class="javax.swing.JLabel">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="ca29c"/>
<text value="Module &amp;kind:"/>
</properties>
</component>
<component id="ca29c" class="javax.swing.JComboBox" binding="moduleKindComboBox">
<constraints>
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model/>
</properties>
</component>
</children>
</grid>
</children>
@@ -40,8 +40,18 @@ import org.jetbrains.kotlin.idea.PluginStartupComponent;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.util.List;
import static java.util.Arrays.asList;
public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Configurable.NoScroll{
private static final List<String> moduleKindDescriptions = asList(
"Plain (put to global scope)",
"AMD",
"CommonJS/node.js",
"UMD (detect AMD or CommonJS if available, fallback to plain)"
);
private static final List<String> moduleKindIds = asList("plain", "amd", "commonjs", "umd");
private final CommonCompilerArguments commonCompilerArguments;
private final K2JSCompilerArguments k2jsCompilerArguments;
private final CompilerSettings compilerSettings;
@@ -61,6 +71,7 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co
private JCheckBox copyRuntimeFilesCheckBox;
private JCheckBox keepAliveCheckBox;
private JCheckBox enablePreciseIncrementalCheckBox;
private JComboBox moduleKindComboBox;
public KotlinCompilerConfigurableTab(Project project) {
this.commonCompilerArguments = KotlinCommonCompilerArgumentsHolder.getInstance(project).getSettings();
@@ -84,6 +95,16 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co
labelForOutputDirectory.setEnabled(copyRuntimeFilesCheckBox.isSelected());
}
});
fillModuleKindList();
}
@SuppressWarnings("unchecked")
private void fillModuleKindList() {
for (String description : moduleKindDescriptions) {
// TODO: not sure if it's a right way to add items to a combo box
moduleKindComboBox.addItem(description);
}
}
@NotNull
@@ -116,7 +137,12 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co
ComparingUtils.isModified(generateSourceMapsCheckBox, k2jsCompilerArguments.sourceMap) ||
isModified(outputPrefixFile, k2jsCompilerArguments.outputPrefix) ||
isModified(outputPostfixFile, k2jsCompilerArguments.outputPostfix);
isModified(outputPostfixFile, k2jsCompilerArguments.outputPostfix) ||
!getSelectedModuleKind().equals(k2jsCompilerArguments.moduleKind);
}
private String getSelectedModuleKind() {
return moduleKindIds.get(moduleKindComboBox.getSelectedIndex());
}
@Override
@@ -137,6 +163,7 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co
k2jsCompilerArguments.sourceMap = generateSourceMapsCheckBox.isSelected();
k2jsCompilerArguments.outputPrefix = StringUtil.nullize(outputPrefixFile.getText(), true);
k2jsCompilerArguments.outputPostfix = StringUtil.nullize(outputPostfixFile.getText(), true);
k2jsCompilerArguments.moduleKind = getSelectedModuleKind();
BuildManager.getInstance().clearState(project);
}
@@ -154,6 +181,13 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co
generateSourceMapsCheckBox.setSelected(k2jsCompilerArguments.sourceMap);
outputPrefixFile.setText(k2jsCompilerArguments.outputPrefix);
outputPostfixFile.setText(k2jsCompilerArguments.outputPostfix);
String moduleKind = k2jsCompilerArguments.moduleKind;
int index = moduleKind != null ? moduleKindIds.indexOf(moduleKind) : 0;
if (index < 0) {
index = 0;
}
moduleKindComboBox.setSelectedIndex(index);
}
@Override
@@ -347,7 +347,7 @@ public abstract class BasicTest extends KotlinTestWithEnvironment {
configuration.put(JSConfigurationKeys.UNIT_TEST_CONFIG, shouldBeTranslateAsUnitTestClass());
setupConfig(configBuilder);
setupConfig(configuration);
return new LibrarySourcesConfig(project, configuration);
}
@@ -18,9 +18,10 @@ package org.jetbrains.kotlin.js.test
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.util.text.StringUtil
import org.jetbrains.kotlin.js.config.Config
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.js.config.EcmaVersion
import org.jetbrains.kotlin.js.config.LibrarySourcesConfig
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
import org.jetbrains.kotlin.js.config.JsConfig
import org.jetbrains.kotlin.js.facade.MainCallParameters
import org.jetbrains.kotlin.js.test.rhino.RhinoFunctionResultChecker
import org.jetbrains.kotlin.js.test.utils.JsTestUtils.getAllFilesInDir
@@ -28,7 +29,7 @@ import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.serialization.js.ModuleKind
import org.jetbrains.kotlin.utils.KotlinJavascriptMetadataUtils
import java.io.File
import java.util.LinkedHashMap
import java.util.*
abstract class MultipleModulesTranslationTest(main: String) : BasicTest(main) {
private val MAIN_MODULE_NAME: String = "main"
@@ -70,7 +71,7 @@ abstract class MultipleModulesTranslationTest(main: String) : BasicTest(main) {
private fun getMetaFileOutputPath(moduleDirectoryName: String, version: EcmaVersion) =
KotlinJavascriptMetadataUtils.replaceSuffix(getOutputFilePath(moduleDirectoryName, version))
override fun setupConfig(builder: LibrarySourcesConfig.Builder) {
override fun setupConfig(configuration: CompilerConfiguration) {
val method = try {
javaClass.getMethod(name)
}
@@ -79,7 +80,7 @@ abstract class MultipleModulesTranslationTest(main: String) : BasicTest(main) {
}
method.getAnnotation(WithModuleKind::class.java)?.let { moduleKind = it.value }
builder.moduleKind(moduleKind)
configuration.put(JSConfigurationKeys.MODULE_KIND, moduleKind)
}
override fun shouldGenerateMetaInfo() = true
@@ -100,7 +101,7 @@ abstract class MultipleModulesTranslationTest(main: String) : BasicTest(main) {
}
override fun translateFiles(jetFiles: MutableList<KtFile>, outputFile: File, mainCallParameters: MainCallParameters,
config: Config) {
config: JsConfig) {
super.translateFiles(jetFiles, outputFile, mainCallParameters, config)
if (config.moduleKind == ModuleKind.COMMON_JS) {