diff options
author | Andrzej Ratajczak <andrzej.ratajczak98@gmail.com> | 2020-09-18 15:53:37 +0200 |
---|---|---|
committer | Paweł Marks <Kordyjan@users.noreply.github.com> | 2020-09-28 16:54:22 +0200 |
commit | 2274d9261a59570cc3a1a26c3f7ddc167678fe8b (patch) | |
tree | bde5200a8d97b77d07db18a153b107c4b39bad1e | |
parent | 4b4bd919f0ae94190d475c99c3d5fa0140822b84 (diff) | |
download | dokka-2274d9261a59570cc3a1a26c3f7ddc167678fe8b.tar.gz dokka-2274d9261a59570cc3a1a26c3f7ddc167678fe8b.tar.bz2 dokka-2274d9261a59570cc3a1a26c3f7ddc167678fe8b.zip |
Add default values for primitive proprties. Add presenting in signature default values
4 files changed, 55 insertions, 9 deletions
diff --git a/core/src/main/kotlin/model/defaultValues.kt b/core/src/main/kotlin/model/defaultValues.kt index ab6cd376..d60c90cd 100644 --- a/core/src/main/kotlin/model/defaultValues.kt +++ b/core/src/main/kotlin/model/defaultValues.kt @@ -3,11 +3,11 @@ package org.jetbrains.dokka.model import org.jetbrains.dokka.model.properties.ExtraProperty import org.jetbrains.dokka.model.properties.MergeStrategy -class DefaultValue(val value: String): ExtraProperty<DParameter> { - companion object : ExtraProperty.Key<DParameter, DefaultValue> { - override fun mergeStrategyFor(left: DefaultValue, right: DefaultValue): MergeStrategy<DParameter> = MergeStrategy.Remove // TODO pass a logger somehow and log this +class DefaultValue(val value: String): ExtraProperty<Documentable> { + companion object : ExtraProperty.Key<Documentable, DefaultValue> { + override fun mergeStrategyFor(left: DefaultValue, right: DefaultValue): MergeStrategy<Documentable> = MergeStrategy.Remove // TODO pass a logger somehow and log this } - override val key: ExtraProperty.Key<DParameter, *> + override val key: ExtraProperty.Key<Documentable, *> get() = Companion -}
\ No newline at end of file +} diff --git a/plugins/base/src/main/kotlin/signatures/KotlinSignatureProvider.kt b/plugins/base/src/main/kotlin/signatures/KotlinSignatureProvider.kt index 324ef303..0a22cece 100644 --- a/plugins/base/src/main/kotlin/signatures/KotlinSignatureProvider.kt +++ b/plugins/base/src/main/kotlin/signatures/KotlinSignatureProvider.kt @@ -220,6 +220,9 @@ class KotlinSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLog link(p.name, p.dri) text(": ") signatureForProjection(p.type) + p.extra[DefaultValue]?.run { + text(" = $value") + } } } @@ -255,6 +258,9 @@ class KotlinSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLog text(it.name!!) text(": ") signatureForProjection(it.type) + it.extra[DefaultValue]?.run { + text(" = $value") + } } text(")") if (f.documentReturnType()) { diff --git a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt index b1c82efb..f58b2b36 100644 --- a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt @@ -343,11 +343,12 @@ private class DokkaDescriptorVisitor( sourceSets = setOf(sourceSet), generics = descriptor.typeParameters.map { it.toVariantTypeParameter() }, isExpectActual = (isExpect || isActual), - extra = PropertyContainer.withAll( + extra = PropertyContainer.withAll(listOfNotNull( (descriptor.additionalExtras() + descriptor.getAnnotationsWithBackingField() .toAdditionalExtras()).toSet().toSourceSetDependent().toAdditionalModifiers(), - descriptor.getAnnotationsWithBackingField().toSourceSetDependent().toAnnotations() - ) + descriptor.getAnnotationsWithBackingField().toSourceSetDependent().toAnnotations(), + descriptor.getDefaultValue()?.let { DefaultValue(it) } + )) ) } @@ -770,10 +771,12 @@ private class DokkaDescriptorVisitor( } } - private fun ValueParameterDescriptor.getDefaultValue(): String? = (source as? KotlinSourceElement)?.psi?.children?.find { it is KtExpression }?.text + private fun PropertyDescriptor.getDefaultValue(): String? = + (source as? KotlinSourceElement)?.psi?.children?.find { it is KtConstantExpression }?.text + private fun ClassDescriptor.getAppliedConstructorParameters() = (source as PsiSourceElement).psi?.children?.flatMap { it.safeAs<KtInitializerList>()?.initializersAsText().orEmpty() diff --git a/plugins/base/src/test/kotlin/signatures/SignatureTest.kt b/plugins/base/src/test/kotlin/signatures/SignatureTest.kt index f5b1bf2e..c6a52271 100644 --- a/plugins/base/src/test/kotlin/signatures/SignatureTest.kt +++ b/plugins/base/src/test/kotlin/signatures/SignatureTest.kt @@ -523,4 +523,41 @@ class SignatureTest : AbstractCoreTest() { } } } + + @Test + fun `fun with default values`() { + val source = source("fun simpleFun(int: Int = 1, string: String = \"string\"): String = \"\"") + val writerPlugin = TestOutputWriterPlugin() + + testInline( + source, + configuration, + pluginOverrides = listOf(writerPlugin) + ) { + renderingStage = { _, _ -> + writerPlugin.writer.renderedContent("root/example/simple-fun.html").firstSignature().match( + "fun", A("simpleFun"), "(int: ", A("Int"), " = 1, string: ", A("String"), + " = \"string\"): ", A("String"), Span() + ) + } + } + } + + @Test + fun `const val with default values`() { + val source = source("const val simpleVal = 1") + val writerPlugin = TestOutputWriterPlugin() + + testInline( + source, + configuration, + pluginOverrides = listOf(writerPlugin) + ) { + renderingStage = { _, _ -> + writerPlugin.writer.renderedContent("root/example.html").firstSignature().match( + "const val ", A("simpleVal"), ": ", A("Int"), " = 1", Span() + ) + } + } + } } |