From 76334ec6e8d66b377fc9c37187725f8d267d5ba2 Mon Sep 17 00:00:00 2001 From: Ignat Beresnev Date: Thu, 30 Jun 2022 16:02:27 +0200 Subject: Do not generate source links for synthetic elements (#2547) Fixes #2544 --- .../pages/sourcelinks/SourceLinksTransformer.kt | 52 ++++++++++++++-------- 1 file changed, 33 insertions(+), 19 deletions(-) (limited to 'plugins/base/src/main/kotlin/transformers/pages') 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().querySingle { commentsToContentConverter }, context.plugin().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().flatMap { resolveSources(it) } - .takeIf { it.isNotEmpty() } - ?.let { node.addSourcesContent(it) } - ?: node + node.documentables + .filterIsInstance() + .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 { + 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, documentable: WithSources + ): List> { + 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>) = 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) -- cgit