-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preserve semicolons in enums with members but no entries
Inspired by #425
- Loading branch information
Showing
4 changed files
with
153 additions
and
8 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
core/src/main/java/com/facebook/ktfmt/format/EnumEntryList.kt
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.facebook.ktfmt.format | ||
|
||
import org.jetbrains.kotlin.com.intellij.psi.PsiElement | ||
import org.jetbrains.kotlin.psi.KtClass | ||
import org.jetbrains.kotlin.psi.KtClassBody | ||
import org.jetbrains.kotlin.psi.KtEnumEntry | ||
import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespaceAndComments | ||
|
||
/** | ||
* PSI-like model of a list of enum entries. | ||
* | ||
* See https://youtrack.jetbrains.com/issue/KT-65157 | ||
*/ | ||
class EnumEntryList | ||
private constructor( | ||
val enumEntries: List<KtEnumEntry>, | ||
val trailingComma: PsiElement?, | ||
val terminatingSemicolon: PsiElement?, | ||
) { | ||
companion object { | ||
fun extractParentList(enumEntry: KtEnumEntry): EnumEntryList { | ||
return extractChildList(enumEntry.parent as KtClassBody)!! | ||
} | ||
|
||
fun extractChildList(classBody: KtClassBody): EnumEntryList? { | ||
val clazz = classBody.parent | ||
if (clazz !is KtClass || !clazz.isEnum()) return null | ||
|
||
val enumEntries = classBody.children.filterIsInstance<KtEnumEntry>() | ||
|
||
if (enumEntries.isEmpty()) { | ||
var semicolon = classBody.firstChild | ||
while (semicolon != null) { | ||
if (semicolon.text == ";") break | ||
semicolon = semicolon.nextSibling | ||
} | ||
|
||
return EnumEntryList( | ||
enumEntries = enumEntries, | ||
trailingComma = null, | ||
terminatingSemicolon = semicolon, | ||
) | ||
} | ||
|
||
var semicolon: PsiElement? = null | ||
var comma: PsiElement? = null | ||
val lastToken = | ||
enumEntries | ||
.last() | ||
.lastChild | ||
.getPrevSiblingIgnoringWhitespaceAndComments(withItself = true)!! | ||
when (lastToken.text) { | ||
"," -> { | ||
comma = lastToken | ||
} | ||
";" -> { | ||
semicolon = lastToken | ||
val prevSibling = semicolon.getPrevSiblingIgnoringWhitespaceAndComments() | ||
if (prevSibling?.text == ",") { | ||
comma = prevSibling | ||
} | ||
} | ||
} | ||
|
||
return EnumEntryList( | ||
enumEntries = enumEntries, | ||
trailingComma = comma, | ||
terminatingSemicolon = semicolon, | ||
) | ||
} | ||
} | ||
} |
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
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