aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/Kotlin/KotlinLanguageService.kt
blob: e54772b38ae024006d6149db93bf1e18d884a701 (plain)
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package org.jetbrains.dokka

import org.jetbrains.dokka.LanguageService.RenderMode

/**
 * Implements [LanguageService] and provides rendering of symbols in Kotlin language
 */
class KotlinLanguageService : LanguageService {
    private val fullOnlyModifiers = setOf("public", "protected", "private", "inline", "noinline", "crossinline", "reified")

    override fun render(node: DocumentationNode, renderMode: RenderMode): ContentNode {
        return content {
            when (node.kind) {
                NodeKind.Package -> if (renderMode == RenderMode.FULL) renderPackage(node)
                in NodeKind.classLike -> renderClass(node, renderMode)

                NodeKind.EnumItem,
                NodeKind.ExternalClass -> if (renderMode == RenderMode.FULL) identifier(node.name)

                NodeKind.TypeParameter -> renderTypeParameter(node, renderMode)
                NodeKind.Type,
                NodeKind.UpperBound -> renderType(node, renderMode)

                NodeKind.Modifier -> renderModifier(node)
                NodeKind.Constructor,
                NodeKind.Function,
                NodeKind.CompanionObjectFunction -> renderFunction(node, renderMode)
                NodeKind.Property,
                NodeKind.CompanionObjectProperty -> renderProperty(node, renderMode)
                else -> identifier(node.name)
            }
        }
    }

    override fun renderName(node: DocumentationNode): String {
        return when (node.kind) {
            NodeKind.Constructor -> node.owner!!.name
            else -> node.name
        }
    }

    override fun summarizeSignatures(nodes: List<DocumentationNode>): ContentNode? {
        if (nodes.size < 2) return null
        val receiverKind = nodes.getReceiverKind() ?: return null
        val functionWithTypeParameter = nodes.firstOrNull { it.details(NodeKind.TypeParameter).any() } ?: return null
        return content {
            val typeParameter = functionWithTypeParameter.details(NodeKind.TypeParameter).first()
            if (functionWithTypeParameter.kind == NodeKind.Function) {
                renderFunction(functionWithTypeParameter, RenderMode.SUMMARY, SummarizingMapper(receiverKind, typeParameter.name))
            }
            else {
                renderProperty(functionWithTypeParameter, RenderMode.SUMMARY, SummarizingMapper(receiverKind, typeParameter.name))
            }
        }
    }

    private fun List<DocumentationNode>.getReceiverKind(): ReceiverKind? {
        val qNames = map { it.getReceiverQName() }.filterNotNull()
        if (qNames.size != size)
            return null

        return ReceiverKind.values().firstOrNull { kind -> qNames.all { it in kind.classes } }
    }

    private fun DocumentationNode.getReceiverQName(): String? {
        if (kind != NodeKind.Function && kind != NodeKind.Property) return null
        val receiver = details(NodeKind.Receiver).singleOrNull() ?: return null
        return receiver.detail(NodeKind.Type).qualifiedNameFromType()
    }

    companion object {
        private val arrayClasses = setOf(
                "kotlin.Array",
                "kotlin.BooleanArray",
                "kotlin.ByteArray",
                "kotlin.CharArray",
                "kotlin.ShortArray",
                "kotlin.IntArray",
                "kotlin.LongArray",
                "kotlin.FloatArray",
                "kotlin.DoubleArray"
        )

        private val arrayOrListClasses = setOf("kotlin.List") + arrayClasses

        private val iterableClasses = setOf(
                "kotlin.Collection",
                "kotlin.Sequence",
                "kotlin.Iterable",
                "kotlin.Map",
                "kotlin.String",
                "kotlin.CharSequence") + arrayOrListClasses
    }

    private enum class ReceiverKind(val receiverName: String, val classes: Collection<String>) {
        ARRAY("any_array", arrayClasses),
        ARRAY_OR_LIST("any_array_or_list", arrayOrListClasses),
        ITERABLE("any_iterable", iterableClasses),
    }

    interface SignatureMapper {
        fun renderReceiver(receiver: DocumentationNode, to: ContentBlock)
    }

