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

import org.jetbrains.jet.lang.descriptors.*
import org.jetbrains.jet.lang.resolve.name.*
import org.jetbrains.jet.lang.resolve.*

public data class DocumentationOptions(val includeNonPublic: Boolean = false)

class DocumentationBuildingVisitor(val context: BindingContext,
                                   val options: DocumentationOptions,
                                   private val worker: DeclarationDescriptorVisitor<DocumentationNode, DocumentationNode>)
: DeclarationDescriptorVisitor<DocumentationNode, DocumentationNode> {

    val visibleToDocumentation = setOf(Visibilities.INTERNAL, Visibilities.PROTECTED, Visibilities.PUBLIC)

    private fun visitChildren(descriptors: Collection<DeclarationDescriptor>, data: DocumentationNode) {
        for (descriptor in descriptors) {
            visitChild(descriptor, data)
        }
    }

    private fun visitChild(descriptor: DeclarationDescriptor?, data: DocumentationNode) {
        if (descriptor != null && descriptor.isUserCode()) {
            if (options.includeNonPublic
                    || descriptor !is MemberDescriptor
                    || descriptor.getVisibility() in visibleToDocumentation) {
                descriptor.accept(this, data)
            }
        }
    }

    private fun createDocumentation(descriptor: DeclarationDescriptor, data: DocumentationNode): DocumentationNode {
        return descriptor.accept(worker, data)
    }

    private fun processCallable(descriptor: CallableDescriptor, data: DocumentationNode): DocumentationNode {
        val node = createDocumentation(descriptor, data)
        visitChildren(descriptor.getTypeParameters(), node)
        visitChild(descriptor.getExtensionReceiverParameter(), node)
        visitChildren(descriptor.getValueParameters(), node)
        return node
    }

    public override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val node = createDocumentation(descriptor!!, data!!)
        visitChildren(descriptor.getMemberScope().getAllDescriptors(), node)
        return node
    }

    public override fun visitPackageViewDescriptor(descriptor: PackageViewDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val node = createDocumentation(descriptor!!, data!!)
        visitChildren(descriptor.getMemberScope().getAllDescriptors(), node)
        return node
    }

    public override fun visitVariableDescriptor(descriptor: VariableDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val node = processCallable(descriptor!!, data!!)
        return node
    }

    public override fun visitPropertyDescriptor(descriptor: PropertyDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val node = processCallable(descriptor!!, data!!)
        visitChild(descriptor.getGetter(), node)
        visitChild(descriptor.getSetter(), node)
        return node
    }

    public override fun visitFunctionDescriptor(descriptor: FunctionDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val node = processCallable(descriptor!!, data!!)
        return node
    }

    public override fun visitTypeParameterDescriptor(descriptor: TypeParameterDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val node = createDocumentation(descriptor!!, data!!)
        return node
    }

    public override fun visitClassDescriptor(descriptor: ClassDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val node = createDocumentation(descriptor!!, data!!)
        if (descriptor.getKind() != ClassKind.OBJECT) {
            // do not go inside object for class object and constructors, they are generated
            visitChildren(descriptor.getTypeConstructor().getParameters(), node)
            visitChildren(descriptor.getConstructors(), node)
            visitChild(descriptor.getClassObjectDescriptor(), node)
        }
        visitChildren(descriptor.getDefaultType().getMemberScope().getAllDescriptors(), node)
        return node
    }

    public override fun visitModuleDeclaration(descriptor: ModuleDescriptor, data: DocumentationNode): DocumentationNode {
        val node = createDocumentation(descriptor, data)
        visitChild(descriptor.getPackage(FqName.ROOT), node)
        return node
    }

    public override fun visitConstructorDescriptor(descriptor: ConstructorDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val node = createDocumentation(descriptor!!, data!!)
        visitChildren(descriptor.getValueParameters(), node)
        return node
    }

    public override fun visitScriptDescriptor(scriptDescriptor: ScriptDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val classDescriptor = scriptDescriptor!!.getClassDescriptor()
        val node = visitClassDescriptor(classDescriptor, data)
        return node
    }

    public override fun visitValueParameterDescriptor(descriptor: ValueParameterDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val node = visitVariableDescriptor(descriptor!!, data)
        return node
    }

    public override fun visitPropertyGetterDescriptor(descriptor: PropertyGetterDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val node = visitFunctionDescriptor(descriptor!!, data)
        return node
    }

    public override fun visitPropertySetterDescriptor(descriptor: PropertySetterDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val node = visitFunctionDescriptor(descriptor!!, data)
        return node
    }

    public override fun visitReceiverParameterDescriptor(descriptor: ReceiverParameterDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val node = createDocumentation(descriptor!!, data!!)
        return node
    }
}