diff options
author | Szymon Świstun <sswistun@virtuslab.com> | 2020-03-16 13:08:29 +0100 |
---|---|---|
committer | Kamil Doległo <kamilok1965@users.noreply.github.com> | 2020-03-18 14:40:29 +0100 |
commit | 3de1182033ca5a5db374e16a6ab3555627faa79b (patch) | |
tree | 192580f4a08340466fd293a7199f110e34fe1dbf | |
parent | 03329b0eb98b309d208839344052633028e00984 (diff) | |
download | dokka-3de1182033ca5a5db374e16a6ab3555627faa79b.tar.gz dokka-3de1182033ca5a5db374e16a6ab3555627faa79b.tar.bz2 dokka-3de1182033ca5a5db374e16a6ab3555627faa79b.zip |
Add default values for parameters
-rw-r--r-- | core/src/main/kotlin/model/defaultValues.kt | 18 | ||||
-rw-r--r-- | core/src/main/kotlin/model/properties/PropertyContainer.kt | 5 | ||||
-rw-r--r-- | core/src/main/kotlin/transformers/pages/PageTransformerBuilders.kt | 6 | ||||
-rw-r--r-- | plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt | 20 | ||||
-rw-r--r-- | plugins/base/src/test/kotlin/transformerBuilders/PageTransformerBuilderTest.kt (renamed from core/src/test/kotlin/transformerBuilders/PageTransformerBuilderTest.kt) | 15 |
5 files changed, 48 insertions, 16 deletions
diff --git a/core/src/main/kotlin/model/defaultValues.kt b/core/src/main/kotlin/model/defaultValues.kt new file mode 100644 index 00000000..63b69cbb --- /dev/null +++ b/core/src/main/kotlin/model/defaultValues.kt @@ -0,0 +1,18 @@ +package org.jetbrains.dokka.model + +import org.jetbrains.dokka.model.properties.ExtraProperty +import org.jetbrains.dokka.model.properties.MergeStrategy +import java.lang.IllegalStateException + +class DefaultValue(val value: String): ExtraProperty<DParameter> { + companion object : ExtraProperty.Key<DParameter, DefaultValue> { + override fun mergeStrategyFor(left: DefaultValue, right: DefaultValue): MergeStrategy<DParameter> = if (left.value == right.value) + MergeStrategy.Replace(left) + else + MergeStrategy.Fail {throw IllegalStateException("Default values need to be the same")} + + } + + override val key: ExtraProperty.Key<DParameter, *> + get() = Companion +}
\ No newline at end of file diff --git a/core/src/main/kotlin/model/properties/PropertyContainer.kt b/core/src/main/kotlin/model/properties/PropertyContainer.kt index 5ea42e42..107bede5 100644 --- a/core/src/main/kotlin/model/properties/PropertyContainer.kt +++ b/core/src/main/kotlin/model/properties/PropertyContainer.kt @@ -15,12 +15,13 @@ class PropertyContainer<C : Any> internal constructor( } inline fun <reified T : Any> allOfType(): List<T> = map.values.filterIsInstance<T>() - fun <D : C> addAll(vararg extras: ExtraProperty<D>): PropertyContainer<D> = + fun <D : C> addAll(extras: Collection<ExtraProperty<D>>): PropertyContainer<D> = PropertyContainer(map + extras.map { p -> p.key to p }) companion object { fun <T : Any> empty(): PropertyContainer<T> = PropertyContainer(emptyMap()) - fun <T : Any> withAll(vararg extras: ExtraProperty<T>) = empty<T>().addAll(*extras) + fun <T : Any> withAll(vararg extras: ExtraProperty<T>) = empty<T>().addAll(extras.toList()) + fun <T : Any> withAll(extras: Collection<ExtraProperty<T>>) = empty<T>().addAll(extras) } } diff --git a/core/src/main/kotlin/transformers/pages/PageTransformerBuilders.kt b/core/src/main/kotlin/transformers/pages/PageTransformerBuilders.kt index 89b22c19..291b72ef 100644 --- a/core/src/main/kotlin/transformers/pages/PageTransformerBuilders.kt +++ b/core/src/main/kotlin/transformers/pages/PageTransformerBuilders.kt @@ -3,15 +3,15 @@ package org.jetbrains.dokka.transformers.pages import org.jetbrains.dokka.pages.PageNode import org.jetbrains.dokka.pages.RootPageNode -fun pageScanner(block: PageNode.() -> Unit) = object : PageNodeTransformer { +fun pageScanner(block: PageNode.() -> Unit) = object : PageTransformer { override fun invoke(input: RootPageNode): RootPageNode = input.invokeOnAll(block) as RootPageNode } -fun pageMapper(block: PageNode.() -> PageNode) = object : PageNodeTransformer { +fun pageMapper(block: PageNode.() -> PageNode) = object : PageTransformer { override fun invoke(input: RootPageNode): RootPageNode = input.alterChildren(block) as RootPageNode } -fun pageStructureTransformer(block: RootPageNode.() -> RootPageNode) = object : PageNodeTransformer { +fun pageStructureTransformer(block: RootPageNode.() -> RootPageNode) = object : PageTransformer { override fun invoke(input: RootPageNode): RootPageNode = block(input) } diff --git a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt index 44679d8d..6d834f9c 100644 --- a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt @@ -19,16 +19,19 @@ import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.descriptors.impl.DeclarationDescriptorVisitorEmptyBodies import org.jetbrains.kotlin.idea.kdoc.findKDoc +import org.jetbrains.kotlin.psi.KtConstantExpression import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.calls.components.isVararg import org.jetbrains.kotlin.resolve.calls.tasks.isDynamic -import org.jetbrains.kotlin.resolve.descriptorUtil.* +import org.jetbrains.kotlin.resolve.descriptorUtil.annotationClass +import org.jetbrains.kotlin.resolve.descriptorUtil.getAllSuperclassesWithoutAny +import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassNotAny +import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperInterfaces import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter import org.jetbrains.kotlin.resolve.scopes.MemberScope +import org.jetbrains.kotlin.resolve.source.KotlinSourceElement import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.TypeProjection -import org.jetbrains.dokka.model.Variance -import org.jetbrains.kotlin.idea.kdoc.findKDoc class DefaultDescriptorToDocumentableTranslator( private val context: DokkaContext @@ -367,7 +370,12 @@ private class DokkaDescriptorVisitor( // TODO: close this class and make it priv type = descriptor.type.toBound(), documentation = descriptor.resolveDescriptorData(platformData), platformData = listOf(platformData), - extra = PropertyContainer.withAll(descriptor.additionalExtras(), descriptor.getAnnotations()) + extra = PropertyContainer.withAll( + listOfNotNull( + descriptor.additionalExtras(), + descriptor.getAnnotations(), + descriptor.getDefaultValue()?.let { DefaultValue(it) }) + ) ) private fun MemberScope.functions(parent: DRIWithPlatformInfo): List<DFunction> = @@ -523,6 +531,10 @@ private class DokkaDescriptorVisitor( // TODO: close this class and make it priv ) }.let(::Annotations) + fun ValueParameterDescriptor.getDefaultValue(): String? = (source as? KotlinSourceElement)?.let { + it.psi.children.find { it is KtConstantExpression }?.text + } + data class ClassInfo(val supertypes: List<DRI>, val docs: PlatformDependent<DocumentationNode>) private fun Visibility.toDokkaVisibility(): org.jetbrains.dokka.model.Visibility = when (this) { diff --git a/core/src/test/kotlin/transformerBuilders/PageTransformerBuilderTest.kt b/plugins/base/src/test/kotlin/transformerBuilders/PageTransformerBuilderTest.kt index 13ea5580..20ee7490 100644 --- a/core/src/test/kotlin/transformerBuilders/PageTransformerBuilderTest.kt +++ b/plugins/base/src/test/kotlin/transformerBuilders/PageTransformerBuilderTest.kt @@ -1,19 +1,20 @@ package transformerBuilders; import org.jetbrains.dokka.CoreExtensions -import org.jetbrains.dokka.pages.* +import org.jetbrains.dokka.pages.PageNode +import org.jetbrains.dokka.pages.RendererSpecificResourcePage +import org.jetbrains.dokka.pages.RenderingStrategy import org.jetbrains.dokka.plugability.DokkaPlugin -import org.jetbrains.dokka.transformers.pages.PageNodeTransformer +import org.jetbrains.dokka.testApi.testRunner.AbstractCoreTest +import org.jetbrains.dokka.transformers.pages.PageTransformer import org.jetbrains.dokka.transformers.pages.pageMapper import org.jetbrains.dokka.transformers.pages.pageScanner import org.jetbrains.dokka.transformers.pages.pageStructureTransformer -import org.jetbrains.dokka.utilities.DokkaConsoleLogger -import org.junit.Test -import testApi.testRunner.AbstractCoreTest +import org.junit.jupiter.api.Test class PageTransformerBuilderTest : AbstractCoreTest() { - class ProxyPlugin(transformer: PageNodeTransformer) : DokkaPlugin() { + class ProxyPlugin(transformer: PageTransformer) : DokkaPlugin() { val pageTransformer by extending { CoreExtensions.pageTransformer with transformer } } @@ -48,7 +49,7 @@ class PageTransformerBuilderTest : AbstractCoreTest() { orig = it } pagesTransformationStage = { root -> - list.assertCount(8, "Page list: ") + list.assertCount(7, "Page list: ") orig?.let { root.assertTransform(it) } } } |