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

import org.jetbrains.jet.lang.resolve.scopes.JetScope
import org.jetbrains.jet.lang.resolve.BindingContext
import org.jetbrains.jet.lang.psi.JetFile

public enum class DocumentationNodeKind {
    Unknown

    Package
    Class
    Trait
    Object

    Constructor
    Function
    Property

    Parameter
    Receiver
    TypeParameter
    Type
    UpperBound
    LowerBound
    Exception

    Model
}

public enum class DocumentationReferenceKind {
    Owner
    Member
    Detail
    Link
    Override
}

public open class DocumentationNode(val name: String,
                                    val doc: DocumentationContent,
                                    val kind: DocumentationNodeKind,
                                    val scope: JetScope) {
    private val references = arrayListOf<DocumentationReference>()

    public val owner: DocumentationNode?
        get() = references(DocumentationReferenceKind.Owner).firstOrNull()?.to // TODO: should be singleOrNull, but bugz!
    public val details: List<DocumentationNode>
        get() = references(DocumentationReferenceKind.Detail).map { it.to }
    public val members: List<DocumentationNode>
        get() = references(DocumentationReferenceKind.Member).map { it.to }
    public val links: List<DocumentationNode>
        get() = references(DocumentationReferenceKind.Link).map { it.to }

    // TODO: Should we allow node mutation? Model merge will copy by ref, so references are transparent, which could nice
    public fun addReferenceTo(to: DocumentationNode, kind: DocumentationReferenceKind) {
        references.add(DocumentationReference(this, to, kind))
    }

    public fun addAllReferencesFrom(other: DocumentationNode) {
        references.addAll(other.references)
    }

    public fun references(kind: DocumentationReferenceKind): List<DocumentationReference> = references.filter { it.kind == kind }
    public fun allReferences(): List<DocumentationReference> = references

    public override fun toString(): String {
        return "$kind:$name"
    }
}

public class DocumentationModel : DocumentationNode("model", DocumentationContent.Empty, DocumentationNodeKind.Model, JetScope.EMPTY) {
    fun merge(other: DocumentationModel): DocumentationModel {
        val model = DocumentationModel()
        model.addAllReferencesFrom(other)
        model.addAllReferencesFrom(this)
        return model
    }

    public val nodes: List<DocumentationNode>
        get() = members
}

public data class DocumentationReference(val from: DocumentationNode, val to: DocumentationNode, val kind: DocumentationReferenceKind)

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

    val visitor = DocumentationNodeBuilder(this)
    packageFragment.accept(DocumentationBuildingVisitor(this, visitor), model)

    model.checkResolve()

    return model
}