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