diff --git a/idea/src/org/jetbrains/kotlin/idea/roots/KotlinNonJvmSourceRootConverterProvider.kt b/idea/src/org/jetbrains/kotlin/idea/roots/KotlinNonJvmSourceRootConverterProvider.kt index fcb28fb2989..29930314981 100644 --- a/idea/src/org/jetbrains/kotlin/idea/roots/KotlinNonJvmSourceRootConverterProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/roots/KotlinNonJvmSourceRootConverterProvider.kt @@ -50,13 +50,11 @@ class KotlinNonJvmSourceRootConverterProvider : ConverterProvider("kotlin-non-jv JavaResourceRootType.TEST_RESOURCE ) - private val TargetPlatform.stdlibDetector: ((Array) -> Boolean)? - get() = when (this) { - is JvmPlatform -> { roots -> JavaRuntimeDetectionUtil.getRuntimeJar(roots.toList()) != null } - is JsPlatform -> { roots -> JsLibraryStdDetectionUtil.getJsStdLibJar(roots.toList()) != null } - is CommonPlatform -> { roots -> getLibraryJar(roots, PathUtil.KOTLIN_STDLIB_COMMON_JAR_PATTERN) != null } - else -> null - } + private val PLATFORM_TO_STDLIB_DETECTORS: Map) -> Boolean> = mapOf( + JvmPlatform to { roots: Array -> JavaRuntimeDetectionUtil.getRuntimeJar(roots.toList()) != null }, + JsPlatform to { roots: Array -> JsLibraryStdDetectionUtil.getJsStdLibJar(roots.toList()) != null }, + CommonPlatform to { roots: Array -> getLibraryJar(roots, PathUtil.KOTLIN_STDLIB_COMMON_JAR_PATTERN) != null } + ) } sealed class LibInfo { @@ -88,14 +86,16 @@ class KotlinNonJvmSourceRootConverterProvider : ConverterProvider("kotlin-non-jv abstract val explicitKind: PersistentLibraryKind<*>? abstract fun getRoots(): Array - val platform by lazy { - val explicitKind = explicitKind - val kind = if (explicitKind is KotlinLibraryKind) explicitKind else detectLibraryKind(getRoots()) - kind?.platform ?: JvmPlatform - } + val stdlibPlatform: TargetPlatform? by lazy { + val roots = getRoots() + for ((platform, detector) in PLATFORM_TO_STDLIB_DETECTORS) { + if (detector.invoke(roots)) { + return@lazy platform + } + } - val isStdlib: Boolean - get() = platform.stdlibDetector?.invoke(getRoots()) ?: false + return@lazy null + } } class ConverterImpl(private val context: ConversionContext) : ProjectConverter() { @@ -144,15 +144,12 @@ class KotlinNonJvmSourceRootConverterProvider : ConverterProvider("kotlin-non-jv .asSequence() .mapNotNull { createLibInfo(it, this) } .forEach { - when (val platform = it.platform) { - is CommonPlatform -> { - if (!hasCommonStdlib && it.isStdlib) { - hasCommonStdlib = true - } - } - - else -> { - if (it.isStdlib) return platform + val stdlibPlatform = it.stdlibPlatform + if (stdlibPlatform != null) { + if (stdlibPlatform == CommonPlatform) { + hasCommonStdlib = true + } else { + return stdlibPlatform } } } @@ -173,6 +170,7 @@ class KotlinNonJvmSourceRootConverterProvider : ConverterProvider("kotlin-non-jv .flatMap { it.getChildren(SourceFolderImpl.ELEMENT_NAME) } } + @Suppress("UnstableApiUsage") private fun ModuleSettings.isExternalModule(): Boolean { return when { rootElement.getAttributeValue(ExternalProjectSystemRegistry.EXTERNAL_SYSTEM_ID_KEY) != null -> true