aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorPaweł Marks <pmarks@virtuslab.com>2020-01-22 13:32:58 +0100
committerPaweł Marks <Kordyjan@users.noreply.github.com>2020-01-31 15:07:06 +0100
commitc29605d92d1999434ecc79e774281a8280ae2823 (patch)
tree0fb4aff8382aaf1e925ecddd26518fc6840083f0 /core
parent71428219389d5fe429c0bad0bd31b2cda55cdfce (diff)
downloaddokka-c29605d92d1999434ecc79e774281a8280ae2823.tar.gz
dokka-c29605d92d1999434ecc79e774281a8280ae2823.tar.bz2
dokka-c29605d92d1999434ecc79e774281a8280ae2823.zip
Cache of location provider added
Diffstat (limited to 'core')
-rw-r--r--core/src/main/kotlin/resolvers/DefaultLocationProvider.kt30
1 files changed, 21 insertions, 9 deletions
diff --git a/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt b/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt
index 3412d975..8e848b63 100644
--- a/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt
+++ b/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt
@@ -1,12 +1,12 @@
package org.jetbrains.dokka.resolvers
import org.jetbrains.dokka.CoreExtensions
-import org.jetbrains.dokka.DokkaConfiguration
-import org.jetbrains.dokka.utilities.htmlEscape
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.*
open class DefaultLocationProvider(
private val pageGraphRoot: ModulePageNode,
@@ -14,6 +14,9 @@ open class DefaultLocationProvider(
) : LocationProvider { // TODO: cache
private val extension = dokkaContext.single(CoreExtensions.fileExtension)
+ private val pagesCache = mutableMapOf<DRI, Maybe<PageNode>>()
+ private val pathCache: MutableMap<PageNode, List<String>> = IdentityHashMap<PageNode, List<String>>()
+
override fun resolve(node: PageNode, context: PageNode?): String = pathTo(node, context) + extension
override fun resolve(dri: DRI, platforms: List<PlatformData>, context: PageNode?): String =
@@ -22,7 +25,8 @@ open class DefaultLocationProvider(
ExternalLocationProvider.getLocation(dri,
this.dokkaContext.configuration.passesConfigurations
.filter { passConfig ->
- platforms.toSet().contains(PlatformData(passConfig.moduleName, passConfig.analysisPlatform, passConfig.targets))
+ platforms.toSet()
+ .contains(PlatformData(passConfig.moduleName, passConfig.analysisPlatform, passConfig.targets))
} // TODO: change targets to something better?
.flatMap { it.externalDocumentationLinks }.distinct()
)
@@ -41,7 +45,8 @@ open class DefaultLocationProvider(
override fun top(): PageNode = pageGraphRoot
protected open fun findInPageGraph(dri: DRI, platforms: List<PlatformData>): PageNode? =
- pageGraphRoot.dfs { it.dri == dri }
+ pagesCache.getOrPut(dri) { pageGraphRoot.dfs { it.dri == dri }.wrapped }.unwrapped
+
protected open fun pathTo(node: PageNode, context: PageNode?): String {
@@ -49,14 +54,17 @@ open class DefaultLocationProvider(
if (this is PackagePageNode) name
else identifierToFilename(name)
- fun getPath(pathNode: PageNode?, path: List<String> = mutableListOf()): List<String> = when (pathNode) {
+ fun getPathInternal(pathNode: PageNode?, path: List<String>): List<String> = when (pathNode) {
null -> path
- else -> getPath(pathNode.parent(), path + pathNode.pathName().ifEmpty { "root" })
+ else -> getPathInternal(pathNode.parent(), path + pathNode.pathName().ifEmpty { "root" })
}
- val contextNode = if (context?.children?.isEmpty() == true && context.parent() != null) context.parent() else context
+ fun getPath(pathNode: PageNode) = pathCache.getOrPut(pathNode) { getPathInternal(pathNode, emptyList()) }
+
+ val contextNode =
+ if (context?.children?.isEmpty() == true && context.parent() != null) context.parent() else context
val nodePath = getPath(node).reversed()
- val contextPath = getPath(contextNode).reversed()
+ val contextPath = contextNode?.let(::getPath).orEmpty().reversed()
val commonPathElements = nodePath.zip(contextPath).takeWhile { (a, b) -> a == b }.size
@@ -112,4 +120,8 @@ private fun identifierToFilename(name: String): String {
val escaped = name.replace('<', '-').replace('>', '-')
val lowercase = escaped.replace("[A-Z]".toRegex()) { matchResult -> "-" + matchResult.value.toLowerCase() }
return if (lowercase in reservedFilenames) "--$lowercase--" else lowercase
-} \ No newline at end of file
+}
+
+private class Maybe<T : Any>(val unwrapped: T?)
+
+private val <T : Any> T?.wrapped: Maybe<T> get() = Maybe(this) \ No newline at end of file