Skip to content

Commit

Permalink
Sort groups of divergent elements by their key first ignoring case, t…
Browse files Browse the repository at this point in the history
…hen preserving it
  • Loading branch information
ilya-g committed Feb 15, 2023
1 parent daed35f commit d3ac5f2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.jetbrains.dokka.base.transformers.documentables.isDeprecated
import org.jetbrains.dokka.base.transformers.documentables.isException
import org.jetbrains.dokka.base.translators.documentables.DocumentableLanguage
import org.jetbrains.dokka.base.translators.documentables.documentableLanguage
import org.jetbrains.dokka.base.utils.canonicalAlphabeticalOrder
import org.jetbrains.dokka.model.*
import org.jetbrains.dokka.pages.*

Expand Down Expand Up @@ -90,14 +91,17 @@ abstract class NavigationDataProvider {
}
}

private val navigationNodeOrder: Comparator<NavigationNode> =
compareBy(canonicalAlphabeticalOrder) { it.name }

private fun ContentPage.navigableChildren() =
if (this is ClasslikePage) {
this.navigableChildren()
} else {
children
.filterIsInstance<ContentPage>()
.map { visit(it) }
.sortedBy { it.name.toLowerCase() }
.sortedWith(navigationNodeOrder)
}

private fun ClasslikePage.navigableChildren(): List<NavigationNode> {
Expand All @@ -111,7 +115,7 @@ abstract class NavigationDataProvider {
// no sorting for enum entries, should be the same order as in source code
navigableChildren
} else {
navigableChildren.sortedBy { it.name.toLowerCase() }
navigableChildren.sortedWith(navigationNodeOrder)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.jetbrains.dokka.base.transformers.documentables.ClashingDriIdentifier
import org.jetbrains.dokka.base.transformers.pages.comments.CommentsToContentConverter
import org.jetbrains.dokka.base.transformers.pages.tags.CustomTagContentProvider
import org.jetbrains.dokka.base.translators.documentables.PageContentBuilder.DocumentableContentBuilder
import org.jetbrains.dokka.base.utils.canonicalAlphabeticalOrder
import org.jetbrains.dokka.links.Callable
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.model.*
Expand Down Expand Up @@ -508,6 +509,9 @@ open class DefaultPageCreator(
}
}

private val groupKeyComparator =
compareBy<Map.Entry<String?, *>, String?>(canonicalAlphabeticalOrder) { it.key }

protected open fun DocumentableContentBuilder.divergentBlock(
name: String,
collection: Collection<Documentable>,
Expand All @@ -525,7 +529,7 @@ open class DefaultPageCreator(
.groupBy { it.name } // This groupBy should probably use LocationProvider
// This hacks displaying actual typealias signatures along classlike ones
.mapValues { if (it.value.any { it is DClasslike }) it.value.filter { it !is DTypeAlias } else it.value }
.entries.sortedBy { it.key }
.entries.sortedWith(groupKeyComparator)
.forEach { (elementName, elements) -> // This groupBy should probably use LocationProvider
val sortedElements = sortDivergentElementsDeterministically(elements)
row(
Expand Down
7 changes: 7 additions & 0 deletions plugins/base/src/main/kotlin/utils/alphabeticalOrder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.jetbrains.dokka.base.utils


/**
* Canonical alphabetical order to sort named elements
*/
internal val canonicalAlphabeticalOrder: Comparator<String?> = nullsFirst(String.CASE_INSENSITIVE_ORDER).thenBy { it }

0 comments on commit d3ac5f2

Please sign in to comment.