From ac2c1adf18289acbff8a79e8cbabe58e00f30f63 Mon Sep 17 00:00:00 2001 From: Marcin Aman Date: Thu, 4 Mar 2021 17:23:30 +0100 Subject: Fix clashing properties and external links ordering (#1765) --- .../renderers/html/SearchbarDataInstaller.kt | 63 +++++++++++----------- .../resolvers/local/DefaultLocationProvider.kt | 32 ++++++++--- 2 files changed, 56 insertions(+), 39 deletions(-) (limited to 'plugins/base/src') diff --git a/plugins/base/src/main/kotlin/renderers/html/SearchbarDataInstaller.kt b/plugins/base/src/main/kotlin/renderers/html/SearchbarDataInstaller.kt index e2def59b..4d494c82 100644 --- a/plugins/base/src/main/kotlin/renderers/html/SearchbarDataInstaller.kt +++ b/plugins/base/src/main/kotlin/renderers/html/SearchbarDataInstaller.kt @@ -3,6 +3,7 @@ package org.jetbrains.dokka.base.renderers.html import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import org.jetbrains.dokka.Platform import org.jetbrains.dokka.base.templating.AddToSearch +import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.DisplaySourceSet import org.jetbrains.dokka.model.dfs import org.jetbrains.dokka.model.withDescendants @@ -22,28 +23,31 @@ data class SearchRecord( } open class SearchbarDataInstaller(val context: DokkaContext) : PageTransformer { - data class PageWithId(val id: PageId, val page: ContentPage) { - val displayableSignature = getSymbolSignature(page)?.let { flattenToText(it) } ?: page.name + data class PageWithId(val dri: DRI, val page: ContentPage) { + val displayableSignature = getSymbolSignature(page, dri)?.let { flattenToText(it) } ?: page.name + val id: String + get() = listOfNotNull( + dri.packageName, + dri.classNames, + dri.callable?.name + ).joinToString(".") } private val mapper = jacksonObjectMapper() - open fun generatePagesList(pages: Map, locationResolver: PageResolver): List = - pages.entries - .filter { it.key.isNotEmpty() } - .sortedWith(compareBy({ it.key }, { it.value.displayableSignature })) - .groupBy { it.key.substringAfterLast(".") } - .entries - .flatMap { entry -> - entry.value.map { subentry -> - createSearchRecord( - name = subentry.value.displayableSignature, - description = subentry.key, - location = resolveLocation(locationResolver, subentry.value.page).orEmpty(), - searchKeys = listOf(entry.key, subentry.value.displayableSignature) - ) - } - } + open fun generatePagesList(pages: List, locationResolver: PageResolver): List = + pages.map { pageWithId -> + createSearchRecord( + name = pageWithId.displayableSignature, + description = pageWithId.id, + location = resolveLocation(locationResolver, pageWithId.page).orEmpty(), + searchKeys = listOf( + pageWithId.id.substringAfterLast("."), + pageWithId.displayableSignature, + pageWithId.id, + ) + ) + }.sortedWith(compareBy({ it.name }, { it.description })) open fun createSearchRecord( name: String, @@ -53,19 +57,11 @@ open class SearchbarDataInstaller(val context: DokkaContext) : PageTransformer { ): SearchRecord = SearchRecord(name, description, location, searchKeys) - open fun processPage(page: PageNode): PageWithId? = + open fun processPage(page: PageNode): List = when (page) { - is ContentPage -> page.takeIf { page !is ModulePageNode && page !is PackagePageNode }?.documentable - ?.let { documentable -> - listOfNotNull( - documentable.dri.packageName, - documentable.dri.classNames, - documentable.dri.callable?.name - ).takeIf { it.isNotEmpty() }?.joinToString(".") - }?.let { id -> - PageWithId(id, page) - } - else -> null + is ContentPage -> page.takeIf { page !is ModulePageNode && page !is PackagePageNode }?.dri + ?.map { dri -> PageWithId(dri, page) }.orEmpty() + else -> emptyList() } private fun resolveLocation(locationResolver: PageResolver, page: ContentPage): String? = @@ -78,8 +74,8 @@ open class SearchbarDataInstaller(val context: DokkaContext) : PageTransformer { name = "scripts/pages.json", children = emptyList(), strategy = RenderingStrategy.PageLocationResolvableWrite { resolver -> - val content = input.withDescendants().fold(emptyMap()) { pageList, page -> - processPage(page)?.let { pageList + Pair(it.id, it) } ?: pageList + val content = input.withDescendants().fold(emptyList()) { pageList, page -> + pageList + processPage(page) }.run { generatePagesList(this, resolver) } @@ -95,7 +91,8 @@ open class SearchbarDataInstaller(val context: DokkaContext) : PageTransformer { } } -private fun getSymbolSignature(page: ContentPage) = page.content.dfs { it.dci.kind == ContentKind.Symbol } +private fun getSymbolSignature(page: ContentPage, dri: DRI) = + page.content.dfs { it.dci.kind == ContentKind.Symbol && it.dci.dri.contains(dri) } private fun flattenToText(node: ContentNode): String { fun getContentTextNodes(node: ContentNode, sourceSetRestriction: DisplaySourceSet): List = diff --git a/plugins/base/src/main/kotlin/resolvers/local/DefaultLocationProvider.kt b/plugins/base/src/main/kotlin/resolvers/local/DefaultLocationProvider.kt index c956b308..87683414 100644 --- a/plugins/base/src/main/kotlin/resolvers/local/DefaultLocationProvider.kt +++ b/plugins/base/src/main/kotlin/resolvers/local/DefaultLocationProvider.kt @@ -1,7 +1,11 @@ package org.jetbrains.dokka.base.resolvers.local import org.jetbrains.dokka.base.DokkaBase +import org.jetbrains.dokka.base.resolvers.external.DefaultExternalLocationProvider +import org.jetbrains.dokka.base.resolvers.external.Dokka010ExternalLocationProvider import org.jetbrains.dokka.base.resolvers.external.ExternalLocationProvider +import org.jetbrains.dokka.base.resolvers.external.javadoc.AndroidExternalLocationProvider +import org.jetbrains.dokka.base.resolvers.external.javadoc.JavadocExternalLocationProvider import org.jetbrains.dokka.base.resolvers.shared.ExternalDocumentation import org.jetbrains.dokka.base.resolvers.shared.PackageList import org.jetbrains.dokka.links.DRI @@ -36,12 +40,16 @@ abstract class DefaultLocationProvider( } .toMap() - protected val packagesIndex: Map = externalLocationProviders - .flatMap { (extDocInfo, externalLocationProvider) -> - extDocInfo.packageList.packages.map { packageName -> packageName to externalLocationProvider } - } - .toMap() - .filterKeys(String::isNotBlank) + protected val packagesIndex: Map = + externalLocationProviders + .flatMap { (extDocInfo, externalLocationProvider) -> + extDocInfo.packageList.packages.map { packageName -> packageName to externalLocationProvider } + }.groupBy { it.first }.mapValues { (_, lst) -> + lst.map { it.second } + .sortedWith(compareBy(nullsLast(ExternalLocationProviderOrdering)) { it }) + .firstOrNull() + } + .filterKeys(String::isNotBlank) protected val locationsIndex: Map = externalLocationProviders @@ -56,4 +64,16 @@ abstract class DefaultLocationProvider( ?: locationsIndex[dri.toString()]?.resolve(dri) ?: externalLocationProviders.values.mapNotNull { it?.resolve(dri) }.firstOrNull() + private object ExternalLocationProviderOrdering : Comparator { + private val desiredOrdering = listOf( + DefaultExternalLocationProvider::class, + Dokka010ExternalLocationProvider::class, + AndroidExternalLocationProvider::class, + JavadocExternalLocationProvider::class + ) + + override fun compare(o1: ExternalLocationProvider, o2: ExternalLocationProvider): Int = + desiredOrdering.indexOf(o1::class).compareTo(desiredOrdering.indexOf(o2::class)) + } + } -- cgit