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

interface ContentNode {
    val textLength: Int
}

object ContentEmpty : ContentNode {
    override val textLength: Int get() = 0
}

open class ContentBlock() : ContentNode {
    open val children = arrayListOf<ContentNode>()

    fun append(node: ContentNode)  {
        children.add(node)
    }

    fun isEmpty() = children.isEmpty()

    override fun equals(other: Any?): Boolean =
        other is ContentBlock && javaClass == other.javaClass && children == other.children

    override fun hashCode(): Int =
        children.hashCode()

    override val textLength: Int
        get() = children.sumBy { it.textLength }
}

class NodeRenderContent(
    val node: DocumentationNode,
    val mode: LanguageService.RenderMode
): ContentNode {
    override val textLength: Int
        get() = 0 //TODO: Clarify?
}

class LazyContentBlock(private val fillChildren: (ContentBlock) -> Unit) : ContentBlock() {
    private var computed = false
    override val children: ArrayList<ContentNode>
        get() {
            if (!computed) {
                computed = true
                fillChildren(this)
            }
            return super.children
        }

    override fun equals(other: Any?): Boolean {
        return other is LazyContentBlock && other.fillChildren == fillChildren && super.equals(other)
    }

    override fun hashCode(): Int {
        return super.hashCode() + 31 * fillChildren.hashCode()
    }
}

enum class IdentifierKind {
    TypeName,
    ParameterName,
    AnnotationName,
    SummarizedTypeName,
    Other
}

data class ContentText(val text: String) : ContentNode {
    override val textLength: Int
        get() = text.length
}

data class ContentKeyword(val text: String) : ContentNode {
    override val textLength: Int
        get() = text.length
}

data class ContentIdentifier(val text: String,
                             val kind: IdentifierKind = IdentifierKind.Other,
                             val signature: String? = null) : ContentNode {
    override val textLength: Int
        get() = text.length
}

data class ContentSymbol(val text: String) : ContentNode {
    override val textLength: Int
        get() = text.length
}

data class ContentEntity(val text: String) : ContentNode {
    override val textLength: Int
        get() = text.length
}

object ContentNonBreakingSpace: ContentNode {
    override val textLength: Int
        get() = 1
}

object ContentSoftLineBreak: ContentNode {
    override val textLength: Int
        get() = 0
}

object ContentIndentedSoftLineBreak: ContentNode {
    override val textLength: Int
        get() = 0
}
class ScriptBlock(val type: String?, val src: String) : ContentBlock()

class ContentParagraph() : ContentBlock()
class ContentEmphasis() : ContentBlock()
class ContentStrong() : ContentBlock()
class ContentStrikethrough() : ContentBlock()
class ContentCode() : ContentBlock()

class ContentDescriptionList() : ContentBlock()
class ContentDescriptionTerm() : ContentBlock()
class ContentDescriptionDefinition() : ContentBlock()

class ContentTable() : ContentBlock()
class ContentTableBody() : ContentBlock()
class ContentTableRow() : ContentBlock()
class ContentTableHeader(val colspan: String? = null, val rowspan: String? = null) : ContentBlock()
class ContentTableCell(val colspan: String? = null, val rowspan: String? = null) : ContentBlock()

class ContentSpecialReference() : ContentBlock()

open class ContentBlockCode(val language: String = "") : ContentBlock()
class ContentBlockSampleCode(language: String = "kotlin", val importsBlock: ContentBlockCode = ContentBlockCode(language)) : ContentBlockCode(language)

abstract class ContentNodeLink() : ContentBlock() {
    abstract val node: DocumentationNode?
}

object ContentHardLineBreak : ContentNode {
    override val textLength: Int
        get() = 0
}

class ContentNodeDirectLink(override val node: DocumentationNode): ContentNodeLink() {
    override fun equals(other: Any?): Boolean =
            super.equals(other) && other is ContentNodeDirectLink && node.name == other.node.name

    override fun hashCode(): Int =
            children.hashCode() * 31 + node.name.hashCode()
}

class ContentNodeLazyLink(val linkText: String, val lazyNode: () -> DocumentationNode?): ContentNodeLink() {
    override val node: DocumentationNode? get() = lazyNode()

    override fun equals(other: Any?): Boolean =
            super.equals(other) && other is ContentNodeLazyLink && linkText == other.linkText

    override fun hashCode(): Int =
            children.hashCode() * 31 + linkText.hashCode()
}

