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

Supported comments after latest enum element #1302

Merged
merged 13 commits into from
May 27, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import org.cqfn.diktat.ruleset.utils.getAllChildrenWithType
import org.cqfn.diktat.ruleset.utils.hasChildOfType
import org.cqfn.diktat.ruleset.utils.isClassEnum

import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.CLASS
import com.pinterest.ktlint.core.ast.ElementType.CLASS_BODY
import com.pinterest.ktlint.core.ast.ElementType.COMMA
import com.pinterest.ktlint.core.ast.ElementType.ENUM_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER
import com.pinterest.ktlint.core.ast.ElementType.LBRACE
import com.pinterest.ktlint.core.ast.ElementType.RBRACE
Expand Down Expand Up @@ -87,10 +89,19 @@ class EnumsSeparated(configRules: List<RulesConfig>) : DiktatRule(
if (!node.hasChildOfType(COMMA)) {
ENUMS_SEPARATED.warnAndFix(configRules, emitWarn, isFixMode, "last enum entry must end with a comma",
node.startOffset, node) {
node.addChild(LeafPsiElement(COMMA, ","), node.findChildByType(SEMICOLON)!!.treePrev)
val commaLocation = node.findChildByType(SEMICOLON)!!.findLatestTreePrevMatching {
!setOf(EOL_COMMENT, BLOCK_COMMENT, WHITE_SPACE).contains(it.elementType)
petertrr marked this conversation as resolved.
Show resolved Hide resolved
}
node.addChild(LeafPsiElement(COMMA, ","), commaLocation.treeNext)
}
}
}

private fun ASTNode.findLatestTreePrevMatching(predicate: (ASTNode) -> Boolean): ASTNode {
val result = this.treePrev
return if (predicate(result)) result else result.findLatestTreePrevMatching(predicate)
}

companion object {
const val NAME_ID = "abq-enum-separated"
private val simpleValue = listOf(IDENTIFIER, WHITE_SPACE, COMMA, SEMICOLON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ class EnumsSeparatedFixTest : FixTestBase("test/paragraph3/enum_separated", ::En
fun `test enums split`() {
fixAndCompare("EnumSeparatedExpected.kt", "EnumSeparatedTest.kt")
}

@Test
@Tag(WarningNames.ENUMS_SEPARATED)
fun `last element with comment`() {
fixAndCompare("LastElementCommentExpected.kt", "LastElementCommentTest.kt")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ enum class ENUM {
enum class ENUM {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF) /*sdcsc*/,
BLUE(0x0000FF), /*sdcsc*/
;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package test.paragraph3.enum_separated

enum class Foo1 {
A,
B,
C, // some commnet
;
}

enum class Foo2 {
A,
B,
C,// some commnet
;
}

enum class Foo3 {
A,
B,
C, /* some commnet */
;
}

enum class Foo4 {
A,
B,
C,/* some commnet */
;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package test.paragraph3.enum_separated

enum class Foo1 {
A,
B,
C // some commnet
}

enum class Foo2 {
A,
B,
C// some commnet
}

enum class Foo3 {
A,
B,
C /* some commnet */
}

enum class Foo4 {
A,
B,
C/* some commnet */
petertrr marked this conversation as resolved.
Show resolved Hide resolved
}