Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reversed ordering of enum entries #2469

Merged
merged 3 commits into from Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 15 additions & 9 deletions plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt
Expand Up @@ -26,15 +26,21 @@ abstract class NavigationDataProvider {
children = page.navigableChildren()
)

private fun ContentPage.navigableChildren(): List<NavigationNode> =
when {
this !is ClasslikePageNode ->
children.filterIsInstance<ContentPage>().map { visit(it) }
documentables.any { it is DEnum } ->
children.filter { it is WithDocumentables && it.documentables.any { it is DEnumEntry } }
.map { visit(it as ContentPage) }
else -> emptyList()
}.sortedBy { it.name.toLowerCase() }
private fun ContentPage.navigableChildren(): List<NavigationNode> {
return if (this !is ClasslikePageNode) {
children
.filterIsInstance<ContentPage>()
.map { visit(it) }
.sortedBy { it.name.toLowerCase() }
} else if (documentables.any { it is DEnum }) {
// no sorting for enum entries, should be the same as in source code
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure we should have a different sorting for enum entries in a tree. We can discuss it on our meetings.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to discuss it first or can I merge the PR already? :) I'll revert it if we decide it should be alphabetical

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

up to you)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll merge it then because another PR (related to spaces) depends on it, I'll need to rebase on these changes before merging

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure we should have a different sorting for enum entries in a tree. We can discuss it on our meetings.

Hello. As the person who reported #2466, I wanted to make a case that enum members should always be listed in the order they appear in the source code, not alphabetical order. The order of enumerated type members often reflects important semantics about the problem domain. In my case, it is important to me that the planets are listed in order of distance from the Sun. Sorting them by their English names will confuse/distract people reading my documentation. In a sense, it makes their English names more important than their natural order, which is unhelpful for non-English speakers. I would also suggest that any developer who wants their enumerated types in alphabetical order can simply write their code with that order. If there is no consensus about this, I would hope I could at least have some kind of @preserveOrder tag I can put in the comments above my enum class. Thank you for your consideration in this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I actually totally agree with everything up above and wanted to convey the same message.

Another point is that if you have a bunch of classes up in the air - it's unclear how to sort them in a single list other than alphabetically. This is not the case with enum entries though, which have strict linear ordering. Some people even write code around ordinal property (which is bad, but still). So it only makes sense to preserve the ordering.

children
.filter { child -> child is WithDocumentables && child.documentables.any { it is DEnumEntry } }
.map { visit(it as ContentPage) }
} else {
emptyList()
}
}

/**
* Parenthesis is applied in 1 case:
Expand Down
Expand Up @@ -216,7 +216,7 @@ class DocumentableVisibilityFilterTransformer(val context: DokkaContext) : PreMe
}

private fun filterEnumEntries(entries: List<DEnumEntry>, filteredPlatforms: Set<DokkaSourceSet>): Pair<Boolean, List<DEnumEntry>> =
entries.foldRight(Pair(false, emptyList())) { entry, acc ->
entries.fold(Pair(false, emptyList())) { acc, entry ->
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This caused elements to be reversed

val intersection = filteredPlatforms.intersect(entry.sourceSets)
if (intersection.isEmpty()) Pair(true, acc.second)
else {
Expand Down