From 8e313553a3435042cdf295d7436b01af4ea4938a Mon Sep 17 00:00:00 2001 From: Vadim Mishenev Date: Tue, 28 Feb 2023 17:26:10 +0200 Subject: [PATCH] Fix unresolved link to declaration from another source set (#2878) --- .../src/main/kotlin/model/DisplaySourceSet.kt | 8 ++++ .../resolvers/local/DokkaLocationProvider.kt | 27 +++++++---- .../linkableContent/LinkableContentTest.kt | 47 +++++++++++++++++++ 3 files changed, 74 insertions(+), 8 deletions(-) diff --git a/core/src/main/kotlin/model/DisplaySourceSet.kt b/core/src/main/kotlin/model/DisplaySourceSet.kt index 85904db69f..5ea1ba3d25 100644 --- a/core/src/main/kotlin/model/DisplaySourceSet.kt +++ b/core/src/main/kotlin/model/DisplaySourceSet.kt @@ -5,6 +5,14 @@ import org.jetbrains.dokka.DokkaSourceSetID import org.jetbrains.dokka.Platform import org.jetbrains.dokka.utilities.SelfRepresentingSingletonSet +/** + * TODO: fix the example (asymmetric equivalence relation with [Set]): + * ``` + * val ds = DokkaSourceSetImpl(sourceSetID = DokkaSourceSetID("", "")).toDisplaySourceSet() + * println(setOf(ds) == ds) // true + * println(ds == setOf(ds)) // false + * ``` + */ data class DisplaySourceSet( val sourceSetIDs: CompositeSourceSetID, val name: String, diff --git a/plugins/base/src/main/kotlin/resolvers/local/DokkaLocationProvider.kt b/plugins/base/src/main/kotlin/resolvers/local/DokkaLocationProvider.kt index cae0ccaf5c..4934e8f487 100644 --- a/plugins/base/src/main/kotlin/resolvers/local/DokkaLocationProvider.kt +++ b/plugins/base/src/main/kotlin/resolvers/local/DokkaLocationProvider.kt @@ -89,24 +89,35 @@ open class DokkaLocationProvider( private fun getLocalLocation(driWithSourceSets: DRIWithSourceSets, context: PageNode?): String? { val (dri, originalSourceSet) = driWithSourceSets - val allSourceSets = + val allSourceSets: List> = listOf(originalSourceSet) + originalSourceSet.let { oss -> dokkaContext.configuration.sourceSets.filter { it.sourceSetID in oss.sourceSetIDs } .flatMap { it.dependentSourceSets } .mapNotNull { ssid -> dokkaContext.configuration.sourceSets.find { it.sourceSetID == ssid }?.toDisplaySourceSet() + }.map { + // be careful `data DisplaySourceSet: Set` but `setOf(someDisplaySourceSet) != someDisplaySourceSet` + setOf(it) } } - return allSourceSets.asSequence().mapNotNull { displaySourceSet -> - pagesIndex[DRIWithSourceSets(dri, displaySourceSet)]?.let { page -> resolve(page, context) } - ?: anchorsIndex[driWithSourceSets]?.let { (page, kind) -> - val dci = DCI(setOf(dri), kind) - resolve(page, context) + "#" + anchorForDCI(dci, displaySourceSet) - } - }.firstOrNull() + return getLocalPageLink(dri, allSourceSets, context) + ?: getLocalAnchor(dri, allSourceSets, context) } + private fun getLocalPageLink(dri: DRI, allSourceSets: Iterable>, context: PageNode?) = + allSourceSets.mapNotNull { displaySourceSet -> + pagesIndex[DRIWithSourceSets(dri, displaySourceSet)] + }.firstOrNull()?.let { page -> resolve(page, context) } + + private fun getLocalAnchor(dri: DRI, allSourceSets: Iterable>, context: PageNode?) = + allSourceSets.mapNotNull { displaySourceSet -> + anchorsIndex[DRIWithSourceSets(dri, displaySourceSet)]?.let { (page, kind) -> + val dci = DCI(setOf(dri), kind) + resolve(page, context) + "#" + anchorForDCI(dci, displaySourceSet) + } + }.firstOrNull() + override fun pathToRoot(from: PageNode): String = pathTo(pageGraphRoot, from).removeSuffix(PAGE_WITH_CHILDREN_SUFFIX) diff --git a/plugins/base/src/test/kotlin/linkableContent/LinkableContentTest.kt b/plugins/base/src/test/kotlin/linkableContent/LinkableContentTest.kt index 7dfe1e1d53..d7ac8b9723 100644 --- a/plugins/base/src/test/kotlin/linkableContent/LinkableContentTest.kt +++ b/plugins/base/src/test/kotlin/linkableContent/LinkableContentTest.kt @@ -10,11 +10,14 @@ import org.jetbrains.dokka.model.doc.Text import org.jetbrains.dokka.pages.* import org.jetbrains.kotlin.utils.addToStdlib.cast import org.jetbrains.kotlin.utils.addToStdlib.safeAs +import org.jsoup.Jsoup import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test +import utils.TestOutputWriterPlugin import utils.assertNotNull import java.net.URL import java.nio.file.Paths +import kotlin.test.assertEquals class LinkableContentTest : BaseAbstractTest() { @@ -360,4 +363,48 @@ class LinkableContentTest : BaseAbstractTest() { } } + + @Test + fun `should have a correct link to declaration from another source set`() { + val writerPlugin = TestOutputWriterPlugin() + val configuration = dokkaConfiguration { + sourceSets { + val common = sourceSet { + sourceRoots = listOf("src/commonMain") + analysisPlatform = "common" + name = "common" + displayName = "common" + } + sourceSet { + sourceRoots = listOf("src/jvmMain/") + analysisPlatform = "jvm" + name = "jvm" + displayName = "jvm" + dependentSourceSets = setOf(common.value.sourceSetID) + } + } + } + + testInline( + """ + /src/commonMain/main.kt + class A + /src/jvmMain/main.kt + /** + * link to [A] + */ + class B + """.trimIndent() + , + pluginOverrides = listOf(writerPlugin), + configuration = configuration + ) { + renderingStage = { _, _ -> + val page = + Jsoup.parse(writerPlugin.writer.contents.getValue("root/[root]/-b/index.html")) + val link = page.select(".paragraph a").single() + assertEquals("../-a/index.html", link.attr("href")) + } + } + } }