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
|
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.pages.PlatformData
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.Visibility
abstract class Documentable {
abstract val name: String?
abstract val dri: DRI
abstract val children: List<Documentable>
abstract val documentation: PlatformDependent<DocumentationNode>
abstract val original: PlatformDependent<Documentable>
override fun toString(): String =
"${javaClass.simpleName}($dri)" //+ briefDocTagString.takeIf { it.isNotBlank() }?.let { " [$it]" }.orEmpty()
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() = if (map.all { values.first() == it.value }) values.first() else null
companion object {
fun <T> empty() = PlatformDependent(mapOf<PlatformData, T>())
}
}
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
}
}
interface WithCompanion {
val companion: Object?
}
interface WithConstructors {
val constructors: List<Function>
}
interface WithGenerics {
val generics: List<TypeParameter>
}
interface Callable : WithVisibility, WithType, WithAbstraction, WithExpectActual {
val receiver: Parameter?
}
abstract class Classlike : Documentable(), WithScope, WithVisibility, WithExpectActual {
abstract val supertypes: PlatformDependent<List<DRI>>
}
class Module(
override val name: String,
override val packages: List<Package>,
override val documentation: PlatformDependent<DocumentationNode>,
override val original: PlatformDependent<Module> = PlatformDependent.empty()
) : Documentable(), WithPackages {
override val dri: DRI = DRI.topLevel
override val children: List<Documentable>
get() = packages
}
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 original: PlatformDependent<Package> = PlatformDependent.empty()
) : Documentable(), WithScope, WithPackages {
override val name = dri.packageName.orEmpty()
override val children: List<Documentable>
get() = (properties + functions + classlikes + packages) as List<Documentable>
}
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 original: PlatformDependent<Class> = PlatformDependent.empty(),
override val modifier: WithAbstraction.Modifier
) : Classlike(), WithAbstraction, WithCompanion, WithConstructors, WithGenerics {
override val children: List<Documentable>
get() = (functions + properties + classlikes + listOfNotNull(companion) + constructors) as List<Documentable>
}
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 original: PlatformDependent<Enum> = PlatformDependent.empty()
) : Classlike(), WithCompanion, WithConstructors {
override val children: List<Documentable>
get() = (entries + functions + properties + classlikes + listOfNotNull(companion) + constructors) as List<Documentable>
}
class EnumEntry(
override val name: String?,
override val dri: DRI,
override val documentation: PlatformDependent<DocumentationNode>,
override val functions: List<Function>,
override val properties: List<Property>,
override val classlikes: List<Classlike>,
override val original: PlatformDependent<EnumEntry> = PlatformDependent.empty()
) : Documentable(), WithScope {
override val children: List<Documentable>
get() = (functions + properties + classlikes) as List<Documentable>
}
class Function(
override val dri: DRI,
override val name: String,
val isConstructor: Boolean,
val returnType: TypeWrapper?,
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 original: PlatformDependent<Function> = PlatformDependent.empty(),
override val modifier: WithAbstraction.Modifier
) : Documentable(), Callable, WithGenerics {
override val children: List<Documentable>
get() = parameters
}
class Interface(
override val name: String?,
override val dri: DRI,
override val documentation: PlatformDependent<DocumentationNode>,
override val original: PlatformDependent<Interface>,
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>>
) : Classlike(), WithCompanion, WithGenerics {
override val children: List<Documentable>
get() = (functions + properties + classlikes + listOfNotNull(companion)) as List<Documentable>
}
class Object(
override val name: String?,
override val dri: DRI,
override val documentation: PlatformDependent<DocumentationNode>,
override val original: PlatformDependent<Object>,
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>>
) : Classlike() {
override val children: List<Documentable>
get() = (functions + properties + classlikes) as List<Documentable>
}
class Annotation(
override val name: String?,
override val dri: DRI,
override val documentation: PlatformDependent<DocumentationNode>,
override val original: PlatformDependent<Annotation>,
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>
) : Documentable(), WithScope, WithVisibility, WithCompanion, WithConstructors, WithExpectActual {
override val children: List<Documentable>
get() = (functions + properties + classlikes + constructors + listOfNotNull(companion)) as List<Documentable>
}
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 accessors: PlatformDependent<Function>, // TODO > extra
override val original: PlatformDependent<Property> = PlatformDependent.empty(),
override val modifier: WithAbstraction.Modifier
) : Documentable(), Callable {
override val children: List<Nothing>
get() = emptyList()
}
// TODO: treat named Parameters and receivers differently
class Parameter(
override val dri: DRI,
override val name: String?,
override val documentation: PlatformDependent<DocumentationNode>,
val type: TypeWrapper,
override val original: PlatformDependent<Parameter>
) : Documentable() {
override val children: List<Nothing>
get() = emptyList()
}
class TypeParameter(
override val dri: DRI,
override val name: String,
override val documentation: PlatformDependent<DocumentationNode>,
override val original: PlatformDependent<TypeParameter> = PlatformDependent.empty(),
val bounds: List<Projection>
): Documentable() {
override val children: List<Nothing>
get() = emptyList()
}
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()
}
interface Extra
object STATIC : Extra
object INHERITED: Extra
sealed class DocumentableSource
class DescriptorDocumentableSource(val descriptor: DeclarationDescriptor): DocumentableSource()
class PsiDocumentableSource(val psi: PsiNamedElement): DocumentableSource()
|