package org.jetbrains.dokka.plugability import kotlin.properties.ReadOnlyProperty import kotlin.reflect.KProperty import kotlin.reflect.KProperty1 import kotlin.reflect.KTypeProjection import kotlin.reflect.full.createInstance import kotlin.reflect.full.createType import kotlin.reflect.full.declaredMemberProperties import kotlin.reflect.jvm.isAccessible import kotlin.reflect.jvm.jvmErasure private typealias ExtensionDelegate = ReadOnlyProperty> abstract class DokkaPlugin { private val extensionDelegates = mutableListOf>() @PublishedApi internal var context: DokkaContext? = null protected open fun install(context: DokkaContext) {} protected inline fun plugin(): T = context?.plugin(T::class) as? T ?: T::class.createInstance().also { it.context = this.context } protected fun extensionPoint() = object : ReadOnlyProperty> { override fun getValue(thisRef: DokkaPlugin, property: KProperty<*>) = ExtensionPoint( thisRef::class.qualifiedName ?: throw AssertionError("Plugin must be named class"), property.name ) } protected fun extending(definition: ExtendingDSL.() -> Extension) = ExtensionProvider(definition) protected class ExtensionProvider internal constructor( private val definition: ExtendingDSL.() -> Extension ) { operator fun provideDelegate(thisRef: DokkaPlugin, property: KProperty<*>) = lazy { ExtendingDSL( thisRef::class.qualifiedName ?: throw AssertionError("Plugin must be named class"), property.name ).definition() }.also { thisRef.extensionDelegates += property } } internal fun internalInstall(ctx: DokkaContext) { context = ctx install(ctx) extensionDelegates.asSequence() .filterIsInstance>>() // always true .map { it.get(this) } .forEach { ctx.addExtension(it) } } }