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

import org.jetbrains.jet.lang.resolve.name.Name

/**
 * Generates cross-references for documentation such as extensions for a type, inheritors, etc
 *
 * $receiver: [DocumentationContext] for node/descriptor resolutions
 * $node: [DocumentationNode] to visit
 */
public fun DocumentationContext.resolveReferences(node: DocumentationNode) {
    node.details(DocumentationNode.Kind.Receiver).forEach { detail ->
        val receiverType = detail.detail(DocumentationNode.Kind.Type)
        val descriptor = relations[receiverType]
        if (descriptor != null) {
            val typeNode = descriptorToNode[descriptor]
            // if typeNode is null, extension is to external type like in a library
            // should we create dummy node here?
            typeNode?.addReferenceTo(node, DocumentationReference.Kind.Extension)
        }
    }
    node.details(DocumentationNode.Kind.Supertype).forEach { detail ->
        val descriptor = relations[detail]
        if (descriptor != null) {
            val typeNode = descriptorToNode[descriptor]
            typeNode?.addReferenceTo(node, DocumentationReference.Kind.Inheritor)
        }
    }
    node.details.forEach { detail ->
        val descriptor = relations[detail]
        if (descriptor != null) {
            val typeNode = descriptorToNode[descriptor]
            if (typeNode != null) {
                detail.addReferenceTo(typeNode, DocumentationReference.Kind.Link)
            }
        }
    }

    resolveContentLinks(node, node.doc)

    for (child in node.members) {
        resolveReferences(child)
    }
    for (child in node.details) {
        resolveReferences(child)
    }
}

fun DocumentationContext.resolveContentLinks(node: DocumentationNode, content: ContentNode) {
    val snapshot = content.children.toList()
    for (child in snapshot) {
        val referenceText = when (child) {
            is ContentExternalLink -> child.href
            is ContentNameLink -> child.name
            else -> null
        }

        if (referenceText != null && Name.isValidIdentifier(referenceText)) {
            val scope = getResolutionScope(node)
            val symbolName = Name.guess(referenceText)
            val symbol =
                    scope.getLocalVariable(symbolName) ?:
                            scope.getProperties(symbolName).firstOrNull() ?:
                            scope.getFunctions(symbolName).firstOrNull() ?:
                            scope.getClassifier(symbolName)

            if (symbol != null) {
                val targetNode = descriptorToNode[symbol]
                if (targetNode != null) {
                    val index = content.children.indexOf(child)
                    content.children.remove(index)
                    val contentLink = ContentNodeLink(targetNode)
                    contentLink.children.addAll(child.children)
                    content.children.add(index, contentLink)
                }
            }
        }

        resolveContentLinks(node, child)
    }
}