aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/Languages/NewJavaLanguageService.kt
blob: 22ad62b26562eac15ab27783af7b3e4144730c66 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package org.jetbrains.dokka

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

                NodeKind.Modifier -> renderModifier(this, 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 summarizeSignatures(nodes: List<DocumentationNode>): ContentNode? = null


    override fun renderModifier(block: ContentBlock, node: DocumentationNode, nowrap: Boolean) {
        when (node.name) {
            "open", "internal" -> {
            }
            else -> super.renderModifier(block, node, nowrap)
        }
    }

    fun getArrayElementType(node: DocumentationNode): DocumentationNode? = when (node.qualifiedName()) {
        "kotlin.Array" ->
            node.details(NodeKind.Type).singleOrNull()?.let { et -> getArrayElementType(et) ?: et }
                    ?: DocumentationNode("Object", node.content, NodeKind.ExternalClass)

        "kotlin.IntArray", "kotlin.LongArray", "kotlin.ShortArray", "kotlin.ByteArray",
        "kotlin.CharArray", "kotlin.DoubleArray", "kotlin.FloatArray", "kotlin.BooleanArray" ->
            DocumentationNode(node.name.removeSuffix("Array").toLowerCase(), node.content, NodeKind.Type)

        else -> null
    }

    fun getArrayDimension(node: DocumentationNode): Int = when (node.qualifiedName()) {
        "kotlin.Array" ->
            1 + (node.details(NodeKind.Type).singleOrNull()?.let { getArrayDimension(it) } ?: 0)

        "kotlin.IntArray", "kotlin.LongArray", "kotlin.ShortArray", "kotlin.ByteArray",
        "kotlin.CharArray", "kotlin.DoubleArray", "kotlin.FloatArray", "kotlin.BooleanArray" ->
            1
        else -> 0
    }

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

    private fun ContentBlock.renderTypeParameter(node: DocumentationNode) {
        val constraints = node.details(NodeKind.UpperBound)
        if (constraints.none())
            identifier(node.name)
        else {
            identifier(node.name)
            text(" ")
            keyword("extends")
            text(" ")
            constraints.forEach { renderType(node) }
        }
    }

    private fun ContentBlock.renderParameter(node: DocumentationNode) {
        renderType(node.detail(NodeKind.Type))
        text(" ")
        identifier(node.name)
    }

    private fun ContentBlock.renderTypeParametersForNode(node: DocumentationNode) {
        val typeParameters = node.details(NodeKind.TypeParameter)
        if (typeParameters.any()) {
            symbol("<")
            renderList(typeParameters, noWrap = true) {
                renderTypeParameter(it)
            }
            symbol(">")
            text(" ")
        }
    }

//    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 ContentBlock.renderClassKind(node: DocumentationNode) {
        when (node.kind) {
            NodeKind.Interface -> {
                keyword("interface")
            }
            NodeKind.EnumItem -> {
                keyword("enum value")
            }
            NodeKind.Enum -> {
                keyword("enum")
            }
            NodeKind.Class, NodeKind.Exception, NodeKind.Object -> {
                keyword("class")
            }
            else -> throw IllegalArgumentException("Node $node is not a class-like object")
        }
        text(" ")
    }

    private fun ContentBlock.renderClass(node: DocumentationNode) {
        renderClassKind(node)

        identifier(node.name)
        renderTypeParametersForNode(node)
    }

    private fun ContentBlock.renderParameters(nodes: List<DocumentationNode>) {
        renderList(nodes) {
            renderParameter(it)
        }
    }

    private fun ContentBlock.renderFunction(node: DocumentationNode) {
        when (node.kind) {
            NodeKind.Constructor -> identifier(node.owner?.name ?: "")
            NodeKind.Function -> {
                renderTypeParametersForNode(node)
                renderType(node.detail(NodeKind.Type))
                text(" ")
                identifier(node.name)

            }
            else -> throw IllegalArgumentException("Node $node is not a function-like object")
        }

        val receiver = node.details(NodeKind.Receiver).singleOrNull()
        symbol("(")
        if (receiver != null)
            renderParameters(listOf(receiver) + node.details(NodeKind.Parameter))
        else
            renderParameters(node.details(NodeKind.Parameter))

        symbol(")")
    }

    private fun ContentBlock.renderProperty(node: DocumentationNode) {

        when (node.kind) {
            NodeKind.Property -> {
                keyword("val")
                text(" ")
            }
            else -> throw IllegalArgumentException("Node $node is not a property")
        }
        renderTypeParametersForNode(node)
        val receiver = node.details(NodeKind.Receiver).singleOrNull()
        if (receiver != null) {
            renderType(receiver.detail(NodeKind.Type))
            symbol(".")
        }

        identifier(node.name)
        symbol(":")
        text(" ")
        renderType(node.detail(NodeKind.Type))

    }
}