aboutsummaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorPaweł Marks <pmarks@virtuslab.com>2019-11-25 17:45:23 +0100
committerPaweł Marks <pmarks@virtuslab.com>2019-11-26 15:27:04 +0100
commit202845bf4c9e72cfcda578a548f32bc7cc04f64d (patch)
treead2138057800514c6bb347df037b744b15093393 /core/src
parent893dd637e3727d455e82f72ff90e823c6e017f7a (diff)
downloaddokka-202845bf4c9e72cfcda578a548f32bc7cc04f64d.tar.gz
dokka-202845bf4c9e72cfcda578a548f32bc7cc04f64d.tar.bz2
dokka-202845bf4c9e72cfcda578a548f32bc7cc04f64d.zip
Default context for dokka
Diffstat (limited to 'core/src')
-rw-r--r--core/src/main/kotlin/plugability/DokkaContext.kt33
1 files changed, 28 insertions, 5 deletions
diff --git a/core/src/main/kotlin/plugability/DokkaContext.kt b/core/src/main/kotlin/plugability/DokkaContext.kt
index a5bcd695..b917ff99 100644
--- a/core/src/main/kotlin/plugability/DokkaContext.kt
+++ b/core/src/main/kotlin/plugability/DokkaContext.kt
@@ -8,7 +8,8 @@ import kotlin.reflect.KClass
import kotlin.reflect.full.createInstance
interface DokkaContext {
- operator fun <T : Any, E : ExtensionPoint<T>> get(point: E): List<Extension<T>>
+ operator fun <T, E> get(point: E, askDefault: AskDefault = AskDefault.WhenEmpty): List<Extension<T>>
+ where T : Any, E : ExtensionPoint<T>
fun <T : DokkaPlugin> plugin(kclass: KClass<T>): T?
@@ -16,7 +17,7 @@ interface DokkaContext {
companion object {
fun create(pluginsClasspath: Iterable<File>, logger: DokkaLogger): DokkaContext =
- DokkaContextConfigurationImpl(logger).apply {
+ DokkaContextConfigurationImpl(logger, DefaultContext(logger)).apply {
pluginsClasspath.map { it.relativeTo(File(".").absoluteFile).toURI().toURL() }
.toTypedArray()
.let { URLClassLoader(it, this.javaClass.classLoader) }
@@ -32,7 +33,8 @@ interface DokkaContextConfiguration {
}
private class DokkaContextConfigurationImpl(
- override val logger: DokkaLogger
+ override val logger: DokkaLogger,
+ private val defaultContext: DokkaContext?
) : DokkaContext, DokkaContextConfiguration {
private val plugins = mutableMapOf<KClass<*>, DokkaPlugin>()
@@ -41,10 +43,17 @@ private class DokkaContextConfigurationImpl(
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>>
+ override operator fun <T, E> get(point: E, askDefault: AskDefault) where T : Any, E : ExtensionPoint<T> =
+ when (askDefault) {
+ AskDefault.Never -> extensions[point].orEmpty()
+ AskDefault.Always -> extensions[point].orEmpty() + defaultContext?.get(point, askDefault).orEmpty()
+ AskDefault.WhenEmpty ->
+ extensions[point]?.takeIf { it.isNotEmpty() } ?: defaultContext?.get(point, askDefault).orEmpty()
+ } as List<Extension<T>>
@Suppress("UNCHECKED_CAST")
- override fun <T : DokkaPlugin> plugin(kclass: KClass<T>) = (plugins[kclass] ?: pluginStubFor(kclass)) as T
+ override fun <T : DokkaPlugin> plugin(kclass: KClass<T>) =
+ (plugins[kclass] ?: defaultContext?.plugin(kclass) ?: pluginStubFor(kclass)) as T
private fun <T : DokkaPlugin> pluginStubFor(kclass: KClass<T>): DokkaPlugin =
pluginStubs.getOrPut(kclass) { kclass.createInstance().also { it.context = this } }
@@ -78,4 +87,18 @@ private fun checkClasspath(classLoader: URLClassLoader) {
"Please fix your plugins dependencies or exclude dokka api artifact from plugin classpath"
)
}
+}
+
+class DefaultContext(override val logger: DokkaLogger) : DokkaContext {
+ override fun <T : Any, E : ExtensionPoint<T>> get(point: E, askDefault: AskDefault): List<Extension<T>> =
+ when (point) {
+
+ else -> emptyList()
+ }
+
+ override fun <T : DokkaPlugin> plugin(kclass: KClass<T>): Nothing? = null
+}
+
+enum class AskDefault {
+ Always, Never, WhenEmpty
} \ No newline at end of file