1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
package org.jetbrains.dokka.base.signatures
import org.jetbrains.dokka.base.translators.documentables.PageContentBuilder
import org.jetbrains.dokka.links.DRI
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.drisOfAllNestedBounds
import org.jetbrains.dokka.model.AnnotationTarget
interface JvmSignatureUtils {
fun PageContentBuilder.DocumentableContentBuilder.annotationsBlock(d: AnnotationTarget)
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 : AnnotationTarget> WithExtraProperties<T>.annotations(): SourceSetDependent<List<Annotations.Annotation>> =
extra[Annotations]?.directAnnotations ?: emptyMap()
private fun PageContentBuilder.DocumentableContentBuilder.annotations(
d: AnnotationTarget,
ignored: Set<Annotations.Annotation>,
styles: Set<Style>,
operation: PageContentBuilder.DocumentableContentBuilder.(Annotations.Annotation) -> Unit
): Unit = when (d) {
is DFunction -> d.annotations()
is DProperty -> d.annotations()
is DClass -> d.annotations()
is DInterface -> d.annotations()
is DObject -> d.annotations()
is DEnum -> d.annotations()
is DAnnotation -> d.annotations()
is DTypeParameter -> d.annotations()
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 {
it.value.filter { it !in ignored && it.mustBeDocumented }.takeIf { it.isNotEmpty() }?.let { annotations ->
group(sourceSets = setOf(it.key), styles = styles, kind = ContentKind.Annotations) {
annotations.forEach {
operation(it)
}
}
}
}
} ?: Unit
fun PageContentBuilder.DocumentableContentBuilder.toSignatureString(
a: Annotations.Annotation,
renderAtStrategy: AtStrategy,
listBrackets: Pair<Char, Char>,
classExtension: String
) {
when (renderAtStrategy) {
is All, is OnlyOnce -> text("@")
is Never -> Unit
}
link(a.dri.classNames!!, a.dri)
val isNoWrappedBrackets = a.params.entries.isEmpty() && renderAtStrategy is OnlyOnce
listParams(
a.params.entries,
if (isNoWrappedBrackets) null else Pair('(', ')')
) {
text(it.key + " = ")
when (renderAtStrategy) {
is All -> All
is Never, is OnlyOnce -> Never
}.let { strategy ->
valueToSignature(it.value, strategy, listBrackets, classExtension)
}
}
}
private fun PageContentBuilder.DocumentableContentBuilder.valueToSignature(
a: AnnotationParameterValue,
renderAtStrategy: AtStrategy,
listBrackets: Pair<Char, Char>,
classExtension: String
): Unit = when (a) {
is AnnotationValue -> toSignatureString(a.annotation, renderAtStrategy, listBrackets, classExtension)
is ArrayValue -> {
listParams(a.value, listBrackets) { valueToSignature(it, renderAtStrategy, listBrackets, classExtension) }
}
is EnumValue -> link(a.enumName, a.enumDri)
is ClassValue -> link(a.className + classExtension, a.classDRI)
is StringValue -> group(styles = setOf(TextStyle.Breakable)) { text( "\"${a.text()}\"") }
is LiteralValue -> group(styles = setOf(TextStyle.Breakable)) { text(a.text()) }
}
private fun<T> PageContentBuilder.DocumentableContentBuilder.listParams(
params: Collection<T>,
listBrackets: Pair<Char, Char>?,
outFn: PageContentBuilder.DocumentableContentBuilder.(T) -> Unit
) {
listBrackets?.let{ text(it.first.toString()) }
params.forEachIndexed { i, it ->
group(styles = setOf(TextStyle.BreakableAfter)) {
this.outFn(it)
if (i != params.size - 1) text(", ")
}
}
listBrackets?.let{ text(it.second.toString()) }
}
fun PageContentBuilder.DocumentableContentBuilder.annotationsBlockWithIgnored(
d: AnnotationTarget,
ignored: Set<Annotations.Annotation>,
renderAtStrategy: AtStrategy,
listBrackets: Pair<Char, Char>,
classExtension: String
) {
annotations(d, ignored, setOf(TextStyle.Block)) {
group {
toSignatureString(it, renderAtStrategy, listBrackets, classExtension)
}
}
}
fun PageContentBuilder.DocumentableContentBuilder.annotationsInlineWithIgnored(
d: AnnotationTarget,
ignored: Set<Annotations.Annotation>,
renderAtStrategy: AtStrategy,
listBrackets: Pair<Char, Char>,
classExtension: String
) {
annotations(d, ignored, setOf(TextStyle.Span)) {
toSignatureString(it, renderAtStrategy, listBrackets, classExtension)
text(Typography.nbsp.toString())
}
}
fun <T : Documentable> WithExtraProperties<T>.stylesIfDeprecated(sourceSetData: DokkaSourceSet): Set<TextStyle> =
if (extra[Annotations]?.directAnnotations?.get(sourceSetData)?.any {
it.dri == DRI("kotlin", "Deprecated")
|| it.dri == DRI("java.lang", "Deprecated")
} == true) setOf(TextStyle.Strikethrough) else emptySet()
infix fun DFunction.uses(t: DTypeParameter): Boolean {
val allDris: List<DRI> = (listOfNotNull(receiver?.dri, *receiver?.type?.drisOfAllNestedBounds?.toTypedArray() ?: emptyArray()) +
parameters.flatMap { listOf(it.dri) + it.type.drisOfAllNestedBounds })
return t.dri in allDris
}
}
sealed class AtStrategy
object All : AtStrategy()
object OnlyOnce : AtStrategy()
object Never : AtStrategy()
|