package org.jetbrains.dokka.pages import kotlin.reflect.KClass inline fun R.mapTransform(noinline operation: (T) -> T): R = mapTransform(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 -> 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 }