aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/Model/DocumentationReference.kt
blob: 9223c17a2f0d20c51d0f9280332958874bb6b562 (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
package org.jetbrains.dokka

import com.google.inject.Singleton

enum class RefKind {
    Owner,
    Member,
    InheritedMember,
    InheritedCompanionObjectMember,
    Detail,
    Link,
    HiddenLink,
    Extension,
    Inheritor,
    Superclass,
    Override,
    Annotation,
    HiddenAnnotation,
    Deprecation,
    TopLevelPage,
    Platform,
    ExternalType
}

data class DocumentationReference(val from: DocumentationNode, val to: DocumentationNode, val kind: RefKind) {
}

sealed class NodeResolver {
    abstract fun resolve(): DocumentationNode?
    class BySignature(var signature: String, var nodeMap: Map<String, DocumentationNode>) : NodeResolver() {
        override fun resolve(): DocumentationNode? {
            return nodeMap[signature]
        }
    }

    class Exact(var exactNode: DocumentationNode?) : NodeResolver() {
        override fun resolve(): DocumentationNode? {
            return exactNode
        }
    }
}

class PendingDocumentationReference(val lazyNodeFrom: NodeResolver,
                                    val lazyNodeTo: NodeResolver,
                                    val kind: RefKind) {
    fun resolve() {
        val fromNode = lazyNodeFrom.resolve()
        val toNode = lazyNodeTo.resolve()
        if (fromNode != null && toNode != null) {
            fromNode.addReferenceTo(toNode, kind)
        }
    }
}

class NodeReferenceGraph {
    private val nodeMap = hashMapOf<String, DocumentationNode>()
    val nodeMapView: Map<String, DocumentationNode>
            get() = HashMap(nodeMap)

    val references = arrayListOf<PendingDocumentationReference>()

    fun register(signature: String, node: DocumentationNode) {
        nodeMap[signature] = node
    }

    fun link(fromNode: DocumentationNode, toSignature: String, kind: RefKind) {
        references.add(
            PendingDocumentationReference(
                NodeResolver.Exact(fromNode),
                NodeResolver.BySignature(toSignature, nodeMap),
                kind
            ))
    }

    fun link(fromSignature: String, toNode: DocumentationNode, kind: RefKind) {
        references.add(
            PendingDocumentationReference(
                NodeResolver.BySignature(fromSignature, nodeMap),
                NodeResolver.Exact(toNode),
                kind
            )
        )
    }

    fun link(fromSignature: String, toSignature: String, kind: RefKind) {
        references.add(
            PendingDocumentationReference(
                NodeResolver.BySignature(fromSignature, nodeMap),
                NodeResolver.BySignature(toSignature, nodeMap),
                kind
            )
        )
    }

    fun addReference(reference: PendingDocumentationReference) {
        references.add(reference)
    }

    fun lookup(signature: String) = nodeMap[signature]

    fun lookupOrWarn(signature: String, logger: DokkaLogger): DocumentationNode? {
        val result = nodeMap[signature]
        if (result == null) {
            logger.warn("Can't find node by signature `$signature`")
        }
        return result
    }

    fun resolveReferences() {
        references.forEach { it.resolve() }
    }
}

@Singleton
class PlatformNodeRegistry {
    private val platformNodes = hashMapOf<String, DocumentationNode>()

    operator fun get(platform: String): DocumentationNode {
        return platformNodes.getOrPut(platform) {
            DocumentationNode(platform, Content.Empty, NodeKind.Platform)
        }
    }
}