Kotlin Facet: Do not show implicit compiler arguments in "Additional arguments" field

This commit is contained in:
Alexey Sedunov
2017-02-08 11:55:35 +03:00
parent 52102d359a
commit ba73269ee0
6 changed files with 33 additions and 13 deletions
@@ -127,8 +127,8 @@ class KotlinMavenImporter : MavenImporter(KOTLIN_PLUGIN_GROUP_ID, KOTLIN_PLUGIN_
with(kotlinFacet.configuration.settings) {
versionInfo.apiLevel = apiVersion
}
parseCompilerArgumentsToFacet(sharedArguments, kotlinFacet)
parseCompilerArgumentsToFacet(executionArguments, kotlinFacet)
parseCompilerArgumentsToFacet(sharedArguments, emptyList(), kotlinFacet)
parseCompilerArgumentsToFacet(executionArguments, emptyList(), kotlinFacet)
MavenProjectImportHandler.getInstances(module.project).forEach { it(kotlinFacet, mavenProject) }
}
@@ -25,13 +25,15 @@ import java.lang.Exception
interface KotlinGradleModel : Serializable {
val implements: String?
val serializedCompilerArguments: List<String>?
val currentCompilerArguments: List<String>?
val defaultCompilerArguments: List<String>?
val coroutines: String?
}
class KotlinGradleModelImpl(
override val implements: String?,
override val serializedCompilerArguments: List<String>?,
override val currentCompilerArguments: List<String>?,
override val defaultCompilerArguments: List<String>?,
override val coroutines: String?
) : KotlinGradleModel
@@ -56,11 +58,11 @@ class KotlinGradleModelBuilder : ModelBuilderService {
}
@Suppress("UNCHECKED_CAST")
private fun getCompilerArguments(project: Project): List<String>? {
private fun getCompilerArguments(project: Project, methodName: String): List<String>? {
val compileTask = compileTasks.mapNotNull { project.getTasksByName(it, false).firstOrNull() }.firstOrNull() ?: return null
val taskClass = compileTask.javaClass
return try {
taskClass.getDeclaredMethod("getSerializedCompilerArguments").invoke(compileTask) as List<String>
taskClass.getDeclaredMethod(methodName).invoke(compileTask) as List<String>
}
catch (e : NoSuchMethodException) {
null
@@ -85,5 +87,10 @@ class KotlinGradleModelBuilder : ModelBuilderService {
}
override fun buildAll(modelName: String?, project: Project) =
KotlinGradleModelImpl(getImplements(project), getCompilerArguments(project), getCoroutines(project))
KotlinGradleModelImpl(
getImplements(project),
getCompilerArguments(project, "getSerializedCompilerArguments"),
getCompilerArguments(project, "getDefaultSerializedCompilerArguments"),
getCoroutines(project)
)
}
@@ -33,7 +33,8 @@ import org.jetbrains.plugins.gradle.model.data.GradleSourceSetData
import org.jetbrains.plugins.gradle.service.project.AbstractProjectResolverExtension
import org.jetbrains.plugins.gradle.service.project.GradleProjectResolverUtil.getModuleId
var DataNode<ModuleData>.serializedCompilerArguments by UserDataProperty(Key.create<List<String>>("SERIALIZED_COMPILER_ARGUMENTS"))
var DataNode<ModuleData>.currentCompilerArguments by UserDataProperty(Key.create<List<String>>("CURRENT_COMPILER_ARGUMENTS"))
var DataNode<ModuleData>.defaultCompilerArguments by UserDataProperty(Key.create<List<String>>("DEFAULT_COMPILER_ARGUMENTS"))
var DataNode<ModuleData>.coroutines by UserDataProperty(Key.create<String>("KOTLIN_COROUTINES"))
class KotlinGradleProjectResolverExtension : AbstractProjectResolverExtension() {
@@ -60,7 +61,8 @@ class KotlinGradleProjectResolverExtension : AbstractProjectResolverExtension()
}
}
ideModule.serializedCompilerArguments = gradleModel.serializedCompilerArguments
ideModule.currentCompilerArguments = gradleModel.currentCompilerArguments
ideModule.defaultCompilerArguments = gradleModel.defaultCompilerArguments
ideModule.coroutines = gradleModel.coroutines
super.populateModuleDependencies(gradleModule, ideModule, ideProject)
@@ -121,7 +121,11 @@ private fun configureFacetByGradleModule(
val kotlinFacet = ideModule.getOrCreateFacet(modelsProvider, false)
kotlinFacet.configureFacet(compilerVersion, coroutinesProperty, platformKind, modelsProvider)
moduleNode.serializedCompilerArguments?.let { parseCompilerArgumentsToFacet(it, kotlinFacet) }
val currentCompilerArguments = moduleNode.currentCompilerArguments
val defaultCompilerArguments = moduleNode.defaultCompilerArguments ?: emptyList()
if (currentCompilerArguments != null) {
parseCompilerArgumentsToFacet(currentCompilerArguments, defaultCompilerArguments, kotlinFacet)
}
return kotlinFacet
}
@@ -35,6 +35,7 @@ import org.jetbrains.kotlin.idea.compiler.configuration.KotlinCompilerSettings
import org.jetbrains.kotlin.idea.framework.JSLibraryStdPresentationProvider
import org.jetbrains.kotlin.idea.framework.JavaRuntimePresentationProvider
import org.jetbrains.kotlin.idea.versions.*
import java.lang.reflect.Field
private fun getRuntimeLibraryVersions(
module: Module,
@@ -207,7 +208,7 @@ private val CommonCompilerArguments.exposedFields: List<String>
else -> commonExposedFields
}
fun parseCompilerArgumentsToFacet(arguments: List<String>, kotlinFacet: KotlinFacet) {
fun parseCompilerArgumentsToFacet(arguments: List<String>, defaultArguments: List<String>, kotlinFacet: KotlinFacet) {
val argumentArray = arguments.toTypedArray()
with(kotlinFacet.configuration.settings) {
@@ -220,6 +221,9 @@ fun parseCompilerArgumentsToFacet(arguments: List<String>, kotlinFacet: KotlinFa
else -> commonCompilerArguments
}!!
val defaultCompilerArguments = compilerArguments.javaClass.newInstance()
parseArguments(defaultArguments.toTypedArray(), defaultCompilerArguments, true)
val oldCoroutineSupport = CoroutineSupport.byCompilerArguments(commonCompilerArguments)
commonCompilerArguments.coroutinesEnable = false
commonCompilerArguments.coroutinesWarn = false
@@ -248,15 +252,17 @@ fun parseCompilerArgumentsToFacet(arguments: List<String>, kotlinFacet: KotlinFa
val exposedFields = compilerArguments.exposedFields
fun exposeAsAdditionalArgument(field: Field) = field.name !in exposedFields && field.get(compilerArguments) != field.get(defaultCompilerArguments)
val additionalArgumentsString = with(compilerArguments.javaClass.newInstance()) {
copyFieldsSatisfying(compilerArguments, this) { it.name !in exposedFields }
copyFieldsSatisfying(compilerArguments, this, ::exposeAsAdditionalArgument)
ArgumentUtils.convertArgumentsToStringList(this).joinToString(separator = " ")
}
compilerInfo.compilerSettings!!.additionalArguments =
if (additionalArgumentsString.isNotEmpty()) additionalArgumentsString else CompilerSettings.DEFAULT_ADDITIONAL_ARGUMENTS
with(compilerArguments.javaClass.newInstance()) {
copyFieldsSatisfying(this, compilerArguments) { it.name !in exposedFields }
copyFieldsSatisfying(this, compilerArguments, ::exposeAsAdditionalArgument)
}
copyInheritedFields(compilerArguments, commonCompilerArguments)
@@ -28,6 +28,7 @@ class GradleFacetImportTest : GradleImportingTestCase() {
private val facetSettings: KotlinFacetSettings
get() = KotlinFacet.get(getModule("project_main"))!!.configuration.settings
// TODO: Update this test to 1.1-RC when it's available
@Test
fun testJvmImport() {
createProjectSubFile("build.gradle", """