Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect links for inherited java methods from a collection #3529

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -60,6 +60,7 @@ public open class DokkaLocationProvider(
if (first) page else throw AssertionError("Multiple pages associated with key: ${key.dri}/${key.sourceSet}")
}

@Deprecated("This is not used anymore and will be removed, since resolving references to anchors is removed to fix #3054")
protected val anchorsIndex: Map<DRIWithSourceSets, PageWithKind> =
pageGraphRoot.withDescendants().filterIsInstance<ContentPage>()
.flatMap { page ->
Expand Down Expand Up @@ -107,22 +108,13 @@ public open class DokkaLocationProvider(
}

return getLocalPageLink(dri, allSourceSets, context)
?: getLocalAnchor(dri, allSourceSets, context)
}

private fun getLocalPageLink(dri: DRI, allSourceSets: Iterable<Set<DisplaySourceSet>>, context: PageNode?) =
allSourceSets.mapNotNull { displaySourceSet ->
pagesIndex[DRIWithSourceSets(dri, displaySourceSet)]
}.firstOrNull()?.let { page -> resolve(page, context) }

private fun getLocalAnchor(dri: DRI, allSourceSets: Iterable<Set<DisplaySourceSet>>, 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)

Expand Down
Expand Up @@ -5,9 +5,11 @@
package linkableContent

import org.jetbrains.dokka.SourceLinkDefinitionImpl
import org.jetbrains.dokka.base.resolvers.local.DokkaLocationProvider
import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
import org.jetbrains.dokka.base.transformers.pages.DefaultSamplesTransformer
import org.jetbrains.dokka.base.transformers.pages.sourcelinks.SourceLinksTransformer
import org.jetbrains.dokka.links.Callable
import org.jetbrains.dokka.model.WithGenerics
import org.jetbrains.dokka.model.dfs
import org.jetbrains.dokka.model.doc.Text
Expand Down Expand Up @@ -418,4 +420,5 @@ class LinkableContentTest : BaseAbstractTest() {
}
}
}

}
Expand Up @@ -5,16 +5,21 @@
package locationProvider

import org.jetbrains.dokka.base.resolvers.external.DefaultExternalLocationProvider
import org.jetbrains.dokka.base.resolvers.local.DokkaLocationProvider
import org.jetbrains.dokka.base.resolvers.shared.ExternalDocumentation
import org.jetbrains.dokka.base.resolvers.shared.PackageList
import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
import org.jetbrains.dokka.links.Callable
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.links.TypeConstructor
import org.jetbrains.dokka.model.dfs
import org.jetbrains.dokka.pages.ClasslikePageNode
import org.jetbrains.dokka.plugability.DokkaContext
import utils.TestOutputWriterPlugin
import java.net.URL
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class DefaultExternalLocationProviderTest : BaseAbstractTest() {
private val testDataDir =
Expand Down Expand Up @@ -75,4 +80,82 @@ class DefaultExternalLocationProviderTest : BaseAbstractTest() {

assertEquals(null, locationProvider.resolve(dri))
}

@Test
fun `should have a correct url to an external inherited member #2879`() {
val writerPlugin = TestOutputWriterPlugin()
val configuration = dokkaConfiguration {


sourceSets {
sourceSet {
externalDocumentationLinks = listOf(stdlibExternalDocumentationLink)
sourceRoots = listOf("src/")
classpath = listOfNotNull(jvmStdlibPath)
}
}
}

testInline(
"""
/src/kotlin/main.kt
open interface C : Collection<C>
interface A : C()
interface B : C()
""".trimIndent()
,
pluginOverrides = listOf(writerPlugin),
configuration = configuration
) {
renderingStage = { rootPage, ctx ->
val location = DokkaLocationProvider(rootPage, ctx, ".html")
val classA = rootPage.dfs { it is ClasslikePageNode && it.name == "A" }
val classB = rootPage.dfs { it is ClasslikePageNode && it.name == "B" }
val classC = rootPage.dfs { it is ClasslikePageNode && it.name == "C" }
val sourceSet = (classA as ClasslikePageNode).content.sourceSets
val dri = DRI("kotlin.collections", "Collection", Callable(name="isEmpty", receiver=null, params=emptyList()))
assertEquals("https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/is-empty.html", location.resolve(dri, sourceSet, classA))
assertEquals("https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/is-empty.html", location.resolve(dri, sourceSet, classB))
assertEquals("https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/is-empty.html", location.resolve(dri, sourceSet, classC))
}
}
}

@Test
fun `should have external links for external inherited members`() {
val writerPlugin = TestOutputWriterPlugin()
val configuration = dokkaConfiguration {
sourceSets {
sourceSet {
externalDocumentationLinks = listOf(stdlibExternalDocumentationLink)
sourceRoots = listOf("src/")
classpath = listOfNotNull(jvmStdlibPath)
}
}
}

testInline(
"""
/src/kotlin/main.kt
interface MyCharSequence: CharSequence
""".trimIndent()
,
pluginOverrides = listOf(writerPlugin),
configuration = configuration
) {
renderingStage = { _, _ ->
"".chars()
val content = writerPlugin.writer.contents["root/[root]/-my-char-sequence/index.html"] ?: ""
assertTrue(content.contains("<a href=\"https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/length.html\"><span><span>length</span></span></a>"))
assertTrue(content.contains("<a href=\"https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/get.html\"><span><span>get</span></span></a>"))
assertTrue(content.contains("<a href=\"https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/sub-sequence.html\"><span>sub</span><wbr></wbr><span><span>Sequence</span></span></a>"))
// TODO #3542
// these links are invalid
// chars() and codePoints() are absent in https://kotlinlang.org/ since they come from mapping Kotlin to Java
// see https://kotlinlang.org/docs/java-interop.html#mapped-types
assertTrue(content.contains("<a href=\"https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/chars.html\"><span><span>chars</span></span></a>"))
assertTrue(content.contains("<a href=\"https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/code-points.html\"><span>code</span><wbr></wbr><span><span>Points</span></span></a>"))
}
}
}
}