diff options
Diffstat (limited to 'core')
8 files changed, 48 insertions, 32 deletions
diff --git a/core/src/main/kotlin/DokkaGenerator.kt b/core/src/main/kotlin/DokkaGenerator.kt index 6a21e031..573ba13e 100644 --- a/core/src/main/kotlin/DokkaGenerator.kt +++ b/core/src/main/kotlin/DokkaGenerator.kt @@ -10,7 +10,6 @@ import org.jetbrains.dokka.pages.PlatformData import org.jetbrains.dokka.pages.RootPageNode import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.plugability.DokkaPlugin -import org.jetbrains.dokka.plugability.single import org.jetbrains.dokka.utilities.DokkaLogger import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation diff --git a/core/src/main/kotlin/plugability/DokkaContext.kt b/core/src/main/kotlin/plugability/DokkaContext.kt index 6a8ac4a3..b4be5862 100644 --- a/core/src/main/kotlin/plugability/DokkaContext.kt +++ b/core/src/main/kotlin/plugability/DokkaContext.kt @@ -17,6 +17,8 @@ interface DokkaContext { operator fun <T, E> get(point: E, askDefault: AskDefault = AskDefault.WhenEmpty): List<T> where T : Any, E : ExtensionPoint<T> + fun <T, E> single(point: E): T where T : Any, E : ExtensionPoint<T> + val logger: DokkaLogger val configuration: DokkaConfiguration val platforms: Map<PlatformData, EnvironmentAndFacade> @@ -41,19 +43,6 @@ interface DokkaContext { } } -fun <T, E> DokkaContext.single(point: E): T where T : Any, E : ExtensionPoint<T> { - fun throwBadArity(substitution: String): Nothing = throw IllegalStateException( - "$point was expected to have exactly one extension registered, but $substitution found." - ) - - val extensions = get(point, AskDefault.WhenEmpty) - return when (extensions.size) { - 0 -> throwBadArity("none was") - 1 -> extensions.first() - else -> throwBadArity("multiple were") - } -} - interface DokkaContextConfiguration { fun addExtensionDependencies(extension: Extension<*>) } @@ -110,6 +99,23 @@ private class DokkaContextConfigurationImpl( actions(point)?.takeIf { it.isNotEmpty() } ?: DefaultExtensions.get(point, this) } as List<T> + @Suppress("UNCHECKED_CAST") + override fun <T, E> single(point: E): T where T : Any, E : ExtensionPoint<T> { + fun throwBadArity(substitution: String): Nothing = throw IllegalStateException( + "$point was expected to have exactly one extension registered, but $substitution found." + ) + + val extensions = extensions[point].orEmpty() as List<Extension<T>> + return when (extensions.size) { + 0 -> DefaultExtensions.get(point, this).single() ?: throwBadArity("none was") + 1 -> extensions.single().action.get(this) + else -> { + val notFallbacks = extensions.filterNot { it.isFallback } + if (notFallbacks.size == 1) notFallbacks.single().action.get(this) else throwBadArity("many were") + } + } + } + private fun <E : ExtensionPoint<*>> actions(point: E) = extensions[point]?.map { it.action.get(this) } @Suppress("UNCHECKED_CAST") diff --git a/core/src/main/kotlin/plugability/DokkaPlugin.kt b/core/src/main/kotlin/plugability/DokkaPlugin.kt index 1fcbd934..cdc92ca5 100644 --- a/core/src/main/kotlin/plugability/DokkaPlugin.kt +++ b/core/src/main/kotlin/plugability/DokkaPlugin.kt @@ -14,7 +14,7 @@ abstract class DokkaPlugin { internal var context: DokkaContext? = null protected inline fun <reified T : DokkaPlugin> plugin(): T = context?.plugin(T::class) - ?: throw IllegalStateException("Querying about plugins is only possible with dokka context initialised") + ?: throw IllegalStateException("Querying about plugins is only possible with dokka context initialised") protected fun <T : Any> extensionPoint() = object : ReadOnlyProperty<DokkaPlugin, ExtensionPoint<T>> { @@ -24,7 +24,12 @@ abstract class DokkaPlugin { ) } - protected fun <T : Any> extending(definition: ExtendingDSL.() -> Extension<T>) = ExtensionProvider(definition) + protected fun <T : Any> extending(isFallback: Boolean = false, definition: ExtendingDSL.() -> Extension<T>) = + if (isFallback) { + ExtensionProvider { definition().markedAsFallback() } + } else { + ExtensionProvider(definition) + } protected class ExtensionProvider<T : Any> internal constructor( private val definition: ExtendingDSL.() -> Extension<T> @@ -41,6 +46,6 @@ abstract class DokkaPlugin { extensionDelegates.asSequence() .filterIsInstance<KProperty1<DokkaPlugin, Extension<*>>>() // should be always true .map { it.get(this) } - .forEach { if(it.condition.invoke(configuration)) ctx.addExtensionDependencies(it) } + .forEach { if (it.condition.invoke(configuration)) ctx.addExtensionDependencies(it) } } }
\ No newline at end of file diff --git a/core/src/main/kotlin/plugability/extensions.kt b/core/src/main/kotlin/plugability/extensions.kt index 4cbb61eb..46c356df 100644 --- a/core/src/main/kotlin/plugability/extensions.kt +++ b/core/src/main/kotlin/plugability/extensions.kt @@ -15,7 +15,8 @@ abstract class Extension<T : Any> internal constructor( internal val extensionName: String, internal val action: LazyEvaluated<T>, internal val ordering: (OrderDsl.() -> Unit)? = null, - internal val condition: DokkaConfiguration.() -> Boolean = { true } + internal val condition: DokkaConfiguration.() -> Boolean = { true }, + internal val isFallback: Boolean ) { override fun toString() = "Extension: $pluginClass/$extensionName" @@ -26,6 +27,8 @@ abstract class Extension<T : Any> internal constructor( override fun hashCode() = listOf(pluginClass, extensionName).hashCode() abstract fun setCondition(condition: (DokkaConfiguration.() -> Boolean)): Extension<T> + + abstract fun markedAsFallback(): Extension<T> } class ExtensionOrdered<T : Any> internal constructor( @@ -34,17 +37,22 @@ class ExtensionOrdered<T : Any> internal constructor( extensionName: String, action: LazyEvaluated<T>, ordering: (OrderDsl.() -> Unit), - condition: DokkaConfiguration.() -> Boolean = { true } + condition: DokkaConfiguration.() -> Boolean = { true }, + isFallback: Boolean = false ) : Extension<T>( extensionPoint, pluginClass, extensionName, action, ordering, - condition + condition, + isFallback ) { override fun setCondition(condition: DokkaConfiguration.() -> Boolean) = ExtensionOrdered(extensionPoint, pluginClass, extensionName, action, ordering!!, condition) + + override fun markedAsFallback() = + ExtensionOrdered(extensionPoint, pluginClass, extensionName, action, ordering!!, condition, true) } class ExtensionUnordered<T : Any> internal constructor( @@ -52,17 +60,22 @@ class ExtensionUnordered<T : Any> internal constructor( pluginClass: String, extensionName: String, action: LazyEvaluated<T>, - condition: DokkaConfiguration.() -> Boolean = { true } + condition: DokkaConfiguration.() -> Boolean = { true }, + isFallback: Boolean = false ) : Extension<T>( extensionPoint, pluginClass, extensionName, action, null, - condition + condition, + isFallback ) { override fun setCondition(condition: DokkaConfiguration.() -> Boolean) = ExtensionUnordered(extensionPoint, pluginClass, extensionName, action, condition) + + override fun markedAsFallback() = + ExtensionUnordered(extensionPoint, pluginClass, extensionName, action, condition, true) } internal data class Ordering(val previous: Set<Extension<*>>, val following: Set<Extension<*>>) @@ -79,10 +92,11 @@ class ExtendingDSL(private val pluginClass: String, private val extensionName: S infix fun <T : Any> ExtensionPoint<T>.providing(action: (DokkaContext) -> T) = ExtensionUnordered(this, this@ExtendingDSL.pluginClass, extensionName, LazyEvaluated.fromRecipe(action)) - infix fun <T: Any> ExtensionUnordered<T>.order(block: OrderDsl.() -> Unit) = + infix fun <T : Any> ExtensionUnordered<T>.order(block: OrderDsl.() -> Unit) = ExtensionOrdered(extensionPoint, pluginClass, extensionName, action, block) - infix fun <T : Any> Extension<T>.applyIf(condition: DokkaConfiguration.() -> Boolean): Extension<T> = this.setCondition(condition) + infix fun <T : Any> Extension<T>.applyIf(condition: DokkaConfiguration.() -> Boolean): Extension<T> = + this.setCondition(condition) } @ExtensionsDsl diff --git a/core/src/main/kotlin/renderers/DefaultRenderer.kt b/core/src/main/kotlin/renderers/DefaultRenderer.kt index 16cdb4c1..d09d9ded 100644 --- a/core/src/main/kotlin/renderers/DefaultRenderer.kt +++ b/core/src/main/kotlin/renderers/DefaultRenderer.kt @@ -3,7 +3,6 @@ package org.jetbrains.dokka.renderers import org.jetbrains.dokka.CoreExtensions import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.plugability.DokkaContext -import org.jetbrains.dokka.plugability.single import org.jetbrains.dokka.resolvers.LocationProvider import org.jetbrains.dokka.transformers.pages.PageNodeTransformer diff --git a/core/src/main/kotlin/renderers/OutputWriter.kt b/core/src/main/kotlin/renderers/OutputWriter.kt index 30b2b0b6..e317f8ef 100644 --- a/core/src/main/kotlin/renderers/OutputWriter.kt +++ b/core/src/main/kotlin/renderers/OutputWriter.kt @@ -1,9 +1,5 @@ package org.jetbrains.dokka.renderers -import org.jetbrains.dokka.CoreExtensions -import org.jetbrains.dokka.plugability.DokkaContext -import org.jetbrains.dokka.plugability.single - interface OutputWriter { fun write(path: String, text: String, ext: String) diff --git a/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt b/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt index d30264bb..65d2f794 100644 --- a/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt +++ b/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt @@ -1,10 +1,8 @@ package org.jetbrains.dokka.resolvers -import org.jetbrains.dokka.CoreExtensions import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.plugability.DokkaContext -import org.jetbrains.dokka.plugability.single import org.jetbrains.dokka.utilities.htmlEscape import java.util.* diff --git a/core/src/main/kotlin/transformers/documentation/DefaultDocumentationToPageTranslator.kt b/core/src/main/kotlin/transformers/documentation/DefaultDocumentationToPageTranslator.kt index 81c6dfea..c2a21150 100644 --- a/core/src/main/kotlin/transformers/documentation/DefaultDocumentationToPageTranslator.kt +++ b/core/src/main/kotlin/transformers/documentation/DefaultDocumentationToPageTranslator.kt @@ -6,7 +6,6 @@ import org.jetbrains.dokka.pages.DefaultPageBuilder import org.jetbrains.dokka.pages.DefaultPageContentBuilder import org.jetbrains.dokka.pages.ModulePageNode import org.jetbrains.dokka.plugability.DokkaContext -import org.jetbrains.dokka.plugability.single object DefaultDocumentationToPageTranslator : DocumentationToPageTranslator { |