diff options
author | Andrzej Ratajczak <andrzej.ratajczak98@gmail.com> | 2020-04-17 15:57:25 +0200 |
---|---|---|
committer | Paweł Marks <Kordyjan@users.noreply.github.com> | 2020-05-13 10:03:48 +0200 |
commit | a6d13e23a32622be9207e86e9cffc67447049259 (patch) | |
tree | 24658029c001055c21c4ed65fda6121d58c16ad1 /plugins/base/src/main/kotlin/transformers/pages/sourcelinks | |
parent | 1241973d7160704e545a792bca46392d1c173189 (diff) | |
download | dokka-a6d13e23a32622be9207e86e9cffc67447049259.tar.gz dokka-a6d13e23a32622be9207e86e9cffc67447049259.tar.bz2 dokka-a6d13e23a32622be9207e86e9cffc67447049259.zip |
Refactor sources to be presented in table
Diffstat (limited to 'plugins/base/src/main/kotlin/transformers/pages/sourcelinks')
-rw-r--r-- | plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt | 74 |
1 files changed, 35 insertions, 39 deletions
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) |