Skip to content

Commit

Permalink
[K2] Resolve java samples as well
Browse files Browse the repository at this point in the history
`sourcePsiSafe` does not support Java KT-53669
  • Loading branch information
vmishenev committed Nov 21, 2023
1 parent 829ca18 commit 2444951
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import org.jetbrains.dokka.analysis.test.api.mixedJvmTestProject
import org.jetbrains.dokka.analysis.test.api.useServices
import org.jetbrains.dokka.analysis.test.api.util.CollectingDokkaConsoleLogger
import org.jetbrains.dokka.analysis.test.api.util.singleSourceSet
import org.junit.jupiter.api.Tag
import kotlin.test.*

class SampleAnalysisTest {
Expand Down Expand Up @@ -323,7 +322,6 @@ class SampleAnalysisTest {
}

@Test
@Tag("onlyDescriptors") // TODO #3359
fun `should return null if trying to resolve a non-kotlin sample link`() {
val testProject = mixedJvmTestProject {
kotlinSourceDirectory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import org.jetbrains.dokka.plugability.plugin
import org.jetbrains.dokka.plugability.querySingle
import org.jetbrains.dokka.utilities.DokkaLogger
import org.jetbrains.kotlin.analysis.api.analyze
import org.jetbrains.kotlin.analysis.api.symbols.sourcePsiSafe
import org.jetbrains.kotlin.analysis.api.symbols.KtSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtSymbolOrigin
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.psi.KtBlockExpression
import org.jetbrains.kotlin.psi.KtDeclarationWithBody
Expand Down Expand Up @@ -79,6 +80,7 @@ private class SymbolSampleAnalysisEnvironment(
}

// copy-pasted from StdLib 1.5
// TODO use it from Stdlib
private inline fun <T, R : Any> Iterable<T>.firstNotNullOfOrNull(transform: (T) -> R?): R? {
for (element in this) {
val result = transform(element)
Expand All @@ -89,6 +91,28 @@ private class SymbolSampleAnalysisEnvironment(
return null
}

// TODO: remove after KT-53669 and use [org.jetbrains.kotlin.analysis.api.symbols.sourcePsiSafe] from Analysis API
private inline fun <reified PSI : PsiElement> KtSymbol.kotlinAndJavaSourcePsiSafe(): PSI? {
// TODO: support Java sources after KT-53669
val sourcePsi = when (origin) {
KtSymbolOrigin.SOURCE -> this.psi
KtSymbolOrigin.JAVA -> this.psi

KtSymbolOrigin.SOURCE_MEMBER_GENERATED -> null
KtSymbolOrigin.LIBRARY -> null
KtSymbolOrigin.SAM_CONSTRUCTOR -> null
KtSymbolOrigin.INTERSECTION_OVERRIDE -> null
KtSymbolOrigin.SUBSTITUTION_OVERRIDE -> null
KtSymbolOrigin.DELEGATED -> null
KtSymbolOrigin.JAVA_SYNTHETIC_PROPERTY -> null
KtSymbolOrigin.PROPERTY_BACKING_FIELD -> null
KtSymbolOrigin.PLUGIN -> null
KtSymbolOrigin.JS_DYNAMIC -> null
}

return sourcePsi as? PSI
}

private fun findPsiElement(sourceSet: DokkaSourceSet, fqLink: String): PsiElement? {
// fallback to default roots of the source set even if sample roots are assigned
val ktSourceModules =
Expand All @@ -97,7 +121,7 @@ private class SymbolSampleAnalysisEnvironment(
return ktSourceModules.firstNotNullOfOrNull { ktSourceModule ->
analyze(ktSourceModule) {
resolveKDocTextLinkSymbol(fqLink)
?.sourcePsiSafe()
?.kotlinAndJavaSourcePsiSafe()
}
}
}
Expand Down

0 comments on commit 2444951

Please sign in to comment.