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

import kotlin.reflect.KClass

inline fun <reified T : ContentNode, R : ContentNode> R.mapTransform(noinline operation: (T) -> T): R =
    mapTransform(T::class, operation)

@PublishedApi
@Suppress("UNCHECKED_CAST")
internal fun <T : ContentNode, R : ContentNode> R.mapTransform(type: KClass<T>, operation: (T) -> T): R {
    if (this::class == type) {
        return operation(this as T) as R
    }
    val new = when (this) {
        is ContentGroup -> this.copy(children.map { it.mapTransform(type, operation) })
        is ContentHeader -> this.copy(children.map { it.mapTransform(type, operation) })
        is ContentCodeBlock -> this.copy(children.map { it.mapTransform(type, operation) })
        is ContentCodeInline -> this.copy(children.map { it.mapTransform(type, operation) })
        is ContentTable -> this.copy(children.map { it.mapTransform(type, operation) })
        is ContentList -> this.copy(children.map { it.mapTransform(type, operation) })
        is ContentDivergentGroup -> this.copy(children.map { it.mapTransform(type, operation) })
        is ContentDivergentInstance -> this.copy(
            before = before?.mapTransform(type, operation),
            divergent = divergent.mapTransform(type, operation),
            after = after?.mapTransform(type, operation)
        )
        is PlatformHintedContent -> this.copy(inner.mapTransform(type, operation))
        else -> this
    }
    return new as R
}