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
Do not generate source links for synthetic elements #2547
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A bit of refactoring to make it not run if sources are not configured and to not parse configuration on every documentable |
||
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package enums | ||
|
||
import org.jetbrains.dokka.SourceLinkDefinitionImpl | ||
import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest | ||
import org.junit.jupiter.api.Test | ||
import signatures.renderedContent | ||
import utils.TestOutputWriterPlugin | ||
import java.net.URL | ||
import kotlin.test.assertEquals | ||
|
||
class JavaEnumsTest : BaseAbstractTest() { | ||
|
||
// Shouldn't try to give source links to synthetic methods (values, valueOf) if any are present | ||
// https://github.com/Kotlin/dokka/issues/2544 | ||
@Test | ||
fun `java enum with configured source links should not fail build due to any synthetic methods`() { | ||
val configuration = dokkaConfiguration { | ||
sourceSets { | ||
sourceSet { | ||
sourceRoots = listOf("src/") | ||
sourceLinks = listOf( | ||
SourceLinkDefinitionImpl( | ||
localDirectory = "src/main/java", | ||
remoteUrl = URL("https://github.com/user/repo/tree/master/src/main/java"), | ||
remoteLineSuffix = "#L" | ||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
val writerPlugin = TestOutputWriterPlugin() | ||
|
||
testInline( | ||
""" | ||
|/src/main/java/basic/JavaEnum.java | ||
|package testpackage | ||
| | ||
|public enum JavaEnum { | ||
| ONE, TWO, THREE | ||
|} | ||
""".trimMargin(), | ||
configuration, | ||
pluginOverrides = listOf(writerPlugin) | ||
) { | ||
renderingStage = { _, _ -> | ||
val enumPage = writerPlugin.writer.renderedContent("root/testpackage/-java-enum/index.html") | ||
val sourceLink = enumPage.select("div[data-togglable=Sources]") | ||
.select("a[href]") | ||
.attr("href") | ||
|
||
|
||
assertEquals( | ||
"https://github.com/user/repo/tree/master/src/main/java/basic/JavaEnum.java#L3", | ||
sourceLink | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with paths was here.
canonicalPath
resolves symlinks, whileabsolutePath
does not, so on windows it resulted in two different temp folder locations.I'll refactor this bit later on in a separate PR once I get a hold of a windows machine