From 5e958900282bb15927d0a76c9baa9ddec1918020 Mon Sep 17 00:00:00 2001 From: Dmitry Savvinov Date: Mon, 17 Jun 2019 17:18:08 +0300 Subject: [PATCH] [Resolve] Introduce KotlinMultiplatformAnalysisModeComponent Introduce a component which tells which resolution mode should be used. COMPOSITE mode will analyze all sources for all platforms in one facade, and will use proper support in the frontend instead of CombinedModuleInfo. SEPARATE mode is essentially a "legacy" mode, where each platform is analyzed in separate facade (leading, in particular, to multiple re-analyzation of common sources) Facilities for support of COMPOSITE mode will be introduced in the following commits. resolution component --- ...otlinMultiplatformAnalysisModeComponent.kt | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/KotlinMultiplatformAnalysisModeComponent.kt diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/KotlinMultiplatformAnalysisModeComponent.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/KotlinMultiplatformAnalysisModeComponent.kt new file mode 100644 index 00000000000..822501134fe --- /dev/null +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/KotlinMultiplatformAnalysisModeComponent.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.idea.project + +import com.intellij.ide.util.PropertiesComponent +import com.intellij.openapi.module.ModuleManager +import com.intellij.openapi.project.Project +import com.intellij.openapi.util.registry.Registry +import com.intellij.util.text.nullize +import org.jetbrains.kotlin.caches.project.cacheByClassInvalidatingOnRootModifications +import org.jetbrains.kotlin.cli.common.toBooleanLenient +import org.jetbrains.kotlin.idea.facet.KotlinFacet + +object KotlinMultiplatformAnalysisModeComponent { + private const val resolutionModeOption = "kotlin.multiplatform.analysis.mode" + private val defaultState = Mode.SEPARATE + + @JvmStatic + fun setMode(project: Project, mode: Mode) { + PropertiesComponent.getInstance(project).setValue(resolutionModeOption, mode.name, defaultState.name) + } + + @JvmStatic + fun getMode(project: Project): Mode { + @Suppress("InvalidBundleOrProperty") + val explicitIdeaSetting = PropertiesComponent.getInstance(project).getValue(resolutionModeOption) + if (explicitIdeaSetting != null) return Mode.valueOf(explicitIdeaSetting) + + if (project.containsImportedHmppModules()) return Mode.COMPOSITE + + return defaultState + } + + private fun Project.containsImportedHmppModules(): Boolean = + cacheByClassInvalidatingOnRootModifications(KotlinMultiplatformAnalysisModeComponent::class.java) { + ModuleManager.getInstance(this).modules.asSequence() + .mapNotNull { KotlinFacet.get(it) } + .any { it.configuration.settings.isHmppEnabled } + } + + enum class Mode { + // Analyses each platform in a separate [GlobalFacade] + SEPARATE, + + // Analyses all platforms in one facade. Uses type refinement and [CompositeResolverForModuleFactory] + COMPOSITE + } +} + +val Project.useCompositeAnalysis: Boolean + get() = KotlinMultiplatformAnalysisModeComponent.getMode(this) == KotlinMultiplatformAnalysisModeComponent.Mode.COMPOSITE \ No newline at end of file