diff options
-rw-r--r-- | core/src/main/kotlin/plugability/DokkaContext.kt | 12 | ||||
-rw-r--r-- | core/src/main/kotlin/plugability/DokkaPlugin.kt | 11 |
2 files changed, 14 insertions, 9 deletions
diff --git a/core/src/main/kotlin/plugability/DokkaContext.kt b/core/src/main/kotlin/plugability/DokkaContext.kt index 7727329c..a5bcd695 100644 --- a/core/src/main/kotlin/plugability/DokkaContext.kt +++ b/core/src/main/kotlin/plugability/DokkaContext.kt @@ -5,6 +5,7 @@ import java.io.File import java.net.URLClassLoader import java.util.* import kotlin.reflect.KClass +import kotlin.reflect.full.createInstance interface DokkaContext { operator fun <T : Any, E : ExtensionPoint<T>> get(point: E): List<Extension<T>> @@ -35,13 +36,18 @@ private class DokkaContextConfigurationImpl( ) : DokkaContext, DokkaContextConfiguration { private val plugins = mutableMapOf<KClass<*>, DokkaPlugin>() + private val pluginStubs = mutableMapOf<KClass<*>, DokkaPlugin>() + internal val extensions = mutableMapOf<ExtensionPoint<*>, MutableList<Extension<*>>>() @Suppress("UNCHECKED_CAST") override operator fun <T : Any, E : ExtensionPoint<T>> get(point: E) = extensions[point] as List<Extension<T>> @Suppress("UNCHECKED_CAST") - override fun <T : DokkaPlugin> plugin(kclass: KClass<T>) = plugins[kclass] as T + override fun <T : DokkaPlugin> plugin(kclass: KClass<T>) = (plugins[kclass] ?: pluginStubFor(kclass)) as T + + private fun <T : DokkaPlugin> pluginStubFor(kclass: KClass<T>): DokkaPlugin = + pluginStubs.getOrPut(kclass) { kclass.createInstance().also { it.context = this } } fun install(plugin: DokkaPlugin) { plugins[plugin::class] = plugin @@ -59,8 +65,8 @@ private class DokkaContextConfigurationImpl( val loadedListForDebug = extensions.run { keys + values.flatten() }.toList() .joinToString(prefix = "[\n", separator = ",\n", postfix = "\n]") { "\t$it" } - logger.progress("Loaded plugins: ${pluginNames}") - logger.progress("Loaded: ${loadedListForDebug}") + logger.progress("Loaded plugins: $pluginNames") + logger.progress("Loaded: $loadedListForDebug") } } diff --git a/core/src/main/kotlin/plugability/DokkaPlugin.kt b/core/src/main/kotlin/plugability/DokkaPlugin.kt index 663cf88e..7a968b8b 100644 --- a/core/src/main/kotlin/plugability/DokkaPlugin.kt +++ b/core/src/main/kotlin/plugability/DokkaPlugin.kt @@ -3,7 +3,6 @@ package org.jetbrains.dokka.plugability import kotlin.properties.ReadOnlyProperty import kotlin.reflect.KProperty import kotlin.reflect.KProperty1 -import kotlin.reflect.full.createInstance private typealias ExtensionDelegate<T> = ReadOnlyProperty<DokkaPlugin, Extension<T>> @@ -13,8 +12,8 @@ abstract class DokkaPlugin { @PublishedApi internal var context: DokkaContext? = null - protected inline fun <reified T : DokkaPlugin> plugin(): T = - context?.plugin(T::class) ?: T::class.createInstance().also { it.context = this.context } + protected inline fun <reified T : DokkaPlugin> plugin(): T = context?.plugin(T::class) + ?: throw IllegalStateException("Querying about plugins is only possible with dokka context initialised") protected fun <T : Any> extensionPoint() = object : ReadOnlyProperty<DokkaPlugin, ExtensionPoint<T>> { @@ -24,9 +23,9 @@ abstract class DokkaPlugin { ) } - protected fun <T: Any> extending(definition: ExtendingDSL.() -> Extension<T>) = ExtensionProvider(definition) + protected fun <T : Any> extending(definition: ExtendingDSL.() -> Extension<T>) = ExtensionProvider(definition) - protected class ExtensionProvider<T: Any> internal constructor( + protected class ExtensionProvider<T : Any> internal constructor( private val definition: ExtendingDSL.() -> Extension<T> ) { operator fun provideDelegate(thisRef: DokkaPlugin, property: KProperty<*>) = lazy { @@ -39,7 +38,7 @@ abstract class DokkaPlugin { internal fun internalInstall(ctx: DokkaContextConfiguration) { extensionDelegates.asSequence() - .filterIsInstance<KProperty1<DokkaPlugin, Extension<*>>>() // always true + .filterIsInstance<KProperty1<DokkaPlugin, Extension<*>>>() // should be always true .map { it.get(this) } .forEach { ctx.addExtension(it) } } |