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

Do not wrap values in a single line enum when it is preceded by a comment or an annotation #2229

Merged
merged 2 commits into from
Sep 4, 2023
Merged
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
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))

### Changed
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()
}
}