    private class SummarizingMapper(val kind: ReceiverKind, val typeParameterName: String): SignatureMapper {
        override fun renderReceiver(receiver: DocumentationNode, to: ContentBlock) {
            to.append(ContentIdentifier(kind.receiverName, IdentifierKind.SummarizedTypeName))
            to.text("<$typeParameterName>")
        }
    }

    private fun ContentBlock.renderPackage(node: DocumentationNode) {
        keyword("package")
        text(" ")
        identifier(node.name)
    }

    private fun <T> ContentBlock.renderList(nodes: List<T>, separator: String = ", ",
                                        noWrap: Boolean = false, renderItem: (T) -> Unit) {
        if (nodes.none())
            return
        renderItem(nodes.first())
        nodes.drop(1).forEach {
            if (noWrap) {
                symbol(separator.removeSuffix(" "))
                nbsp()
            } else {
                symbol(separator)
            }
            renderItem(it)
        }
    }

    private fun ContentBlock.renderLinked(node: DocumentationNode, body: ContentBlock.(DocumentationNode)->Unit) {
        val to = node.links.firstOrNull()
        if (to == null)
            body(node)
        else
            link(to) {
                body(node)
            }
    }

    private fun ContentBlock.renderFunctionalTypeParameterName(node: DocumentationNode, renderMode: RenderMode) {
        node.references(RefKind.HiddenAnnotation).map { it.to }
                .find { it.name == "ParameterName" }?.let {
            val parameterNameValue = it.detail(NodeKind.Parameter).detail(NodeKind.Value)
            identifier(parameterNameValue.name.removeSurrounding("\""), IdentifierKind.ParameterName)
            symbol(":")
            nbsp()
        }
    }

    private fun ContentBlock.renderFunctionalType(node: DocumentationNode, renderMode: RenderMode) {
        var typeArguments = node.details(NodeKind.Type)

        if (node.name.startsWith("Suspend")) {
            keyword("suspend ")
        }

        // lambda
        val isExtension = node.annotations.any { it.name == "ExtensionFunctionType" }
        if (isExtension) {
            renderType(typeArguments.first(), renderMode)
            symbol(".")
            typeArguments = typeArguments.drop(1)
        }
        symbol("(")
        renderList(typeArguments.take(typeArguments.size - 1), noWrap = true) {
            renderFunctionalTypeParameterName(it, renderMode)
            renderType(it, renderMode)
        }
        symbol(")")
        nbsp()
        symbol("->")
        nbsp()
        renderType(typeArguments.last(), renderMode)

    }

    private fun DocumentationNode.isFunctionalType(): Boolean {
        val typeArguments = details(NodeKind.Type)
        val functionalTypeName = "Function${typeArguments.count() - 1}"
        val suspendFunctionalTypeName = "Suspend$functionalTypeName"
        return name == functionalTypeName || name == suspendFunctionalTypeName
    }

    private fun ContentBlock.renderType(node: DocumentationNode, renderMode: RenderMode) {
        if (node.name == "dynamic") {
            keyword("dynamic")
            return
        }
        if (node.isFunctionalType()) {
            renderFunctionalType(node, renderMode)
            return
        }
        if (renderMode == RenderMode.FULL) {
            renderAnnotationsForNode(node)
        }
        renderModifiersForNode(node, renderMode, true)
        renderLinked(node) { identifier(it.name, IdentifierKind.TypeName) }
        val typeArguments = node.details(NodeKind.Type)
        if (typeArguments.isNotEmpty()) {
            symbol("<")
            renderList(typeArguments, noWrap = true) {
                renderType(it, renderMode)
            }
            symbol(">")
        }
        val nullabilityModifier = node.details(NodeKind.NullabilityModifier).singleOrNull()
        if (nullabilityModifier != null) {
            symbol(nullabilityModifier.name)
        }
    }

    private fun ContentBlock.renderModifier(node: DocumentationNode, nowrap: Boolean = false) {
        when (node.name) {
            "final", "public", "var" -> {}
            else -> {
                keyword(node.name)
                if (nowrap) {
                    nbsp()
                }
                else {
                    text(" ")
                }
            }
        }
    }

