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 source link to the line with name #2724

Merged
merged 4 commits into from Oct 26, 2022
Merged
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 @@ -2,6 +2,7 @@ package org.jetbrains.dokka.base.transformers.pages.sourcelinks

import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiIdentifier
import org.jetbrains.dokka.DokkaConfiguration
import org.jetbrains.dokka.DokkaConfiguration.DokkaSourceSet
import org.jetbrains.dokka.analysis.DescriptorDocumentableSource
Expand All @@ -16,6 +17,8 @@ import org.jetbrains.dokka.plugability.plugin
import org.jetbrains.dokka.plugability.querySingle
import org.jetbrains.dokka.transformers.pages.PageTransformer
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithSource
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
import org.jetbrains.kotlin.resolve.source.getPsi
import org.jetbrains.kotlin.utils.addToStdlib.cast
import java.io.File
Expand Down Expand Up @@ -92,8 +95,10 @@ class SourceLinksTransformer(val context: DokkaContext) : PageTransformer {
}

private fun PsiElement.lineNumber(): Int? {
val ktIdentifierTextRange = this.node?.findChildByType(KtTokens.IDENTIFIER)?.textRange
val javaIdentifierTextRange = this.getChildOfType<PsiIdentifier>()?.textRange
// synthetic and some light methods might return null
val textRange = textRange ?: return null
val textRange = ktIdentifierTextRange ?: javaIdentifierTextRange ?: textRange ?: return null

val doc = PsiDocumentManager.getInstance(project).getDocument(containingFile)
// IJ uses 0-based line-numbers; external source browsers use 1-based
Expand Down
5 changes: 4 additions & 1 deletion plugins/base/src/test/kotlin/enums/JavaEnumsTest.kt
Expand Up @@ -47,6 +47,9 @@ class JavaEnumsTest : BaseAbstractTest() {
|/src/main/java/basic/JavaEnum.java
|package testpackage
|
|/**
|* doc
|*/
|public enum JavaEnum {
| ONE, TWO, THREE
|}
Expand All @@ -62,7 +65,7 @@ class JavaEnumsTest : BaseAbstractTest() {


assertEquals(
"https://github.com/user/repo/tree/master/src/main/java/basic/JavaEnum.java#L3",
"https://github.com/user/repo/tree/master/src/main/java/basic/JavaEnum.java#L6",
sourceLink
)
}
Expand Down
5 changes: 4 additions & 1 deletion plugins/base/src/test/kotlin/enums/KotlinEnumsTest.kt
Expand Up @@ -398,6 +398,9 @@ class KotlinEnumsTest : BaseAbstractTest() {
|/src/main/kotlin/basic/KotlinEnum.kt
|package testpackage
|
|/**
|* Doc
|*/
|enum class KotlinEnum {
| ONE, TWO, THREE
|}
Expand All @@ -412,7 +415,7 @@ class KotlinEnumsTest : BaseAbstractTest() {
.attr("href")

assertEquals(
"https://github.com/user/repo/tree/master/src/main/kotlin/basic/KotlinEnum.kt#L3",
"https://github.com/user/repo/tree/master/src/main/kotlin/basic/KotlinEnum.kt#L6",
sourceLink
)
}
Expand Down
@@ -0,0 +1,67 @@
package transformers

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
import org.jsoup.nodes.Element

class SourceLinkTransformerTest : BaseAbstractTest() {

private fun Element.getSourceLink() = select(".symbol .floating-right")
.select("a[href]")
.attr("href")

@Test
fun `source link should lead to name`() {
val configuration = dokkaConfiguration {
sourceSets {
sourceSet {
sourceRoots = listOf("src/")
sourceLinks = listOf(
SourceLinkDefinitionImpl(
localDirectory = "src/main/kotlin",
remoteUrl = URL("https://github.com/user/repo/tree/master/src/main/kotlin"),
remoteLineSuffix = "#L"
)
)
}
}
}

val writerPlugin = TestOutputWriterPlugin()

testInline(
"""
|/src/main/kotlin/basic/Deprecated.kt
|package testpackage
|
|/**
|* Marks the annotated declaration as deprecated. ...
|*/
|@Target(CLASS, FUNCTION, PROPERTY, ANNOTATION_CLASS, CONSTRUCTOR, PROPERTY_SETTER, PROPERTY_GETTER, TYPEALIAS)
|@MustBeDocumented
|public annotation class Deprecated(
| val message: String,
| val replaceWith: ReplaceWith = ReplaceWith(""),
| val level: DeprecationLevel = DeprecationLevel.WARNING
|)
""".trimMargin(),
configuration,
pluginOverrides = listOf(writerPlugin)
) {
renderingStage = { _, _ ->
val page = writerPlugin.writer.renderedContent("root/testpackage/-deprecated/index.html")
val sourceLink = page.getSourceLink()

assertEquals(
"https://github.com/user/repo/tree/master/src/main/kotlin/basic/Deprecated.kt#L8",
sourceLink
)
}
}
}
}