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

import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor
import org.jetbrains.jet.lang.resolve.BindingContext
import org.jetbrains.jet.lang.resolve.scopes.JetScope
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor
import org.jetbrains.jet.lang.resolve.name.FqName

/**
 * Context for documentation generation.
 *
 * Holds information about relations between nodes and descriptors during documentation generation
 *
 * %bindingContext: symbol resolution context
 */
public class DocumentationContext(val bindingContext: BindingContext) {
    val descriptorToNode = hashMapOf<DeclarationDescriptor, DocumentationNode>()
    val nodeToDescriptor = hashMapOf<DocumentationNode, DeclarationDescriptor>()

    val relations = hashMapOf<DocumentationNode, DeclarationDescriptor>()

    fun attach(node: DocumentationNode, descriptor: DeclarationDescriptor) {
        relations.put(node, descriptor)
    }

    fun register(descriptor: DeclarationDescriptor, node: DocumentationNode) {
        descriptorToNode.put(descriptor, node)
        nodeToDescriptor.put(node, descriptor)
    }

    fun getResolutionScope(node: DocumentationNode): JetScope {
        val descriptor = nodeToDescriptor[node] ?: throw IllegalArgumentException("Node is not known to this context")
        return bindingContext.getResolutionScope(descriptor)
    }

    fun parseDocumentation(descriptor: DeclarationDescriptor): Content {
        val docText = bindingContext.getDocumentationElements(descriptor).map { it.extractText() }.join("\n")
        val tree = MarkdownProcessor.parse(docText)
        //println(tree.toTestString())
        val content = tree.toContent()
        return content
    }
}

fun BindingContext.createDocumentationModule(name: String,
                                             module: ModuleDescriptor,
                                             packages: Set<FqName>,
                                             options: DocumentationOptions = DocumentationOptions()): DocumentationModule {
    val documentationModule = DocumentationModule(name)
    val context = DocumentationContext(this)
    val visitor = DocumentationNodeBuilder(context)
    for (packageName in packages) {
        val pkg = module.getPackage(packageName)
        pkg!!.accept(DocumentationBuildingVisitor(this, options, visitor), documentationModule)
    }

    context.resolveReferences(documentationModule)

    // TODO: Uncomment for resolve verification
    // checkResolveChildren(documentationModule)
    return documentationModule
}