    private fun ContentBlock.renderTypeParameter(node: DocumentationNode, renderMode: RenderMode) {
        renderModifiersForNode(node, renderMode, true)

        identifier(node.name)

        val constraints = node.details(NodeKind.UpperBound)
        if (constraints.size == 1) {
            nbsp()
            symbol(":")
            nbsp()
            renderList(constraints, noWrap=true) {
                renderType(it, renderMode)
            }
        }
    }
    private fun ContentBlock.renderParameter(node: DocumentationNode, renderMode: RenderMode) {
        if (renderMode == RenderMode.FULL) {
            renderAnnotationsForNode(node)
        }
        renderModifiersForNode(node, renderMode)
        identifier(node.name, IdentifierKind.ParameterName, node.detailOrNull(NodeKind.Signature)?.name)
        symbol(":")
        nbsp()
        val parameterType = node.detail(NodeKind.Type)
        renderType(parameterType, renderMode)
        val valueNode = node.details(NodeKind.Value).firstOrNull()
        if (valueNode != null) {
            nbsp()
            symbol("=")
            nbsp()
            text(valueNode.name)
        }
    }

    private fun ContentBlock.renderTypeParametersForNode(node: DocumentationNode, renderMode: RenderMode) {
        val typeParameters = node.details(NodeKind.TypeParameter)
        if (typeParameters.any()) {
            symbol("<")
            renderList(typeParameters) {
                renderTypeParameter(it, renderMode)
            }
            symbol(">")
        }
    }

    private fun ContentBlock.renderExtraTypeParameterConstraints(node: DocumentationNode, renderMode: RenderMode) {
        val parametersWithMultipleConstraints = node.details(NodeKind.TypeParameter).filter { it.details(NodeKind.UpperBound).size > 1 }
        val parametersWithConstraints = parametersWithMultipleConstraints
                .flatMap { parameter -> parameter.details(NodeKind.UpperBound).map { constraint -> parameter to constraint } }
        if (parametersWithMultipleConstraints.isNotEmpty()) {
            keyword(" where ")
            renderList(parametersWithConstraints) {
                identifier(it.first.name)
                nbsp()
                symbol(":")
                nbsp()
                renderType(it.second, renderMode)
            }
        }
    }

    private fun ContentBlock.renderSupertypesForNode(node: DocumentationNode, renderMode: RenderMode) {
        val supertypes = node.details(NodeKind.Supertype)
        if (supertypes.any()) {
            nbsp()
            symbol(":")
            nbsp()
            renderList(supertypes) {
                indentedSoftLineBreak()
                renderType(it, renderMode)
            }
        }
    }

    private fun ContentBlock.renderModifiersForNode(node: DocumentationNode,
                                                    renderMode: RenderMode,
                                                    nowrap: Boolean = false) {
        val modifiers = node.details(NodeKind.Modifier)
        for (it in modifiers) {
            if (node.kind == org.jetbrains.dokka.NodeKind.Interface && it.name == "abstract")
                continue
            if (renderMode == RenderMode.SUMMARY && it.name in fullOnlyModifiers) {
                continue
            }
            renderModifier(it, nowrap)
        }
    }

    private fun ContentBlock.renderAnnotationsForNode(node: DocumentationNode) {
        node.annotations.forEach {
            renderAnnotation(it)
        }
    }

    private fun ContentBlock.renderAnnotation(node: DocumentationNode) {
        identifier("@" + node.name, IdentifierKind.AnnotationName)
        val parameters = node.details(NodeKind.Parameter)
        if (!parameters.isEmpty()) {
            symbol("(")
            renderList(parameters) {
                text(it.detail(NodeKind.Value).name)
            }
            symbol(")")
        }
        text(" ")
    }

    private fun ContentBlock.renderClass(node: DocumentationNode, renderMode: RenderMode) {
        if (renderMode == RenderMode.FULL) {
            renderAnnotationsForNode(node)
        }
        renderModifiersForNode(node, renderMode)
        when (node.kind) {
            NodeKind.Class,
            NodeKind.AnnotationClass,
            NodeKind.Exception,
            NodeKind.Enum -> keyword("class ")
            NodeKind.Interface -> keyword("interface ")
            NodeKind.EnumItem -> keyword("enum val ")
            NodeKind.Object -> keyword("object ")
            NodeKind.TypeAlias -> keyword("typealias ")
            else -> throw IllegalArgumentException("Node $node is not a class-like object")
        }

        identifierOrDeprecated(node)
        renderTypeParametersForNode(node, renderMode)
        renderSupertypesForNode(node, renderMode)
        renderExtraTypeParameterConstraints(node, renderMode)

        if (node.kind == NodeKind.TypeAlias) {
            nbsp()
            symbol("=")
            nbsp()
            renderType(node.detail(NodeKind.TypeAliasUnderlyingType), renderMode)
        }
    }

