package org.jetbrains.dokka.plugability import org.jetbrains.dokka.DokkaConfiguration class ExtensionBuilderStart internal constructor(){ fun extensionPoint(ext: ExtensionPoint): ProvidedExtension = ProvidedExtension(ext) } class ProvidedExtension internal constructor(val ext: ExtensionPoint){ fun fromInstance(inst: T): ExtensionBuilder = createBuilder( LazyEvaluated.fromInstance( inst ) ) fun fromRecipe(recipe: (DokkaContext) -> T): ExtensionBuilder = createBuilder( LazyEvaluated.fromRecipe(recipe) ) private val defaultName = "${ext.pointName}/in/${javaClass.simpleName}" private fun createBuilder(action: LazyEvaluated) = ExtensionBuilder(defaultName, ext, action, emptyList(), emptyList(), OverrideKind.None, emptyList()) } data class ExtensionBuilder internal constructor( private val name: String, private val ext: ExtensionPoint, private val action: LazyEvaluated, private val before: List>, private val after: List>, private val override: OverrideKind = OverrideKind.None, private val conditions: List<(DokkaConfiguration) -> Boolean> ){ fun build(): Extension = Extension( ext, javaClass.name, name, action, OrderingKind.ByDsl { before(*before.toTypedArray()) after(*after.toTypedArray()) }, override, conditions ) fun overrideExtension(extension: Extension) = copy(override = OverrideKind.Present(listOf(extension))) fun newOrdering(before: Array>, after: Array>) = copy(before = this.before + before, after = this.after + after) fun before(vararg exts: Extension<*, *, *>) = copy(before = this.before + exts) fun after(vararg exts: Extension<*, *, *>) = copy(after = this.after + exts) fun addCondition(c: (DokkaConfiguration) -> Boolean) = copy(conditions = conditions + c) fun name(name: String) = copy(name = name) } abstract class DokkaJavaPlugin: DokkaPlugin() { fun plugin(clazz: Class): T = context?.plugin(clazz.kotlin) ?: throwIllegalQuery() fun extend(func: (ExtensionBuilderStart) -> ExtensionBuilder): Lazy> = lazy { func(ExtensionBuilderStart()).build() }.also { unsafeInstall(it) } }