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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
package org.jetbrains.dokka.model
import com.intellij.psi.PsiNamedElement
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.model.doc.DocumentationNode
import org.jetbrains.dokka.model.properties.ExtraProperty
import org.jetbrains.dokka.model.properties.MergeStrategy
import org.jetbrains.dokka.model.properties.PropertyContainer
import org.jetbrains.dokka.model.properties.WithExtraProperties
import org.jetbrains.dokka.pages.PlatformData
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.load.kotlin.toSourceElement
abstract class Documentable {
abstract val name: String?
abstract val dri: DRI
abstract val children: List<Documentable>
abstract val documentation: PlatformDependent<DocumentationNode>
abstract val platformData: List<PlatformData>
override fun toString(): String =
"${javaClass.simpleName}($dri)"
override fun equals(other: Any?) =
other is Documentable && this.dri == other.dri // TODO: https://github.com/Kotlin/dokka/pull/667#discussion_r382555806
override fun hashCode() = dri.hashCode()
val briefDocTagString: String by lazy {
// TODO > utils
documentation.values
.firstOrNull()
?.children
?.firstOrNull()
?.root
?.docTagSummary()
?.shorten(40) ?: ""
}
}
data class PlatformDependent<out T>(
val map: Map<PlatformData, T>,
val expect: T? = null
) : Map<PlatformData, T> by map {
val prevalentValue: T?
get() = map.values.distinct().singleOrNull()
companion object {
fun <T> empty(): PlatformDependent<T> = PlatformDependent(emptyMap())
fun <T> from(platformData: PlatformData, element: T) = PlatformDependent(mapOf(platformData to element))
}
}
interface WithExpectActual {
val actual: PlatformDependent<DocumentableSource>
}
interface WithScope {
val functions: List<Function>
val properties: List<Property>
val classlikes: List<Classlike>
}
interface WithPackages {
val packages: List<Package>
}
interface WithVisibility {
val visibility: PlatformDependent<Visibility> // TODO custom visibility
}
interface WithType {
val type: TypeWrapper
}
interface WithAbstraction {
val modifier: Modifier?
enum class Modifier {
Abstract, Open, Final, Sealed, Empty
}
}
interface WithCompanion {
val companion: Object?
}
interface WithConstructors {
val constructors: List<Function>
}
interface WithGenerics {
val generics: List<TypeParameter>
}
interface WithSupertypes {
val supertypes: PlatformDependent<List<DRI>>
}
interface Callable : WithVisibility, WithType, WithAbstraction, WithExpectActual {
val receiver: Parameter?
}
abstract class Classlike : Documentable(), WithScope, WithVisibility, WithExpectActual
data class Module(
override val name: String,
override val packages: List<Package>,
override val documentation: PlatformDependent<DocumentationNode>,
override val platformData: List<PlatformData>,
override val extra: PropertyContainer<Module> = PropertyContainer.empty()
) : Documentable(), WithPackages, WithExtraProperties<Module> {
override val dri: DRI = DRI.topLevel
override val children: List<Documentable>
get() = packages
override fun withNewExtras(newExtras: PropertyContainer<Module>) = copy(extra = newExtras)
}
data class Package(
override val dri: DRI,
override val functions: List<Function>,
override val properties: List<Property>,
override val classlikes: List<Classlike>,
override val packages: List<Package>,
override val documentation: PlatformDependent<DocumentationNode>,
override val platformData: List<PlatformData>,
override val extra: PropertyContainer<Package> = PropertyContainer.empty()
) : Documentable(), WithScope, WithPackages, WithExtraProperties<Package> {
override val name = dri.packageName.orEmpty()
override val children: List<Documentable>
get() = (properties + functions + classlikes + packages) as List<Documentable>
override fun withNewExtras(newExtras: PropertyContainer<Package>) = copy(extra = newExtras)
}
data class Class(
override val dri: DRI,
override val name: String,
override val constructors: List<Function>,
override val functions: List<Function>,
override val properties: List<Property>,
override val classlikes: List<Classlike>,
override val actual: PlatformDependent<DocumentableSource>,
override val visibility: PlatformDependent<Visibility>,
override val companion: Object?,
override val generics: List<TypeParameter>,
override val supertypes: PlatformDependent<List<DRI>>,
override val documentation: PlatformDependent<DocumentationNode>,
override val modifier: WithAbstraction.Modifier?,
override val platformData: List<PlatformData>,
override val extra: PropertyContainer<Class> = PropertyContainer.empty()
) : Classlike(), WithAbstraction, WithCompanion, WithConstructors, WithGenerics, WithSupertypes,
WithExtraProperties<Class> {
override val children: List<Documentable>
get() = (functions + properties + classlikes + listOfNotNull(companion) + constructors) as List<Documentable>
override fun withNewExtras(newExtras: PropertyContainer<Class>) = copy(extra = newExtras)
}
data class Enum(
override val dri: DRI,
override val name: String,
val entries: List<EnumEntry>,
override val documentation: PlatformDependent<DocumentationNode>,
override val actual: PlatformDependent<DocumentableSource>,
override val functions: List<Function>,
override val properties: List<Property>,
override val classlikes: List<Classlike>,
override val visibility: PlatformDependent<Visibility>,
override val companion: Object?,
override val constructors: List<Function>,
override val supertypes: PlatformDependent<List<DRI>>,
override val platformData: List<PlatformData>,
override val extra: PropertyContainer<Enum> = PropertyContainer.empty()
) : Classlike(), WithCompanion, WithConstructors, WithSupertypes, WithExtraProperties<Enum> {
override val children: List<Documentable>
get() = (entries + functions + properties + classlikes + listOfNotNull(companion) + constructors) as List<Documentable>
override fun withNewExtras(newExtras: PropertyContainer<Enum>) = copy(extra = newExtras)
}
data class EnumEntry(
override val dri: DRI,
override val name: String?,
override val documentation: PlatformDependent<DocumentationNode>,
override val functions: List<Function>,
override val properties: List<Property>,
override val classlikes: List<Classlike>,
override val platformData: List<PlatformData>,
override val extra: PropertyContainer<EnumEntry> = PropertyContainer.empty()
) : Documentable(), WithScope, WithExtraProperties<EnumEntry> {
override val children: List<Documentable>
get() = (functions + properties + classlikes) as List<Documentable>
override fun withNewExtras(newExtras: PropertyContainer<EnumEntry>) = copy(extra = newExtras)
}
data class Function(
override val dri: DRI,
override val name: String,
val isConstructor: Boolean,
val parameters: List<Parameter>,
override val documentation: PlatformDependent<DocumentationNode>,
override val actual: PlatformDependent<DocumentableSource>,
override val visibility: PlatformDependent<Visibility>,
override val type: TypeWrapper,
override val generics: List<TypeParameter>,
override val receiver: Parameter?,
override val modifier: WithAbstraction.Modifier?,
override val platformData: List<PlatformData>,
override val extra: PropertyContainer<Function> = PropertyContainer.empty()
) : Documentable(), Callable, WithGenerics, WithExtraProperties<Function> {
override val children: List<Documentable>
get() = parameters
override fun withNewExtras(newExtras: PropertyContainer<Function>) = copy(extra = newExtras)
}
data class Interface(
override val dri: DRI,
override val name: String?,
override val documentation: PlatformDependent<DocumentationNode>,
override val actual: PlatformDependent<DocumentableSource>,
override val functions: List<Function>,
override val properties: List<Property>,
override val classlikes: List<Classlike>,
override val visibility: PlatformDependent<Visibility>,
override val companion: Object?,
override val generics: List<TypeParameter>,
override val supertypes: PlatformDependent<List<DRI>>,
override val platformData: List<PlatformData>,
override val extra: PropertyContainer<Interface> = PropertyContainer.empty()
) : Classlike(), WithCompanion, WithGenerics, WithSupertypes, WithExtraProperties<Interface> {
override val children: List<Documentable>
get() = (functions + properties + classlikes + listOfNotNull(companion)) as List<Documentable>
override fun withNewExtras(newExtras: PropertyContainer<Interface>) = copy(extra = newExtras)
}
data class Object(
override val name: String?,
override val dri: DRI,
override val documentation: PlatformDependent<DocumentationNode>,
override val actual: PlatformDependent<DocumentableSource>,
override val functions: List<Function>,
override val properties: List<Property>,
override val classlikes: List<Classlike>,
override val visibility: PlatformDependent<Visibility>,
override val supertypes: PlatformDependent<List<DRI>>,
override val platformData: List<PlatformData>,
override val extra: PropertyContainer<Object> = PropertyContainer.empty()
) : Classlike(), WithSupertypes, WithExtraProperties<Object> {
override val children: List<Documentable>
get() = (functions + properties + classlikes) as List<Documentable>
override fun withNewExtras(newExtras: PropertyContainer<Object>) = copy(extra = newExtras)
}
data class Annotation(
override val name: String?,
override val dri: DRI,
override val documentation: PlatformDependent<DocumentationNode>,
override val actual: PlatformDependent<DocumentableSource>,
override val functions: List<Function>,
override val properties: List<Property>,
override val classlikes: List<Classlike>,
override val visibility: PlatformDependent<Visibility>,
override val companion: Object?,
override val constructors: List<Function>,
override val platformData: List<PlatformData>,
override val extra: PropertyContainer<Annotation> = PropertyContainer.empty()
) : Classlike(), WithCompanion, WithConstructors, WithExtraProperties<Annotation> {
override val children: List<Documentable>
get() = (functions + properties + classlikes + constructors + listOfNotNull(companion)) as List<Documentable>
override fun withNewExtras(newExtras: PropertyContainer<Annotation>) = copy(extra = newExtras)
}
data class Property(
override val dri: DRI,
override val name: String,
override val documentation: PlatformDependent<DocumentationNode>,
override val actual: PlatformDependent<DocumentableSource>,
override val visibility: PlatformDependent<Visibility>,
override val type: TypeWrapper,
override val receiver: Parameter?,
val setter: Function?,
val getter: Function?,
override val modifier: WithAbstraction.Modifier?,
override val platformData: List<PlatformData>,
override val extra: PropertyContainer<Property> = PropertyContainer.empty()
) : Documentable(), Callable, WithExtraProperties<Property> {
override val children: List<Nothing>
get() = emptyList()
override fun withNewExtras(newExtras: PropertyContainer<Property>) = copy(extra = newExtras)
}
// TODO: treat named Parameters and receivers differently
data class Parameter(
override val dri: DRI,
override val name: String?,
override val documentation: PlatformDependent<DocumentationNode>,
val type: TypeWrapper,
override val platformData: List<PlatformData>,
override val extra: PropertyContainer<Parameter> = PropertyContainer.empty()
) : Documentable(), WithExtraProperties<Parameter> {
override val children: List<Nothing>
get() = emptyList()
override fun withNewExtras(newExtras: PropertyContainer<Parameter>) = copy(extra = newExtras)
}
data class TypeParameter(
override val dri: DRI,
override val name: String,
override val documentation: PlatformDependent<DocumentationNode>,
val bounds: List<Projection>,
override val platformData: List<PlatformData>,
override val extra: PropertyContainer<TypeParameter> = PropertyContainer.empty()
) : Documentable(), WithExtraProperties<TypeParameter> {
override val children: List<Nothing>
get() = emptyList()
override fun withNewExtras(newExtras: PropertyContainer<TypeParameter>) = copy(extra = newExtras)
}
sealed class Projection {
data class OtherParameter(val name: String) : Projection()
object Star : Projection()
data class TypeConstructor(val dri: DRI, val projections: List<Projection>) : Projection()
data class Nullable(val inner: Projection) : Projection()
}
private fun String.shorten(maxLength: Int) = lineSequence().first().let {
if (it.length != length || it.length > maxLength) it.take(maxLength - 3) + "..." else it
}
fun Documentable.dfs(predicate: (Documentable) -> Boolean): Documentable? =
if (predicate(this)) {
this
} else {
this.children.asSequence().mapNotNull { it.dfs(predicate) }.firstOrNull()
}
fun <T> PlatformDependent<T>?.orEmpty(): PlatformDependent<T> = this ?: PlatformDependent.empty()
sealed class DocumentableSource(val path: String)
class DescriptorDocumentableSource(val descriptor: DeclarationDescriptor) :
DocumentableSource(descriptor.toSourceElement.containingFile.toString())
class PsiDocumentableSource(val psi: PsiNamedElement) : DocumentableSource(psi.containingFile.virtualFile.path)
|