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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
package org.jetbrains.dokka
import org.jetbrains.dokka.LanguageService.RenderMode
/**
* Implements [LanguageService] and provides rendering of symbols in Kotlin language
*/
class KotlinLanguageService : LanguageService {
override fun render(node: DocumentationNode, renderMode: RenderMode): ContentNode {
return content {
when (node.kind) {
DocumentationNode.Kind.Package -> renderPackage(node)
DocumentationNode.Kind.Class,
DocumentationNode.Kind.Interface,
DocumentationNode.Kind.Enum,
DocumentationNode.Kind.AnnotationClass,
DocumentationNode.Kind.Object -> renderClass(node)
DocumentationNode.Kind.EnumItem,
DocumentationNode.Kind.ExternalClass -> if (renderMode == RenderMode.FULL) identifier(node.name)
DocumentationNode.Kind.TypeParameter -> renderTypeParameter(node)
DocumentationNode.Kind.Type,
DocumentationNode.Kind.UpperBound -> renderType(node)
DocumentationNode.Kind.Modifier -> renderModifier(node)
DocumentationNode.Kind.Constructor,
DocumentationNode.Kind.Function,
DocumentationNode.Kind.ClassObjectFunction,
DocumentationNode.Kind.PropertyAccessor -> renderFunction(node)
DocumentationNode.Kind.Property,
DocumentationNode.Kind.ClassObjectProperty -> renderProperty(node)
else -> identifier(node.name)
}
}
}
override fun renderName(node: DocumentationNode): String {
return when (node.kind) {
DocumentationNode.Kind.Constructor -> node.owner!!.name
else -> node.name
}
}
private fun ContentBlock.renderPackage(node: DocumentationNode) {
keyword("package")
text(" ")
identifier(node.name)
}
private fun ContentBlock.renderList(nodes: List<DocumentationNode>, separator: String = ", ", renderItem: (DocumentationNode) -> Unit) {
if (nodes.none())
return
renderItem(nodes.first())
nodes.drop(1).forEach {
symbol(separator)
renderItem(it)
}
}
private fun ContentBlock.renderLinked(node: DocumentationNode, body: ContentBlock.(DocumentationNode)->Unit) {
val to = node.links.firstOrNull()
if (to == null)
body(node)
else
link(to) {
body(node)
}
}
private fun ContentBlock.renderType(node: DocumentationNode) {
val typeArguments = node.details(DocumentationNode.Kind.Type)
if (node.name == "Function${typeArguments.count() - 1}") {
// lambda
symbol("(")
renderList(typeArguments.take(typeArguments.size - 1)) {
renderType(it)
}
symbol(")")
text(" ")
symbol("->")
text(" ")
renderType(typeArguments.last())
return
}
if (node.name == "ExtensionFunction${typeArguments.count() - 2}") {
// extension lambda
renderType(typeArguments.first())
symbol(".")
symbol("(")
renderList(typeArguments.drop(1).take(typeArguments.size - 2)) {
renderType(it)
}
symbol(")")
text(" ")
symbol("->")
text(" ")
renderType(typeArguments.last())
return
}
renderLinked(node) { identifier(it.name) }
if (typeArguments.any()) {
symbol("<")
renderList(typeArguments) {
renderType(it)
}
symbol(">")
}
}
private fun ContentBlock.renderModifier(node: DocumentationNode) {
when (node.name) {
"final", "internal", "var" -> {}
else -> {
keyword(node.name)
text(" ")
}
}
}
private fun ContentBlock.renderTypeParameter(node: DocumentationNode) {
val constraints = node.details(DocumentationNode.Kind.UpperBound)
identifier(node.name)
if (constraints.any()) {
symbol(" : ")
renderList(constraints) {
renderType(it)
}
}
}
private fun ContentBlock.renderParameter(node: DocumentationNode) {
renderAnnotationsForNode(node)
identifier(node.name)
symbol(": ")
val parameterType = node.detail(DocumentationNode.Kind.Type)
renderType(parameterType)
val valueNode = node.details(DocumentationNode.Kind.Value).firstOrNull()
if (valueNode != null) {
symbol(" = ")
text(valueNode.name)
}
}
private fun ContentBlock.renderTypeParametersForNode(node: DocumentationNode) {
val typeParameters = node.details(DocumentationNode.Kind.TypeParameter)
if (typeParameters.any()) {
symbol("<")
renderList(typeParameters) {
renderTypeParameter(it)
}
symbol("> ")
}
}
private fun ContentBlock.renderSupertypesForNode(node: DocumentationNode) {
val supertypes = node.details(DocumentationNode.Kind.Supertype)
if (supertypes.any()) {
symbol(" : ")
renderList(supertypes) {
renderType(it)
}
}
}
private fun ContentBlock.renderModifiersForNode(node: DocumentationNode) {
val modifiers = node.details(DocumentationNode.Kind.Modifier)
for (it in modifiers) {
if (node.kind == org.jetbrains.dokka.DocumentationNode.Kind.Interface && it.name == "abstract")
continue
renderModifier(it)
}
}
private fun ContentBlock.renderAnnotationsForNode(node: DocumentationNode) {
node.annotations.forEach {
renderAnnotation(it)
}
}
private fun ContentBlock.renderAnnotation(node: DocumentationNode) {
identifier(node.name)
val parameters = node.details(DocumentationNode.Kind.Parameter)
if (!parameters.isEmpty()) {
symbol("(")
renderList(parameters) {
text(it.detail(DocumentationNode.Kind.Value).name)
}
symbol(")")
}
text(" ")
}
private fun ContentBlock.renderClass(node: DocumentationNode) {
renderModifiersForNode(node)
renderAnnotationsForNode(node)
when (node.kind) {
DocumentationNode.Kind.Class -> keyword("class ")
DocumentationNode.Kind.Interface -> keyword("trait ")
DocumentationNode.Kind.Enum -> keyword("enum class ")
DocumentationNode.Kind.AnnotationClass -> keyword("annotation class ")
DocumentationNode.Kind.EnumItem -> keyword("enum val ")
DocumentationNode.Kind.Object -> keyword("object ")
else -> throw IllegalArgumentException("Node $node is not a class-like object")
}
identifierOrDeprecated(node)
renderTypeParametersForNode(node)
renderSupertypesForNode(node)
}
private fun ContentBlock.renderFunction(node: DocumentationNode) {
renderModifiersForNode(node)
renderAnnotationsForNode(node)
when (node.kind) {
DocumentationNode.Kind.Constructor -> identifier(node.owner!!.name)
DocumentationNode.Kind.Function,
DocumentationNode.Kind.ClassObjectFunction -> keyword("fun ")
DocumentationNode.Kind.PropertyAccessor -> {}
else -> throw IllegalArgumentException("Node $node is not a function-like object")
}
renderTypeParametersForNode(node)
val receiver = node.details(DocumentationNode.Kind.Receiver).singleOrNull()
if (receiver != null) {
renderType(receiver.detail(DocumentationNode.Kind.Type))
symbol(".")
}
if (node.kind != org.jetbrains.dokka.DocumentationNode.Kind.Constructor)
identifierOrDeprecated(node)
symbol("(")
renderList(node.details(DocumentationNode.Kind.Parameter)) {
renderParameter(it)
}
symbol(")")
if (needReturnType(node)) {
symbol(": ")
renderType(node.detail(DocumentationNode.Kind.Type))
}
}
private fun needReturnType(node: DocumentationNode) = when(node.kind) {
DocumentationNode.Kind.Constructor -> false
DocumentationNode.Kind.PropertyAccessor -> node.name == "get"
else -> true
}
private fun ContentBlock.renderProperty(node: DocumentationNode) {
renderModifiersForNode(node)
renderAnnotationsForNode(node)
when (node.kind) {
DocumentationNode.Kind.Property,
DocumentationNode.Kind.ClassObjectProperty -> keyword("${node.getPropertyKeyword()} ")
else -> throw IllegalArgumentException("Node $node is not a property")
}
renderTypeParametersForNode(node)
val receiver = node.details(DocumentationNode.Kind.Receiver).singleOrNull()
if (receiver != null) {
renderType(receiver.detail(DocumentationNode.Kind.Type))
symbol(".")
}
identifierOrDeprecated(node)
symbol(": ")
renderType(node.detail(DocumentationNode.Kind.Type))
}
fun DocumentationNode.getPropertyKeyword() =
if (details(DocumentationNode.Kind.Modifier).any { it.name == "var" }) "var" else "val"
fun ContentBlock.identifierOrDeprecated(node: DocumentationNode) {
if (node.deprecation != null) {
val strike = ContentStrikethrough()
strike.identifier(node.name)
append(strike)
} else {
identifier(node.name)
}
}
}
|