aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/pages/utils.kt
blob: 5292dea18d68285b9cd980de8a76551ffc4c053c (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
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 ContentCode -> 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
}