diff options
author | Paweł Marks <pmarks@virtuslab.com> | 2020-03-04 00:29:36 +0100 |
---|---|---|
committer | Paweł Marks <Kordyjan@users.noreply.github.com> | 2020-03-04 14:28:14 +0100 |
commit | 89c33f431a43caa07365ec69b2b57f3b4e9a01c6 (patch) | |
tree | a48238a60970e7ac75efbc4e3ca5f182f6430b8d /plugins | |
parent | d08591dea709917553c0d6de024820bb29b328fb (diff) | |
download | dokka-89c33f431a43caa07365ec69b2b57f3b4e9a01c6.tar.gz dokka-89c33f431a43caa07365ec69b2b57f3b4e9a01c6.tar.bz2 dokka-89c33f431a43caa07365ec69b2b57f3b4e9a01c6.zip |
Big refactor of signature provider
Diffstat (limited to 'plugins')
4 files changed, 56 insertions, 62 deletions
diff --git a/plugins/base/src/main/kotlin/signatures/KotlinSignatureProvider.kt b/plugins/base/src/main/kotlin/signatures/KotlinSignatureProvider.kt index 2d4694bd..0706d387 100644 --- a/plugins/base/src/main/kotlin/signatures/KotlinSignatureProvider.kt +++ b/plugins/base/src/main/kotlin/signatures/KotlinSignatureProvider.kt @@ -2,9 +2,9 @@ package org.jetbrains.dokka.base.signatures import org.jetbrains.dokka.base.transformers.pages.comments.CommentsToContentConverter import org.jetbrains.dokka.base.translators.documentables.PageContentBuilder -import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.links.sureClassNames import org.jetbrains.dokka.model.* +import org.jetbrains.dokka.model.Annotation import org.jetbrains.dokka.model.Enum import org.jetbrains.dokka.model.Function import org.jetbrains.dokka.pages.ContentKind @@ -15,56 +15,51 @@ import org.jetbrains.dokka.utilities.DokkaLogger class KotlinSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLogger) : SignatureProvider { private val contentBuilder = PageContentBuilder(ctcc, this, logger) - override fun signature(documentable: Documentable): List<ContentNode> = when (documentable) { + override fun signature(documentable: Documentable): ContentNode = when (documentable) { is Function -> signature(documentable) is Classlike -> signature(documentable) + is TypeParameter -> signature(documentable) else -> throw NotImplementedError( "Cannot generate signature for ${documentable::class.qualifiedName} ${documentable.name}" ) } - private fun signature(f: Function) = f.platformData.map { signature(f, it) }.distinct() - - private fun signature(c: Classlike) = c.platformData.map { signature(c, it) }.distinct() - - private fun signature(c: Classlike, platform: PlatformData) = contentBuilder.contentFor(c, ContentKind.Symbol) { - text(c.visibility[platform]?.externalDisplayName ?: "") + private fun signature(c: Classlike) = contentBuilder.contentFor(c, ContentKind.Symbol) { + platformText(c.visibility) { it.externalDisplayName + " " } if (c is Class) { - text(c.modifier.toString()) + text(c.modifier.toString() + " ") } when (c) { - is Class -> text(" class ") - is Interface -> text(" interface ") - is Enum -> text(" enum ") - is Object -> text(" object ") + is Class -> text("class ") + is Interface -> text("interface ") + is Enum -> text("enum ") + is Object -> text("object ") + is Annotation -> text("annotation class ") } text(c.name!!) if (c is WithSupertypes) { - list(c.supertypes.getValue(platform), prefix = " : ") { - link(it.sureClassNames, it) + c.supertypes.map { (p, dris) -> + list(dris, prefix = " : ", platformData = setOf(p)) { + link(it.sureClassNames, it, platformData = setOf(p)) + } } } } - private fun signature(f: Function, platform: PlatformData) = contentBuilder.contentFor(f, ContentKind.Symbol) { - text(f.visibility[platform]?.externalDisplayName ?: "") - text(f.modifier.toString()) - text(" fun ") + private fun signature(f: Function) = contentBuilder.contentFor(f, ContentKind.Symbol) { + platformText(f.visibility) { it.externalDisplayName + " " } + text(f.modifier.toString() + " ") + text("fun ") f.receiver?.also { type(it.type) text(".") } link(f.name, f.dri) - val generics = f.generics.filterOnPlatform(platform) - if (generics.isNotEmpty()) { - text("<") - generics.forEach { - this@KotlinSignatureProvider.signature(it) - } - text(">") + list(f.generics, prefix = "<", suffix = ">") { + +buildSignature(it) } text("(") - list(f.parameters.filterOnPlatform(platform)) { + list(f.parameters) { link(it.name!!, it.dri) text(": ") type(it.type) @@ -73,41 +68,45 @@ class KotlinSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLog val returnType = f.type if (!f.isConstructor && returnType.constructorFqName != Unit::class.qualifiedName) { text(": ") - type(returnType) } } private fun signature(t: TypeParameter) = contentBuilder.contentFor(t, ContentKind.Symbol) { link(t.name, t.dri) - if (t.bounds.isNotEmpty()) { - text("<") - t.bounds.forEach { - signature(it, t.dri, t.platformData) - } - text(">") + list(t.bounds, prefix = " : ") { + signatureForProjection(it) } } - private fun signature(p: Projection, dri: DRI, platforms: List<PlatformData>): List<ContentNode> = when (p) { - is OtherParameter -> contentBuilder.contentFor(dri, platforms.toSet()) { text(p.name) }.children + private fun PageContentBuilder.DocumentableContentBuilder.signatureForProjection(p: Projection): Unit = when (p) { + is OtherParameter -> text(p.name) - is TypeConstructor -> contentBuilder.contentFor(dri, platforms.toSet()) { + is TypeConstructor -> group { link(p.dri.classNames.orEmpty(), p.dri) - }.children + p.projections.flatMap { signature(it, dri, platforms) } + list(p.projections, prefix = "<", suffix = ">") { + signatureForProjection(it) + } + } - is Variance -> contentBuilder.contentFor(dri, platforms.toSet()) { + is Variance -> group { text(p.kind.toString() + " ") - }.children + signature(p.inner, dri, platforms) + signatureForProjection(p.inner) + } - is Star -> contentBuilder.contentFor(dri, platforms.toSet()) { text("*") }.children + is Star -> text("*") - is Nullable -> signature(p.inner, dri, platforms) + contentBuilder.contentFor( - dri, - platforms.toSet() - ) { text("?") }.children + is Nullable -> group { + signatureForProjection(p.inner) + text("?") + } } private fun <T : Documentable> Collection<T>.filterOnPlatform(platformData: PlatformData) = this.filter { it.platformData.contains(platformData) } -}
\ No newline at end of file +} + +private fun <T> PageContentBuilder.DocumentableContentBuilder.platformText( + value: PlatformDependent<T>, + transform: (T) -> String +) = value.entries.forEach { (p, v) -> text(transform(v), platformData = setOf(p)) }
\ No newline at end of file diff --git a/plugins/base/src/main/kotlin/signatures/SignatureProvider.kt b/plugins/base/src/main/kotlin/signatures/SignatureProvider.kt index 738f405a..041015fc 100644 --- a/plugins/base/src/main/kotlin/signatures/SignatureProvider.kt +++ b/plugins/base/src/main/kotlin/signatures/SignatureProvider.kt @@ -4,5 +4,5 @@ import org.jetbrains.dokka.model.Documentable import org.jetbrains.dokka.pages.ContentNode interface SignatureProvider { - fun signature(documentable: Documentable): List<ContentNode> + fun signature(documentable: Documentable): ContentNode }
\ No newline at end of file diff --git a/plugins/base/src/main/kotlin/translators/documentables/DefaultPageCreator.kt b/plugins/base/src/main/kotlin/translators/documentables/DefaultPageCreator.kt index 5f3772ce..d92bec19 100644 --- a/plugins/base/src/main/kotlin/translators/documentables/DefaultPageCreator.kt +++ b/plugins/base/src/main/kotlin/translators/documentables/DefaultPageCreator.kt @@ -62,12 +62,12 @@ open class DefaultPageCreator( ) = contentBuilder.contentFor(s as Documentable) { block("Types", 2, ContentKind.Classlikes, s.classlikes, platformData.toSet()) { link(it.name.orEmpty(), it.dri) - +signature(it) + +buildSignature(it) text(it.briefDocTagString) } block("Functions", 2, ContentKind.Functions, s.functions, platformData.toSet()) { link(it.name, it.dri) - +signature(it) + +buildSignature(it) text(it.briefDocTagString) } block("Properties", 2, ContentKind.Properties, s.properties, platformData.toSet()) { @@ -77,13 +77,13 @@ open class DefaultPageCreator( } protected open fun contentForClasslike(c: Classlike) = contentBuilder.contentFor(c) { - +signature(c) + +buildSignature(c) +contentForComments(c) if (c is WithConstructors) { block("Constructors", 2, ContentKind.Constructors, c.constructors, c.platformData.toSet()) { link(it.name, it.dri) - signature(it) + +buildSignature(it) text(it.briefDocTagString) } } @@ -107,7 +107,7 @@ open class DefaultPageCreator( protected open fun contentForFunction(f: Function) = contentBuilder.contentFor(f) { header(1) { text(f.name) } - +signature(f) + +buildSignature(f) +contentForComments(f) block("Parameters", 2, ContentKind.Parameters, f.children, f.platformData.toSet()) { text(it.name ?: "<receiver>") diff --git a/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt b/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt index 0c08241f..369c9c81 100644 --- a/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt +++ b/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt @@ -93,13 +93,7 @@ open class PageContentBuilder( contents += createText(text, kind, platformData, styles, extras) } - fun signature(d: Documentable) = ContentGroup( - signatureProvider.signature(d), - DCI(setOf(d.dri), ContentKind.Symbol), - d.platformData.toSet(), - mainStyles, - mainExtras - ) + fun buildSignature(d: Documentable) = signatureProvider.signature(d) fun linkTable( elements: List<DRI>, @@ -152,16 +146,17 @@ open class PageContentBuilder( prefix: String = "", suffix: String = "", separator: String = ", ", + platformData: Set<PlatformData> = mainPlatformData, // TODO: children should be aware of this platform data operation: DocumentableContentBuilder.(T) -> Unit ) { if (elements.isNotEmpty()) { - if (prefix.isNotEmpty()) text(prefix) + if (prefix.isNotEmpty()) text(prefix, platformData = platformData) elements.dropLast(1).forEach { operation(it) - text(separator) + text(separator, platformData = platformData) } operation(elements.last()) - if (suffix.isNotEmpty()) text(suffix) + if (suffix.isNotEmpty()) text(suffix, platformData = platformData) } } |