aboutsummaryrefslogtreecommitdiff
path: root/src/Kotlin/DocumentationNodeBuilder.kt
blob: a2686b00f7b69274929c5aad6a1b0ab8d81fbafe (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
package org.jetbrains.dokka

import org.jetbrains.jet.lang.descriptors.impl.DeclarationDescriptorVisitorEmptyBodies
import org.jetbrains.jet.lang.descriptors.MemberDescriptor
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor
import org.jetbrains.jet.lang.types.JetType
import org.jetbrains.jet.lang.descriptors.Named
import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor
import org.jetbrains.jet.lang.descriptors.ClassDescriptor
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor
import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor
import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor
import org.jetbrains.jet.lang.descriptors.PackageFragmentDescriptor
import org.jetbrains.jet.lang.descriptors.ClassKind
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns
import org.jetbrains.jet.lang.types.TypeProjection
import org.jetbrains.jet.lang.types.Variance
import org.jetbrains.dokka.DocumentationNode.Kind

class DocumentationNodeBuilder(val context: DocumentationContext) : DeclarationDescriptorVisitorEmptyBodies<DocumentationNode, DocumentationNode>() {

    fun reference(from: DocumentationNode, to: DocumentationNode, kind: DocumentationReference.Kind) {
        from.addReferenceTo(to, kind)
        when (kind) {
            DocumentationReference.Kind.Detail -> to.addReferenceTo(from, DocumentationReference.Kind.Owner)
            DocumentationReference.Kind.Member -> to.addReferenceTo(from, DocumentationReference.Kind.Owner)
            DocumentationReference.Kind.Owner -> to.addReferenceTo(from, DocumentationReference.Kind.Member)
        }
    }

    fun addModality(descriptor: MemberDescriptor, data: DocumentationNode) {
        val modifier = descriptor.getModality().name().toLowerCase()
        val node = DocumentationNode(modifier, Content.Empty, DocumentationNode.Kind.Modifier)
        reference(data, node, DocumentationReference.Kind.Detail)
    }

    fun addVisibility(descriptor: MemberDescriptor, data: DocumentationNode) {
        val modifier = descriptor.getVisibility().toString()
        val node = DocumentationNode(modifier, Content.Empty, DocumentationNode.Kind.Modifier)
        reference(data, node, DocumentationReference.Kind.Detail)
    }

    fun addSupertypes(descriptor: ClassDescriptor, data: DocumentationNode) {
        val superTypes = descriptor.getTypeConstructor().getSupertypes()
        for (superType in superTypes) {
            if (superType.toString() != "Any")
                addType(superType, data, DocumentationNode.Kind.Supertype)
        }
    }

    fun addProjection(projection: TypeProjection, data: DocumentationNode, kind: DocumentationNode.Kind = DocumentationNode.Kind.Type) {
        val prefix = when (projection.getProjectionKind()) {
            Variance.IN_VARIANCE -> "in "
            Variance.OUT_VARIANCE -> "out "
            else -> ""
        }
        addType(projection.getType(), data, kind, prefix)
    }

    fun addType(jetType: JetType?, data: DocumentationNode, kind: DocumentationNode.Kind = DocumentationNode.Kind.Type, prefix : String = "") {
        if (jetType == null)
            return
        val classifierDescriptor = jetType.getConstructor().getDeclarationDescriptor()
        val name = when (classifierDescriptor) {
            is Named -> prefix + classifierDescriptor.getName().asString() + if (jetType.isNullable()) "?" else ""
            else -> "<anonymous>"
        }
        val node = DocumentationNode(name, Content.Empty, kind)
        if (classifierDescriptor != null)
            context.attach(node, classifierDescriptor)

        reference(data, node, DocumentationReference.Kind.Detail)
        for (typeArgument in jetType.getArguments())
            addProjection(typeArgument, node)
    }

    override fun visitDeclarationDescriptor(descriptor: DeclarationDescriptor?, data: DocumentationNode?): DocumentationNode? {
        descriptor!!
        val doc = context.parseDocumentation(descriptor)
        val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNode.Kind.Unknown)
        reference(data!!, node, DocumentationReference.Kind.Member)
        context.register(descriptor, node)
        return node
    }

    override fun visitReceiverParameterDescriptor(descriptor: ReceiverParameterDescriptor?, data: DocumentationNode?): DocumentationNode? {
        descriptor!!
        val node = DocumentationNode(descriptor.getName().asString(), Content.Empty, DocumentationNode.Kind.Receiver)
        reference(data!!, node, DocumentationReference.Kind.Detail)

        addType(descriptor.getType(), node)

        return node
    }

    override fun visitValueParameterDescriptor(descriptor: ValueParameterDescriptor?, data: DocumentationNode?): DocumentationNode? {
        descriptor!!
        val doc = context.parseDocumentation(descriptor)
        val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNode.Kind.Parameter)
        reference(data!!, node, DocumentationReference.Kind.Detail)

        addType(descriptor.getType(), node)

        return node
    }

    override fun visitClassDescriptor(descriptor: ClassDescriptor?, data: DocumentationNode?): DocumentationNode? {
        descriptor!!
        val doc = context.parseDocumentation(descriptor)
        val node = DocumentationNode(descriptor.getName().asString(), doc, when (descriptor.getKind()) {
            ClassKind.OBJECT -> Kind.Object
            ClassKind.CLASS_OBJECT -> Kind.Object
            ClassKind.TRAIT -> Kind.Interface
            ClassKind.ENUM_CLASS -> Kind.Enum
            ClassKind.ENUM_ENTRY -> Kind.EnumItem
            else -> Kind.Class
        })
        reference(data!!, node, DocumentationReference.Kind.Member)
        addModality(descriptor, node)
        addVisibility(descriptor, node)
        addSupertypes(descriptor, node)
        context.register(descriptor, node)
        return node
    }

    override fun visitFunctionDescriptor(descriptor: FunctionDescriptor?, data: DocumentationNode?): DocumentationNode? {
        descriptor!!
        val doc = context.parseDocumentation(descriptor)
        val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNode.Kind.Function)
        reference(data!!, node, DocumentationReference.Kind.Member)

        addType(descriptor.getReturnType(), node)
        addModality(descriptor, node)
        addVisibility(descriptor, node)
        context.register(descriptor, node)
        return node
    }

    override fun visitTypeParameterDescriptor(descriptor: TypeParameterDescriptor?, data: DocumentationNode?): DocumentationNode? {
        descriptor!!
        val doc = context.parseDocumentation(descriptor)
        val name = descriptor.getName().asString()
        val prefix = when (descriptor.getVariance()) {
            Variance.IN_VARIANCE -> "in "
            Variance.OUT_VARIANCE -> "out "
            else -> ""
        }

        val node = DocumentationNode(prefix + name, doc, DocumentationNode.Kind.TypeParameter)
        reference(data!!, node, DocumentationReference.Kind.Detail)
        val builtIns = KotlinBuiltIns.getInstance()
        for (constraint in descriptor.getUpperBounds()) {
            if (constraint == builtIns.getDefaultBound())
                continue
            val constraintNode = DocumentationNode(constraint.toString(), Content.Empty, DocumentationNode.Kind.UpperBound)
            reference(node, constraintNode, DocumentationReference.Kind.Detail)
        }
        for (constraint in descriptor.getLowerBounds()) {
            if (builtIns.isNothing(constraint))
                continue
            val constraintNode = DocumentationNode(constraint.toString(), Content.Empty, DocumentationNode.Kind.LowerBound)
            reference(node, constraintNode, DocumentationReference.Kind.Detail)
        }
        return node
    }

    override fun visitPropertyDescriptor(descriptor: PropertyDescriptor?, data: DocumentationNode?): DocumentationNode? {
        descriptor!!
        val doc = context.parseDocumentation(descriptor)
        val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNode.Kind.Property)
        reference(data!!, node, DocumentationReference.Kind.Member)

        addType(descriptor.getType(), node)
        addModality(descriptor, node)
        addVisibility(descriptor, node)
        context.register(descriptor, node)
        return node
    }

    override fun visitConstructorDescriptor(descriptor: ConstructorDescriptor?, data: DocumentationNode?): DocumentationNode? {
        descriptor!!
        val doc = context.parseDocumentation(descriptor)
        val node = DocumentationNode("<init>", doc, DocumentationNode.Kind.Constructor)
        reference(data!!, node, DocumentationReference.Kind.Member)

        addVisibility(descriptor, node)
        context.register(descriptor, node)
        return node
    }

    override fun visitPackageViewDescriptor(descriptor: PackageViewDescriptor?, data: DocumentationNode?): DocumentationNode? {
        descriptor!!
        val node = DocumentationNode(descriptor.getFqName().asString(), Content.Empty, DocumentationNode.Kind.Package)
        reference(data!!, node, DocumentationReference.Kind.Member)
        return node
    }

    override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor?, data: DocumentationNode?): DocumentationNode? {
        descriptor!!
        val node = DocumentationNode(descriptor.fqName.asString(), Content.Empty, DocumentationNode.Kind.Package)
        reference(data!!, node, DocumentationReference.Kind.Member)
        return node
    }
}