From f8e1428a3235dae9fca9faaa4837313693d6456f Mon Sep 17 00:00:00 2001 From: Kamil Doległo Date: Wed, 2 Sep 2020 00:08:26 +0200 Subject: Implement more reliable PackageListService --- .../main/kotlin/renderers/PackageListService.kt | 25 +++++++++++----------- .../src/main/kotlin/renderers/preprocessors.kt | 20 +++++++++++------ .../kotlin/resolvers/local/LocationProvider.kt | 13 +++++++++++ 3 files changed, 39 insertions(+), 19 deletions(-) (limited to 'plugins/base') diff --git a/plugins/base/src/main/kotlin/renderers/PackageListService.kt b/plugins/base/src/main/kotlin/renderers/PackageListService.kt index 3d631f5c..9b753cb1 100644 --- a/plugins/base/src/main/kotlin/renderers/PackageListService.kt +++ b/plugins/base/src/main/kotlin/renderers/PackageListService.kt @@ -2,25 +2,23 @@ package org.jetbrains.dokka.base.renderers import org.jetbrains.dokka.base.DokkaBase import org.jetbrains.dokka.links.DRI -import org.jetbrains.dokka.links.parent import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.plugability.plugin import org.jetbrains.dokka.plugability.querySingle import org.jetbrains.kotlin.utils.addToStdlib.safeAs -class PackageListService(val context: DokkaContext) { +class PackageListService(val context: DokkaContext, val rootPage: RootPageNode) { - fun formatPackageList(module: RootPageNode, format: String, linkExtension: String): String { + fun createPackageList(module: ModulePage, format: String, linkExtension: String): String { val packages = mutableSetOf() val nonStandardLocations = mutableMapOf() val locationProvider = - context.plugin().querySingle { locationProviderFactory }.getLocationProvider(module) - - fun visit(node: PageNode, parentDris: Set) { + context.plugin().querySingle { locationProviderFactory }.getLocationProvider(rootPage) + fun visit(node: PageNode) { if (node is PackagePage) { node.name .takeUnless { name -> name.startsWith("[") && name.endsWith("]") } // Do not include the package name for declarations without one @@ -28,18 +26,19 @@ class PackageListService(val context: DokkaContext) { } val contentPage = node.safeAs() - contentPage?.dri?.forEach { - if (parentDris.isNotEmpty() && it.parent !in parentDris) { - locationProvider.resolve(node) - ?.let { nodeLocation -> nonStandardLocations[it.toString()] = nodeLocation } - ?: context.logger.error("Cannot resolve path for ${node.name}!") + contentPage?.dri?.forEach { dri -> + val nodeLocation = locationProvider.resolve(node, context = module, skipExtension = true) + ?: run { context.logger.error("Cannot resolve path for ${node.name}!"); null } + + if (dri != DRI.topLevel && locationProvider.expectedLocationForDri(dri) != nodeLocation) { + nonStandardLocations[dri.toString()] = "$nodeLocation.$linkExtension" } } - node.children.forEach { visit(it, contentPage?.dri ?: setOf()) } + node.children.forEach { visit(it) } } - visit(module, setOf()) + visit(module) return buildString { appendLine("$DOKKA_PARAM_PREFIX.format:${format}") diff --git a/plugins/base/src/main/kotlin/renderers/preprocessors.kt b/plugins/base/src/main/kotlin/renderers/preprocessors.kt index 2640398c..b64d2e1f 100644 --- a/plugins/base/src/main/kotlin/renderers/preprocessors.kt +++ b/plugins/base/src/main/kotlin/renderers/preprocessors.kt @@ -10,20 +10,28 @@ object RootCreator : PageTransformer { RendererSpecificRootPage("", listOf(input), RenderingStrategy.DoNothing) } - -class PackageListCreator(val context: DokkaContext, val format: LinkFormat, val outputFilesNames: List = listOf("package-list")) : PageTransformer { +class PackageListCreator( + val context: DokkaContext, + val format: LinkFormat, + val outputFilesNames: List = listOf("package-list"), + val removeModulePrefix: Boolean = true +) : PageTransformer { override fun invoke(input: RootPageNode) = input.modified(children = input.children.map { it.takeUnless { it is ModulePage } - ?: it.modified(children = it.children + packageList(input)) // TODO packageList should take module as an input + ?: it.modified(children = it.children + packageList(input, it as ModulePage)) }) - private fun packageList(pageNode: RootPageNode): List { - val content = PackageListService(context).formatPackageList(pageNode, format.formatName, format.linkExtension) + private fun packageList(rootPageNode: RootPageNode, module: ModulePage): List { + val content = PackageListService(context, rootPageNode).createPackageList( + module, + format.formatName, + format.linkExtension + ) return outputFilesNames.map { fileName -> RendererSpecificResourcePage( - "${pageNode.name}/${fileName}", + "${rootPageNode.name}/${fileName}", emptyList(), RenderingStrategy.Write(content) ) diff --git a/plugins/base/src/main/kotlin/resolvers/local/LocationProvider.kt b/plugins/base/src/main/kotlin/resolvers/local/LocationProvider.kt index 0a095153..6d3ad215 100644 --- a/plugins/base/src/main/kotlin/resolvers/local/LocationProvider.kt +++ b/plugins/base/src/main/kotlin/resolvers/local/LocationProvider.kt @@ -1,6 +1,7 @@ package org.jetbrains.dokka.base.resolvers.local import org.jetbrains.dokka.DokkaException +import org.jetbrains.dokka.base.resolvers.local.DokkaLocationProvider.Companion.identifierToFilename import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.DisplaySourceSet import org.jetbrains.dokka.pages.PageNode @@ -10,6 +11,18 @@ interface LocationProvider { fun resolve(node: PageNode, context: PageNode? = null, skipExtension: Boolean = false): String? fun pathToRoot(from: PageNode): String fun ancestors(node: PageNode): List + + /** + * This method should return guessed filesystem location for a given [DRI] + * It is used to decide if a [DRI] should be present in the relocation list of the + * generated package-list so it is ok if the path differs from the one returned by [resolve] + * @return Path to a giver [DRI] or null if path should not be considered for relocations + */ + fun expectedLocationForDri(dri: DRI): String = + (listOf(dri.packageName) + + dri.classNames?.split(".")?.map { identifierToFilename(it) }.orEmpty() + + listOf(dri.callable?.let { identifierToFilename(it.name) } ?: "index") + ).filterNotNull().joinToString("/") } fun LocationProvider.resolveOrThrow(dri: DRI, sourceSets: Set, context: PageNode? = null): String = -- cgit