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
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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 { | ||
|
@@ -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 {} | ||
|
@@ -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" } | ||
} | ||
} | ||
} | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| | ||
| | ||
|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); | ||
|} | ||
|
@@ -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()) | ||
} | ||
|
@@ -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" | ||
| } | ||
|
@@ -176,7 +311,7 @@ class EnumsTest : BaseAbstractTest() { | |
} | ||
|
||
@Test | ||
fun enumWithAnnotationsOnEntries(){ | ||
fun enumWithAnnotationsOnEntries() { | ||
val configuration = dokkaConfiguration { | ||
sourceSets { | ||
sourceSet { | ||
|
@@ -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 | ||
| **/ | ||
|
@@ -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" } | ||
|
@@ -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 } | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
up to you)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.There was a problem hiding this comment.
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.