diff options
Diffstat (limited to 'kotlin-analysis')
7 files changed, 177 insertions, 24 deletions
diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/AnalysisEnvironment.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/AnalysisEnvironment.kt index 72b29002..c9aa3b07 100644 --- a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/AnalysisEnvironment.kt +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/AnalysisEnvironment.kt @@ -13,7 +13,6 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.roots.OrderEnumerationHandler import com.intellij.openapi.roots.ProjectFileIndex import com.intellij.openapi.roots.ProjectRootManager -import com.intellij.openapi.roots.impl.ProjectRootManagerImpl import com.intellij.openapi.util.Disposer import com.intellij.openapi.vfs.StandardFileSystems import com.intellij.psi.impl.source.javadoc.JavadocManagerImpl @@ -22,6 +21,7 @@ import com.intellij.psi.javadoc.JavadocManager import com.intellij.psi.javadoc.JavadocTagInfo import com.intellij.psi.search.GlobalSearchScope import org.jetbrains.dokka.Platform +import org.jetbrains.dokka.analysis.resolve.* import org.jetbrains.kotlin.analyzer.* import org.jetbrains.kotlin.analyzer.common.CommonAnalysisParameters import org.jetbrains.kotlin.analyzer.common.CommonPlatformAnalyzerServices @@ -39,7 +39,10 @@ import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment import org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM -import org.jetbrains.kotlin.cli.jvm.config.* +import org.jetbrains.kotlin.cli.jvm.config.addJavaSourceRoot +import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoot +import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots +import org.jetbrains.kotlin.cli.jvm.config.jvmClasspathRoots import org.jetbrains.kotlin.cli.jvm.index.JavaRoot import org.jetbrains.kotlin.config.* import org.jetbrains.kotlin.context.ProjectContext @@ -237,7 +240,7 @@ class AnalysisEnvironment(val messageCollector: MessageCollector, val analysisPl when (it) { library -> ModuleContent(it, emptyList(), GlobalSearchScope.notScope(sourcesScope)) module -> ModuleContent(it, emptyList(), GlobalSearchScope.allScope(environment.project)) - is DokkaNativeKlibLibraryInfo -> { + is DokkaKlibLibraryInfo -> { if (it.libraryRoot in nativeLibraries) ModuleContent(it, emptyList(), GlobalSearchScope.notScope(sourcesScope)) else null @@ -311,9 +314,9 @@ class AnalysisEnvironment(val messageCollector: MessageCollector, val analysisPl @OptIn(ExperimentalStdlibApi::class) private fun loadNativeLibraries(): Map<AbsolutePathString, LibraryModuleInfo> { - if (analysisPlatform != Platform.native) return emptyMap() + if (analysisPlatform != Platform.native && analysisPlatform != Platform.js) return emptyMap() - val dependencyResolver = DokkaNativeKlibLibraryDependencyResolver() + val dependencyResolver = DokkaKlibLibraryDependencyResolver() val analyzerServices = analysisPlatform.analyzerServices() return buildMap { @@ -327,7 +330,10 @@ class AnalysisEnvironment(val messageCollector: MessageCollector, val analysisPl // exists, is KLIB, has compatible format put( libraryFile.absolutePath, - DokkaNativeKlibLibraryInfo(kotlinLibrary, analyzerServices, dependencyResolver) + if (analysisPlatform == Platform.native) + DokkaNativeKlibLibraryInfo(kotlinLibrary, analyzerServices, dependencyResolver) + else + DokkaJsKlibLibraryInfo(kotlinLibrary, analyzerServices, dependencyResolver) ) } } @@ -387,9 +393,7 @@ class AnalysisEnvironment(val messageCollector: MessageCollector, val analysisPl override fun createResolverForModule( descriptor: ModuleDescriptor, moduleInfo: ModuleInfo - ): ResolverForModule = JsResolverForModuleFactory( - CompilerEnvironment - ).createResolverForModule( + ): ResolverForModule = DokkaJsResolverForModuleFactory(CompilerEnvironment).createResolverForModule( descriptor as ModuleDescriptorImpl, projectContext.withModule(descriptor), modulesContent(moduleInfo), diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/resolve/DokkaJsKlibLibraryInfo.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/resolve/DokkaJsKlibLibraryInfo.kt new file mode 100644 index 00000000..9d28cc3c --- /dev/null +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/resolve/DokkaJsKlibLibraryInfo.kt @@ -0,0 +1,30 @@ +package org.jetbrains.dokka.analysis.resolve + +import org.jetbrains.kotlin.analyzer.ModuleInfo +import org.jetbrains.kotlin.library.KotlinLibrary +import org.jetbrains.kotlin.library.shortName +import org.jetbrains.kotlin.library.uniqueName +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.platform.TargetPlatform +import org.jetbrains.kotlin.platform.js.JsPlatforms +import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices + +/** TODO: replace by [org.jetbrains.kotlin.caches.resolve.JsKlibLibraryInfo] after fix of KT-40734 */ +internal class DokkaJsKlibLibraryInfo( + override val kotlinLibrary: KotlinLibrary, + override val analyzerServices: PlatformDependentAnalyzerServices, + private val dependencyResolver: DokkaKlibLibraryDependencyResolver +) : DokkaKlibLibraryInfo() { + init { + dependencyResolver.registerLibrary(this) + } + + override val name: Name by lazy { + val libraryName = kotlinLibrary.shortName ?: kotlinLibrary.uniqueName + Name.special("<$libraryName>") + } + + override val platform: TargetPlatform = JsPlatforms.defaultJsPlatform + override fun dependencies(): List<ModuleInfo> = listOf(this) + dependencyResolver.resolveDependencies(this) + override fun getLibraryRoots(): Collection<String> = listOf(libraryRoot) +}
\ No newline at end of file diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/resolve/DokkaJsResolverForModuleFactory.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/resolve/DokkaJsResolverForModuleFactory.kt new file mode 100644 index 00000000..93214592 --- /dev/null +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/resolve/DokkaJsResolverForModuleFactory.kt @@ -0,0 +1,114 @@ +package org.jetbrains.dokka.analysis.resolve + +import org.jetbrains.kotlin.analyzer.* +import org.jetbrains.kotlin.builtins.konan.KonanBuiltIns +import org.jetbrains.kotlin.config.LanguageVersionSettings +import org.jetbrains.kotlin.container.StorageComponentContainer +import org.jetbrains.kotlin.container.get +import org.jetbrains.kotlin.context.ModuleContext +import org.jetbrains.kotlin.descriptors.PackageFragmentProvider +import org.jetbrains.kotlin.descriptors.impl.CompositePackageFragmentProvider +import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl +import org.jetbrains.kotlin.frontend.di.createContainerForLazyResolve +import org.jetbrains.kotlin.idea.klib.createKlibPackageFragmentProvider +import org.jetbrains.kotlin.incremental.components.LookupTracker +import org.jetbrains.kotlin.js.resolve.JsPlatformAnalyzerServices +import org.jetbrains.kotlin.konan.util.KlibMetadataFactories +import org.jetbrains.kotlin.library.metadata.NullFlexibleTypeDeserializer +import org.jetbrains.kotlin.resolve.CodeAnalyzerInitializer +import org.jetbrains.kotlin.resolve.SealedClassInheritorsProvider +import org.jetbrains.kotlin.resolve.TargetEnvironment +import org.jetbrains.kotlin.resolve.lazy.ResolveSession +import org.jetbrains.kotlin.resolve.lazy.declarations.DeclarationProviderFactoryService +import org.jetbrains.kotlin.serialization.js.KotlinJavascriptSerializationUtil +import org.jetbrains.kotlin.serialization.js.createKotlinJavascriptPackageFragmentProvider +import org.jetbrains.kotlin.utils.KotlinJavascriptMetadataUtils +import java.io.File + +/** TODO: replace by [org.jetbrains.kotlin.caches.resolve.JsResolverForModuleFactory] after fix of KT-40734 */ +internal class DokkaJsResolverForModuleFactory( + private val targetEnvironment: TargetEnvironment +) : ResolverForModuleFactory() { + companion object { + private val metadataFactories = KlibMetadataFactories(::KonanBuiltIns, NullFlexibleTypeDeserializer) + } + + override fun <M : ModuleInfo> createResolverForModule( + moduleDescriptor: ModuleDescriptorImpl, + moduleContext: ModuleContext, + moduleContent: ModuleContent<M>, + resolverForProject: ResolverForProject<M>, + languageVersionSettings: LanguageVersionSettings, + sealedInheritorsProvider: SealedClassInheritorsProvider + ): ResolverForModule { + val declarationProviderFactory = DeclarationProviderFactoryService.createDeclarationProviderFactory( + moduleContext.project, + moduleContext.storageManager, + moduleContent.syntheticFiles, + moduleContent.moduleContentScope, + moduleContent.moduleInfo + ) + + val container = createContainerForLazyResolve( + moduleContext, + declarationProviderFactory, + CodeAnalyzerInitializer.getInstance(moduleContext.project).createTrace(), // BindingTraceContext(/* allowSliceRewrite = */ true), + moduleDescriptor.platform!!, + JsPlatformAnalyzerServices, + targetEnvironment, + languageVersionSettings + ) + + var packageFragmentProvider = container.get<ResolveSession>().packageFragmentProvider + + val libraryProviders = createPackageFragmentProvider(moduleContent.moduleInfo, container, moduleContext, moduleDescriptor, languageVersionSettings) + + if (libraryProviders.isNotEmpty()) { + packageFragmentProvider = + CompositePackageFragmentProvider(listOf(packageFragmentProvider) + libraryProviders, "DokkaCompositePackageFragmentProvider") + } + return ResolverForModule(packageFragmentProvider, container) + } + + internal fun <M : ModuleInfo> createPackageFragmentProvider( + moduleInfo: M, + container: StorageComponentContainer, + moduleContext: ModuleContext, + moduleDescriptor: ModuleDescriptorImpl, + languageVersionSettings: LanguageVersionSettings + ): List<PackageFragmentProvider> = when (moduleInfo) { + is DokkaJsKlibLibraryInfo -> { + listOfNotNull( + moduleInfo.kotlinLibrary + .createKlibPackageFragmentProvider( + storageManager = moduleContext.storageManager, + metadataModuleDescriptorFactory = metadataFactories.DefaultDeserializedDescriptorFactory, + languageVersionSettings = languageVersionSettings, + moduleDescriptor = moduleDescriptor, + lookupTracker = LookupTracker.DO_NOTHING + ) + ) + } + is LibraryModuleInfo -> { + moduleInfo.getLibraryRoots() + .flatMap { + if (File(it).exists()) { + KotlinJavascriptMetadataUtils.loadMetadata(it) + } else { + // TODO can/should we warn a user about a problem in a library root? If so how? + emptyList() + } + } + .filter { it.version.isCompatible() } + .map { metadata -> + val (header, packageFragmentProtos) = + KotlinJavascriptSerializationUtil.readModuleAsProto(metadata.body, metadata.version) + createKotlinJavascriptPackageFragmentProvider( + moduleContext.storageManager, moduleDescriptor, header, packageFragmentProtos, metadata.version, + container.get(), LookupTracker.DO_NOTHING + ) + } + } + else -> emptyList() + } +}
\ No newline at end of file diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DokkaNativeKlibLibraryDependencyResolver.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/resolve/DokkaKlibLibraryDependencyResolver.kt index 83d5be0b..9259deff 100644 --- a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DokkaNativeKlibLibraryDependencyResolver.kt +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/resolve/DokkaKlibLibraryDependencyResolver.kt @@ -1,17 +1,17 @@ -package org.jetbrains.dokka.analysis +package org.jetbrains.dokka.analysis.resolve import org.jetbrains.kotlin.library.uniqueName import org.jetbrains.kotlin.library.unresolvedDependencies /** TODO: replace by [NativeKlibLibraryInfo] after fix of KT-40734 */ -internal class DokkaNativeKlibLibraryDependencyResolver { - private val cachedDependencies = mutableMapOf</* libraryName */String, DokkaNativeKlibLibraryInfo>() +internal class DokkaKlibLibraryDependencyResolver { + private val cachedDependencies = mutableMapOf</* libraryName */String, DokkaKlibLibraryInfo>() - fun registerLibrary(libraryInfo: DokkaNativeKlibLibraryInfo) { + fun registerLibrary(libraryInfo: DokkaKlibLibraryInfo) { cachedDependencies[libraryInfo.kotlinLibrary.uniqueName] = libraryInfo } - fun resolveDependencies(libraryInfo: DokkaNativeKlibLibraryInfo): List<DokkaNativeKlibLibraryInfo> { + fun resolveDependencies(libraryInfo: DokkaKlibLibraryInfo): List<DokkaKlibLibraryInfo> { return libraryInfo.kotlinLibrary.unresolvedDependencies.mapNotNull { cachedDependencies[it.path] } } } diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/resolve/DokkaKlibLibraryInfo.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/resolve/DokkaKlibLibraryInfo.kt new file mode 100644 index 00000000..e4e12b66 --- /dev/null +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/resolve/DokkaKlibLibraryInfo.kt @@ -0,0 +1,10 @@ +package org.jetbrains.dokka.analysis.resolve + +import org.jetbrains.kotlin.analyzer.LibraryModuleInfo +import org.jetbrains.kotlin.library.KotlinLibrary + +abstract class DokkaKlibLibraryInfo : LibraryModuleInfo { + abstract val kotlinLibrary: KotlinLibrary + internal val libraryRoot: String + get() = kotlinLibrary.libraryFile.path +}
\ No newline at end of file diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DokkaNativeKlibLibraryInfo.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/resolve/DokkaNativeKlibLibraryInfo.kt index 0b56f187..5e936533 100644 --- a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DokkaNativeKlibLibraryInfo.kt +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/resolve/DokkaNativeKlibLibraryInfo.kt @@ -1,8 +1,6 @@ -package org.jetbrains.dokka.analysis +package org.jetbrains.dokka.analysis.resolve -import org.jetbrains.kotlin.analyzer.LibraryModuleInfo import org.jetbrains.kotlin.analyzer.ModuleInfo -import org.jetbrains.kotlin.descriptors.ModuleDescriptor import org.jetbrains.kotlin.descriptors.konan.DeserializedKlibModuleOrigin import org.jetbrains.kotlin.descriptors.konan.KlibModuleOrigin import org.jetbrains.kotlin.descriptors.ModuleCapability @@ -19,17 +17,14 @@ import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices /** TODO: replace by [NativeKlibLibraryInfo] after fix of KT-40734 */ internal class DokkaNativeKlibLibraryInfo( - val kotlinLibrary: KotlinLibrary, + override val kotlinLibrary: KotlinLibrary, override val analyzerServices: PlatformDependentAnalyzerServices, - private val dependencyResolver: DokkaNativeKlibLibraryDependencyResolver -) : LibraryModuleInfo { + private val dependencyResolver: DokkaKlibLibraryDependencyResolver +) : DokkaKlibLibraryInfo() { init { dependencyResolver.registerLibrary(this) } - internal val libraryRoot: String - get() = kotlinLibrary.libraryFile.path - override val name: Name by lazy { val libraryName = kotlinLibrary.shortName ?: kotlinLibrary.uniqueName Name.special("<$libraryName>") diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DokkaNativeResolverForModuleFactory.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/resolve/DokkaNativeResolverForModuleFactory.kt index d1d7d314..0114f1ac 100644 --- a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DokkaNativeResolverForModuleFactory.kt +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/resolve/DokkaNativeResolverForModuleFactory.kt @@ -1,4 +1,4 @@ -package org.jetbrains.dokka.analysis +package org.jetbrains.dokka.analysis.resolve import org.jetbrains.kotlin.analyzer.* import org.jetbrains.kotlin.builtins.konan.KonanBuiltIns |