diff options
author | Vadim Mishenev <vad-mishenev@yandex.ru> | 2022-06-08 01:15:23 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-08 01:15:23 +0300 |
commit | 8c2a79f39377892be44d752d0958efb9b83f1fce (patch) | |
tree | 8ee91d4fa05fa31fb82df9ac1639812ff2840d4c /plugins/base | |
parent | 2e4121457a0fbae1a216007aee6c84a01be628f5 (diff) | |
download | dokka-8c2a79f39377892be44d752d0958efb9b83f1fce.tar.gz dokka-8c2a79f39377892be44d752d0958efb9b83f1fce.tar.bz2 dokka-8c2a79f39377892be44d752d0958efb9b83f1fce.zip |
Support definitely non-nullable types (#2520)
* Support definitely non-nullable types
[Definitely non-nullable types](https://github.com/Kotlin/KEEP/blob/c72601cf35c1e95a541bb4b230edb474a6d1d1a8/proposals/definitely-non-nullable-types.md) are enabled since Kotlin 1.7
Diffstat (limited to 'plugins/base')
6 files changed, 49 insertions, 1 deletions
diff --git a/plugins/base/src/main/kotlin/signatures/KotlinSignatureProvider.kt b/plugins/base/src/main/kotlin/signatures/KotlinSignatureProvider.kt index 294c1d24..02da3f24 100644 --- a/plugins/base/src/main/kotlin/signatures/KotlinSignatureProvider.kt +++ b/plugins/base/src/main/kotlin/signatures/KotlinSignatureProvider.kt @@ -430,6 +430,11 @@ class KotlinSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLog signatureForProjection(p.inner, showFullyQualifiedName) operator("?") } + is DefinitelyNonNullable -> group(styles = emptySet()) { + signatureForProjection(p.inner, showFullyQualifiedName) + operator(" & ") + link("Any", DriOfAny) + } is TypeAliased -> signatureForProjection(p.typeAlias) is JavaObject -> { diff --git a/plugins/base/src/main/kotlin/signatures/KotlinSignatureUtils.kt b/plugins/base/src/main/kotlin/signatures/KotlinSignatureUtils.kt index 0fea9132..381fadf1 100644 --- a/plugins/base/src/main/kotlin/signatures/KotlinSignatureUtils.kt +++ b/plugins/base/src/main/kotlin/signatures/KotlinSignatureUtils.kt @@ -39,6 +39,7 @@ object KotlinSignatureUtils : JvmSignatureUtils { is TypeParameter -> dri is TypeConstructor -> dri is Nullable -> inner.driOrNull + is DefinitelyNonNullable -> inner.driOrNull is PrimitiveJavaType -> dri is Void -> DriOfUnit is JavaObject -> DriOfAny @@ -52,6 +53,7 @@ object KotlinSignatureUtils : JvmSignatureUtils { is TypeParameter -> listOf(dri) is TypeConstructor -> listOf(dri) + projections.flatMap { it.drisOfAllNestedBounds } is Nullable -> inner.drisOfAllNestedBounds + is DefinitelyNonNullable -> inner.drisOfAllNestedBounds is PrimitiveJavaType -> listOf(dri) is Void -> listOf(DriOfUnit) is JavaObject -> listOf(DriOfAny) diff --git a/plugins/base/src/main/kotlin/transformers/documentables/ExtensionExtractorTransformer.kt b/plugins/base/src/main/kotlin/transformers/documentables/ExtensionExtractorTransformer.kt index 2a8baf0b..73023a86 100644 --- a/plugins/base/src/main/kotlin/transformers/documentables/ExtensionExtractorTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/documentables/ExtensionExtractorTransformer.kt @@ -9,7 +9,6 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.toList -import org.jetbrains.dokka.base.signatures.KotlinSignatureUtils.driOrNull import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.links.DriOfAny import org.jetbrains.dokka.model.* @@ -91,6 +90,7 @@ private fun Callable.asPairsWithReceiverDRIs(): Sequence<Pair<DRI, Callable>> = // care about it since there is nowhere to put documentation of given extension. private fun Callable.findReceiverDRIs(bound: Bound): Sequence<DRI> = when (bound) { is Nullable -> findReceiverDRIs(bound.inner) + is DefinitelyNonNullable -> findReceiverDRIs(bound.inner) is TypeParameter -> if (this is DFunction && bound.dri == this.dri) generics.find { it.name == bound.name }?.bounds?.asSequence()?.flatMap(::findReceiverDRIs).orEmpty() diff --git a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt index 6c4d7a0d..bfd755cd 100644 --- a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt @@ -910,6 +910,9 @@ private class DokkaDescriptorVisitor( expandedType.toBound(), annotations() ) + is DefinitelyNotNullType -> DefinitelyNonNullable( + original.toBound() + ) else -> when (val ctor = constructor.declarationDescriptor) { is TypeParameterDescriptor -> TypeParameter( dri = DRI.from(ctor), diff --git a/plugins/base/src/test/kotlin/signatures/SignatureTest.kt b/plugins/base/src/test/kotlin/signatures/SignatureTest.kt index 2d17b7f8..75c199fa 100644 --- a/plugins/base/src/test/kotlin/signatures/SignatureTest.kt +++ b/plugins/base/src/test/kotlin/signatures/SignatureTest.kt @@ -2,8 +2,12 @@ package signatures import org.jetbrains.dokka.DokkaSourceSetID import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest +import org.jetbrains.dokka.model.DFunction +import org.jetbrains.dokka.model.DefinitelyNonNullable +import org.jetbrains.dokka.model.dfs import org.junit.jupiter.api.Test import utils.* +import kotlin.test.assertEquals import kotlin.test.assertFalse class SignatureTest : BaseAbstractTest() { @@ -186,6 +190,39 @@ class SignatureTest : BaseAbstractTest() { } @Test + fun `fun with definitely non-nullable types`() { + val source = source("fun <T> elvisLike(x: T, y: T & Any): T & Any = x ?: y") + val writerPlugin = TestOutputWriterPlugin() + + testInline( + source, + configuration, + pluginOverrides = listOf(writerPlugin) + ) { + documentablesTransformationStage = { + val fn = (it.dfs { it.name == "elvisLike" } as? DFunction).assertNotNull("Function elvisLike") + + assert(fn.type is DefinitelyNonNullable) + assert(fn.parameters[1].type is DefinitelyNonNullable) + } + renderingStage = { _, _ -> + val signature = writerPlugin.writer.renderedContent("root/example/elvis-like.html") + assertEquals(2, signature.select("a[href=\"https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html\"]").size) + signature.firstSignature().match( + "fun <", A("T"), "> ", A("elvisLike"), + "(", + Span( + Span("x: ", A("T"), ", "), + Span("y: ", A("T"), " & ", A("Any")) + ), + "): ", A("T"), " & ", A("Any"), Span(), + ignoreSpanWithTokenStyle = true + ) + } + } + } + + @Test fun `fun with keywords, params and generic bound`() { val source = source("inline suspend fun <T : String> simpleFun(a: Int, b: String): T = \"Celebrimbor\" as T") val writerPlugin = TestOutputWriterPlugin() diff --git a/plugins/base/src/test/kotlin/utils/TestUtils.kt b/plugins/base/src/test/kotlin/utils/TestUtils.kt index 99082110..e4bc4a1e 100644 --- a/plugins/base/src/test/kotlin/utils/TestUtils.kt +++ b/plugins/base/src/test/kotlin/utils/TestUtils.kt @@ -66,6 +66,7 @@ val DClass.supers val Bound.name: String? get() = when (this) { is Nullable -> inner.name + is DefinitelyNonNullable -> inner.name is TypeParameter -> name is PrimitiveJavaType -> name is TypeConstructor -> dri.classNames |