diff options
author | Ignat Beresnev <ignat.beresnev@jetbrains.com> | 2022-06-30 16:02:27 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-30 16:02:27 +0200 |
commit | 76334ec6e8d66b377fc9c37187725f8d267d5ba2 (patch) | |
tree | d3423cd6b47dc1e50edf378c8d33b131c606e19b /plugins/base/src/main/kotlin/transformers/pages | |
parent | 623b0e729c805e2ec019b1f56f67c5a2cf7eb327 (diff) | |
download | dokka-76334ec6e8d66b377fc9c37187725f8d267d5ba2.tar.gz dokka-76334ec6e8d66b377fc9c37187725f8d267d5ba2.tar.bz2 dokka-76334ec6e8d66b377fc9c37187725f8d267d5ba2.zip |
Do not generate source links for synthetic elements (#2547)
Fixes #2544
Diffstat (limited to 'plugins/base/src/main/kotlin/transformers/pages')
-rw-r--r-- | plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt | 52 |
1 files changed, 33 insertions, 19 deletions
diff --git a/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt b/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt index 93305055..50add451 100644 --- a/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt @@ -24,36 +24,47 @@ import java.io.File class SourceLinksTransformer(val context: DokkaContext) : PageTransformer { - private val builder : PageContentBuilder = PageContentBuilder( + private val builder : PageContentBuilder = PageContentBuilder( context.plugin<DokkaBase>().querySingle { commentsToContentConverter }, context.plugin<DokkaBase>().querySingle { signatureProvider }, context.logger ) - override fun invoke(input: RootPageNode) = - input.transformContentPagesTree { node -> + override fun invoke(input: RootPageNode): RootPageNode { + val sourceLinks = getSourceLinksFromConfiguration() + if (sourceLinks.isEmpty()) { + return input + } + return input.transformContentPagesTree { node -> when (node) { is WithDocumentables -> - node.documentables.filterIsInstance<WithSources>().flatMap { resolveSources(it) } - .takeIf { it.isNotEmpty() } - ?.let { node.addSourcesContent(it) } - ?: node + node.documentables + .filterIsInstance<WithSources>() + .flatMap { resolveSources(sourceLinks, it) } + .takeIf { it.isNotEmpty() } + ?.let { node.addSourcesContent(it) } + ?: node else -> node } } + } - private fun getSourceLinks() = context.configuration.sourceSets - .flatMap { it.sourceLinks.map { sl -> SourceLink(sl, it) } } + private fun getSourceLinksFromConfiguration(): List<SourceLink> { + return context.configuration.sourceSets + .flatMap { it.sourceLinks.map { sl -> SourceLink(sl, it) } } + } - private fun resolveSources(documentable: WithSources) = documentable.sources - .mapNotNull { entry -> - getSourceLinks().find { File(entry.value.path).startsWith(it.path) && it.sourceSetData == entry.key }?.let { - Pair( - entry.key, - entry.value.toLink(it) - ) - } + private fun resolveSources( + sourceLinks: List<SourceLink>, documentable: WithSources + ): List<Pair<DokkaSourceSet, String>> { + return documentable.sources.mapNotNull { (sourceSet, documentableSource) -> + val sourceLink = sourceLinks.find { sourceLink -> + File(documentableSource.path).startsWith(sourceLink.path) && sourceLink.sourceSetData == sourceSet + } ?: return@mapNotNull null + + sourceSet to documentableSource.toLink(sourceLink) } + } private fun ContentPage.addSourcesContent(sources: List<Pair<DokkaSourceSet, String>>) = builder .buildSourcesContent(this, sources) @@ -89,8 +100,8 @@ class SourceLinksTransformer(val context: DokkaContext) : PageTransformer { } private fun DocumentableSource.toLink(sourceLink: SourceLink): String { - val sourcePath = File(this.path).canonicalPath.replace("\\", "/") - val sourceLinkPath = File(sourceLink.path).canonicalPath.replace("\\", "/") + val sourcePath = File(this.path).invariantSeparatorsPath + val sourceLinkPath = File(sourceLink.path).invariantSeparatorsPath val lineNumber = when (this) { is DescriptorDocumentableSource -> this.descriptor @@ -134,6 +145,9 @@ class SourceLinksTransformer(val context: DokkaContext) : PageTransformer { } private fun PsiElement.lineNumber(): Int? { + // synthetic and some light methods might return null + val textRange = textRange ?: return null + val doc = PsiDocumentManager.getInstance(project).getDocument(containingFile) // IJ uses 0-based line-numbers; external source browsers use 1-based return doc?.getLineNumber(textRange.startOffset)?.plus(1) |