More correct detection of Kotlin/Native stdlib by open IDEA projects

This commit is contained in:
Dmitriy Dolovov
2018-09-06 18:47:06 +03:00
committed by Mikhail Glukhikh
parent e8f238d53c
commit fbeff9245e
@@ -5,29 +5,23 @@
package org.jetbrains.kotlin.ide.konan
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.roots.libraries.PersistentLibraryKind
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.io.exists
import org.jetbrains.konan.KONAN_CURRENT_ABI_VERSION
import org.jetbrains.kotlin.ide.konan.analyzer.NativeAnalyzerFacade
import org.jetbrains.kotlin.builtins.DefaultBuiltIns
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.caches.resolve.IdePlatformKindResolution
import org.jetbrains.kotlin.config.KotlinFacetSettingsProvider
import org.jetbrains.kotlin.config.LanguageVersionSettingsImpl
import org.jetbrains.kotlin.context.ProjectContext
import org.jetbrains.kotlin.ide.konan.analyzer.NativeAnalyzerFacade
import org.jetbrains.kotlin.idea.caches.project.LibraryInfo
import org.jetbrains.kotlin.idea.caches.project.getModuleInfosFromIdeaModel
import org.jetbrains.kotlin.idea.caches.resolve.PlatformAnalysisSettings
import org.jetbrains.kotlin.konan.file.File
import org.jetbrains.kotlin.konan.library.KONAN_STDLIB_NAME
import org.jetbrains.kotlin.konan.library.createKonanLibrary
import org.jetbrains.kotlin.konan.utils.KonanFactories.DefaultDeserializedDescriptorFactory
import java.nio.file.Path
import java.nio.file.Paths
import org.jetbrains.kotlin.resolve.konan.platform.KonanPlatform
class NativePlatformKindResolution : IdePlatformKindResolution {
@@ -47,38 +41,28 @@ class NativePlatformKindResolution : IdePlatformKindResolution {
createKotlinNativeBuiltIns(projectContext)
}
val Module.isKotlinNativeModule: Boolean
get() {
val settings = KotlinFacetSettingsProvider.getInstance(project).getInitializedSettings(this)
return settings.platform.isKotlinNative
}
private fun createKotlinNativeBuiltIns(projectContext: ProjectContext): KotlinBuiltIns {
// TODO: It depends on a random project's stdlib, propagate the actual project here.
val stdlib: Pair<Path, LibraryInfo>? = ProjectManager.getInstance().openProjects.asSequence().mapNotNull { project ->
ModuleManager.getInstance(project).modules.asSequence().filter { it.isKotlinNativeModule }.mapNotNull { module ->
var result: Pair<Path, LibraryInfo>? = null
ModuleRootManager.getInstance(module).orderEntries().forEachLibrary { library ->
if (library.name == KONAN_STDLIB_NAME) {
val libraryInfo = LibraryInfo(project, library)
val path = libraryInfo.getLibraryRoots().firstOrNull()?.let { Paths.get(it) }?.takeIf { it.exists() }
if (path != null) result = path to libraryInfo
fun findStdlib(): Pair<String, LibraryInfo>? {
getModuleInfosFromIdeaModel(projectContext.project, KonanPlatform).forEach { module ->
module.dependencies().forEach { dependency ->
(dependency as? LibraryInfo)?.getLibraryRoots()?.forEach { path ->
if (path.endsWith(KONAN_STDLIB_NAME)) {
return path to dependency
}
}
result == null
}
result
}
}.flatten().firstOrNull()
return null
}
val stdlib: Pair<String, LibraryInfo>? = findStdlib()
if (stdlib != null) {
val (path, libraryInfo) = stdlib
val library = createKonanLibrary(path.File(), KONAN_CURRENT_ABI_VERSION)
val library = createKonanLibrary(File(path), KONAN_CURRENT_ABI_VERSION)
val builtInsModule = DefaultDeserializedDescriptorFactory.createDescriptorAndNewBuiltIns(
library,