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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
package org.jetbrains.dokka.base.signatures
import org.jetbrains.dokka.DokkaConfiguration.DokkaSourceSet
import org.jetbrains.dokka.Platform
import org.jetbrains.dokka.base.DokkaBase
import org.jetbrains.dokka.base.signatures.KotlinSignatureUtils.dri
import org.jetbrains.dokka.base.signatures.KotlinSignatureUtils.driOrNull
import org.jetbrains.dokka.base.transformers.pages.comments.CommentsToContentConverter
import org.jetbrains.dokka.base.translators.documentables.PageContentBuilder
import org.jetbrains.dokka.links.*
import org.jetbrains.dokka.model.*
import org.jetbrains.dokka.model.Nullable
import org.jetbrains.dokka.model.TypeConstructor
import org.jetbrains.dokka.model.properties.WithExtraProperties
import org.jetbrains.dokka.pages.ContentKind
import org.jetbrains.dokka.pages.ContentNode
import org.jetbrains.dokka.pages.TextStyle
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.plugability.plugin
import org.jetbrains.dokka.plugability.querySingle
import org.jetbrains.dokka.utilities.DokkaLogger
import kotlin.text.Typography.nbsp
class KotlinSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLogger)
: SignatureProvider, JvmSignatureUtils by KotlinSignatureUtils {
constructor(context: DokkaContext) : this(
context.plugin<DokkaBase>().querySingle { commentsToContentConverter },
context.logger
)
private val contentBuilder = PageContentBuilder(ctcc, this, logger)
private val ignoredVisibilities = setOf(JavaVisibility.Public, KotlinVisibility.Public)
private val ignoredModifiers = setOf(JavaModifier.Final, KotlinModifier.Final)
private val ignoredExtraModifiers = setOf(
ExtraModifiers.KotlinOnlyModifiers.TailRec,
ExtraModifiers.KotlinOnlyModifiers.External
)
private val platformSpecificModifiers: Map<ExtraModifiers, Set<Platform>> = mapOf(
ExtraModifiers.KotlinOnlyModifiers.External to setOf(Platform.js)
)
override fun signature(documentable: Documentable): List<ContentNode> = when (documentable) {
is DFunction -> functionSignature(documentable)
is DProperty -> propertySignature(documentable)
is DClasslike -> classlikeSignature(documentable)
is DTypeParameter -> signature(documentable)
is DEnumEntry -> signature(documentable)
is DTypeAlias -> signature(documentable)
else -> throw NotImplementedError(
"Cannot generate signature for ${documentable::class.qualifiedName} ${documentable.name}"
)
}
private fun <T> PageContentBuilder.DocumentableContentBuilder.processExtraModifiers(t: T)
where T : Documentable, T : WithExtraProperties<T> {
sourceSetDependentText(
t.modifiers()
.mapValues { entry ->
entry.value.filter {
it !in ignoredExtraModifiers || entry.key.analysisPlatform in (platformSpecificModifiers[it]
?: emptySet())
}
}
) {
it.toSignatureString()
}
}
private fun signature(e: DEnumEntry): List<ContentNode> =
e.sourceSets.map {
contentBuilder.contentFor(
e,
ContentKind.Symbol,
setOf(TextStyle.Monospace) + e.stylesIfDeprecated(it),
sourceSets = setOf(it)
) {
group(styles = setOf(TextStyle.Block)) {
annotationsBlock(e)
link(e.name, e.dri, styles = emptySet())
e.extra[ConstructorValues]?.let { constructorValues ->
constructorValues.values[it]
text(constructorValues.values[it]?.joinToString(prefix = "(", postfix = ")") ?: "")
}
}
}
}
private fun actualTypealiasedSignature(c: DClasslike, sourceSet: DokkaSourceSet, aliasedType: Bound) =
contentBuilder.contentFor(
c,
ContentKind.Symbol,
setOf(TextStyle.Monospace) + ((c as? WithExtraProperties<out Documentable>)?.stylesIfDeprecated(sourceSet)
?: emptySet()),
sourceSets = setOf(sourceSet)
) {
text("typealias ")
link(c.name.orEmpty(), c.dri)
text(" = ")
signatureForProjection(aliasedType)
}
@Suppress("UNCHECKED_CAST")
private fun <T : DClasslike> classlikeSignature(c: T): List<ContentNode> =
c.sourceSets.map { sourceSetData ->
(c as? WithExtraProperties<out DClasslike>)?.extra?.get(ActualTypealias)?.underlyingType?.get(sourceSetData)
?.let {
actualTypealiasedSignature(c, sourceSetData, it)
} ?: regularSignature(c, sourceSetData)
}
private fun regularSignature(c: DClasslike, sourceSet: DokkaSourceSet) =
contentBuilder.contentFor(
c,
ContentKind.Symbol,
setOf(TextStyle.Monospace) + ((c as? WithExtraProperties<out Documentable>)?.stylesIfDeprecated(sourceSet)
?: emptySet()),
sourceSets = setOf(sourceSet)
) {
annotationsBlock(c)
text(c.visibility[sourceSet]?.takeIf { it !in ignoredVisibilities }?.name?.let { "$it " } ?: "")
if (c is DClass) {
text(
if (c.modifier[sourceSet] !in ignoredModifiers)
when {
c.extra[AdditionalModifiers]?.content?.get(sourceSet)?.contains(ExtraModifiers.KotlinOnlyModifiers.Data) == true -> ""
c.modifier[sourceSet] is JavaModifier.Empty -> "${KotlinModifier.Open.name} "
else -> c.modifier[sourceSet]?.name?.let { "$it " } ?: ""
}
else
""
)
}
when (c) {
is DClass -> {
processExtraModifiers(c)
text("class ")
}
is DInterface -> {
processExtraModifiers(c)
text("interface ")
}
is DEnum -> {
processExtraModifiers(c)
text("enum ")
}
is DObject -> {
processExtraModifiers(c)
text("object ")
}
is DAnnotation -> {
processExtraModifiers(c)
text("annotation class ")
}
}
link(c.name!!, c.dri)
if (c is WithGenerics) {
list(c.generics, prefix = "<", suffix = ">") {
annotationsInline(it)
+buildSignature(it)
}
}
if (c is WithConstructors) {
val pConstructor = c.constructors.singleOrNull { it.extra[PrimaryConstructorExtra] != null }
if (pConstructor?.sourceSets?.contains(sourceSet) == true) {
if (pConstructor.annotations().values.any { it.isNotEmpty() }) {
text(nbsp.toString())
annotationsInline(pConstructor)
text("constructor")
}
list(
pConstructor.parameters,
"(",
")",
", ",
pConstructor.sourceSets.toSet()
) {
annotationsInline(it)
text(it.name ?: "", styles = mainStyles.plus(TextStyle.Bold))
text(": ")
signatureForProjection(it.type)
}
}
}
if (c is WithSupertypes) {
c.supertypes.filter { it.key == sourceSet }.map { (s, typeConstructors) ->
list(typeConstructors, prefix = " : ", sourceSets = setOf(s)) {
link(it.typeConstructor.dri.sureClassNames, it.typeConstructor.dri, sourceSets = setOf(s))
list(it.typeConstructor.projections, prefix = "<", suffix = "> ") {
signatureForProjection(it)
}
}
}
}
}
private fun propertySignature(p: DProperty) =
p.sourceSets.map {
contentBuilder.contentFor(
p,
ContentKind.Symbol,
setOf(TextStyle.Monospace) + p.stylesIfDeprecated(it),
sourceSets = setOf(it)
) {
annotationsBlock(p)
text(p.visibility[it].takeIf { it !in ignoredVisibilities }?.name?.let { "$it " } ?: "")
text(
p.modifier[it].takeIf { it !in ignoredModifiers }?.let {
if (it is JavaModifier.Empty) KotlinModifier.Open else it
}?.name?.let { "$it " } ?: ""
)
text(p.modifiers()[it]?.toSignatureString() ?: "")
p.setter?.let { text("var ") } ?: text("val ")
list(p.generics, prefix = "<", suffix = "> ") {
annotationsInline(it)
+buildSignature(it)
}
p.receiver?.also {
signatureForProjection(it.type)
text(".")
}
link(p.name, p.dri)
text(": ")
signatureForProjection(p.type)
p.extra[DefaultValue]?.run {
text(" = $value")
}
}
}
private fun functionSignature(f: DFunction) =
f.sourceSets.map {
contentBuilder.contentFor(
f,
ContentKind.Symbol,
setOf(TextStyle.Monospace) + f.stylesIfDeprecated(it),
sourceSets = setOf(it)
) {
annotationsBlock(f)
text(f.visibility[it]?.takeIf { it !in ignoredVisibilities }?.name?.let { "$it " } ?: "")
text(f.modifier[it]?.takeIf { it !in ignoredModifiers }?.let {
if (it is JavaModifier.Empty) KotlinModifier.Open else it
}?.name?.let { "$it " } ?: ""
)
text(f.modifiers()[it]?.toSignatureString() ?: "")
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 {
signatureForProjection(it.type)
text(".")
}
link(f.name, f.dri)
text("(")
list(f.parameters) {
annotationsInline(it)
processExtraModifiers(it)
text(it.name!!)
text(": ")
signatureForProjection(it.type)
it.extra[DefaultValue]?.run {
text(" = $value")
}
}
text(")")
if (f.documentReturnType()) {
text(": ")
signatureForProjection(f.type)
}
}
}
private fun DFunction.documentReturnType() = when {
this.isConstructor -> false
this.type is TypeConstructor && (this.type as TypeConstructor).dri == DriOfUnit -> false
this.type is Void -> false
else -> true
}
private fun signature(t: DTypeAlias) =
t.sourceSets.map {
contentBuilder.contentFor(t, sourceSets = setOf(it)) {
t.underlyingType.entries.groupBy({ it.value }, { it.key }).map { (type, platforms) ->
+contentBuilder.contentFor(
t,
ContentKind.Symbol,
setOf(TextStyle.Monospace) + t.stylesIfDeprecated(it),
sourceSets = platforms.toSet()
) {
annotationsBlock(t)
text(t.visibility[it]?.takeIf { it !in ignoredVisibilities }?.name?.let { "$it " } ?: "")
processExtraModifiers(t)
text("typealias ")
signatureForProjection(t.type)
text(" = ")
signatureForTypealiasTarget(t, type)
}
}
}
}
private fun signature(t: DTypeParameter) =
t.sourceSets.map {
contentBuilder.contentFor(t, styles = t.stylesIfDeprecated(it), sourceSets = setOf(it)) {
signatureForProjection(t.variantTypeParameter.withDri(t.dri.withTargetToDeclaration()))
list(t.nontrivialBounds, prefix = " : ") { bound ->
signatureForProjection(bound)
}
}
}
private fun PageContentBuilder.DocumentableContentBuilder.signatureForTypealiasTarget(
typeAlias: DTypeAlias, bound: Bound
) {
signatureForProjection(
p = bound,
showFullyQualifiedName = bound.driOrNull?.classNames == typeAlias.dri.classNames
)
}
private fun PageContentBuilder.DocumentableContentBuilder.signatureForProjection(
p: Projection, showFullyQualifiedName: Boolean = false
): 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)
}
}
is Variance<*> -> group(styles = emptySet()) {
text("$p ".takeIf { it.isNotBlank() } ?: "")
signatureForProjection(p.inner, showFullyQualifiedName)
}
is Star -> text("*")
is Nullable -> group(styles = emptySet()) {
signatureForProjection(p.inner, showFullyQualifiedName)
text("?")
}
is TypeAliased -> signatureForProjection(p.typeAlias)
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) {
if (type.presentableName != null) text(type.presentableName + ": ")
if (type.isSuspendable) text("suspend ")
if (type.isExtensionFunction) {
signatureForProjection(type.projections.first())
text(".")
}
val args = if (type.isExtensionFunction)
type.projections.drop(1)
else
type.projections
text("(")
args.subList(0, args.size - 1).forEachIndexed { i, arg ->
signatureForProjection(arg)
if (i < args.size - 2) text(", ")
}
text(") -> ")
signatureForProjection(args.last())
}
}
private fun PrimitiveJavaType.translateToKotlin() = GenericTypeConstructor(
dri = dri,
projections = emptyList(),
presentableName = null
)
private val DTypeParameter.nontrivialBounds: List<Bound>
get() = bounds.filterNot { it is Nullable && it.inner.driOrNull == DriOfAny }
|