aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/Model/DocumentationNode.kt
blob: 66ee33e193277bc7c2324c19378ae25c73f06c36 (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
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
package org.jetbrains.dokka

import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import java.util.*
import kotlin.reflect.KClass
import kotlin.reflect.full.primaryConstructor

class DocumentationNodes {

    class Unknown(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class Package(name: String) :
        DocumentationNode(name)

    class Class(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor) {
        override val classLike: Boolean = true
    }

    class Interface(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor) {
        override val classLike: Boolean = true
        override val superclassType: DocumentationNode? = null
    }

    class Enum(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor) {
        override val classLike: Boolean = true
    }

    class AnnotationClass(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor) {
        override val classLike: Boolean = true
    }

    class Exception(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor) {
        override val classLike: Boolean = true
    }

    class EnumItem(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor) {
        override val memberLike = true
    }

    class Object(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor) {
        override val classLike: Boolean = true
    }

    class TypeAlias(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class Constructor(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor) {
        override val memberLike = true
    }

    class Function(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor) {
        override val memberLike = true
    }

    class Property(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor) {
        override val memberLike = true
    }

    class Field(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor) {
        override val memberLike = true
    }

    class CompanionObjectProperty(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor) {
        override val memberLike = true
    }

    class CompanionObjectFunction(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor) {
        override val memberLike = true
    }

    class Parameter(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class Receiver(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class TypeParameter(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class Type(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class Supertype(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor) {
        override val superclassType: DocumentationNode? =
            (links + listOfNotNull(externalType)).firstOrNull { it.classLike }?.superclassType
    }

    class UpperBound(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class LowerBound(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class TypeAliasUnderlyingType(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class NullabilityModifier(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class Module(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class ExternalClass(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class Annotation(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class Value(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class SourceUrl(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class SourcePosition(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class ExternalLink(name: String) : DocumentationNode(name)

    class QualifiedName(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class Platform(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class AllTypes(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)

    class OverloadGroupNote(name: String, descriptor: DeclarationDescriptor?) :
        DocumentationNode(name, descriptor)
}

abstract class DocumentationNode(
    var name: String,
    val descriptor: DeclarationDescriptor? = null
) {

    private val references = LinkedHashSet<DocumentationReference>()

    open val classLike = false

    open val memberLike = false

    val owner: DocumentationNode?
        get() = references(RefKind.Owner).singleOrNull()?.to
    val details: List<DocumentationNode>
        get() = references(RefKind.Detail).map { it.to }
    val members: List<DocumentationNode>
        get() = references(RefKind.Member).map { it.to }
    val origins: List<DocumentationNode>
        get() = references(RefKind.Origin).map { it.to }

    val inheritedMembers: List<DocumentationNode>
        get() = references(RefKind.InheritedMember).map { it.to }
    val allInheritedMembers: List<DocumentationNode>
        get() = recursiveInheritedMembers()
    val inheritedCompanionObjectMembers: List<DocumentationNode>
        get() = references(RefKind.InheritedCompanionObjectMember).map { it.to }
    val extensions: List<DocumentationNode>
        get() = references(RefKind.Extension).map { it.to }
    val inheritors: List<DocumentationNode>
        get() = references(RefKind.Inheritor).map { it.to }
    val overrides: List<DocumentationNode>
        get() = references(RefKind.Override).map { it.to }
    val links: List<DocumentationNode>
        get() = references(RefKind.Link).map { it.to }
    val hiddenLinks: List<DocumentationNode>
        get() = references(RefKind.HiddenLink).map { it.to }
    val annotations: List<DocumentationNode>
        get() = references(RefKind.Annotation).map { it.to }
    val deprecation: DocumentationNode?
        get() = references(RefKind.Deprecation).singleOrNull()?.to
    val platforms: List<String>
        get() = references(RefKind.Platform).map { it.to.name }
    val externalType: DocumentationNode?
        get() = references(RefKind.ExternalType).map { it.to }.firstOrNull()

    open val superclassType: DocumentationNode?
        get() = if (classLike) {
            supertypes.firstOrNull {
                (it.links + listOfNotNull(it.externalType)).any { it.isSuperclassFor(this) }
            }
        } else null

    val superclassTypeSequence: Sequence<DocumentationNode>
        get() = generateSequence(superclassType) {
            it.superclassType
        }

    // TODO: Should we allow node mutation? Model merge will copy by ref, so references are transparent, which could nice
    fun addReferenceTo(to: DocumentationNode, kind: RefKind) {
        references.add(DocumentationReference(this, to, kind))
    }

    fun addReference(reference: DocumentationReference) {
        references.add(reference)
    }

    fun dropReferences(predicate: (DocumentationReference) -> Boolean) {
        references.removeAll(predicate)
    }

    fun addAllReferencesFrom(other: DocumentationNode) {
        references.addAll(other.references)
    }

    private fun Collection<DocumentationNode>.filterByKind(kind: KClass<out DocumentationNode>) =
        filter { node: DocumentationNode -> node::class == kind }

    private fun Collection<DocumentationNode>.singleByKind(kind: KClass<out DocumentationNode>) =
        single { node: DocumentationNode -> node::class == kind }

    fun details(kind: KClass<out DocumentationNode>) = details.filterByKind(kind)
    fun members(kind: KClass<out DocumentationNode>) = members.filterByKind(kind)
    fun inheritedMembers(kind: KClass<out DocumentationNode>): List<DocumentationNode> =
        inheritedMembers.filterByKind(kind)

    fun inheritedCompanionObjectMembers(kind: KClass<out DocumentationNode>): List<DocumentationNode> =
        inheritedCompanionObjectMembers.filterByKind(kind)

    fun links(kind: KClass<out DocumentationNode>): List<DocumentationNode> = links.filterByKind(kind)

    fun detail(kind: KClass<out DocumentationNode>): DocumentationNode = details.singleByKind(kind)
    fun detailOrNull(kind: KClass<out DocumentationNode>): DocumentationNode? =
        details.singleOrNull { it::class == kind }

    fun member(kind: KClass<out DocumentationNode>): DocumentationNode = members.singleByKind(kind)
    fun link(kind: KClass<out DocumentationNode>): DocumentationNode = links.singleByKind(kind)


    fun references(kind: RefKind): List<DocumentationReference> = references.filter { it.kind == kind }
    fun allReferences(): Set<DocumentationReference> = references

    override fun toString(): String {
        return "${javaClass.name}:$name"
    }
}

fun KClass<out DocumentationNode>.createNode(name: String, descriptor: DeclarationDescriptor? = null) =
    primaryConstructor?.call(name, descriptor)
            ?: throw IllegalArgumentException("Cannot create node of type ${this::class}: invalid primary constructor")

val DocumentationNode.supertypes: List<DocumentationNode>
    get() = details(DocumentationNodes.Supertype::class)

class DocumentationModule(name: String) : DocumentationNode(name)

val DocumentationNode.path: List<DocumentationNode>
    get() {
        val parent = owner ?: return listOf(this)
        return parent.path + this
    }

fun DocumentationNode.append(child: DocumentationNode, kind: RefKind) {
    addReferenceTo(child, kind)
    when (kind) {
        RefKind.Detail -> child.addReferenceTo(this, RefKind.Owner)
        RefKind.Member -> child.addReferenceTo(this, RefKind.Owner)
        RefKind.Owner -> child.addReferenceTo(this, RefKind.Member)
        RefKind.Origin -> child.addReferenceTo(this, RefKind.Owner)
        else -> { /* Do not add any links back for other types */
        }
    }
}

fun DocumentationNode.appendTextNode(
    text: String,
    kind: KClass<out DocumentationNode>,
    descriptor: DeclarationDescriptor? = null,
    refKind: RefKind = RefKind.Detail
) {
    append(kind.createNode(text, descriptor), refKind)
}

fun DocumentationNode.qualifiedName(): String {
    if (this is DocumentationNodes.Type) {
        return qualifiedNameFromType()
    } else if (this is DocumentationNodes.Package) {
        return name
    }
    return path.dropWhile { it is DocumentationNodes.Module }.map { it.name }.filter { it.isNotEmpty() }
        .joinToString(".")
}

fun DocumentationNode.simpleName() = name.substringAfterLast('.')

private fun DocumentationNode.recursiveInheritedMembers(): List<DocumentationNode> {
    val allInheritedMembers = mutableListOf<DocumentationNode>()
    recursiveInheritedMembers(allInheritedMembers)
    return allInheritedMembers
}

private fun DocumentationNode.recursiveInheritedMembers(allInheritedMembers: MutableList<DocumentationNode>) {
    allInheritedMembers.addAll(inheritedMembers)
    System.out.println(allInheritedMembers.size)
    inheritedMembers.groupBy { it.owner!! }.forEach { (node, _) ->
        node.recursiveInheritedMembers(allInheritedMembers)
    }
}

private fun DocumentationNode.isSuperclassFor(node: DocumentationNode): Boolean {
    return when (node) {
        is DocumentationNodes.Object,
        is DocumentationNodes.Class,
        is DocumentationNodes.Enum -> this is DocumentationNodes.Class
        is DocumentationNodes.Exception -> this is DocumentationNodes.Class || this is DocumentationNodes.Exception
        else -> false
    }
}

fun DocumentationNode.classNodeNameWithOuterClass(): String {
    assert(classLike)
    return path.dropWhile { it is DocumentationNodes.Package || it is DocumentationNodes.Module }
        .joinToString(separator = ".") { it.name }
}