Skip to content

Commit

Permalink
Do not wrap values in a single line enum when it is preceded by a com…
Browse files Browse the repository at this point in the history
…ment or an annotation (#2229)
  • Loading branch information
paul-dingemans authored Sep 4, 2023
1 parent 698d976 commit 955d5e0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ If an `EditorConfigProperty` is defined in a `Rule` that is only provided via a
* Do not wrap expression after a spread operator `multiline-expression-wrapping` [#2188](https://github.com/pinterest/ktlint/issues/2188)
* Prevent cyclic dependency between `modifier-list-spacing` rule and `indent` rule when the super type call entry is annotated ([#2227](https://github.com/pinterest/ktlint/pull/2227))
* Do not remove parenthesis after explicit constructor keyword when it has no parameters ([#2226](https://github.com/pinterest/ktlint/pull/2226))
* Do not wrap values in a single line enum when it is preceded by a comment or an annotation ([#2229](https://github.com/pinterest/ktlint/pull/2229))=======
* Fix indentation of super type list of class in case it is preceded by a comment ([#2228](https://github.com/pinterest/ktlint/pull/2228))
* Allow a super type list starting with an annotation having a parameter to be on the same line as the closing parenthesis of the enclosing class ([#2230](https://github.com/pinterest/ktlint/pull/2230))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.ElementType
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ANNOTATION_ENTRY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ARROW
import com.pinterest.ktlint.rule.engine.core.api.ElementType.BLOCK
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS_BODY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUNCTION_LITERAL
import com.pinterest.ktlint.rule.engine.core.api.ElementType.LBRACE
import com.pinterest.ktlint.rule.engine.core.api.ElementType.MODIFIER_LIST
import com.pinterest.ktlint.rule.engine.core.api.ElementType.RBRACE
import com.pinterest.ktlint.rule.engine.core.api.ElementType.SEMICOLON
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER_LIST
Expand All @@ -21,6 +23,7 @@ import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_SIZE_PROPER
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_STYLE_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.firstChildLeafOrSelf
import com.pinterest.ktlint.rule.engine.core.api.indent
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpace
import com.pinterest.ktlint.rule.engine.core.api.lastChildLeafOrSelf
import com.pinterest.ktlint.rule.engine.core.api.nextCodeLeaf
import com.pinterest.ktlint.rule.engine.core.api.noNewLineInClosedRange
Expand Down Expand Up @@ -94,6 +97,9 @@ public class StatementWrappingRule :
}?.takeUnless {
// Allow
// enum class FooBar { FOO, BAR }
// or
// /** Some comment */
// enum class FooBar { FOO, BAR }
it.treeParent.isEnumClassOnSingleLine
}?.findChildByType(LBRACE)
?.applyIf(node.isFunctionLiteralWithParameterList) {
Expand Down Expand Up @@ -154,9 +160,24 @@ public class StatementWrappingRule :

private inline val ASTNode.isEnumClassOnSingleLine: Boolean
get() =
takeIf { psi.isEnumClass }
?.let { !it.textContains('\n') }
?: false
if (psi.isEnumClass) {
val lastChildLeaf = lastChildLeafOrSelf()
// Ignore the leading comment
noNewLineInClosedRange(firstCodeLeafOrNull!!, lastChildLeaf)
} else {
false
}

private inline val ASTNode.firstCodeLeafOrNull: ASTNode?
get() =
// Skip the comment on top of the node by getting modifier list
findChildByType(MODIFIER_LIST)
?.children()
?.dropWhile {
// Ignore annotations placed on separate lines above the node
it.elementType == ANNOTATION_ENTRY || it.isWhiteSpace()
}?.firstOrNull()
?.firstChildLeafOrSelf()

private inline val PsiElement.isEnumClass: Boolean
get() = (this as? KtClass)?.isEnum() ?: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1223,4 +1223,26 @@ class StatementWrappingRuleTest {
).isFormattedAs(formattedCode)
}
}

@Test
fun `Given a single line enumeration class preceded by a comment`() {
val code =
"""
/**
* Some comment
*/
enum class Foobar { FOO, BAR }
""".trimIndent()
statementWrappingRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Given a single line enumeration class preceded by one or more annotations`() {
val code =
"""
@FooBar
enum class Foobar { FOO, BAR }
""".trimIndent()
statementWrappingRuleAssertThat(code).hasNoLintViolations()
}
}

0 comments on commit 955d5e0

Please sign in to comment.