diff --git a/idea/src/META-INF/gradle.xml b/idea/src/META-INF/gradle.xml index a5ab6928978..6e6c3b6f485 100644 --- a/idea/src/META-INF/gradle.xml +++ b/idea/src/META-INF/gradle.xml @@ -12,6 +12,7 @@ + ) + fun importBySourceSet(facet: KotlinFacet, sourceSetNode: DataNode) + fun importByModule(facet: KotlinFacet, moduleNode: DataNode) } -class KotlinGradleProjectDataService : AbstractProjectDataService() { +class KotlinGradleSourceSetDataService : AbstractProjectDataService() { override fun getTargetDataKey() = GradleSourceSetData.KEY - private fun detectPlatformByPlugin(moduleNode: DataNode): TargetPlatformKind<*>? { - val projectNode = ExternalSystemApiUtil.findParent(moduleNode, ProjectKeys.PROJECT) - val externalProjectNode = ExternalSystemApiUtil.find(projectNode as DataNode<*>, ExternalProjectDataService.KEY) - return externalProjectNode?.let { - when (it.data.plugins.values.map { it.id }.firstOrNull { it.startsWith("kotlin-platform-") }) { - "kotlin-platform-jvm" -> TargetPlatformKind.Jvm[JvmTarget.JVM_1_6] - "kotlin-platform-js" -> TargetPlatformKind.JavaScript - "kotlin-platform-common" -> TargetPlatformKind.Common - else -> null - } - } - } - - private fun detectPlatformByLibrary(moduleNode: DataNode): TargetPlatformKind<*>? { - return TargetPlatformKind.ALL_PLATFORMS.firstOrNull { moduleNode.getResolvedKotlinStdlibVersionByModuleData(it.mavenLibraryId) != null } - } - override fun postProcess( toImport: Collection>, projectData: ProjectData?, @@ -76,21 +61,70 @@ class KotlinGradleProjectDataService : AbstractProjectDataService() { + override fun getTargetDataKey() = ProjectKeys.MODULE + + override fun postProcess( + toImport: MutableCollection>, + projectData: ProjectData?, + project: Project, + modelsProvider: IdeModifiableModelsProvider + ) { + for (moduleNode in toImport) { + // If source sets are present, configure facets in the their modules + if (ExternalSystemApiUtil.getChildren(moduleNode, GradleSourceSetData.KEY).isNotEmpty()) continue + + val moduleData = moduleNode.data + val ideModule = modelsProvider.findIdeModule(moduleData) ?: continue + val kotlinFacet = configureFacetByGradleModule(moduleNode, ideModule, modelsProvider) ?: continue + GradleProjectImportHandler.getInstances(project).forEach { it.importByModule(kotlinFacet, moduleNode) } + } + } +} + +private fun detectPlatformByPlugin(moduleNode: DataNode): TargetPlatformKind<*>? { + val projectNode = ExternalSystemApiUtil.findParent(moduleNode, ProjectKeys.PROJECT) + val externalProjectNode = ExternalSystemApiUtil.find(projectNode as DataNode<*>, ExternalProjectDataService.KEY) + return externalProjectNode?.let { + when (it.data.plugins.values.map { it.id }.firstOrNull { it.startsWith("kotlin-platform-") }) { + "kotlin-platform-jvm" -> TargetPlatformKind.Jvm[JvmTarget.JVM_1_6] + "kotlin-platform-js" -> TargetPlatformKind.JavaScript + "kotlin-platform-common" -> TargetPlatformKind.Common + else -> null + } + } +} + +private fun detectPlatformByLibrary(moduleNode: DataNode): TargetPlatformKind<*>? { + return TargetPlatformKind.ALL_PLATFORMS.firstOrNull { moduleNode.getResolvedKotlinStdlibVersionByModuleData(it.mavenLibraryId) != null } +} + +private fun configureFacetByGradleModule( + moduleNode: DataNode, + ideModule: Module, + modelsProvider: IdeModifiableModelsProvider +): KotlinFacet? { + val compilerVersion = moduleNode.findAll(BuildScriptClasspathData.KEY).firstOrNull()?.data?.let(::findKotlinPluginVersion) + ?: return null + val platformKind = detectPlatformByPlugin(moduleNode) ?: detectPlatformByLibrary(moduleNode) + + val coroutinesProperty = findKotlinCoroutinesProperty(ideModule.project) + + val kotlinFacet = ideModule.getOrCreateFacet(modelsProvider, false) + kotlinFacet.configureFacet(compilerVersion, coroutinesProperty, platformKind, modelsProvider) + + moduleNode.serializedCompilerArguments?.let { parseCompilerArgumentsToFacet(it, kotlinFacet) } + + return kotlinFacet +} + private val gradlePropertyFiles = listOf("local.properties", "gradle.properties") private fun findKotlinCoroutinesProperty(project: Project): CoroutineSupport { diff --git a/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt b/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt index a8322206be1..d122afc314f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt @@ -155,10 +155,9 @@ val TargetPlatformKind<*>.mavenLibraryId: String fun Module.getOrCreateFacet(modelsProvider: IdeModifiableModelsProvider, useProjectSettings: Boolean): KotlinFacet { val facetModel = modelsProvider.getModifiableFacetModel(this) - facetModel.findFacet(KotlinFacetType.TYPE_ID, KotlinFacetType.INSTANCE.defaultFacetName)?.let { return it } - - val facet = with(KotlinFacetType.INSTANCE) { createFacet(this@getOrCreateFacet, defaultFacetName, createDefaultConfiguration(), null) } - facetModel.addFacet(facet) + val facet = facetModel.findFacet(KotlinFacetType.TYPE_ID, KotlinFacetType.INSTANCE.defaultFacetName) + ?: with(KotlinFacetType.INSTANCE) { createFacet(this@getOrCreateFacet, defaultFacetName, createDefaultConfiguration(), null) } + .apply { facetModel.addFacet(this) } facet.configuration.settings.useProjectSettings = useProjectSettings return facet } diff --git a/plugins/annotation-based-compiler-plugins-ide-support/src/AbstractGradleImportHandler.kt b/plugins/annotation-based-compiler-plugins-ide-support/src/AbstractGradleImportHandler.kt index 8d11d33e37d..71dab3f9b2e 100644 --- a/plugins/annotation-based-compiler-plugins-ide-support/src/AbstractGradleImportHandler.kt +++ b/plugins/annotation-based-compiler-plugins-ide-support/src/AbstractGradleImportHandler.kt @@ -17,8 +17,10 @@ package org.jetbrains.kotlin.annotation.plugin.ide import com.intellij.openapi.externalSystem.model.DataNode +import com.intellij.openapi.externalSystem.model.ProjectKeys import com.intellij.openapi.externalSystem.model.project.ModuleData import com.intellij.openapi.externalSystem.model.task.TaskData +import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil import org.jetbrains.kotlin.idea.configuration.GradleProjectImportHandler import org.jetbrains.kotlin.idea.facet.KotlinFacet import org.jetbrains.plugins.gradle.model.data.GradleSourceSetData @@ -35,18 +37,23 @@ abstract class AbstractGradleImportHandler : GradleProjectImportHandler { abstract val dataStorageTaskName: String abstract val pluginJarFileFromIdea: File - override fun invoke(facet: KotlinFacet, sourceSetNode: DataNode) { - modifyCompilerArgumentsForPlugin(facet, getPluginSetup(sourceSetNode), + override fun importBySourceSet(facet: KotlinFacet, sourceSetNode: DataNode) { + modifyCompilerArgumentsForPlugin(facet, getPluginSetupBySourceSet(sourceSetNode), compilerPluginId = compilerPluginId, pluginName = pluginName, annotationOptionName = annotationOptionName) } - private fun getPluginSetup( - sourceSetNode: DataNode + override fun importByModule(facet: KotlinFacet, moduleNode: DataNode) { + modifyCompilerArgumentsForPlugin(facet, getPluginSetupByModule(moduleNode), + compilerPluginId = compilerPluginId, + pluginName = pluginName, + annotationOptionName = annotationOptionName) + } + + private fun getPluginSetupByModule( + moduleNode: DataNode ): AnnotationBasedCompilerPluginSetup? { - val moduleNode = sourceSetNode.parent ?: return null - if (moduleNode.data !is ModuleData) return null val dataStorageTaskData = moduleNode.children.firstOrNull { val data = it.data as? TaskData ?: return@firstOrNull false data.name == dataStorageTaskName @@ -63,4 +70,7 @@ abstract class AbstractGradleImportHandler : GradleProjectImportHandler { return AnnotationBasedCompilerPluginSetup(annotationFqNames, classpath) } + + private fun getPluginSetupBySourceSet(sourceSetNode: DataNode) = + ExternalSystemApiUtil.findParent(sourceSetNode, ProjectKeys.MODULE)?.let { getPluginSetupByModule(it) } } \ No newline at end of file