aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrzej Ratajczak <andrzej.ratajczak98@gmail.com>2020-04-17 15:57:25 +0200
committerPaweł Marks <Kordyjan@users.noreply.github.com>2020-05-13 10:03:48 +0200
commita6d13e23a32622be9207e86e9cffc67447049259 (patch)
tree24658029c001055c21c4ed65fda6121d58c16ad1
parent1241973d7160704e545a792bca46392d1c173189 (diff)
downloaddokka-a6d13e23a32622be9207e86e9cffc67447049259.tar.gz
dokka-a6d13e23a32622be9207e86e9cffc67447049259.tar.bz2
dokka-a6d13e23a32622be9207e86e9cffc67447049259.zip
Refactor sources to be presented in table
-rw-r--r--plugins/base/src/main/kotlin/DokkaBase.kt12
-rw-r--r--plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt74
-rw-r--r--plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt17
3 files changed, 63 insertions, 40 deletions
diff --git a/plugins/base/src/main/kotlin/DokkaBase.kt b/plugins/base/src/main/kotlin/DokkaBase.kt
index 50d86892..2da3fbd4 100644
--- a/plugins/base/src/main/kotlin/DokkaBase.kt
+++ b/plugins/base/src/main/kotlin/DokkaBase.kt
@@ -25,6 +25,7 @@ import org.jetbrains.dokka.base.transformers.pages.samples.SamplesTransformer
import org.jetbrains.dokka.base.transformers.pages.sourcelinks.SourceLinksTransformer
import org.jetbrains.dokka.base.translators.descriptors.DefaultDescriptorToDocumentableTranslator
import org.jetbrains.dokka.base.translators.documentables.DefaultDocumentableToPageTranslator
+import org.jetbrains.dokka.base.translators.documentables.PageContentBuilder
import org.jetbrains.dokka.base.translators.psi.DefaultPsiToDocumentableTranslator
import org.jetbrains.dokka.plugability.DokkaPlugin
import org.jetbrains.dokka.transformers.pages.PageTransformer
@@ -135,7 +136,16 @@ class DokkaBase : DokkaPlugin() {
}
val sourceLinksTransformer by extending {
- htmlPreprocessors providing ::SourceLinksTransformer order { after(rootCreator) }
+ htmlPreprocessors providing {
+ SourceLinksTransformer(
+ it,
+ PageContentBuilder(
+ it.single(commentsToContentConverter),
+ it.single(signatureProvider),
+ it.logger
+ )
+ )
+ } order { after(rootCreator) }
}
val navigationPageInstaller by extending {
diff --git a/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt b/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt
index 6cb35e66..bf411a1f 100644
--- a/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt
+++ b/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt
@@ -2,23 +2,19 @@ package org.jetbrains.dokka.base.transformers.pages.sourcelinks
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiDocumentManager
-import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.dokka.DokkaConfiguration
-import org.jetbrains.dokka.links.DRI
+import org.jetbrains.dokka.base.translators.documentables.PageContentBuilder
import org.jetbrains.dokka.model.DescriptorDocumentableSource
import org.jetbrains.dokka.model.WithExpectActual
-import org.jetbrains.dokka.model.properties.PropertyContainer
import org.jetbrains.dokka.pages.*
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.transformers.pages.PageTransformer
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithSource
-import org.jetbrains.kotlin.kdoc.psi.api.KDoc
-import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.resolve.source.getPsi
import org.jetbrains.kotlin.utils.addToStdlib.cast
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
-class SourceLinksTransformer(val context: DokkaContext) : PageTransformer {
+class SourceLinksTransformer(val context: DokkaContext, val builder: PageContentBuilder) : PageTransformer {
override fun invoke(input: RootPageNode): RootPageNode {
@@ -26,15 +22,36 @@ class SourceLinksTransformer(val context: DokkaContext) : PageTransformer {
.flatMap { it.sourceLinks.map { sl -> SourceLink(sl, it.platformData) } }
return input.transformContentPagesTree { node ->
- node.documentable.safeAs<WithExpectActual>()?.sources?.map?.entries?.fold(node) { acc, entry ->
- sourceLinks.find { entry.value.path.contains(it.path) && it.platformData == entry.key }?.run {
- acc.modified(
- content = acc.content.addSource(
+ node.documentable.safeAs<WithExpectActual>()?.sources?.map?.entries?.let { entries ->
+ val resolvedSources = entries.mapNotNull { entry ->
+ sourceLinks.find { entry.value.path.contains(it.path) && it.platformData == entry.key }?.let {
+ Pair(
entry.key,
- entry.value.cast<DescriptorDocumentableSource>().toLink(this)
+ entry.value.cast<DescriptorDocumentableSource>().toLink(it)
)
- )
- } ?: acc
+ }
+ }
+ if (resolvedSources.isNotEmpty()) {
+ val table = builder.contentFor(node.dri.first(), node.documentable!!.platformData.toSet()) {
+ header(2) { text("Sources") }
+ +ContentTable(
+ emptyList(),
+ resolvedSources.map {
+ buildGroup(node.dri.first(), setOf(it.first), ContentKind.Source) {
+ platformDependentHint(node.dri.first(), setOf(it.first)) {
+ +link("(source)", it.second, ContentKind.Source, mainPlatformData, mainStyles, mainExtra)
+ }
+ }
+ },
+ DCI(node.dri, ContentKind.Subtypes),
+ node.documentable!!.platformData.toSet(),
+ style = emptySet()
+ )
+ }
+ node.modified(content = node.content.addTable(table))
+ } else {
+ node
+ }
} ?: node
}
}
@@ -45,40 +62,19 @@ class SourceLinksTransformer(val context: DokkaContext) : PageTransformer {
sourceLink.lineSuffix +
"${this.descriptor.cast<DeclarationDescriptorWithSource>().source.getPsi()?.lineNumber() ?: 1}"
- private fun ContentNode.addSource(platformData: PlatformData, address: String?): ContentNode =
- if (address != null) when (this) {
+ private fun ContentNode.addTable(table: ContentGroup): ContentNode =
+ when (this) {
is ContentGroup -> copy(
- children = children + listOf(platformHintedContentResolvedLink(platformData, dci.dri, address))
+ children = children + table
)
else -> ContentGroup(
- children = listOf(this, platformHintedContentResolvedLink(platformData, dci.dri, address)),
+ children = listOf(this, table),
extra = this.extra,
platforms = this.platforms,
dci = this.dci,
style = this.style
)
- } else this
-
- private fun platformHintedContentResolvedLink(platformData: PlatformData, dri: Set<DRI>, address: String) =
- PlatformHintedContent(
- inner = ContentResolvedLink(
- children = listOf(
- ContentText(
- text = "(source)",
- dci = DCI(dri, ContentKind.BriefComment),
- platforms = setOf(platformData),
- style = emptySet(),
- extra = PropertyContainer.empty()
- )
- ),
- address = address,
- extra = PropertyContainer.empty(),
- dci = DCI(dri, ContentKind.Source),
- platforms = setOf(platformData),
- style = emptySet()
- ),
- platforms = setOf(platformData)
- )
+ }
private fun PsiElement.lineNumber(): Int? {
val doc = PsiDocumentManager.getInstance(project).getDocument(containingFile)
diff --git a/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt b/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt
index a99fc6b3..6cb47b7f 100644
--- a/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt
+++ b/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt
@@ -194,6 +194,23 @@ open class PageContentBuilder(
}
fun link(
+ text: String,
+ address: String,
+ kind: Kind = ContentKind.Main,
+ platformData: Set<PlatformData> = mainPlatformData,
+ styles: Set<Style> = mainStyles,
+ extra: PropertyContainer<ContentNode> = mainExtra
+ ) =
+ ContentResolvedLink(
+ children = listOf(createText(text, kind, platformData, styles, extra)),
+ address = address,
+ extra = PropertyContainer.empty(),
+ dci = DCI(setOf(mainDRI), kind),
+ platforms = platformData,
+ style = emptySet()
+ )
+
+ fun link(
address: DRI,
kind: Kind = ContentKind.Main,
platformData: Set<PlatformData> = mainPlatformData,