diff options
author | Szymon Świstun <sswistun@virtuslab.com> | 2020-01-23 11:10:37 +0100 |
---|---|---|
committer | Paweł Marks <Kordyjan@users.noreply.github.com> | 2020-02-10 12:16:32 +0100 |
commit | 612e8c2f6f1b52f19c6ae51aa3d5655dfb43ba0c (patch) | |
tree | b801dba1dd61c17d0d67b91c5f093151955d0e2b /core/src/main/kotlin/transformers | |
parent | f7c67c2ade8b56c6556ec59d9c0adf8643a8e566 (diff) | |
download | dokka-612e8c2f6f1b52f19c6ae51aa3d5655dfb43ba0c.tar.gz dokka-612e8c2f6f1b52f19c6ae51aa3d5655dfb43ba0c.tar.bz2 dokka-612e8c2f6f1b52f19c6ae51aa3d5655dfb43ba0c.zip |
formatting
Diffstat (limited to 'core/src/main/kotlin/transformers')
-rw-r--r-- | core/src/main/kotlin/transformers/descriptors/DefaultDescriptorToDocumentationTranslator.kt | 61 | ||||
-rw-r--r-- | core/src/main/kotlin/transformers/documentation/DefaultDocumentationNodeMerger.kt | 59 |
2 files changed, 84 insertions, 36 deletions
diff --git a/core/src/main/kotlin/transformers/descriptors/DefaultDescriptorToDocumentationTranslator.kt b/core/src/main/kotlin/transformers/descriptors/DefaultDescriptorToDocumentationTranslator.kt index 54762303..651bd223 100644 --- a/core/src/main/kotlin/transformers/descriptors/DefaultDescriptorToDocumentationTranslator.kt +++ b/core/src/main/kotlin/transformers/descriptors/DefaultDescriptorToDocumentationTranslator.kt @@ -1,15 +1,17 @@ package org.jetbrains.dokka.transformers.descriptors import org.jetbrains.dokka.analysis.DokkaResolutionFacade -import org.jetbrains.dokka.model.* -import org.jetbrains.dokka.model.ClassKind -import org.jetbrains.dokka.model.Function import org.jetbrains.dokka.links.Callable import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.links.withClass +import org.jetbrains.dokka.model.* +import org.jetbrains.dokka.model.Enum +import org.jetbrains.dokka.model.Function import org.jetbrains.dokka.model.Property +import org.jetbrains.dokka.model.ClassKind import org.jetbrains.dokka.model.doc.* import org.jetbrains.dokka.pages.PlatformData +import org.jetbrains.dokka.parsers.MarkdownParser import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.impl.DeclarationDescriptorVisitorEmptyBodies @@ -20,10 +22,9 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperInterfaces import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.types.KotlinType -import org.jetbrains.dokka.parsers.MarkdownParser import kotlin.reflect.KClass -object DefaultDescriptorToDocumentationTranslator: DescriptorToDocumentationTranslator { +object DefaultDescriptorToDocumentationTranslator : DescriptorToDocumentationTranslator { override fun invoke( moduleName: String, packageFragments: Iterable<PackageFragmentDescriptor>, @@ -42,6 +43,8 @@ internal data class DRIWithPlatformInfo( val actual: List<PlatformInfo> ) +private fun DRI.withEmptyInfo() = DRIWithPlatformInfo(this, null, emptyList()) + internal class DokkaDescriptorVisitor( private val platformData: PlatformData, private val resolutionFacade: DokkaResolutionFacade @@ -54,18 +57,42 @@ internal class DokkaDescriptorVisitor( descriptor: PackageFragmentDescriptor, parent: DRIWithPlatformInfo ): Package { - val driWithPlatform = DRIWithPlatformInfo(DRI(packageName = descriptor.fqName.asString()), null, emptyList()) + val driWithPlatform = DRI(packageName = descriptor.fqName.asString()).withEmptyInfo() val scope = descriptor.getMemberScope() return Package( dri = driWithPlatform.dri, functions = scope.functions(driWithPlatform), properties = scope.properties(driWithPlatform), - classes = scope.classes(driWithPlatform) + classlikes = scope.classlikes(driWithPlatform) + ) + } + + override fun visitClassDescriptor(descriptor: ClassDescriptor, parent: DRIWithPlatformInfo): Classlike = when (descriptor.kind) { + org.jetbrains.kotlin.descriptors.ClassKind.ENUM_CLASS -> enumDescriptor(descriptor, parent) + else -> classDescriptor(descriptor, parent) + } + + fun enumDescriptor(descriptor: ClassDescriptor, parent: DRIWithPlatformInfo): Enum { + val driWithPlatform = parent.dri.withClass(descriptor.name.asString()).withEmptyInfo() + val scope = descriptor.getMemberScope(emptyList()) + val descriptorData = descriptor.takeUnless { it.isExpect }?.resolveClassDescriptionData() + + return Enum( + dri = driWithPlatform.dri, + name = descriptor.name.asString(), + entries = scope.classlikes(driWithPlatform).filter { it.kind == KotlinClassKindTypes.ENUM_ENTRY }.map { EnumEntry(it) }, + constructors = descriptor.constructors.map { visitConstructorDescriptor(it, driWithPlatform) }, + functions = scope.functions(driWithPlatform), + properties = scope.properties(driWithPlatform), + classlikes = scope.classlikes(driWithPlatform), + expected = descriptor.takeIf { it.isExpect }?.resolveClassDescriptionData(), + actual = listOfNotNull(descriptorData), + extra = mutableSetOf() // TODO Implement following method to return proper results getXMLDRIs(descriptor, descriptorData).toMutableSet() ) } - override fun visitClassDescriptor(descriptor: ClassDescriptor, parent: DRIWithPlatformInfo): Class { - val driWithPlatform = DRIWithPlatformInfo(parent.dri.withClass(descriptor.name.asString()), null, emptyList()) + fun classDescriptor(descriptor: ClassDescriptor, parent: DRIWithPlatformInfo): Class { + val driWithPlatform = parent.dri.withClass(descriptor.name.asString()).withEmptyInfo() val scope = descriptor.getMemberScope(emptyList()) val descriptorData = descriptor.takeUnless { it.isExpect }?.resolveClassDescriptionData() val expected = descriptor.takeIf { it.isExpect }?.resolveClassDescriptionData() @@ -87,7 +114,7 @@ internal class DokkaDescriptorVisitor( ) }, functions = scope.functions(driWithPlatform), properties = scope.properties(driWithPlatform), - classes = scope.classes(driWithPlatform), + classlikes = scope.classlikes(driWithPlatform), expected = expected, actual = actual, extra = mutableSetOf() // TODO Implement following method to return proper results getXMLDRIs(descriptor, descriptorData).toMutableSet() @@ -193,7 +220,7 @@ internal class DokkaDescriptorVisitor( .filterIsInstance<PropertyDescriptor>() .map { visitPropertyDescriptor(it, parent) } - private fun MemberScope.classes(parent: DRIWithPlatformInfo): List<Class> = + private fun MemberScope.classlikes(parent: DRIWithPlatformInfo): List<Classlike> = getContributedDescriptors(DescriptorKindFilter.CLASSIFIERS) { true } .filterIsInstance<ClassDescriptor>() .map { visitClassDescriptor(it, parent) } @@ -242,10 +269,12 @@ class KotlinTypeWrapper(private val kotlinType: KotlinType) : TypeWrapper { override val constructorFqName = fqNameSafe?.asString() override val constructorNamePathSegments: List<String> = fqNameSafe?.pathSegments()?.map { it.asString() } ?: emptyList() - override val arguments: List<KotlinTypeWrapper> by lazy { kotlinType.arguments.map { - KotlinTypeWrapper( - it.type - ) - } } + override val arguments: List<KotlinTypeWrapper> by lazy { + kotlinType.arguments.map { + KotlinTypeWrapper( + it.type + ) + } + } override val dri: DRI? by lazy { declarationDescriptor?.let { DRI.from(it) } } }
\ No newline at end of file diff --git a/core/src/main/kotlin/transformers/documentation/DefaultDocumentationNodeMerger.kt b/core/src/main/kotlin/transformers/documentation/DefaultDocumentationNodeMerger.kt index 86348206..ec67ea88 100644 --- a/core/src/main/kotlin/transformers/documentation/DefaultDocumentationNodeMerger.kt +++ b/core/src/main/kotlin/transformers/documentation/DefaultDocumentationNodeMerger.kt @@ -1,6 +1,7 @@ package org.jetbrains.dokka.transformers.documentation import org.jetbrains.dokka.model.* +import org.jetbrains.dokka.model.Enum import org.jetbrains.dokka.model.Function import org.jetbrains.dokka.plugability.DokkaContext @@ -18,9 +19,9 @@ internal object DefaultDocumentationNodeMerger : DocumentationNodeMerger { } } -private fun <T: Documentable> merge(elements: List<T>, reducer: (T, T) -> T): List<T> = +private fun <T : Documentable> merge(elements: List<T>, reducer: (T, T) -> T): List<T> = elements.groupingBy { it.dri } - .reduce { _, left, right -> reducer(left, right)} + .reduce { _, left, right -> reducer(left, right) } .values.toList() fun PlatformInfo.mergeWith(other: PlatformInfo?) = BasePlatformInfo( @@ -33,17 +34,17 @@ fun ClassPlatformInfo.mergeWith(other: ClassPlatformInfo?) = ClassPlatformInfo( (inherited + (other?.inherited ?: emptyList())).distinct() ) -fun List<ClassPlatformInfo>.mergeClassPlatformInfo() : List<ClassPlatformInfo> = - groupingBy { it.documentationNode.children + it.inherited}.reduce { - _, left, right -> left.mergeWith(right) +fun List<ClassPlatformInfo>.mergeClassPlatformInfo(): List<ClassPlatformInfo> = + groupingBy { it.documentationNode.children + it.inherited }.reduce { _, left, right -> + left.mergeWith(right) }.values.toList() -fun List<PlatformInfo>.merge() : List<PlatformInfo> = - groupingBy { it.documentationNode }.reduce { - _, left, right -> left.mergeWith(right) +fun List<PlatformInfo>.merge(): List<PlatformInfo> = + groupingBy { it.documentationNode }.reduce { _, left, right -> + left.mergeWith(right) }.values.toList() -fun Function.mergeWith(other: Function) = Function( +fun Function.mergeWith(other: Function): Function = Function( dri, name, returnType, @@ -62,16 +63,34 @@ fun Property.mergeWith(other: Property) = Property( (actual + other.actual).merge() ) +fun Classlike.mergeWith(other: Classlike): Classlike = when { + this is Class && other is Class -> mergeWith(other) + this is Enum && other is Enum -> mergeWith(other) + else -> throw IllegalStateException("${this::class.qualifiedName} ${this.name} cannot be merged with ${other::class.qualifiedName} ${other.name}") +} + fun Class.mergeWith(other: Class) = Class( - dri, - name, - kind, - merge(constructors + other.constructors, Function::mergeWith), - merge(functions + other.functions, Function::mergeWith), - merge(properties + other.properties, Property::mergeWith), - merge(classes + other.classes, Class::mergeWith), - expected?.mergeWith(other.expected), - (actual + other.actual).mergeClassPlatformInfo() + dri = dri, + name = name, + kind = kind, + constructors = merge(constructors + other.constructors, Function::mergeWith), + functions = merge(functions + other.functions, Function::mergeWith), + properties = merge(properties + other.properties, Property::mergeWith), + classlikes = merge(classlikes + other.classlikes, Classlike::mergeWith), + expected = expected?.mergeWith(other.expected), + actual = (actual + other.actual).mergeClassPlatformInfo() +) + +fun Enum.mergeWith(other: Enum) = Enum( + dri = dri, + name = name, + functions = merge(functions + other.functions, Function::mergeWith), + properties = merge(properties + other.properties, Property::mergeWith), + classlikes = merge(classlikes + other.classlikes, Classlike::mergeWith), + expected = expected?.mergeWith(other.expected), + actual = (actual + other.actual).mergeClassPlatformInfo(), + entries = (this.entries + other.entries.distinctBy { it.dri }.toList()), + constructors = merge(constructors + other.constructors, Function::mergeWith) ) fun Parameter.mergeWith(other: Parameter) = Parameter( @@ -82,9 +101,9 @@ fun Parameter.mergeWith(other: Parameter) = Parameter( (actual + other.actual).merge() ) -fun Package.mergeWith(other: Package) = Package( +fun Package.mergeWith(other: Package): Package = Package( dri, merge(functions + other.functions, Function::mergeWith), merge(properties + other.properties, Property::mergeWith), - merge(classes + other.classes, Class::mergeWith) + merge(classlikes + other.classlikes, Classlike::mergeWith) )
\ No newline at end of file |