All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pages.utils.kt Maven / Gradle / Ivy

Go to download

Dokka is an API documentation engine for Kotlin and Java, performing the same function as Javadoc for Java

There is a newer version: 2.0.0
Show newest version
package org.jetbrains.dokka.pages

import kotlin.reflect.KClass

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

inline fun  R.recursiveMapTransform(noinline operation: (T) -> T): R =
        recursiveMapTransform(T::class, operation)

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy