aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/Languages/CommonLanguageService.kt
blob: edefa1b551912a6fef486e4d4eae4be9abb6ae54 (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
package org.jetbrains.dokka


abstract class CommonLanguageService : LanguageService {

    protected fun ContentBlock.renderPackage(node: DocumentationNode) {
        keyword("package")
        nbsp()
        identifier(node.name)
    }

    override fun renderName(node: DocumentationNode): String {
        return when (node.kind) {
            NodeKind.Constructor -> node.owner!!.name
            else -> node.name
        }
    }

    open fun renderModifier(
        block: ContentBlock,
        node: DocumentationNode,
        renderMode: LanguageService.RenderMode,
        nowrap: Boolean = false
    ) = with(block) {
        keyword(node.name)
        if (nowrap) {
            nbsp()
        } else {
            text(" ")
        }
    }

    protected fun renderLinked(
        block: ContentBlock,
        node: DocumentationNode,
        body: ContentBlock.(DocumentationNode) -> Unit
    ) = with(block) {
        val to = node.links.firstOrNull()
        if (to == null)
            body(node)
        else
            link(to) {
                this.body(node)
            }
    }

    protected fun <T> ContentBlock.renderList(
        nodes: List<T>, separator: String = ", ",
        noWrap: Boolean = false, renderItem: (T) -> Unit
    ) {
        if (nodes.none())
            return
        renderItem(nodes.first())
        nodes.drop(1).forEach {
            if (noWrap) {
                symbol(separator.removeSuffix(" "))
                nbsp()
            } else {
                symbol(separator)
            }
            renderItem(it)
        }
    }


}