From c853a41545d461fee7af6f0e8aad11569c7d7d53 Mon Sep 17 00:00:00 2001 From: Filip ZybaƂa Date: Wed, 17 Jun 2020 09:59:19 +0200 Subject: Added descriptions with type and location to distinguish symbols with name-clash --- .../src/main/kotlin/renderers/html/HtmlRenderer.kt | 92 +++++++++++++++++----- .../main/kotlin/signatures/JvmSignatureUtils.kt | 2 +- 2 files changed, 74 insertions(+), 20 deletions(-) (limited to 'plugins/base/src/main/kotlin') diff --git a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt index 6d9d8f1f..8104d399 100644 --- a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt +++ b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt @@ -3,16 +3,16 @@ package org.jetbrains.dokka.base.renderers.html import kotlinx.coroutines.* import kotlinx.html.* import kotlinx.html.stream.createHTML +import org.jetbrains.dokka.Platform import org.jetbrains.dokka.base.DokkaBase import org.jetbrains.dokka.base.renderers.DefaultRenderer import org.jetbrains.dokka.links.DRI -import org.jetbrains.dokka.model.SourceSetData +import org.jetbrains.dokka.model.* import org.jetbrains.dokka.model.properties.PropertyContainer import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.plugability.plugin import org.jetbrains.dokka.plugability.query -import org.jetbrains.dokka.plugability.querySingle import java.io.File import java.net.URI @@ -22,11 +22,11 @@ open class HtmlRenderer( private val sourceSetDependencyMap = with(context.sourceSetCache) { allSourceSets.map { sourceSet -> - sourceSet to allSourceSets.filter { sourceSet.dependentSourceSets.contains(it.sourceSetID ) } + sourceSet to allSourceSets.filter { sourceSet.dependentSourceSets.contains(it.sourceSetID) } }.toMap() } - private val pageList = mutableListOf() + private val pageList = mutableMapOf>() override val preprocessors = context.plugin().query { htmlPreprocessors } @@ -68,13 +68,13 @@ open class HtmlRenderer( if (node.hasStyle(TextStyle.Monospace)) copyButton() } node.hasStyle(TextStyle.BreakableAfter) -> { - span(){ childrenCallback() } - wbr { } + span() { childrenCallback() } + wbr { } } node.hasStyle(TextStyle.Breakable) -> { - span("breakable-word"){ childrenCallback() } + span("breakable-word") { childrenCallback() } } - node.hasStyle(TextStyle.Span) -> span(){ childrenCallback() } + node.hasStyle(TextStyle.Span) -> span() { childrenCallback() } node.dci.kind == ContentKind.Symbol -> div("symbol $additionalClasses") { childrenCallback() } node.dci.kind == ContentKind.BriefComment -> div("brief $additionalClasses") { childrenCallback() } node.dci.kind == ContentKind.Cover -> div("cover $additionalClasses") { @@ -94,7 +94,7 @@ open class HtmlRenderer( button(classes = "platform-tag platform-selector") { attributes["data-active"] = "" attributes["data-filter"] = it.sourceSetID - when(it.platform.key) { + when (it.platform.key) { "common" -> classes = classes + "common-like" "native" -> classes = classes + "native-like" "jvm" -> classes = classes + "jvm-like" @@ -352,7 +352,7 @@ open class HtmlRenderer( it.sourceSetID } } - + it.filterIsInstance().takeIf { it.isNotEmpty() }?.let { div("main-subrow " + node.style.joinToString(" ")) { it.filter { sourceSetRestriction == null || it.sourceSets.any { s -> s in sourceSetRestriction } } @@ -500,12 +500,14 @@ open class HtmlRenderer( span(classes = "anchor-icon") { attributes["pointing-to"] = pointingTo unsafe { - raw(""" + raw( + """ - """.trimIndent()) + """.trimIndent() + ) } } copiedPopup("Link copied to clipboard") @@ -551,14 +553,44 @@ open class HtmlRenderer( } } + private fun getSymbolSignature(page: ContentPage) = page.content.dfs { it.dci.kind == ContentKind.Symbol } + + private fun flattenToText(node: ContentNode): String { + fun getContentTextNodes(node: ContentNode, sourceSetRestriction: SourceSetData): List = + when (node) { + is ContentText -> listOf(node) + is ContentComposite -> node.children + .filter { sourceSetRestriction in it.sourceSets } + .flatMap { getContentTextNodes(it, sourceSetRestriction) } + .takeIf { node.dci.kind != ContentKind.Annotations } + .orEmpty() + else -> emptyList() + } + + val sourceSetRestriction = node.sourceSets.find { it.platform == Platform.common } ?: node.sourceSets.first() + return getContentTextNodes(node, sourceSetRestriction).joinToString("") { it.text } + } + override suspend fun renderPage(page: PageNode) { super.renderPage(page) - if (page is ContentPage) { - pageList.add( - """{ "name": "${page.name}", ${if (page is ClasslikePageNode) "\"class\": \"${page.name}\"," else ""} "location": "${locationProvider.resolve( - page - )}" }""" - ) + if (page is ContentPage && page !is ModulePageNode && page !is PackagePageNode) { + val signature = getSymbolSignature(page) + val textNodes = signature?.let { flattenToText(it) } + val documentable = page.documentable + if (documentable != null) { + listOf( + documentable.dri.packageName, + documentable.dri.classNames, + documentable.dri.callable?.name + ) + .filter { !it.isNullOrEmpty() } + .takeIf { it.isNotEmpty() } + ?.joinToString(".") + ?.let { + pageList.put(it, Pair(textNodes ?: page.name, locationProvider.resolve(page))) + } + + } } } @@ -569,11 +601,33 @@ open class HtmlRenderer( text(textNode.text) } + private fun generatePagesList() = + pageList.entries + .filter { !it.key.isNullOrEmpty() } + .groupBy { it.key.substringAfterLast(".") } + .entries + .mapIndexed { topLevelIndex, entry -> + if (entry.value.size > 1) { + listOf( + "{\'name\': \'${entry.key}\', \'index\': \'$topLevelIndex\', \'disabled\': true}" + ) + entry.value.mapIndexed { index, subentry -> + "{\'name\': \'${subentry.value.first}\', \'level\': 1, \'index\': \'$topLevelIndex.$index\', \'description\':\'${subentry.key}\', \'location\':\'${subentry.value.second}\'}" + } + } else { + val subentry = entry.value.single() + listOf( + "{\'name\': \'${subentry.value.first}\', \'index\': \'$topLevelIndex\', \'description\':\'${subentry.key}\', \'location\':\'${subentry.value.second}\'}" + ) + } + } + .flatten() + .joinToString(prefix = "[", separator = ",\n", postfix = "]") + override fun render(root: RootPageNode) { super.render(root) runBlocking(Dispatchers.Default) { launch { - outputWriter.write("scripts/pages", "var pages = [\n${pageList.joinToString(",\n")}\n]", ".js") + outputWriter.write("scripts/pages", "var pages = ${generatePagesList()}", ".js") } } } diff --git a/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt b/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt index a7fc7570..872dd4db 100644 --- a/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt +++ b/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt @@ -43,7 +43,7 @@ interface JvmSignatureUtils { }?.let { it.entries.forEach { it.value.filter { it !in ignored }.takeIf { it.isNotEmpty() }?.let { annotations -> - group(sourceSets = setOf(it.key), styles = styles) { + group(sourceSets = setOf(it.key), styles = styles, kind = ContentKind.Annotations) { annotations.forEach { operation(it) } -- cgit