aboutsummaryrefslogtreecommitdiff
path: root/src/Generation/KotlinSignatureGenerator.kt
blob: 481e51338f135b63e4649d564ea4c0f6fd834c0b (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
package org.jetbrains.dokka

import org.jetbrains.dokka.DocumentationNode.*

class KotlinSignatureGenerator : SignatureGenerator {
    override fun render(node: DocumentationNode): String {
        return when (node.kind) {
            Kind.Package -> renderPackage(node)
            Kind.Class,
            Kind.Interface,
            Kind.Object -> renderClass(node)

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

            Kind.Modifier -> renderModifier(node)
            Kind.Constructor,
            Kind.Function -> renderFunction(node)
            Kind.Property -> renderProperty(node)
            else -> "${node.kind}: ${node.name}"
        }
    }

    override fun renderPackage(node: DocumentationNode): String {
        return "package ${node.name}"
    }

    override fun renderType(node: DocumentationNode): String {
        return node.name
    }

    override fun renderModifier(node: DocumentationNode): String {
        return when (node.name) {
            "final" -> ""
            "internal" -> ""
            else -> node.name
        }
    }

    override fun renderTypeParameter(node: DocumentationNode): String {
        val constraints = node.details(Kind.UpperBound)
        return if (constraints.none())
            node.name
        else {
            node.name + " : " + constraints.map { renderType(node) }.join()
        }
    }

    override fun renderParameter(node: DocumentationNode): String {
        return node.name + ": " + renderType(node.detail(Kind.Type))
    }

    override fun renderTypeParametersForNode(node: DocumentationNode): String {
        return StringBuilder {
            val typeParameters = node.details(Kind.TypeParameter)
            if (typeParameters.any()) {
                append("<")
                append(typeParameters.map { renderTypeParameter(it) }.join())
                append("> ")
            }
        }.toString()
    }

    override fun renderModifiersForNode(node: DocumentationNode): String {
        val modifiers = node.details(Kind.Modifier).map { renderModifier(it) }.filter { it != ""}
        if (modifiers.none())
            return ""
        return modifiers.join(" ", postfix = " ")
    }

    override fun renderClass(node: DocumentationNode): String {
        return StringBuilder {
            append(renderModifiersForNode(node))
            when (node.kind) {
                Kind.Class -> append("class ")
                Kind.Interface -> append("trait ")
                Kind.Object -> append("object ")
                else -> throw IllegalArgumentException("Node $node is not a class-like object")
            }

            append(node.name)
            append(renderTypeParametersForNode(node))

            append("(")
            append(node.details(Kind.Parameter).map { renderParameter(it) }.join())
            append(")")
        }.toString()
    }

    override fun renderFunction(node: DocumentationNode): String {
        return StringBuilder {
            append(renderModifiersForNode(node))
            when (node.kind) {
                Kind.Constructor -> append("init")
                Kind.Function -> append("fun ")
                else -> throw IllegalArgumentException("Node $node is not a function-like object")
            }
            append(renderTypeParametersForNode(node))
            val receiver = node.details(Kind.Receiver).firstOrNull() // TODO: replace with singleOrNull when fixed
            if (receiver != null) {
                append(renderType(receiver.detail(Kind.Type)))
                append(".")
            }

            if (node.kind != Kind.Constructor)
                append(node.name)

            append("(")
            append(node.details(Kind.Parameter).map { renderParameter(it) }.join())
            append(")")
            if (node.kind != Kind.Constructor) {
                append(": ")
                append(renderType(node.detail(Kind.Type)))
            }
        }.toString()
    }

    override fun renderProperty(node: DocumentationNode): String {
        return StringBuilder {
            append(renderModifiersForNode(node))
            when (node.kind) {
                Kind.Property -> append("val ")
                else -> throw IllegalArgumentException("Node $node is not a property")
            }
            append(renderTypeParametersForNode(node))
            val receiver = node.details(Kind.Receiver).firstOrNull() // TODO: replace with singleOrNull when fixed
            if (receiver != null) {
                append(renderType(receiver.detail(Kind.Type)))
                append(".")
            }

            append(node.name)
            append(": ")
            append(renderType(node.detail(Kind.Type)))
        }.toString()
    }
}