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 1 commit
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
207 changes: 169 additions & 38 deletions plugins/base/src/test/kotlin/enums/EnumsTest.kt
Expand Up @@ -7,11 +7,13 @@ import org.jetbrains.dokka.model.*
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import signatures.renderedContent
import utils.TestOutputWriterPlugin

class EnumsTest : BaseAbstractTest() {

@Test
fun basicEnum() {
fun `should preserve enum source ordering for documentables`() {
val configuration = dokkaConfiguration {
sourceSets {
sourceSet {
Expand All @@ -20,29 +22,55 @@ class EnumsTest : BaseAbstractTest() {
}
}

val writerPlugin = TestOutputWriterPlugin()

testInline(
"""
|/src/main/kotlin/basic/Test.kt
|package enums
|package testpackage
IgnatBeresnev marked this conversation as resolved.
Show resolved Hide resolved
|
|enum class Test {
|enum class TestEnum {
| E1,
| E2
| E2,
IgnatBeresnev marked this conversation as resolved.
Show resolved Hide resolved
| E3,
| E4,
| E5,
| E6,
| E7,
| E8,
| E9,
| E10
|}
""".trimMargin(),
configuration
configuration,
pluginOverrides = listOf(writerPlugin)
) {
pagesGenerationStage = {
val map = it.getClasslikeToMemberMap()
val test = map.filterKeys { it.name == "Test" }.values.firstOrNull()
assertTrue(test != null) { "Test not found" }
assertTrue(test!!.any { it.name == "E1" } && test.any { it.name == "E2" }) { "Enum entries missing in parent" }
documentablesTransformationStage = { module ->
val testPackage = module.packages[0]
assertEquals("testpackage", testPackage.name)

val testEnum = testPackage.classlikes[0] as DEnum
assertEquals("TestEnum", testEnum.name)

val enumEntries = testEnum.entries
assertEquals(10, enumEntries.count())

assertEquals("E1", enumEntries[0].name)
assertEquals("E2", enumEntries[1].name)
assertEquals("E3", enumEntries[2].name)
assertEquals("E4", enumEntries[3].name)
assertEquals("E5", enumEntries[4].name)
assertEquals("E6", enumEntries[5].name)
assertEquals("E7", enumEntries[6].name)
assertEquals("E8", enumEntries[7].name)
assertEquals("E9", enumEntries[8].name)
assertEquals("E10", enumEntries[9].name)
}
}
}

@Test
fun enumWithCompanion() {
fun `should preserve enum source ordering for generated pages`() {
val configuration = dokkaConfiguration {
sourceSets {
sourceSet {
Expand All @@ -51,12 +79,127 @@ class EnumsTest : BaseAbstractTest() {
}
}

val writerPlugin = TestOutputWriterPlugin()

testInline(
"""
|/src/main/kotlin/basic/Test.kt
|package testpackage
|
|enum class TestEnum {
| E1,
| E2,
| E3,
| E4,
| E5,
| E6,
| E7,
| E8,
| E9,
| E10
|}
""".trimMargin(),
configuration,
pluginOverrides = listOf(writerPlugin)
) {
pagesGenerationStage = { rootPage ->
val packagePage = rootPage.children[0]
assertEquals("testpackage", packagePage.name)

val testEnumNode = packagePage.children[0]
assertEquals("TestEnum", testEnumNode.name)

val enumEntries = testEnumNode.children
assertEquals(10, enumEntries.size)

assertEquals("E1", enumEntries[0].name)
assertEquals("E2", enumEntries[1].name)
assertEquals("E3", enumEntries[2].name)
assertEquals("E4", enumEntries[3].name)
assertEquals("E5", enumEntries[4].name)
assertEquals("E6", enumEntries[5].name)
assertEquals("E7", enumEntries[6].name)
assertEquals("E8", enumEntries[7].name)
assertEquals("E9", enumEntries[8].name)
assertEquals("E10", enumEntries[9].name)
}
}
}

@Test
fun `should preserve enum source ordering for rendered entries`() {
val configuration = dokkaConfiguration {
sourceSets {
sourceSet {
sourceRoots = listOf("src/")
}
}
}

val writerPlugin = TestOutputWriterPlugin()

testInline(
"""
|/src/main/kotlin/basic/Test.kt
|package enums
|package testpackage
|
|enum class Test {
|enum class TestEnum {
| E1,
| E2,
| E3,
| E4,
| E5,
| E6,
| E7,
| E8,
| E9,
| E10
|}
""".trimMargin(),
configuration,
pluginOverrides = listOf(writerPlugin)
) {
renderingStage = { rootPage, context ->
val enumEntriesOnPage = writerPlugin.writer.renderedContent("root/testpackage/-test-enum/index.html")
.select("div.table[data-togglable=Entries]")
.select("div.table-row")
.select("div.keyValue")
.select("div.title")
.select("a")

val enumEntries = enumEntriesOnPage.map { it.text() }
assertEquals(10, enumEntries.size)

assertEquals("E1", enumEntries[0])
assertEquals("E2", enumEntries[1])
assertEquals("E3", enumEntries[2])
assertEquals("E4", enumEntries[3])
assertEquals("E5", enumEntries[4])
assertEquals("E6", enumEntries[5])
assertEquals("E7", enumEntries[6])
assertEquals("E8", enumEntries[7])
assertEquals("E9", enumEntries[8])
assertEquals("E10", enumEntries[9])
}
}
}

@Test
fun `should handle companion object within enum`() {
val configuration = dokkaConfiguration {
sourceSets {
sourceSet {
sourceRoots = listOf("src/")
}
}
}

testInline(
"""
|/src/main/kotlin/basic/Test.kt
|package testpackage
|
|enum class TestEnum {
| E1,
| E2;
| companion object {}
Expand All @@ -71,18 +214,10 @@ class EnumsTest : BaseAbstractTest() {
assertTrue(c.isNotEmpty(), "Classlikes list cannot be empty")

val enum = c.first() as DEnum
assertEquals(enum.name, "Test")
assertEquals(enum.entries.count(), 2)
assertNotNull(enum.companion)
}
}
}
pagesGenerationStage = { module ->
val map = module.getClasslikeToMemberMap()
val test = map.filterKeys { it.name == "Test" }.values.firstOrNull()
assertNotNull(test, "Test not found")
assertTrue(test!!.any { it.name == "E1" } && test.any { it.name == "E2" }) { "Enum entries missing in parent" }
}
}
}

Expand All @@ -98,11 +233,11 @@ class EnumsTest : BaseAbstractTest() {

testInline(
"""
|/src/main/kotlin/basic/Test.kt
|package enums
|/src/main/kotlin/basic/TestEnum.kt
|package testpackage
Comment on lines -101 to +292
Copy link
Member Author

Choose a reason for hiding this comment

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

Test and enums were rather generic and caused a bit of confusion. Decided to rename it

|
|
|enum class Test(name: String, index: Int, excluded: Boolean) {
|enum class TestEnum(name: String, index: Int, excluded: Boolean) {
| E1("e1", 1, true),
| E2("e2", 2, false);
|}
Expand All @@ -125,7 +260,7 @@ class EnumsTest : BaseAbstractTest() {
pagesGenerationStage = { module ->
val entryPage = module.dfs { it.name == "E1" } as ClasslikePageNode
val signaturePart = (entryPage.content.dfs {
it is ContentGroup && it.dci.toString() == "[enums/Test.E1///PointingToDeclaration/{\"org.jetbrains.dokka.links.EnumEntryDRIExtra\":{\"key\":\"org.jetbrains.dokka.links.EnumEntryDRIExtra\"}}][Symbol]"
it is ContentGroup && it.dci.toString() == "[testpackage/TestEnum.E1///PointingToDeclaration/{\"org.jetbrains.dokka.links.EnumEntryDRIExtra\":{\"key\":\"org.jetbrains.dokka.links.EnumEntryDRIExtra\"}}][Symbol]"
} as ContentGroup)
assertEquals("(\"e1\", 1, true)", signaturePart.constructorSignature())
}
Expand All @@ -144,15 +279,15 @@ class EnumsTest : BaseAbstractTest() {

testInline(
"""
|/src/main/kotlin/basic/Test.kt
|package enums
|/src/main/kotlin/basic/TestEnum.kt
|package testpackage
|
|
|interface Sample {
| fun toBeImplemented(): String
|}
|
|enum class Test: Sample {
|enum class TestEnum: Sample {
| E1 {
| override fun toBeImplemented(): String = "e1"
| }
Expand All @@ -176,7 +311,7 @@ class EnumsTest : BaseAbstractTest() {
}

@Test
fun enumWithAnnotationsOnEntries(){
fun enumWithAnnotationsOnEntries() {
val configuration = dokkaConfiguration {
sourceSets {
sourceSet {
Expand All @@ -187,10 +322,10 @@ class EnumsTest : BaseAbstractTest() {

testInline(
"""
|/src/main/kotlin/basic/Test.kt
|package enums
|/src/main/kotlin/basic/TestEnum.kt
|package testpackage
|
|enum class Test {
|enum class TestEnum {
| /**
| Sample docs for E1
| **/
Expand All @@ -201,8 +336,8 @@ class EnumsTest : BaseAbstractTest() {
configuration
) {
pagesTransformationStage = { m ->
val entryNode = m.children.first { it.name == "enums" }.children.first { it.name == "Test" }.children.firstIsInstance<ClasslikePageNode>()
val signature = (entryNode.content as ContentGroup).dfs { it is ContentGroup && it.dci.toString() == "[enums/Test.E1///PointingToDeclaration/{\"org.jetbrains.dokka.links.EnumEntryDRIExtra\":{\"key\":\"org.jetbrains.dokka.links.EnumEntryDRIExtra\"}}][Cover]" } as ContentGroup
val entryNode = m.children.first { it.name == "testpackage" }.children.first { it.name == "TestEnum" }.children.firstIsInstance<ClasslikePageNode>()
val signature = (entryNode.content as ContentGroup).dfs { it is ContentGroup && it.dci.toString() == "[testpackage/TestEnum.E1///PointingToDeclaration/{\"org.jetbrains.dokka.links.EnumEntryDRIExtra\":{\"key\":\"org.jetbrains.dokka.links.EnumEntryDRIExtra\"}}][Cover]" } as ContentGroup

signature.assertNode {
header(1) { +"E1" }
Expand All @@ -226,10 +361,6 @@ class EnumsTest : BaseAbstractTest() {
}
}


fun RootPageNode.getClasslikeToMemberMap() =
this.parentMap.filterValues { it is ClasslikePageNode }.entries.groupBy({ it.value }) { it.key }

private fun ContentGroup.constructorSignature(): String =
(children.single() as ContentGroup).children.drop(1).joinToString(separator = "") { (it as ContentText).text }
}