Kotlin Facet: Import more configuration options from Maven
diff --git a/idea/idea-maven/idea-maven.iml b/idea/idea-maven/idea-maven.iml
index d272a4c..6c9da6c 100644
--- a/idea/idea-maven/idea-maven.iml
+++ b/idea/idea-maven/idea-maven.iml
@@ -24,5 +24,6 @@
     <orderEntry type="module" module-name="util" />
     <orderEntry type="module" module-name="cli-common" />
     <orderEntry type="library" name="idea-full" level="project" />
+    <orderEntry type="module" module-name="build-common" />
   </component>
 </module>
\ No newline at end of file
diff --git a/idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/KotlinMavenImporter.kt b/idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/KotlinMavenImporter.kt
index 09be0b9..893fb7f 100644
--- a/idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/KotlinMavenImporter.kt
+++ b/idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/KotlinMavenImporter.kt
@@ -33,6 +33,10 @@
 import org.jetbrains.idea.maven.project.*
 import org.jetbrains.jps.model.java.JavaSourceRootType
 import org.jetbrains.jps.model.module.JpsModuleSourceRootType
+import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
+import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
+import org.jetbrains.kotlin.cli.common.arguments.K2MetadataCompilerArguments
+import org.jetbrains.kotlin.compilerRunner.ArgumentUtils
 import org.jetbrains.kotlin.config.CoroutineSupport
 import org.jetbrains.kotlin.config.JvmTarget
 import org.jetbrains.kotlin.config.LanguageVersion
@@ -102,8 +106,36 @@
         configureFacet(mavenProject, modifiableModelsProvider, module)
     }
 
-    private fun getCompilerArgumentsByConfigurationElement(configuration: Element) =
-            configuration.getChild("args")?.getChildren("arg")?.map { it.text } ?: emptyList()
+    private fun getCompilerArgumentsByConfigurationElement(configuration: Element, platform: TargetPlatformKind<*>): List<String> {
+        val arguments = when (platform) {
+            is TargetPlatformKind.Jvm -> K2JVMCompilerArguments()
+            is TargetPlatformKind.JavaScript -> K2JSCompilerArguments()
+            is TargetPlatformKind.Common -> K2MetadataCompilerArguments()
+        }
+
+        arguments.apiVersion = configuration.getChild("apiVersion")?.text
+        arguments.languageVersion = configuration.getChild("languageVersion")?.text
+        arguments.multiPlatform = configuration.getChild("multiPlatform")?.text?.trim()?.toBoolean() ?: false
+        arguments.suppressWarnings = configuration.getChild("nowarn")?.text?.trim()?.toBoolean() ?: false
+        when (arguments) {
+            is K2JVMCompilerArguments -> {
+                arguments.classpath = configuration.getChild("classpath")?.text
+                arguments.jdkHome = configuration.getChild("jdkHome")?.text
+                arguments.jvmTarget = configuration.getChild("jvmTarget")?.text
+            }
+            is K2JSCompilerArguments -> {
+                arguments.sourceMap = configuration.getChild("sourceMap")?.text?.trim()?.toBoolean() ?: false
+                arguments.outputFile = configuration.getChild("outputFile")?.text
+                arguments.metaInfo = configuration.getChild("metaInfo")?.text?.trim()?.toBoolean() ?: false
+                arguments.moduleKind = configuration.getChild("moduleKind")?.text
+            }
+        }
+
+        return ArrayList<String>().apply {
+            this += ArgumentUtils.convertArgumentsToStringList(arguments)
+            configuration.getChild("args")?.getChildren("arg")?.mapNotNullTo(this) { it.text }
+        }
+    }
 
     private val compilationGoals = listOf(PomFile.KotlinGoals.Compile,
                                           PomFile.KotlinGoals.TestCompile,
@@ -118,19 +150,16 @@
         val platform = detectPlatformByExecutions(mavenProject) ?: detectPlatformByLibraries(mavenProject)
 
         kotlinFacet.configureFacet(compilerVersion, CoroutineSupport.DEFAULT, platform, modifiableModelsProvider)
+        val configuredPlatform = kotlinFacet.configuration.settings.versionInfo.targetPlatformKind!!
         val configuration = mavenPlugin.configurationElement
-        val apiVersion = configuration?.getChild("apiVersion")?.text?.let { LanguageVersion.fromFullVersionString(it) }
-        val sharedArguments = configuration?.let { getCompilerArgumentsByConfigurationElement(it) } ?: emptyList()
+        val sharedArguments = configuration?.let { getCompilerArgumentsByConfigurationElement(it, configuredPlatform) } ?: emptyList()
         val executionArguments = mavenPlugin.executions?.filter { it.goals.any { it in compilationGoals } }
                                          ?.firstOrNull()
-                                         ?.configurationElement?.let { getCompilerArgumentsByConfigurationElement(it) }
-                                 ?: emptyList()
-        with(kotlinFacet.configuration.settings) {
-            versionInfo.apiLevel = apiVersion
-            compilerInfo.k2jvmCompilerArguments?.jvmTarget = configuration?.getChild("jvmTarget")?.text
-        }
+                                         ?.configurationElement?.let { getCompilerArgumentsByConfigurationElement(it, configuredPlatform) }
         parseCompilerArgumentsToFacet(sharedArguments, emptyList(), kotlinFacet)
-        parseCompilerArgumentsToFacet(executionArguments, emptyList(), kotlinFacet)
+        if (executionArguments != null) {
+            parseCompilerArgumentsToFacet(executionArguments, emptyList(), kotlinFacet, true)
+        }
         MavenProjectImportHandler.getInstances(module.project).forEach { it(kotlinFacet, mavenProject) }
     }
 
