diff options
Diffstat (limited to 'core/src/main/kotlin')
3 files changed, 87 insertions, 92 deletions
diff --git a/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt b/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt index a98002d4..51ceb47e 100644 --- a/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt +++ b/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt @@ -57,8 +57,8 @@ open class KotlinWebsiteOutputBuilder( } } - override fun appendAsOverloadGroup(to: StringBuilder, platforms: Set<String>, block: () -> Unit) { - div(to, "overload-group", calculateDataAttributes(platforms), true) { + override fun appendAsOverloadGroup(to: StringBuilder, platforms: PlatformsData, block: () -> Unit) { + div(to, "overload-group", calculateDataAttributes(platforms.keys), true) { ensureParagraph() block() ensureParagraph() @@ -159,14 +159,14 @@ open class KotlinWebsiteOutputBuilder( return "$platformsAttr$kotlinVersionAttr$jreVersionAttr" } - override fun appendIndexRow(platforms: Set<String>, block: () -> Unit) { + override fun appendIndexRow(platforms: PlatformsData, block: () -> Unit) { if (platforms.isNotEmpty()) - wrap("<tr${calculateDataAttributes(platforms)}>", "</tr>", block) + wrap("<tr${calculateDataAttributes(platforms.keys)}>", "</tr>", block) else appendTableRow(block) } - override fun appendPlatforms(platforms: Set<String>) { + override fun appendPlatforms(platforms: PlatformsData) { } } diff --git a/core/src/main/kotlin/Formats/KotlinWebsiteHtmlFormatService.kt b/core/src/main/kotlin/Formats/KotlinWebsiteHtmlFormatService.kt index c412b34e..b12075fb 100644 --- a/core/src/main/kotlin/Formats/KotlinWebsiteHtmlFormatService.kt +++ b/core/src/main/kotlin/Formats/KotlinWebsiteHtmlFormatService.kt @@ -3,7 +3,6 @@ package org.jetbrains.dokka import com.google.inject.Inject import com.google.inject.name.Named import org.jetbrains.dokka.Utilities.impliedPlatformsName -import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty import java.io.File @@ -60,7 +59,7 @@ open class KotlinWebsiteHtmlOutputBuilder( } } - override fun appendAsOverloadGroup(to: StringBuilder, platforms: Set<String>, block: () -> Unit) { + override fun appendAsOverloadGroup(to: StringBuilder, platforms: PlatformsData, block: () -> Unit) { div(to, "overload-group", calculateDataAttributes(platforms)) { block() } @@ -124,27 +123,29 @@ open class KotlinWebsiteHtmlOutputBuilder( else -> "identifier" } - private fun calculatePlatforms(platforms: Set<String>): Map<String, List<String>> { - val kotlinVersion = platforms.singleOrNull(String::isKotlinVersion)?.removePrefix("Kotlin ") - val jreVersion = platforms.filter(String::isJREVersion).min()?.takeUnless { it.endsWith("6") } - val targetPlatforms = platforms.filterNot { it.isKotlinVersion() || it.isJREVersion() } - return mapOf( - "platform" to targetPlatforms, - "kotlin-version" to listOfNotNull(kotlinVersion), - "jre-version" to listOfNotNull(jreVersion) + private data class PlatformsForElement( + val platformToVersion: Map<String, String> + ) + + private fun calculatePlatforms(platforms: PlatformsData): PlatformsForElement { + //val kotlinVersion = platforms.singleOrNull(String::isKotlinVersion)?.removePrefix("Kotlin ") + val jreVersion = platforms.keys.filter(String::isJREVersion).min()?.takeUnless { it.endsWith("6") } + val targetPlatforms = platforms.filterNot { it.key.isJREVersion() } + + listOfNotNull(jreVersion?.let { it to platforms[it]!! }) + + return PlatformsForElement( + targetPlatforms.mapValues { (_, nodes) -> effectiveSinceKotlinForNodes(nodes) } ) } - private fun calculateDataAttributes(platforms: Set<String>): String { - val platformsByKind = calculatePlatforms(platforms) - return platformsByKind - .entries.filterNot { it.value.isEmpty() } - .joinToString(separator = " ") { (kind, values) -> - "data-$kind=\"${values.joinToString()}\"" - } + private fun calculateDataAttributes(platforms: PlatformsData): String { + val platformToVersion = calculatePlatforms(platforms).platformToVersion + val (platformNames, versions) = platformToVersion.toList().unzip() + return "data-platform=\"${platformNames.joinToString()}\" "+ + "data-kotlin-version=\"${versions.joinToString()}\"" } - override fun appendIndexRow(platforms: Set<String>, block: () -> Unit) { + override fun appendIndexRow(platforms: PlatformsData, block: () -> Unit) { // if (platforms.isNotEmpty()) // wrap("<tr${calculateDataAttributes(platforms)}>", "</tr>", block) // else @@ -154,21 +155,23 @@ open class KotlinWebsiteHtmlOutputBuilder( } } - override fun appendPlatforms(platforms: Set<String>) { - val platformsToKind = calculatePlatforms(platforms) + override fun appendPlatforms(platforms: PlatformsData) { + val platformToVersion = calculatePlatforms(platforms).platformToVersion div(to, "tags") { div(to, "spacer") {} - platformsToKind.entries.forEach { (kind, values) -> - values.forEach { value -> - div(to, "tags__tag $kind tag-value-$value") { - to.append(value) - } + platformToVersion.entries.forEach { (platform, version) -> + div(to, "tags__tag platform tag-value-$platform", + otherAttributes = " data-tag-version=\"$version\"") { + to.append(platform) } } + div(to, "tags__tag kotlin-version") { + to.append(mergeVersions(platformToVersion.values.toList())) + } } } - override fun appendAsNodeDescription(platforms: Set<String>, block: () -> Unit) { + override fun appendAsNodeDescription(platforms: PlatformsData, block: () -> Unit) { div(to, "node-page-main", otherAttributes = " ${calculateDataAttributes(platforms)}") { block() } @@ -179,10 +182,10 @@ open class KotlinWebsiteHtmlOutputBuilder( to.append(" / ") } - override fun appendPlatformsAsText(platforms: Set<String>) { + override fun appendPlatformsAsText(platforms: PlatformsData) { appendHeader(5) { to.append("For ") - platforms.filterNot { it.isJREVersion() }.joinTo(to) + platforms.keys.filterNot { it.isJREVersion() }.joinTo(to) } } @@ -208,7 +211,7 @@ open class KotlinWebsiteHtmlOutputBuilder( } } - override fun appendAsPlatformDependentBlock(platforms: Set<String>, block: (Set<String>) -> Unit) { + override fun appendAsPlatformDependentBlock(platforms: PlatformsData, block: (PlatformsData) -> Unit) { if (platforms.isNotEmpty()) wrap("<div ${calculateDataAttributes(platforms)}>", "</div>") { block(platforms) @@ -217,7 +220,7 @@ open class KotlinWebsiteHtmlOutputBuilder( block(platforms) } - override fun appendAsSummaryGroup(platforms: Set<String>, block: (Set<String>) -> Unit) { + override fun appendAsSummaryGroup(platforms: PlatformsData, block: (PlatformsData) -> Unit) { div(to, "summary-group", otherAttributes = " ${calculateDataAttributes(platforms)}") { block(platforms) } diff --git a/core/src/main/kotlin/Formats/StructuredFormatService.kt b/core/src/main/kotlin/Formats/StructuredFormatService.kt index 8b66f187..98414695 100644 --- a/core/src/main/kotlin/Formats/StructuredFormatService.kt +++ b/core/src/main/kotlin/Formats/StructuredFormatService.kt @@ -1,6 +1,7 @@ package org.jetbrains.dokka import org.jetbrains.dokka.LanguageService.RenderMode +import org.jetbrains.kotlin.utils.keysToMap import java.util.* data class FormatLink(val text: String, val href: String) @@ -21,11 +22,11 @@ private data class Summarized( ) data class SummarizedNodes(val content: ContentNode, val nodes: List<DocumentationNode>) { - val platforms = effectivePlatformAndVersion(nodes) + val platforms = effectivePlatformsForMembers(nodes) } data class SummarizedBySummary(val content: ContentNode, val signatures: List<SummarizedNodes>) { - val platforms = effectivePlatformAndVersion(signatures.flatMap { it.nodes }) - val platformsOnSignature = !sameVersionAndPlatforms(signatures.map { it.platforms }) + val platforms = effectivePlatformsForMembers(signatures.flatMap { it.nodes }) + val platformsOnSignature = !samePlatforms(signatures.map { it.platforms }) } @@ -33,13 +34,13 @@ private data class Summarized( if (data.any { it.platformsOnSignature }) { return PlatformPlacement.Signature } - if (sameVersionAndPlatforms(data.map { it.platforms })) { + if (samePlatforms(data.map { it.platforms })) { return PlatformPlacement.Row } return PlatformPlacement.Summary } val platformPlacement: PlatformPlacement = computePlatformLevel() - val platforms = effectivePlatformAndVersion(data.flatMap { it.signatures.flatMap { it.nodes } }) + val platforms = effectivePlatformsForMembers(data.flatMap { it.signatures.flatMap { it.nodes } }) enum class PlatformPlacement { @@ -125,11 +126,11 @@ abstract class StructuredOutputBuilder(val to: StringBuilder, } } - open fun appendAsPlatformDependentBlock(platforms: Set<String>, block: (Set<String>) -> Unit) { + open fun appendAsPlatformDependentBlock(platforms: PlatformsData, block: (PlatformsData) -> Unit) { block(platforms) } - open fun appendAsSummaryGroup(platforms: Set<String>, block: (Set<String>) -> Unit) { + open fun appendAsSummaryGroup(platforms: PlatformsData, block: (PlatformsData) -> Unit) { appendAsPlatformDependentBlock(platforms, block) } @@ -145,7 +146,7 @@ abstract class StructuredOutputBuilder(val to: StringBuilder, appendText(text) } - open fun appendAsNodeDescription(platforms: Set<String>, block: () -> Unit) { + open fun appendAsNodeDescription(platforms: PlatformsData, block: () -> Unit) { block() } @@ -277,25 +278,25 @@ abstract class StructuredOutputBuilder(val to: StringBuilder, block() } - protected open fun appendAsOverloadGroup(to: StringBuilder, platforms: Set<String>, block: () -> Unit) { + protected open fun appendAsOverloadGroup(to: StringBuilder, platforms: PlatformsData, block: () -> Unit) { block() } - protected open fun appendIndexRow(platforms: Set<String>, block: () -> Unit) { + protected open fun appendIndexRow(platforms: PlatformsData, block: () -> Unit) { appendTableRow(block) } - protected open fun appendPlatformsAsText(platforms: Set<String>) { + protected open fun appendPlatformsAsText(platforms: PlatformsData) { if (platforms.isNotEmpty()) { appendLine() - appendText(platforms.joinToString(prefix = "(", postfix = ")")) + appendText(platforms.keys.joinToString(prefix = "(", postfix = ")")) } } - protected open fun appendPlatforms(platforms: Set<String>) { + protected open fun appendPlatforms(platforms: PlatformsData) { if (platforms.isNotEmpty()) { appendLine() - appendText(platforms.joinToString(prefix = "(", postfix = ")")) + appendText(platforms.keys.joinToString(prefix = "(", postfix = ")")) } } @@ -358,19 +359,19 @@ abstract class StructuredOutputBuilder(val to: StringBuilder, private fun appendDocumentation(overloads: Iterable<DocumentationNode>, isSingleNode: Boolean) { val breakdownBySummary = overloads.groupByTo(LinkedHashMap()) { node -> when (node.kind) { - NodeKind.GroupNode -> node.origins.first().content + NodeKind.GroupNode -> node.origins.map { it.content } else -> node.content } } if (breakdownBySummary.size == 1) { val node = breakdownBySummary.values.single() - appendAsNodeDescription(effectivePlatformAndVersion(node)) { + appendAsNodeDescription(effectivePlatformsForMembers(node)) { formatOverloadGroup(node, isSingleNode) } } else { for ((_, items) in breakdownBySummary) { - appendAsOverloadGroup(to, effectivePlatformAndVersion(items)) { + appendAsOverloadGroup(to, effectivePlatformsForMembers(items)) { formatOverloadGroup(items) } } @@ -379,13 +380,13 @@ abstract class StructuredOutputBuilder(val to: StringBuilder, private fun formatOverloadGroup(items: List<DocumentationNode>, isSingleNode: Boolean = false) { - val platformsPerGroup = sameVersionAndPlatforms( + val platformsPerGroup = samePlatforms( items.flatMap { if (it.kind == NodeKind.GroupNode) it.origins else listOf(it) } - .map { effectivePlatformAndVersion(it) } + .map { effectivePlatformsForNode(it) } ) if (platformsPerGroup) { - appendAsPlatformDependentBlock(effectivePlatformAndVersion(items)) { platforms -> + appendAsPlatformDependentBlock(effectivePlatformsForMembers(items)) { platforms -> appendPlatforms(platforms) } } @@ -411,6 +412,7 @@ abstract class StructuredOutputBuilder(val to: StringBuilder, for ((content, origins) in groupByContent) { if (content.isEmpty()) continue appendAsPlatformDependentBlock(effectivePlatformsForMembers(origins)) { platforms -> + println("FOG_GN_MULTI(${groupByContent.count { !it.key.isEmpty() }}):" + item.path.joinToString(" > ")) if (groupByContent.count { !it.key.isEmpty() } > 1) { appendPlatformsAsText(platforms) } @@ -431,7 +433,7 @@ abstract class StructuredOutputBuilder(val to: StringBuilder, fun renderSimpleNode(item: DocumentationNode, isSingleNode: Boolean, withPlatforms: Boolean = true) { - appendAsPlatformDependentBlock(effectivePlatformAndVersion(listOf(item))) { platforms -> + appendAsPlatformDependentBlock(effectivePlatformsForMembers(listOf(item))) { platforms -> // TODO: use summarizesignatures val rendered = languageService.render(item) item.detailOrNull(NodeKind.Signature)?.let { @@ -457,7 +459,7 @@ abstract class StructuredOutputBuilder(val to: StringBuilder, } for ((sign, nodes) in groupBySignature) { - appendAsPlatformDependentBlock(effectivePlatformAndVersion(nodes)) { platforms -> + appendAsPlatformDependentBlock(effectivePlatformsForMembers(nodes)) { platforms -> val first = nodes.first() first.detailOrNull(NodeKind.Signature)?.let { if (item.kind !in NodeKind.classLike || !isSingleNode) @@ -632,7 +634,7 @@ abstract class StructuredOutputBuilder(val to: StringBuilder, appendLine() appendHeader { appendText(node.name) } - appendAsNodeDescription(effectivePlatformAndVersion(node)) { + appendAsNodeDescription(effectivePlatformsForNode(node)) { renderGroupNode(node, true) val groupByContent = node.origins.groupBy { it.content } @@ -747,7 +749,7 @@ abstract class StructuredOutputBuilder(val to: StringBuilder, appendTable("Name", "Summary") { appendTableBody { for ((memberLocation, members) in membersMap) { - val platforms = effectivePlatformAndVersion(members) + val platforms = effectivePlatformsForMembers(members) // val platforms = if (platformsBasedOnMembers) // members.flatMapTo(mutableSetOf()) { platformsOfItems(it.members) } + elementPlatforms // else @@ -915,32 +917,32 @@ abstract class StructuredFormatService(val generator: NodeLocationAwareGenerator } +typealias PlatformsData = Map<String, Set<DocumentationNode>> -fun memberPlatforms(node: DocumentationNode): Sequence<Set<String>> { +fun memberPlatforms(node: DocumentationNode): PlatformsData { val members = when { node.kind == NodeKind.GroupNode -> node.origins else -> node.members } - return members.asSequence() - .map { effectivePlatformsForNode(it) } + return members.map(::effectivePlatformsForNode).fold(mapOf(), ::mergePlatforms) } -fun effectivePlatformsForNode(node: DocumentationNode): Set<String> { - val platforms = - sequenceOf(node.platforms.toSet()) + memberPlatforms(node) - - - // Calculating common platforms for items - return platforms.reduce { result, platformsOfItem -> - result.union(platformsOfItem) +fun mergePlatforms(a: PlatformsData, b: PlatformsData): PlatformsData { + val mutable = a.toMutableMap() + b.forEach { (name, declarations) -> + mutable.merge(name, declarations) { a, b -> a.union(b) } } + return mutable } -fun effectivePlatformsForMembers(nodes: Collection<DocumentationNode>): Set<String> { - return nodes.map { effectivePlatformsForNode(it) }.reduce { acc, set -> - acc.union(set) - } +fun effectivePlatformsForNode(node: DocumentationNode): PlatformsData { + val platforms = node.platforms + memberPlatforms(node).keys + return platforms.keysToMap { setOf(node) } +} + +fun effectivePlatformsForMembers(nodes: Collection<DocumentationNode>): PlatformsData { + return nodes.map { effectivePlatformsForNode(it) }.reduce(::mergePlatforms) } fun mergeVersions(kotlinVersions: List<String>): String { @@ -954,29 +956,19 @@ fun effectiveSinceKotlinForNode(node: DocumentationNode, baseVersion: String = " node.kind in NodeKind.memberLike -> emptyList() else -> node.members } + val newBase = node.sinceKotlin ?: baseVersion + val memberVersion = if (members.isNotEmpty()) effectiveSinceKotlinForNodes(members, newBase) else newBase - val nodeVersion = node.sinceKotlin ?: baseVersion - val memberVersion = if (members.isNotEmpty()) effectiveSinceKotlinForNodes(members, nodeVersion) else nodeVersion - - return mergeVersions(listOf(memberVersion, nodeVersion)) + return node.sinceKotlin ?: memberVersion } fun effectiveSinceKotlinForNodes(nodes: Collection<DocumentationNode>, baseVersion: String = "1.0"): String { - return mergeVersions(nodes.map { effectiveSinceKotlinForNode(it, baseVersion) }) + val map = nodes.map { effectiveSinceKotlinForNode(it, baseVersion) } + return mergeVersions(map) } -fun sinceKotlinAsPlatform(version: String): String = "Kotlin $version" - - -fun effectivePlatformAndVersion(nodes: Collection<DocumentationNode>, baseVersion: String = "1.0"): Set<String> { - return effectivePlatformsForMembers(nodes) + sinceKotlinAsPlatform(effectiveSinceKotlinForNodes(nodes, baseVersion)) -} - -fun effectivePlatformAndVersion(node: DocumentationNode, baseVersion: String = "1.0"): Set<String> { - return effectivePlatformsForNode(node) + sinceKotlinAsPlatform(effectiveSinceKotlinForNode(node, baseVersion)) -} +fun samePlatforms(platformsPerNode: Collection<PlatformsData>): Boolean { -fun sameVersionAndPlatforms(platformsPerNode: Collection<Set<String>>): Boolean { - val first = platformsPerNode.firstOrNull() ?: return true - return platformsPerNode.all { it == first } + val first = platformsPerNode.firstOrNull()?.keys ?: return true + return platformsPerNode.all { it.keys == first } }
\ No newline at end of file |