diff options
author | Andrzej Ratajczak <andrzej.ratajczak98@gmail.com> | 2020-04-08 13:10:28 +0200 |
---|---|---|
committer | BarkingBad <32793002+BarkingBad@users.noreply.github.com> | 2020-04-22 10:37:59 +0200 |
commit | a5557f43c004d34026807d2d25b39fe1144bae94 (patch) | |
tree | cc65cf97e9d6b2ead623cab6a0f6da196ddf6f9c | |
parent | acc2670f6d641880325bb9826a202486a9410c24 (diff) | |
download | dokka-a5557f43c004d34026807d2d25b39fe1144bae94.tar.gz dokka-a5557f43c004d34026807d2d25b39fe1144bae94.tar.bz2 dokka-a5557f43c004d34026807d2d25b39fe1144bae94.zip |
Refactors sources to documentables translation
9 files changed, 80 insertions, 104 deletions
diff --git a/core/src/main/kotlin/CoreExtensions.kt b/core/src/main/kotlin/CoreExtensions.kt index d7b0b285..7b4c9baa 100644 --- a/core/src/main/kotlin/CoreExtensions.kt +++ b/core/src/main/kotlin/CoreExtensions.kt @@ -2,18 +2,16 @@ package org.jetbrains.dokka import org.jetbrains.dokka.plugability.ExtensionPoint import org.jetbrains.dokka.renderers.Renderer -import org.jetbrains.dokka.transformers.descriptors.DescriptorToDocumentableTranslator import org.jetbrains.dokka.transformers.documentation.DocumentableMerger import org.jetbrains.dokka.transformers.documentation.DocumentableToPageTranslator import org.jetbrains.dokka.transformers.documentation.DocumentableTransformer import org.jetbrains.dokka.transformers.documentation.PreMergeDocumentableTransformer import org.jetbrains.dokka.transformers.pages.PageTransformer -import org.jetbrains.dokka.transformers.psi.PsiToDocumentableTranslator +import org.jetbrains.dokka.transformers.sources.SourceToDocumentableTranslator import kotlin.reflect.KProperty object CoreExtensions { - val descriptorToDocumentableTranslator by coreExtension<DescriptorToDocumentableTranslator>() - val psiToDocumentableTranslator by coreExtension<PsiToDocumentableTranslator>() + val sourceToDocumentableTranslator by coreExtension<SourceToDocumentableTranslator>() val preMergeDocumentableTransformer by coreExtension<PreMergeDocumentableTransformer>() val documentableMerger by coreExtension<DocumentableMerger>() val documentableTransformer by coreExtension<DocumentableTransformer>() diff --git a/core/src/main/kotlin/DokkaGenerator.kt b/core/src/main/kotlin/DokkaGenerator.kt index 9616b013..053b4cb6 100644 --- a/core/src/main/kotlin/DokkaGenerator.kt +++ b/core/src/main/kotlin/DokkaGenerator.kt @@ -77,8 +77,7 @@ class DokkaGenerator( fun createDocumentationModels( platforms: Map<PlatformData, EnvironmentAndFacade>, context: DokkaContext - ) = platforms.map { (pdata, _) -> translateDescriptors(pdata, context) } + - platforms.map { (pdata, _) -> translatePsi(pdata, context) } + ) = platforms.flatMap { (pdata, _) -> translateSources(pdata, context) } fun transformDocumentationModelBeforeMerge( modulesFromPlatforms: List<DModule>, @@ -129,40 +128,10 @@ class DokkaGenerator( EnvironmentAndFacade(environment, facade) } - private fun translateDescriptors(platformData: PlatformData, context: DokkaContext): DModule { - val (environment, facade) = context.platforms.getValue(platformData) - - val packageFragments = environment.getSourceFiles().asSequence() - .map { it.packageFqName } - .distinct() - .mapNotNull { facade.resolveSession.getPackageFragment(it) } - .toList() - - return context.single(CoreExtensions.descriptorToDocumentableTranslator) - .invoke(platformData.name, packageFragments, platformData) - } - - private fun translatePsi(platformData: PlatformData, context: DokkaContext): DModule { - val (environment, _) = context.platforms.getValue(platformData) - - val sourceRoots = environment.configuration.get(CLIConfigurationKeys.CONTENT_ROOTS) - ?.filterIsInstance<JavaSourceRoot>() - ?.map { it.file } - ?: listOf() - val localFileSystem = VirtualFileManager.getInstance().getFileSystem("file") - - val psiFiles = sourceRoots.map { sourceRoot -> - sourceRoot.absoluteFile.walkTopDown().mapNotNull { - localFileSystem.findFileByPath(it.path)?.let { vFile -> - PsiManager.getInstance(environment.project).findFile(vFile) as? PsiJavaFile - } - }.toList() - }.flatten() - - return context.single(CoreExtensions.psiToDocumentableTranslator) - .invoke(platformData.name, psiFiles, platformData, context) - - } + private fun translateSources(platformData: PlatformData, context: DokkaContext) = + context[CoreExtensions.sourceToDocumentableTranslator].map { + it.invoke(platformData, context) + } class DokkaMessageCollector(private val logger: DokkaLogger) : MessageCollector { override fun clear() { diff --git a/core/src/main/kotlin/model/Documentable.kt b/core/src/main/kotlin/model/Documentable.kt index f91383de..2155b00d 100644 --- a/core/src/main/kotlin/model/Documentable.kt +++ b/core/src/main/kotlin/model/Documentable.kt @@ -7,6 +7,7 @@ import org.jetbrains.dokka.model.properties.PropertyContainer import org.jetbrains.dokka.model.properties.WithExtraProperties import org.jetbrains.dokka.pages.PlatformData import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor import org.jetbrains.kotlin.load.kotlin.toSourceElement abstract class Documentable { @@ -418,9 +419,14 @@ sealed class JavaVisibility(name: String) : Visibility(name) { fun <T> PlatformDependent<T>?.orEmpty(): PlatformDependent<T> = this ?: PlatformDependent.empty() -sealed class DocumentableSource(val path: String) +interface DocumentableSource { + val path: String +} -class DescriptorDocumentableSource(val descriptor: DeclarationDescriptor) : - DocumentableSource(descriptor.toSourceElement.containingFile.toString()) +class DescriptorDocumentableSource(source: DeclarationDescriptor) : DocumentableSource { + override val path = source.toSourceElement.containingFile.toString() +} -class PsiDocumentableSource(val psi: PsiNamedElement) : DocumentableSource(psi.containingFile.virtualFile.path) +class PsiDocumentableSource(source: PsiNamedElement) : DocumentableSource { + override val path = source.containingFile.virtualFile.path +} diff --git a/core/src/main/kotlin/transformers/descriptors/DescriptorToDocumentableTranslator.kt b/core/src/main/kotlin/transformers/descriptors/DescriptorToDocumentableTranslator.kt deleted file mode 100644 index ca66b90a..00000000 --- a/core/src/main/kotlin/transformers/descriptors/DescriptorToDocumentableTranslator.kt +++ /dev/null @@ -1,13 +0,0 @@ -package org.jetbrains.dokka.transformers.descriptors - -import org.jetbrains.dokka.model.DModule -import org.jetbrains.dokka.pages.PlatformData -import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor - -interface DescriptorToDocumentableTranslator { - fun invoke( - moduleName: String, - packageFragments: Iterable<PackageFragmentDescriptor>, - platformData: PlatformData - ): DModule -}
\ No newline at end of file diff --git a/core/src/main/kotlin/transformers/psi/PsiToDocumentableTranslator.kt b/core/src/main/kotlin/transformers/psi/PsiToDocumentableTranslator.kt deleted file mode 100644 index 6f5025bd..00000000 --- a/core/src/main/kotlin/transformers/psi/PsiToDocumentableTranslator.kt +++ /dev/null @@ -1,15 +0,0 @@ -package org.jetbrains.dokka.transformers.psi - -import com.intellij.psi.PsiJavaFile -import org.jetbrains.dokka.model.DModule -import org.jetbrains.dokka.pages.PlatformData -import org.jetbrains.dokka.plugability.DokkaContext - -interface PsiToDocumentableTranslator { - fun invoke( - moduleName: String, - psiFiles: List<PsiJavaFile>, - platformData: PlatformData, - context: DokkaContext - ): DModule -} diff --git a/core/src/main/kotlin/transformers/sources/SourceToDocumentableTranslator.kt b/core/src/main/kotlin/transformers/sources/SourceToDocumentableTranslator.kt new file mode 100644 index 00000000..a138b3e2 --- /dev/null +++ b/core/src/main/kotlin/transformers/sources/SourceToDocumentableTranslator.kt @@ -0,0 +1,9 @@ +package org.jetbrains.dokka.transformers.sources + +import org.jetbrains.dokka.model.DModule +import org.jetbrains.dokka.pages.PlatformData +import org.jetbrains.dokka.plugability.DokkaContext + +interface SourceToDocumentableTranslator { + fun invoke(platformData: PlatformData, context: DokkaContext): DModule +}
\ No newline at end of file diff --git a/plugins/base/src/main/kotlin/DokkaBase.kt b/plugins/base/src/main/kotlin/DokkaBase.kt index 5a86780b..50d86892 100644 --- a/plugins/base/src/main/kotlin/DokkaBase.kt +++ b/plugins/base/src/main/kotlin/DokkaBase.kt @@ -28,6 +28,7 @@ import org.jetbrains.dokka.base.translators.documentables.DefaultDocumentableToP import org.jetbrains.dokka.base.translators.psi.DefaultPsiToDocumentableTranslator import org.jetbrains.dokka.plugability.DokkaPlugin import org.jetbrains.dokka.transformers.pages.PageTransformer +import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor class DokkaBase : DokkaPlugin() { val pageMergerStrategy by extensionPoint<PageMergerStrategy>() @@ -39,12 +40,12 @@ class DokkaBase : DokkaPlugin() { val htmlPreprocessors by extensionPoint<PageTransformer>() val samplesTransformer by extensionPoint<SamplesTransformer>() - val descriptorToDocumentableTranslator by extending(isFallback = true) { - CoreExtensions.descriptorToDocumentableTranslator providing ::DefaultDescriptorToDocumentableTranslator + val descriptorToDocumentableTranslator by extending { + CoreExtensions.sourceToDocumentableTranslator with DefaultDescriptorToDocumentableTranslator } - val psiToDocumentableTranslator by extending(isFallback = true) { - CoreExtensions.psiToDocumentableTranslator with DefaultPsiToDocumentableTranslator + val psiToDocumentableTranslator by extending { + CoreExtensions.sourceToDocumentableTranslator with DefaultPsiToDocumentableTranslator } val documentableMerger by extending(isFallback = true) { diff --git a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt index 25e6f6ac..678ac0ec 100644 --- a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt @@ -10,8 +10,8 @@ import org.jetbrains.dokka.model.properties.PropertyContainer import org.jetbrains.dokka.pages.PlatformData import org.jetbrains.dokka.parsers.MarkdownParser import org.jetbrains.dokka.plugability.DokkaContext -import org.jetbrains.dokka.transformers.descriptors.DescriptorToDocumentableTranslator import org.jetbrains.dokka.utilities.DokkaLogger +import org.jetbrains.dokka.transformers.sources.SourceToDocumentableTranslator import org.jetbrains.kotlin.builtins.isExtensionFunctionType import org.jetbrains.kotlin.builtins.isFunctionType import org.jetbrains.kotlin.codegen.isJvmStaticInObjectOrClassOrInterface @@ -33,23 +33,28 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.resolve.source.KotlinSourceElement import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.TypeProjection +import org.jetbrains.kotlin.utils.addToStdlib.safeAs -class DefaultDescriptorToDocumentableTranslator( - private val context: DokkaContext -) : DescriptorToDocumentableTranslator { - override fun invoke( - moduleName: String, - packageFragments: Iterable<PackageFragmentDescriptor>, - platformData: PlatformData - ) = DokkaDescriptorVisitor(platformData, context.platforms.getValue(platformData).facade, context.logger).run { - packageFragments.map { - visitPackageFragmentDescriptor( - it, - DRIWithPlatformInfo(DRI.topLevel, PlatformDependent.empty()) - ) - } - }.let { DModule(moduleName, it, PlatformDependent.empty(), listOf(platformData)) } +object DefaultDescriptorToDocumentableTranslator : SourceToDocumentableTranslator { + + override fun invoke(platformData: PlatformData, context: DokkaContext): DModule { + + val (environment, facade) = context.platforms.getValue(platformData) + val packageFragments = environment.getSourceFiles().asSequence() + .map { it.packageFqName } + .distinct() + .mapNotNull { facade.resolveSession.getPackageFragment(it) } + .toList() + return DokkaDescriptorVisitor(platformData, context.platforms.getValue(platformData).facade, context.logger).run { + packageFragments.mapNotNull { it.safeAs<PackageFragmentDescriptor>() }.map { + visitPackageFragmentDescriptor( + it, + DRIWithPlatformInfo(DRI.topLevel, PlatformDependent.empty()) + ) + } + }.let { DModule(platformData.name, it, PlatformDependent.empty(), listOf(platformData)) } + } } data class DRIWithPlatformInfo( diff --git a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt index 33100e3e..c0e1b4af 100644 --- a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt @@ -2,6 +2,7 @@ package org.jetbrains.dokka.base.translators.psi import com.intellij.lang.jvm.JvmModifier import com.intellij.lang.jvm.types.JvmReferenceType +import com.intellij.openapi.vfs.VirtualFileManager import com.intellij.psi.* import com.intellij.psi.impl.source.PsiClassReferenceType import org.jetbrains.dokka.links.DRI @@ -10,8 +11,10 @@ import org.jetbrains.dokka.model.* import org.jetbrains.dokka.model.properties.PropertyContainer import org.jetbrains.dokka.pages.PlatformData import org.jetbrains.dokka.plugability.DokkaContext -import org.jetbrains.dokka.transformers.psi.PsiToDocumentableTranslator +import org.jetbrains.dokka.transformers.sources.SourceToDocumentableTranslator import org.jetbrains.dokka.utilities.DokkaLogger +import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys +import org.jetbrains.kotlin.cli.jvm.config.JavaSourceRoot import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.load.java.JvmAbi import org.jetbrains.kotlin.load.java.propertyNameByGetMethodName @@ -19,30 +22,43 @@ import org.jetbrains.kotlin.load.java.propertyNamesBySetMethodName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.psiUtil.getChildOfType import org.jetbrains.kotlin.resolve.DescriptorUtils +import org.jetbrains.kotlin.utils.addToStdlib.safeAs -object DefaultPsiToDocumentableTranslator : PsiToDocumentableTranslator { +object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { + + override fun invoke(platformData: PlatformData, context: DokkaContext): DModule { + + val (environment, _) = context.platforms.getValue(platformData) + + val sourceRoots = environment.configuration.get(CLIConfigurationKeys.CONTENT_ROOTS) + ?.filterIsInstance<JavaSourceRoot>() + ?.map { it.file } + ?: listOf() + val localFileSystem = VirtualFileManager.getInstance().getFileSystem("file") + + val psiFiles = sourceRoots.map { sourceRoot -> + sourceRoot.absoluteFile.walkTopDown().mapNotNull { + localFileSystem.findFileByPath(it.path)?.let { vFile -> + PsiManager.getInstance(environment.project).findFile(vFile) as? PsiJavaFile + } + }.toList() + }.flatten() - override fun invoke( - moduleName: String, - psiFiles: List<PsiJavaFile>, - platformData: PlatformData, - context: DokkaContext - ): DModule { val docParser = DokkaPsiParser( platformData, context.logger ) return DModule( - moduleName, - psiFiles.groupBy { it.packageName }.map { (packageName, psiFiles) -> + platformData.name, + psiFiles.mapNotNull { it.safeAs<PsiJavaFile>() }.groupBy { it.packageName }.map { (packageName, psiFiles) -> val dri = DRI(packageName = packageName) DPackage( dri, emptyList(), emptyList(), - psiFiles.flatMap { psFile -> - psFile.classes.map { docParser.parseClasslike(it, dri) } + psiFiles.flatMap { psiFile -> + psiFile.classes.map { docParser.parseClasslike(it, dri) } }, emptyList(), PlatformDependent.empty(), |