aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/Languages/JavaLanguageService.kt
blob: d6f9ade53c8fa47afa5349cb55f11782f64cec1c (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package org.jetbrains.dokka

import org.jetbrains.dokka.LanguageService.RenderMode

/**
 * Implements [LanguageService] and provides rendering of symbols in Java language
 */
class JavaLanguageService : LanguageService {
    override fun render(node: DocumentationNode, renderMode: RenderMode): ContentNode {
        return ContentText(when (node.kind) {
            NodeKind.Package -> renderPackage(node)
            in NodeKind.classLike -> renderClass(node)

            NodeKind.TypeParameter -> renderTypeParameter(node)
            NodeKind.Type,
            NodeKind.UpperBound -> renderType(node)

            NodeKind.Constructor,
            NodeKind.Function -> renderFunction(node)
            NodeKind.Property -> renderProperty(node)
            else -> "${node.kind}: ${node.name}"
        })
    }

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

    override fun summarizeSignatures(nodes: List<DocumentationNode>): ContentNode? = null

    private fun renderPackage(node: DocumentationNode): String {
        return "package ${node.name}"
    }

    private fun renderModifier(node: DocumentationNode): String {
        return when (node.name) {
            "open" -> ""
            "internal" -> ""
            else -> node.name
        }
    }

    fun getArrayElementType(node: DocumentationNode): DocumentationNode? = when (node.name) {
        "Array" -> node.details(NodeKind.Type).singleOrNull()?.let { et -> getArrayElementType(et) ?: et } ?: DocumentationNode("Object", node.content, NodeKind.ExternalClass)
        "IntArray", "LongArray", "ShortArray", "ByteArray", "CharArray", "DoubleArray", "FloatArray", "BooleanArray" -> DocumentationNode(node.name.removeSuffix("Array").toLowerCase(), node.content, NodeKind.Type)
        else -> null
    }

    fun getArrayDimension(node: DocumentationNode): Int = when (node.name) {
        "Array" -> 1 + (node.details(NodeKind.Type).singleOrNull()?.let { getArrayDimension(it) } ?: 0)
        "IntArray", "LongArray", "ShortArray", "ByteArray", "CharArray", "DoubleArray", "FloatArray", "BooleanArray" -> 1
        else -> 0
    }

    fun renderType(node: DocumentationNode): String {
        return when (node.name) {
            "Unit" -> "void"
            "Int" -> "int"
            "Long" -> "long"
            "Double" -> "double"
            "Float" -> "float"
            "Char" -> "char"
            "Boolean" -> "bool"
        // TODO: render arrays
            else -> node.name
        }
    }

    private fun renderTypeParameter(node: DocumentationNode): String {
        val constraints = node.details(NodeKind.UpperBound)
        return if (constraints.none())
            node.name
        else {
            node.name + " extends " + constraints.map { renderType(node) }.joinToString()
        }
    }

    private fun renderParameter(node: DocumentationNode): String {
        return "${renderType(node.detail(NodeKind.Type))} ${node.name}"
    }

    private fun renderTypeParametersForNode(node: DocumentationNode): String {
        return StringBuilder().apply {
            val typeParameters = node.details(NodeKind.TypeParameter)
            if (typeParameters.any()) {
                append("<")
                append(typeParameters.map { renderTypeParameter(it) }.joinToString())
                append("> ")
            }
        }.toString()
    }

    private fun renderModifiersForNode(node: DocumentationNode): String {
        val modifiers = node.details(NodeKind.Modifier).map { renderModifier(it) }.filter { it != "" }
        if (modifiers.none())
            return ""
        return modifiers.joinToString(" ", postfix = " ")
    }

    private fun renderClass(node: DocumentationNode): String {
        return StringBuilder().apply {
            when (node.kind) {
                NodeKind.Class -> append("class ")
                NodeKind.Interface -> append("interface ")
                NodeKind.Enum -> append("enum ")
                NodeKind.EnumItem -> append("enum value ")
                NodeKind.Object -> append("class ")
                else -> throw IllegalArgumentException("Node $node is not a class-like object")
            }

            append(node.name)
            append(renderTypeParametersForNode(node))
        }.toString()
    }

    private fun renderFunction(node: DocumentationNode): String {
        return StringBuilder().apply {
            when (node.kind) {
                NodeKind.Constructor -> append(node.owner?.name)
                NodeKind.Function -> {
                    append(renderTypeParametersForNode(node))
                    append(renderType(node.detail(NodeKind.Type)))
                    append(" ")
                    append(node.name)
                }
                else -> throw IllegalArgumentException("Node $node is not a function-like object")
            }

            val receiver = node.details(NodeKind.Receiver).singleOrNull()
            append("(")
            if (receiver != null)
                (listOf(receiver) + node.details(NodeKind.Parameter)).map { renderParameter(it) }.joinTo(this)
            else
                node.details(NodeKind.Parameter).map { renderParameter(it) }.joinTo(this)

            append(")")
        }.toString()
    }

    private fun renderProperty(node: DocumentationNode): String {
        return StringBuilder().apply {
            when (node.kind) {
                NodeKind.Property -> append("val ")
                else -> throw IllegalArgumentException("Node $node is not a property")
            }
            append(renderTypeParametersForNode(node))
            val receiver = node.details(NodeKind.Receiver).singleOrNull()
            if (receiver != null) {
                append(renderType(receiver.detail(NodeKind.Type)))
                append(".")
            }

            append(node.name)
            append(": ")
            append(renderType(node.detail(NodeKind.Type)))
        }.toString()
    }
}