Skip to content

Commit

Permalink
[K2] Process links inside KDoc section (#3449)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmishenev authored Jan 19, 2024
1 parent 42ce2bd commit 053879d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.jetbrains.kotlin.kdoc.parser.KDocKnownTag
import org.jetbrains.kotlin.kdoc.psi.impl.KDocLink
import org.jetbrains.kotlin.kdoc.psi.impl.KDocSection
import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag
import org.jetbrains.kotlin.psi.psiUtil.allChildren
import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType

internal fun parseFromKDocTag(
kDocTag: KDocTag?,
Expand All @@ -34,7 +34,11 @@ internal fun parseFromKDocTag(
listOf(kDocTag) + if (kDocTag.canHaveParent() && parseWithChildren) getAllKDocTags(findParent(kDocTag)) else emptyList()
DocumentationNode(
allTags.map { tag ->
val links = tag.allChildren.filterIsInstance<KDocLink>().associate { it.getLinkText() to externalDri(it) }
val links = mutableMapOf<String, DRI?>()
tag.forEachDescendantOfType<KDocLink>{
links[it.getLinkText()] = externalDri(it)
}

val externalDRIProvider = { linkText: String -> links[linkText] }

when (tag.knownTag) {
Expand Down
54 changes: 54 additions & 0 deletions dokka-subprojects/plugin-base/src/test/kotlin/markdown/LinkTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package markdown
import org.jetbrains.dokka.analysis.kotlin.markdown.MARKDOWN_ELEMENT_FILE_NAME
import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
import org.jetbrains.dokka.links.*
import org.jetbrains.dokka.model.DClass
import org.jetbrains.dokka.model.DFunction
import org.jetbrains.dokka.model.WithGenerics
import org.jetbrains.dokka.model.dfs
Expand Down Expand Up @@ -372,4 +373,57 @@ class LinkTest : BaseAbstractTest() {
}
}
}

@Test
fun `link should be resolved in @constructor section`() {
val configuration = dokkaConfiguration {
sourceSets {
sourceSet {
sourceRoots = listOf("src/")
}
}
}

testInline(
"""
|/src/main/kotlin/Testing.kt
|package example
|
|/**
|* @constructor reference in constructor [AllKDocTagsClass]
| */
|class AllKDocTagsClass(paramInt: Int, paramStr: String = "100"){}
|
""".trimMargin(),
configuration
) {
documentablesMergingStage = { module ->
val functionDocs = (module.packages.flatMap { it.classlikes }.first() as DClass).constructors.first().documentation.values.first()
val expected = Description(
root = CustomDocTag(
children = listOf(
P(
children = listOf(
Text("reference in constructor "),
DocumentationLink(
dri = DRI(
packageName = "example",
classNames = "AllKDocTagsClass",
target = PointingToDeclaration
),
children = listOf(
Text("AllKDocTagsClass")
),
params = mapOf("href" to "[AllKDocTagsClass]")
)
)
)
),
name = "MARKDOWN_FILE"
)
)
assertEquals(expected, functionDocs.children.first())
}
}
}
}

0 comments on commit 053879d

Please sign in to comment.