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

Automatically manage trailing commas in enums #449

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions core/src/main/java/com/facebook/ktfmt/format/TrailingCommas.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package com.facebook.ktfmt.format
import org.jetbrains.kotlin.com.intellij.psi.PsiComment
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.psi.KtClassBody
import org.jetbrains.kotlin.psi.KtCollectionLiteralExpression
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtEnumEntry
import org.jetbrains.kotlin.psi.KtFunctionLiteral
import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.KtParameterList
Expand Down Expand Up @@ -70,6 +72,13 @@ object TrailingCommas {
return // Never add trailing commas to lambda param lists
}
}
is KtClassBody -> {
EnumEntryList.extractChildList(element)?.also {
if (it.terminatingSemicolon != null) {
return // Never add a trailing comma after there is already a terminating semicolon
}
}
}
}

val list = extractManagedList(element) ?: return
Expand All @@ -96,6 +105,16 @@ object TrailingCommas {
ManagedList(element.getInnerExpressions(), element.trailingComma)
}
is KtWhenEntry -> ManagedList(element.conditions.toList(), element.trailingComma)
is KtEnumEntry -> {
EnumEntryList.extractParentList(element).let {
ManagedList(it.enumEntries, it.trailingComma)
}
}
is KtClassBody -> {
EnumEntryList.extractChildList(element)?.let {
ManagedList(it.enumEntries, it.trailingComma)
}
}
else -> null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,105 @@ class GoogleStyleFormatterKtTest {
formattingOptions = Formatter.GOOGLE_FORMAT,
deduceMaxWidth = true)

@Test
fun `trailing commas in enums`() {
val code =
"""
|enum class A {}
|
|enum class B {
| Z // Comment
|}
|
|enum class C {
| Z, // Comment
|}
|
|enum class D {
| Z,
| Y // Comment
|}
|
|enum class E {
| Z,
| Y, // Comment
|}
|
|enum class F {
| Z,
| Y; // Comment
|
| val x = 0
|}
|
|enum class G {
| Z,
| Y,; // Comment
|
| val x = 0
|}
|
|enum class H {
| Z,
| Y() {} // Comment
|}
|
|enum class I {
| Z,
| Y() {}, // Comment
|}
|"""
.trimMargin()
val expected =
"""
|enum class A {}
|
|enum class B {
| Z // Comment
|}
|
|enum class C {
| Z // Comment
|}
|
|enum class D {
| Z,
| Y, // Comment
|}
|
|enum class E {
| Z,
| Y, // Comment
|}
|
|enum class F {
| Z,
| Y; // Comment
|
| val x = 0
|}
|
|enum class G {
| Z,
| Y; // Comment
|
| val x = 0
|}
|
|enum class H {
| Z,
| Y() {}, // Comment
|}
|
|enum class I {
| Z,
| Y() {}, // Comment
|}
|"""
.trimMargin()
assertThatFormatting(code).withOptions(Formatter.GOOGLE_FORMAT).isEqualTo(expected)
}

companion object {
/** Triple quotes, useful to use within triple-quoted strings. */
private const val TQ = "\"\"\""
Expand Down
Loading