diff --git a/idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenImporterTest.kt b/idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenImporterTest.kt
index 609b557..f7994a0 100644
--- a/idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenImporterTest.kt
+++ b/idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenImporterTest.kt
@@ -17,6 +17,7 @@
 package org.jetbrains.kotlin.idea.maven
 
 import org.jetbrains.kotlin.config.KotlinFacetSettings
+import org.jetbrains.kotlin.config.TargetPlatformKind
 import org.jetbrains.kotlin.idea.facet.KotlinFacet
 import org.junit.Assert
 import java.io.File
@@ -387,7 +388,7 @@
         assertTestSources("project", "src/test/java", "src/test/kotlin", "src/test/kotlin.jvm")
     }
 
-    fun testJvmTarget() {
+    fun testJvmFacetConfiguration() {
         createProjectSubDirs("src/main/kotlin", "src/main/kotlin.jvm", "src/test/kotlin", "src/test/kotlin.jvm")
 
         importProject("""
@@ -421,6 +422,153 @@
                         </execution>
                     </executions>
                     <configuration>
+                        <languageVersion>1.1</languageVersion>
+                        <apiVersion>1.0</apiVersion>
+                        <multiPlatform>true</multiPlatform>
+                        <nowarn>true</nowarn>
+                        <args>
+                            <arg>-Xcoroutines=enable</arg>
+                        </args>
+                        <jvmTarget>1.8</jvmTarget>
+                        <jdkHome>JDK_HOME</jdkHome>
+                        <classpath>foobar.jar</classpath>
+                    </configuration>
+                </plugin>
+            </plugins>
+        </build>
+        """)
+
+        assertModules("project")
+        assertImporterStatePresent()
+
+        with (facetSettings) {
+            Assert.assertEquals("1.1", versionInfo.languageLevel!!.versionString)
+            Assert.assertEquals("1.1", compilerInfo.commonCompilerArguments!!.languageVersion)
+            Assert.assertEquals("1.0", versionInfo.apiLevel!!.versionString)
+            Assert.assertEquals("1.0", compilerInfo.commonCompilerArguments!!.apiVersion)
+            Assert.assertEquals(true, compilerInfo.commonCompilerArguments!!.suppressWarnings)
+            Assert.assertEquals("enable", compilerInfo.coroutineSupport.compilerArgument)
+            Assert.assertEquals("JVM 1.8", versionInfo.targetPlatformKind!!.description)
+            Assert.assertEquals("1.8", compilerInfo.k2jvmCompilerArguments!!.jvmTarget)
+            Assert.assertEquals("-cp foobar.jar -jdk-home JDK_HOME -Xmulti-platform",
+                                compilerInfo.compilerSettings!!.additionalArguments)
+        }
+    }
+
+    fun testJsFacetConfiguration() {
+        createProjectSubDirs("src/main/kotlin", "src/main/kotlin.jvm", "src/test/kotlin", "src/test/kotlin.jvm")
+
+        importProject("""
+        <groupId>test</groupId>
+        <artifactId>project</artifactId>
+        <version>1.0.0</version>
+
+        <dependencies>
+            <dependency>
+                <groupId>org.jetbrains.kotlin</groupId>
+                <artifactId>kotlin-stdlib</artifactId>
+                <version>$kotlinVersion</version>
+            </dependency>
+        </dependencies>
+
+        <build>
+            <sourceDirectory>src/main/kotlin</sourceDirectory>
+
+            <plugins>
+                <plugin>
+                    <groupId>org.jetbrains.kotlin</groupId>
+                    <artifactId>kotlin-maven-plugin</artifactId>
+
+                    <executions>
+                        <execution>
+                            <id>compile</id>
+                            <phase>compile</phase>
+                            <goals>
+                                <goal>js</goal>
+                            </goals>
+                        </execution>
+                    </executions>
+                    <configuration>
+                        <languageVersion>1.1</languageVersion>
+                        <apiVersion>1.0</apiVersion>
+                        <multiPlatform>true</multiPlatform>
+                        <nowarn>true</nowarn>
+                        <args>
+                            <arg>-Xcoroutines=enable</arg>
+                        </args>
+                        <sourceMap>true</sourceMap>
+                        <outputFile>test.js</outputFile>
+                        <metaInfo>true</metaInfo>
+                        <moduleKind>commonjs</moduleKind>
+                    </configuration>
+                </plugin>
+            </plugins>
+        </build>
+        """)
+
+        assertModules("project")
+        assertImporterStatePresent()
+
+        with (facetSettings) {
+            Assert.assertEquals("1.1", versionInfo.languageLevel!!.versionString)
+            Assert.assertEquals("1.1", compilerInfo.commonCompilerArguments!!.languageVersion)
+            Assert.assertEquals("1.0", versionInfo.apiLevel!!.versionString)
+            Assert.assertEquals("1.0", compilerInfo.commonCompilerArguments!!.apiVersion)
+            Assert.assertEquals(true, compilerInfo.commonCompilerArguments!!.suppressWarnings)
+            Assert.assertEquals("enable", compilerInfo.coroutineSupport.compilerArgument)
+            Assert.assertTrue(versionInfo.targetPlatformKind is TargetPlatformKind.JavaScript)
+            Assert.assertEquals(true, compilerInfo.k2jsCompilerArguments!!.sourceMap)
+            Assert.assertEquals("commonjs", compilerInfo.k2jsCompilerArguments!!.moduleKind)
+            Assert.assertEquals("-output test.js -meta-info -Xmulti-platform",
+                                compilerInfo.compilerSettings!!.additionalArguments)
+        }
+    }
+
+    fun testFacetSplitConfiguration() {
+        createProjectSubDirs("src/main/kotlin", "src/main/kotlin.jvm", "src/test/kotlin", "src/test/kotlin.jvm")
+
+        importProject("""
+        <groupId>test</groupId>
+        <artifactId>project</artifactId>
+        <version>1.0.0</version>
+
+        <dependencies>
+            <dependency>
+                <groupId>org.jetbrains.kotlin</groupId>
+                <artifactId>kotlin-stdlib</artifactId>
+                <version>$kotlinVersion</version>
+            </dependency>
+        </dependencies>
+
+        <build>
+            <sourceDirectory>src/main/kotlin</sourceDirectory>
+
+            <plugins>
+                <plugin>
+                    <groupId>org.jetbrains.kotlin</groupId>
+                    <artifactId>kotlin-maven-plugin</artifactId>
+
+                    <executions>
+                        <execution>
+                            <id>compile</id>
+                            <phase>compile</phase>
+                            <goals>
+                                <goal>compile</goal>
+                            </goals>
+                            <configuration>
+                                <languageVersion>1.1</languageVersion>
+                                <multiPlatform>true</multiPlatform>
+                                <args>
+                                    <arg>-Xcoroutines=enable</arg>
+                                </args>
+                                <jdkHome>JDK_HOME</jdkHome>
+                                <classpath>foobar.jar</classpath>
+                            </configuration>
+                        </execution>
+                    </executions>
+                    <configuration>
+                        <apiVersion>1.0</apiVersion>
+                        <nowarn>true</nowarn>
                         <jvmTarget>1.8</jvmTarget>
                     </configuration>
                 </plugin>
@@ -432,8 +580,16 @@
         assertImporterStatePresent()
 
         with (facetSettings) {
+            Assert.assertEquals("1.1", versionInfo.languageLevel!!.versionString)
+            Assert.assertEquals("1.1", compilerInfo.commonCompilerArguments!!.languageVersion)
+            Assert.assertEquals("1.0", versionInfo.apiLevel!!.versionString)
+            Assert.assertEquals("1.0", compilerInfo.commonCompilerArguments!!.apiVersion)
+            Assert.assertEquals(true, compilerInfo.commonCompilerArguments!!.suppressWarnings)
+            Assert.assertEquals("enable", compilerInfo.coroutineSupport.compilerArgument)
             Assert.assertEquals("JVM 1.8", versionInfo.targetPlatformKind!!.description)
             Assert.assertEquals("1.8", compilerInfo.k2jvmCompilerArguments!!.jvmTarget)
+            Assert.assertEquals("-version -cp foobar.jar -jdk-home JDK_HOME -Xmulti-platform",
+                                compilerInfo.compilerSettings!!.additionalArguments)
         }
     }
 
diff --git a/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt b/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt
index 6c62c7a..546b752 100644
--- a/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt
+++ b/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt
@@ -156,7 +156,12 @@
         else -> commonExposedFields
     }
 
-fun parseCompilerArgumentsToFacet(arguments: List<String>, defaultArguments: List<String>, kotlinFacet: KotlinFacet) {
+fun parseCompilerArgumentsToFacet(
+        arguments: List<String>,
+        defaultArguments: List<String>,
+        kotlinFacet: KotlinFacet,
+        appendAdditionalArguments: Boolean = false
+) {
     val argumentArray = arguments.toTypedArray()
 
     with(kotlinFacet.configuration.settings) {
@@ -211,8 +216,14 @@
             copyFieldsSatisfying(compilerArguments, this, ::exposeAsAdditionalArgument)
             ArgumentUtils.convertArgumentsToStringList(this).joinToString(separator = " ")
         }
-        compilerInfo.compilerSettings!!.additionalArguments =
-                if (additionalArgumentsString.isNotEmpty()) additionalArgumentsString else CompilerSettings.DEFAULT_ADDITIONAL_ARGUMENTS
+
+        val newAdditionalArguments = if (additionalArgumentsString.isNotEmpty()) additionalArgumentsString else CompilerSettings.DEFAULT_ADDITIONAL_ARGUMENTS
+        if (appendAdditionalArguments) {
+            compilerInfo.compilerSettings!!.additionalArguments += " $newAdditionalArguments"
+        }
+        else {
+            compilerInfo.compilerSettings!!.additionalArguments = newAdditionalArguments
+        }
 
         with(compilerArguments.javaClass.newInstance()) {
             copyFieldsSatisfying(this, compilerArguments, ::exposeAsAdditionalArgument)