class ContentExternalLink(val href : String) : ContentBlock() {
    override fun equals(other: Any?): Boolean =
        super.equals(other) && other is ContentExternalLink && href == other.href

    override fun hashCode(): Int =
        children.hashCode() * 31 + href.hashCode()
}

data class ContentBookmark(val name: String): ContentBlock()
data class ContentLocalLink(val href: String) : ContentBlock()

class ContentUnorderedList() : ContentBlock()
class ContentOrderedList() : ContentBlock()
class ContentListItem() : ContentBlock()

class ContentHeading(val level: Int) : ContentBlock()

class ContentSection(val tag: String, val subjectName: String?) : ContentBlock() {
    override fun equals(other: Any?): Boolean =
        super.equals(other) && other is ContentSection && tag == other.tag && subjectName == other.subjectName

    override fun hashCode(): Int =
        children.hashCode() * 31 * 31 + tag.hashCode() * 31 + (subjectName?.hashCode() ?: 0)
}

object ContentTags {
    val Description = "Description"
    val SeeAlso = "See Also"
    val Return = "Return"
    val Exceptions = "Exceptions"
    val Parameters = "Parameters"
}

fun content(body: ContentBlock.() -> Unit): ContentBlock {
    val block = ContentBlock()
    block.body()
    return block
}

fun ContentBlock.text(value: String) = append(ContentText(value))
fun ContentBlock.keyword(value: String) = append(ContentKeyword(value))
fun ContentBlock.symbol(value: String) = append(ContentSymbol(value))

fun ContentBlock.identifier(value: String, kind: IdentifierKind = IdentifierKind.Other, signature: String? = null) {
    append(ContentIdentifier(value, kind, signature))
}

fun ContentBlock.nbsp() = append(ContentNonBreakingSpace)
fun ContentBlock.softLineBreak() = append(ContentSoftLineBreak)
fun ContentBlock.indentedSoftLineBreak() = append(ContentIndentedSoftLineBreak)
fun ContentBlock.hardLineBreak() = append(ContentHardLineBreak)

fun ContentBlock.strong(body: ContentBlock.() -> Unit) {
    val strong = ContentStrong()
    strong.body()
    append(strong)
}

fun ContentBlock.code(body: ContentBlock.() -> Unit) {
    val code = ContentCode()
    code.body()
    append(code)
}

fun ContentBlock.link(to: DocumentationNode, body: ContentBlock.() -> Unit) {
    val block = if (to.kind == NodeKind.ExternalLink)
        ContentExternalLink(to.name)
    else
        ContentNodeDirectLink(to)

    block.body()
    append(block)
}

open class Content(): ContentBlock() {
    open val sections: List<ContentSection> get() = emptyList()
    open val summary: ContentNode get() = ContentEmpty
    open val description: ContentNode get() = ContentEmpty

    fun findSectionByTag(tag: String): ContentSection? =
            sections.firstOrNull { tag.equals(it.tag, ignoreCase = true) }

    companion object {
        val Empty = Content()

        fun of(vararg child: ContentNode): Content {
            val result = MutableContent()
            child.forEach { result.append(it) }
            return result
        }
    }
}

open class MutableContent() : Content() {
    private val sectionList = arrayListOf<ContentSection>()
    override val sections: List<ContentSection>
        get() = sectionList

    fun addSection(tag: String?, subjectName: String?): ContentSection {
        val section = ContentSection(tag ?: "", subjectName)
        sectionList.add(section)
        return section
    }

    override val summary: ContentNode get() = children.firstOrNull() ?: ContentEmpty

    override val description: ContentNode by lazy {
        val descriptionNodes = children.drop(1)
        if (descriptionNodes.isEmpty()) {
            ContentEmpty
        } else {
            val result = ContentSection(ContentTags.Description, null)
            result.children.addAll(descriptionNodes)
            result
        }
    }

    override fun equals(other: Any?): Boolean {
        if (other !is Content)
            return false
        return sections == other.sections && children == other.children
    }

    override fun hashCode(): Int {
        return sections.map { it.hashCode() }.sum()
    }

    override fun toString(): String {
        if (sections.isEmpty())
            return "<empty>"
        return (listOf(summary, description) + sections).joinToString()
    }
}

fun javadocSectionDisplayName(sectionName: String?): String? =
        when(sectionName) {
            "param" -> "Parameters"
            "throws", "exception" -> ContentTags.Exceptions
            else -> sectionName?.capitalize()
        }