aboutsummaryrefslogtreecommitdiff
path: root/src/Model/DocumentationBuilder.kt
blob: e28d437f39ca59592eaef09763512563b7cf867d (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
package org.jetbrains.dokka

import org.jetbrains.jet.lang.resolve.*
import org.jetbrains.jet.lang.psi.*
import org.jetbrains.jet.lang.descriptors.*
import org.jetbrains.jet.lang.descriptors.impl.*

fun BindingContext.createSourceModel(file: JetFile): DocumentationModel {
    val model = DocumentationModel()
    val packageFragment = getPackageFragment(file)
    if (packageFragment == null) throw IllegalArgumentException("File $file should have package fragment")

    val visitor = DocumentationBuilderVisitor(this)
    visitDescriptor(packageFragment, model, visitor)

    return model
}

class DocumentationBuilderVisitor(val context: BindingContext) : DeclarationDescriptorVisitorEmptyBodies<DocumentationNode, DocumentationNode>() {

    override fun visitDeclarationDescriptor(descriptor: DeclarationDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val doc = context.getDocumentation(descriptor!!)
        val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Unknown)
        data?.addReferenceTo(node, DocumentationReferenceKind.Member)
        return node
    }

    override fun visitValueParameterDescriptor(descriptor: ValueParameterDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val doc = context.getDocumentation(descriptor!!)
        val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Parameter)
        data?.addReferenceTo(node, DocumentationReferenceKind.Detail)
        return node
    }

    override fun visitClassDescriptor(descriptor: ClassDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val doc = context.getDocumentation(descriptor!!)
        val node = DocumentationNode(descriptor.getName().asString(), doc,
                                     when (descriptor.getKind()) {
                                         ClassKind.OBJECT -> DocumentationNodeKind.Object
                                         else -> DocumentationNodeKind.Class
                                     }
                                    )
        data?.addReferenceTo(node, DocumentationReferenceKind.Member)
        return node
    }

    override fun visitFunctionDescriptor(descriptor: FunctionDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val doc = context.getDocumentation(descriptor!!)
        val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Function)
        data?.addReferenceTo(node, DocumentationReferenceKind.Member)
        return node
    }

    override fun visitPropertyDescriptor(descriptor: PropertyDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val doc = context.getDocumentation(descriptor!!)
        val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Property)
        data?.addReferenceTo(node, DocumentationReferenceKind.Member)
        return node
    }

    override fun visitConstructorDescriptor(descriptor: ConstructorDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val doc = context.getDocumentation(descriptor!!)
        val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Constructor)
        data?.addReferenceTo(node, DocumentationReferenceKind.Member)
        return node
    }

    override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor?, data: DocumentationNode?): DocumentationNode? {
        val node = DocumentationNode(descriptor!!.fqName.asString(), "", DocumentationNodeKind.Package)
        data?.addReferenceTo(node, DocumentationReferenceKind.Member)
        return node
    }
}