aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/model/Documentable.kt
blob: f08202562d37c9f37c77f7d12ee7ebb045280518 (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
package org.jetbrains.dokka.model

import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.model.doc.DocumentationNode
import org.jetbrains.dokka.pages.PlatformData
import org.jetbrains.kotlin.descriptors.Visibility

class Module(override val name: String, val packages: List<Package>) : Documentable() {
    override val dri: DRI = DRI.topLevel
    override val children: List<Package> = packages
    override val extra: MutableSet<Extra> = mutableSetOf()
}

class Package(
    override val dri: DRI,
    override val functions: List<Function>,
    override val properties: List<Property>,
    override val classlikes: List<Classlike>,
    override val extra: MutableSet<Extra> = mutableSetOf()
) : ScopeNode() {
    override val name = dri.packageName.orEmpty()
}

class Class(
    override val dri: DRI,
    override val name: String,
    override val kind: ClassKind,
    val constructors: List<Function>,
    override val functions: List<Function>,
    override val properties: List<Property>,
    override val classlikes: List<Classlike>,
    override val expected: ClassPlatformInfo?,
    override val actual: List<ClassPlatformInfo>,
    override val extra: MutableSet<Extra> = mutableSetOf(),
    override val visibility: Map<PlatformData, Visibility>
) : Classlike(
    name = name,
    dri = dri,
    kind = kind,
    functions = functions,
    properties = properties,
    classlikes = classlikes,
    expected = expected,
    actual = actual,
    extra = extra
), WithVisibility

class Enum(
    override val dri: DRI,
    override val name: String,
    val entries: List<EnumEntry>,
    val constructors: List<Function>,
    override val functions: List<Function> = emptyList(),
    override val properties: List<Property> = emptyList(),
    override val classlikes: List<Classlike> = emptyList(),
    override val expected: ClassPlatformInfo? = null,
    override val actual: List<ClassPlatformInfo>,
    override val extra: MutableSet<Extra> = mutableSetOf(),
    override val visibility: Map<PlatformData, Visibility>
) : Classlike(dri = dri, name = name, kind = KotlinClassKindTypes.ENUM_CLASS, actual = actual), WithVisibility {
    constructor(c: Classlike, entries: List<EnumEntry>, ctors: List<Function>) : this(
        dri = c.dri,
        name = c.name,
        entries = entries,
        constructors = ctors,
        functions = c.functions,
        properties = c.properties,
        classlikes = c.classlikes,
        expected = c.expected,
        actual = c.actual,
        extra = c.extra,
        visibility = c.visibility
    )

    override val children: List<Documentable>
        get() = entries
}

class EnumEntry(
    override val dri: DRI,
    override val name: String,
    override val expected: ClassPlatformInfo? = null,
    override val actual: List<ClassPlatformInfo>,
    override val extra: MutableSet<Extra> = mutableSetOf(),
    override val visibility: Map<PlatformData, Visibility>
) : Classlike(
    dri = dri,
    name = name,
    actual = actual,
    expected = expected,
    extra = extra,
    kind = KotlinClassKindTypes.ENUM_ENTRY
) {
    constructor(c: Classlike) : this(
        dri = c.dri,
        name = c.name,
        actual = c.actual,
        expected = c.expected,
        extra = c.extra,
        visibility = c.visibility
    )

    override val children: List<Parameter>
        get() = emptyList()
}

class Function(
    override val dri: DRI,
    override val name: String,
    val returnType: TypeWrapper?,
    val isConstructor: Boolean,
    override val receiver: Parameter?,
    val parameters: List<Parameter>,
    override val expected: PlatformInfo?,
    override val actual: List<PlatformInfo>,
    override val extra: MutableSet<Extra> = mutableSetOf(),
    override val visibility: Map<PlatformData, Visibility>
) : CallableNode(), WithVisibility {
    override val children: List<Parameter>
        get() = listOfNotNull(receiver) + parameters
}

class Property(
    override val dri: DRI,
    override val name: String,
    override val receiver: Parameter?,
    override val expected: PlatformInfo?,
    override val actual: List<PlatformInfo>,
    override val extra: MutableSet<Extra> = mutableSetOf(),
    val accessors: List<Function>,
    override val visibility: Map<PlatformData, Visibility>
) : CallableNode(), WithVisibility {
    override val children: List<Parameter>
        get() = listOfNotNull(receiver)
}

// TODO: treat named Parameters and receivers differently
class Parameter(
    override val dri: DRI,
    override val name: String?,
    val type: TypeWrapper,
    override val expected: PlatformInfo?,
    override val actual: List<PlatformInfo>,
    override val extra: MutableSet<Extra> = mutableSetOf()
) : Documentable() {
    override val children: List<Documentable>
        get() = emptyList()
}

interface PlatformInfo {
    val documentationNode: DocumentationNode
    val platformData: List<PlatformData>
}

class BasePlatformInfo(
    override val documentationNode: DocumentationNode,
    override val platformData: List<PlatformData>
) : PlatformInfo {

    override fun equals(other: Any?): Boolean =
        other is PlatformInfo && documentationNode == other.documentationNode

    override fun hashCode(): Int =
        documentationNode.hashCode()
}

class ClassPlatformInfo(
    val info: PlatformInfo,
    val inherited: List<DRI>
) : PlatformInfo by info

abstract class Documentable {
    open val expected: PlatformInfo? = null
    open val actual: List<PlatformInfo> = emptyList()
    open val name: String? = null
    val platformInfo by lazy { listOfNotNull(expected) + actual }
    val platformData by lazy { platformInfo.flatMap { it.platformData }.toSet() }
    abstract val dri: DRI

    abstract val children: List<Documentable>

    override fun toString(): String {
        return "${javaClass.simpleName}($dri)" + briefDocTagString.takeIf { it.isNotBlank() }?.let { " [$it]" }.orEmpty()
    }

    override fun equals(other: Any?) = other is Documentable && this.dri == other.dri

    override fun hashCode() = dri.hashCode()

    val briefDocTagString: String by lazy {
        platformInfo
            .firstOrNull()
            ?.documentationNode
            ?.children
            ?.firstOrNull()
            ?.root
            ?.docTagSummary()
            ?.shorten(40) ?: ""
    }

    open val extra: MutableSet<Extra> = mutableSetOf()
}

abstract class Classlike(
    override val dri: DRI,
    override val name: String,
    open val kind: ClassKind,
    override val functions: List<Function> = emptyList(),
    override val properties: List<Property> = emptyList(),
    override val classlikes: List<Classlike> = emptyList(),
    override val expected: ClassPlatformInfo? = null,
    override val actual: List<ClassPlatformInfo>,
    override val extra: MutableSet<Extra> = mutableSetOf()
) : ScopeNode(), WithVisibility {
    val inherited by lazy { platformInfo.mapNotNull { (it as? ClassPlatformInfo)?.inherited }.flatten() }
}

abstract class ScopeNode : Documentable() {
    abstract val functions: List<Function>
    abstract val properties: List<Property>
    abstract val classlikes: List<Classlike>

    override val children: List<Documentable> // It is written so awkwardly because of type inference being lost here
        get() = mutableListOf<Documentable>().apply {
            addAll(functions)
            addAll(properties)
            addAll(classlikes)
        }
}

abstract class CallableNode : Documentable() {
    abstract val receiver: Parameter?
}

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

interface WithVisibility {
    val visibility: Map<PlatformData, Visibility>
}