Skip to content

Commit

Permalink
Annotations for parameters (#1710)
Browse files Browse the repository at this point in the history
* Annotations for parameters

* Annotations for parameters
  • Loading branch information
MarcinAman authored Feb 5, 2021
1 parent b44e41e commit 70000c8
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 97 deletions.
60 changes: 46 additions & 14 deletions core/src/main/kotlin/model/Documentable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import org.jetbrains.dokka.model.doc.DocumentationNode
import org.jetbrains.dokka.model.properties.PropertyContainer
import org.jetbrains.dokka.model.properties.WithExtraProperties

interface AnnotationTarget

abstract class Documentable : WithChildren<Documentable> {
abstract class Documentable : WithChildren<Documentable>,
AnnotationTarget {
abstract val name: String?
abstract val dri: DRI
abstract val documentation: SourceSetDependent<DocumentationNode>
Expand Down Expand Up @@ -346,7 +348,14 @@ data class DTypeParameter(
bounds: List<Bound>,
sourceSets: Set<DokkaSourceSet>,
extra: PropertyContainer<DTypeParameter> = PropertyContainer.empty()
) : this(Invariance(TypeParameter(dri, name, presentableName)), documentation, expectPresentInSet, bounds, sourceSets, extra)
) : this(
Invariance(TypeParameter(dri, name, presentableName)),
documentation,
expectPresentInSet,
bounds,
sourceSets,
extra
)

override val dri: DRI by variantTypeParameter.inner::dri
override val name: String by variantTypeParameter.inner::name
Expand Down Expand Up @@ -376,8 +385,17 @@ data class DTypeAlias(
}

sealed class Projection
sealed class Bound : Projection()
data class TypeParameter(val dri: DRI, val name: String, val presentableName: String? = null) : Bound()
sealed class Bound : Projection(), AnnotationTarget
data class TypeParameter(
val dri: DRI,
val name: String,
val presentableName: String? = null,
override val extra: PropertyContainer<TypeParameter> = PropertyContainer.empty()
) : Bound(), WithExtraProperties<TypeParameter> {
override fun withNewExtras(newExtras: PropertyContainer<TypeParameter>): TypeParameter =
copy(extra = extra)
}

object Star : Projection()

sealed class TypeConstructor : Bound() {
Expand All @@ -389,49 +407,63 @@ sealed class TypeConstructor : Bound() {
data class GenericTypeConstructor(
override val dri: DRI,
override val projections: List<Projection>,
override val presentableName: String? = null
) : TypeConstructor()
override val presentableName: String? = null,
override val extra: PropertyContainer<GenericTypeConstructor> = PropertyContainer.empty()
) : TypeConstructor(), WithExtraProperties<GenericTypeConstructor> {
override fun withNewExtras(newExtras: PropertyContainer<GenericTypeConstructor>): GenericTypeConstructor =
copy(extra = newExtras)
}

data class FunctionalTypeConstructor(
override val dri: DRI,
override val projections: List<Projection>,
val isExtensionFunction: Boolean = false,
val isSuspendable: Boolean = false,
override val presentableName: String? = null
) : TypeConstructor()
override val presentableName: String? = null,
override val extra: PropertyContainer<FunctionalTypeConstructor> = PropertyContainer.empty(),
) : TypeConstructor(), WithExtraProperties<FunctionalTypeConstructor> {
override fun withNewExtras(newExtras: PropertyContainer<FunctionalTypeConstructor>): FunctionalTypeConstructor =
copy(extra = newExtras)
}

data class Nullable(val inner: Bound) : Bound()

sealed class Variance<out T : Bound> : Projection() {
abstract val inner: T
}

data class Covariance<out T : Bound>(override val inner: T) : Variance<T>() {
override fun toString() = "out"
}

data class Contravariance<out T : Bound>(override val inner: T) : Variance<T>() {
override fun toString() = "in"
}

data class Invariance<out T : Bound>(override val inner: T) : Variance<T>() {
override fun toString() = ""
}

data class TypeAliased(val typeAlias: Bound, val inner: Bound) : Bound()
data class PrimitiveJavaType(val name: String) : Bound()

object Void : Bound()
object JavaObject : Bound()

data class JavaObject(override val extra: PropertyContainer<JavaObject> = PropertyContainer.empty()) : Bound(),
WithExtraProperties<JavaObject> {
override fun withNewExtras(newExtras: PropertyContainer<JavaObject>): JavaObject =
copy(extra = newExtras)
}

object Dynamic : Bound()
data class UnresolvedBound(val name: String) : Bound()

fun Variance<TypeParameter>.withDri(dri: DRI) = when(this) {
fun Variance<TypeParameter>.withDri(dri: DRI) = when (this) {
is Contravariance -> Contravariance(TypeParameter(dri, inner.name, inner.presentableName))
is Covariance -> Covariance(TypeParameter(dri, inner.name, inner.presentableName))
is Invariance -> Invariance(TypeParameter(dri, inner.name, inner.presentableName))
}

private fun String.shorten(maxLength: Int) = lineSequence().first().let {
if (it.length != length || it.length > maxLength) it.take(maxLength - 3) + "..." else it
}

fun Documentable.dfs(predicate: (Documentable) -> Boolean): Documentable? =
if (predicate(this)) {
this
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/kotlin/model/additionalExtras.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ fun SourceSetDependent<Set<ExtraModifiers>>.toAdditionalModifiers() = Additional

data class Annotations(
private val myContent: SourceSetDependent<List<Annotation>>
) : ExtraProperty<Documentable> {
companion object : ExtraProperty.Key<Documentable, Annotations> {
override fun mergeStrategyFor(left: Annotations, right: Annotations): MergeStrategy<Documentable> =
) : ExtraProperty<AnnotationTarget> {
companion object : ExtraProperty.Key<AnnotationTarget, Annotations> {
override fun mergeStrategyFor(left: Annotations, right: Annotations): MergeStrategy<AnnotationTarget> =
MergeStrategy.Replace(Annotations(left.myContent + right.myContent))
}

override val key: ExtraProperty.Key<Documentable, *> = Annotations
override val key: ExtraProperty.Key<AnnotationTarget, *> = Annotations

data class Annotation(
val dri: DRI,
Expand Down
18 changes: 11 additions & 7 deletions plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import org.jetbrains.dokka.model.*
import org.jetbrains.dokka.model.properties.WithExtraProperties
import org.jetbrains.dokka.pages.*
import org.jetbrains.dokka.DokkaConfiguration.DokkaSourceSet
import org.jetbrains.dokka.base.signatures.KotlinSignatureUtils.driOrNull
import org.jetbrains.dokka.base.signatures.KotlinSignatureUtils.drisOfAllNestedBounds
import org.jetbrains.dokka.model.AnnotationTarget

interface JvmSignatureUtils {

fun PageContentBuilder.DocumentableContentBuilder.annotationsBlock(d: Documentable)
fun PageContentBuilder.DocumentableContentBuilder.annotationsBlock(d: AnnotationTarget)

fun PageContentBuilder.DocumentableContentBuilder.annotationsInline(d: Documentable)
fun PageContentBuilder.DocumentableContentBuilder.annotationsInline(d: AnnotationTarget)

fun <T : Documentable> WithExtraProperties<T>.modifiers(): SourceSetDependent<Set<ExtraModifiers>>

fun Collection<ExtraModifiers>.toSignatureString(): String =
joinToString("") { it.name.toLowerCase() + " " }

fun <T : Documentable> WithExtraProperties<T>.annotations(): SourceSetDependent<List<Annotations.Annotation>> =
fun <T : AnnotationTarget> WithExtraProperties<T>.annotations(): SourceSetDependent<List<Annotations.Annotation>> =
extra[Annotations]?.directAnnotations ?: emptyMap()

private fun PageContentBuilder.DocumentableContentBuilder.annotations(
d: Documentable,
d: AnnotationTarget,
ignored: Set<Annotations.Annotation>,
styles: Set<Style>,
operation: PageContentBuilder.DocumentableContentBuilder.(Annotations.Annotation) -> Unit
Expand All @@ -40,6 +40,10 @@ interface JvmSignatureUtils {
is DEnumEntry -> d.annotations()
is DTypeAlias -> d.annotations()
is DParameter -> d.annotations()
is TypeParameter -> d.annotations()
is GenericTypeConstructor -> d.annotations()
is FunctionalTypeConstructor -> d.annotations()
is JavaObject -> d.annotations()
else -> null
}?.let {
it.entries.forEach {
Expand Down Expand Up @@ -104,7 +108,7 @@ interface JvmSignatureUtils {
}

fun PageContentBuilder.DocumentableContentBuilder.annotationsBlockWithIgnored(
d: Documentable,
d: AnnotationTarget,
ignored: Set<Annotations.Annotation>,
renderAtStrategy: AtStrategy,
listBrackets: Pair<Char, Char>,
Expand All @@ -118,7 +122,7 @@ interface JvmSignatureUtils {
}

fun PageContentBuilder.DocumentableContentBuilder.annotationsInlineWithIgnored(
d: Documentable,
d: AnnotationTarget,
ignored: Set<Annotations.Annotation>,
renderAtStrategy: AtStrategy,
listBrackets: Pair<Char, Char>,
Expand Down
25 changes: 18 additions & 7 deletions plugins/base/src/main/kotlin/signatures/KotlinSignatureProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class KotlinSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLog
link(c.name!!, c.dri)
if (c is WithGenerics) {
list(c.generics, prefix = "<", suffix = ">") {
annotationsInline(it)
+buildSignature(it)
}
}
Expand Down Expand Up @@ -219,6 +220,7 @@ class KotlinSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLog
text(p.modifiers()[it]?.toSignatureString() ?: "")
p.setter?.let { text("var ") } ?: text("val ")
list(p.generics, prefix = "<", suffix = "> ") {
annotationsInline(it)
+buildSignature(it)
}
p.receiver?.also {
Expand Down Expand Up @@ -252,6 +254,7 @@ class KotlinSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLog
text("fun ")
val usedGenerics = if (f.isConstructor) f.generics.filter { f uses it } else f.generics
list(usedGenerics, prefix = "<", suffix = "> ") {
annotationsInline(it)
+buildSignature(it)
}
f.receiver?.also {
Expand Down Expand Up @@ -328,19 +331,23 @@ class KotlinSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLog

private fun PageContentBuilder.DocumentableContentBuilder.signatureForProjection(
p: Projection, showFullyQualifiedName: Boolean = false
): Unit =
when (p) {
is TypeParameter -> link(p.name, p.dri)

is FunctionalTypeConstructor ->
): Unit {
return when (p) {
is TypeParameter -> {
annotationsInline(p)
link(p.name, p.dri)
}
is FunctionalTypeConstructor -> {
annotationsInline(p)
+funType(mainDRI.single(), mainSourcesetData, p)

}
is GenericTypeConstructor ->
group(styles = emptySet()) {
val linkText = if (showFullyQualifiedName && p.dri.packageName != null) {
"${p.dri.packageName}.${p.dri.classNames.orEmpty()}"
} else p.dri.classNames.orEmpty()
if (p.presentableName != null) text(p.presentableName + ": ")
annotationsInline(p)
link(linkText, p.dri)
list(p.projections, prefix = "<", suffix = ">") {
signatureForProjection(it, showFullyQualifiedName)
Expand All @@ -360,12 +367,16 @@ class KotlinSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLog
}

is TypeAliased -> signatureForProjection(p.typeAlias)
is JavaObject -> link("Any", DriOfAny)
is JavaObject -> {
annotationsInline(p)
link("Any", DriOfAny)
}
is Void -> link("Unit", DriOfUnit)
is PrimitiveJavaType -> signatureForProjection(p.translateToKotlin(), showFullyQualifiedName)
is Dynamic -> text("dynamic")
is UnresolvedBound -> text(p.name)
}
}

private fun funType(dri: DRI, sourceSets: Set<DokkaSourceSet>, type: FunctionalTypeConstructor) =
contentBuilder.contentFor(dri, sourceSets, ContentKind.Main) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.links.DriOfAny
import org.jetbrains.dokka.links.DriOfUnit
import org.jetbrains.dokka.model.*
import org.jetbrains.dokka.model.AnnotationTarget
import org.jetbrains.dokka.model.properties.WithExtraProperties

object KotlinSignatureUtils : JvmSignatureUtils {
Expand All @@ -18,10 +19,10 @@ object KotlinSignatureUtils : JvmSignatureUtils {
)


override fun PageContentBuilder.DocumentableContentBuilder.annotationsBlock(d: Documentable) =
override fun PageContentBuilder.DocumentableContentBuilder.annotationsBlock(d: AnnotationTarget) =
annotationsBlockWithIgnored(d, ignoredAnnotations, strategy, listBrackets, classExtension)

override fun PageContentBuilder.DocumentableContentBuilder.annotationsInline(d: Documentable) =
override fun PageContentBuilder.DocumentableContentBuilder.annotationsInline(d: AnnotationTarget) =
annotationsInlineWithIgnored(d, ignoredAnnotations, strategy, listBrackets, classExtension)

override fun <T : Documentable> WithExtraProperties<T>.modifiers() =
Expand Down
Loading

0 comments on commit 70000c8

Please sign in to comment.