    private fun ContentBlock.renderFunction(node: DocumentationNode,
                                            renderMode: RenderMode,
                                            signatureMapper: SignatureMapper? = null) {
        if (renderMode == RenderMode.FULL) {
            renderAnnotationsForNode(node)
        }
        renderModifiersForNode(node, renderMode)
        when (node.kind) {
            NodeKind.Constructor -> identifier(node.owner!!.name)
            NodeKind.Function,
            NodeKind.CompanionObjectFunction -> keyword("fun ")
            else -> throw IllegalArgumentException("Node $node is not a function-like object")
        }
        renderTypeParametersForNode(node, renderMode)
        if (node.details(NodeKind.TypeParameter).any()) {
            text(" ")
        }

        renderReceiver(node, renderMode, signatureMapper)

        if (node.kind != org.jetbrains.dokka.NodeKind.Constructor)
            identifierOrDeprecated(node)

        symbol("(")
        val parameters = node.details(NodeKind.Parameter)
        renderList(parameters) {
            indentedSoftLineBreak()
            renderParameter(it, renderMode)
        }
        if (needReturnType(node)) {
            if (parameters.isNotEmpty()) {
                softLineBreak()
            }
            symbol(")")
            symbol(": ")
            renderType(node.detail(NodeKind.Type), renderMode)
        }
        else {
            symbol(")")
        }
        renderExtraTypeParameterConstraints(node, renderMode)
    }

    private fun ContentBlock.renderReceiver(node: DocumentationNode, renderMode: RenderMode, signatureMapper: SignatureMapper?) {
        val receiver = node.details(NodeKind.Receiver).singleOrNull()
        if (receiver != null) {
            if (signatureMapper != null) {
                signatureMapper.renderReceiver(receiver, this)
            } else {
                renderType(receiver.detail(NodeKind.Type), renderMode)
            }
            symbol(".")
        }
    }

    private fun needReturnType(node: DocumentationNode) = when(node.kind) {
        NodeKind.Constructor -> false
        else -> !node.isUnitReturnType()
    }

    fun DocumentationNode.isUnitReturnType(): Boolean =
            detail(NodeKind.Type).hiddenLinks.firstOrNull()?.qualifiedName() == "kotlin.Unit"

    private fun ContentBlock.renderProperty(node: DocumentationNode,
                                            renderMode: RenderMode,
                                            signatureMapper: SignatureMapper? = null) {
        if (renderMode == RenderMode.FULL) {
            renderAnnotationsForNode(node)
        }
        renderModifiersForNode(node, renderMode)
        when (node.kind) {
            NodeKind.Property,
            NodeKind.CompanionObjectProperty -> keyword("${node.getPropertyKeyword()} ")
            else -> throw IllegalArgumentException("Node $node is not a property")
        }
        renderTypeParametersForNode(node, renderMode)
        if (node.details(NodeKind.TypeParameter).any()) {
            text(" ")
        }

        renderReceiver(node, renderMode, signatureMapper)

        identifierOrDeprecated(node)
        symbol(": ")
        renderType(node.detail(NodeKind.Type), renderMode)
        renderExtraTypeParameterConstraints(node, renderMode)
    }

    fun DocumentationNode.getPropertyKeyword() =
            if (details(NodeKind.Modifier).any { it.name == "var" }) "var" else "val"

    fun ContentBlock.identifierOrDeprecated(node: DocumentationNode) {
        if (node.deprecation != null) {
            val strike = ContentStrikethrough()
            strike.identifier(node.name)
            append(strike)
        } else {
            identifier(node.name)
        }
    }
}

fun DocumentationNode.qualifiedNameFromType() = (links.firstOrNull() ?: hiddenLinks.firstOrNull())?.qualifiedName() ?: name