Skip to content

Commit

Permalink
Fix indent of explicit constructor (#2118)
Browse files Browse the repository at this point in the history
* Fix indent of explicit constructor (also see #2115)
* Fix error in `hasNewLineInClosedRange` and `noNewLineInClosedRange`
  • Loading branch information
paul-dingemans committed Jul 9, 2023
1 parent 1e0773f commit b0b93f0
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,7 @@ private fun String.replaceTabAndNewline(): String = replace("\t", "\\t").replace
public fun hasNewLineInClosedRange(
from: ASTNode,
to: ASTNode,
): Boolean =
from.isWhiteSpaceWithNewline() ||
leavesInOpenRange(from, to).any { it.textContains('\n') } ||
to.isWhiteSpaceWithNewline()
): Boolean = leavesInClosedRange(from, to).any { it.textContains('\n') }

/**
* Verifies that no leaf contains a newline in the closed range [from] - [to]. Also, the boundary nodes [from] and [to]
Expand All @@ -345,10 +342,7 @@ public fun hasNewLineInClosedRange(
public fun noNewLineInClosedRange(
from: ASTNode,
to: ASTNode,
): Boolean =
!from.isWhiteSpaceWithNewline() &&
noNewLineInOpenRange(from, to) &&
!to.isWhiteSpaceWithNewline()
): Boolean = leavesInClosedRange(from, to).none { it.textContains('\n') }

/**
* Verifies that no leaf contains a newline in the open range [from] - [to]. This means that the boundary nodes are excluded from the range
Expand All @@ -373,7 +367,27 @@ public fun leavesInOpenRange(
): Sequence<ASTNode> =
from
.leaves()
.takeWhile { it != to && it != to.firstChildNode }
.takeWhile { it != to && it != to.lastChildLeafOrSelf() }

/**
* Creates a sequence of leaf nodes in the closed range [from] - [to]. This means that the boundary nodes are included from the range in
* case they would happen to be a leaf node. In case [from] is a [CompositeElement] than the first leaf node in the sequence is the first
* leaf node in this [CompositeElement]. In case [to] is a [CompositeElement] than the last node in the sequence is the last leaf node of
* this [CompositeElement].
*/
public fun leavesInClosedRange(
from: ASTNode,
to: ASTNode,
): Sequence<ASTNode> {
val stopAtLeaf =
to
.lastChildLeafOrSelf()
.nextLeaf()
return from
.firstChildLeafOrSelf()
.leavesIncludingSelf()
.takeWhile { it != stopAtLeaf }
}

public fun ASTNode.isValOrVarKeyword(): Boolean = elementType == VAL_KEYWORD || elementType == VAR_KEYWORD || elementType == VARARG_KEYWORD

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ class ASTNodeExtensionTest {
)
}

@Test
fun `Given an enum class body then get all leaves in the closed range of the class body`() {
val code =
"""
enum class Shape {
FOO, FOOBAR, BAR
}
""".trimIndent()
val enumClassBody = code.transformAst(::toEnumClassBodySequence)

val actual =
leavesInClosedRange(enumClassBody.first(), enumClassBody.last())
.map { it.text }
.toList()

assertThat(actual).containsExactly(
"{",
"\n ",
"FOO",
",",
" ",
"FOOBAR",
",",
" ",
"BAR",
"\n",
"}",
)
}

@Nested
inner class NoNewLineInOpenRange {
@Test
Expand Down Expand Up @@ -111,14 +141,9 @@ class ASTNodeExtensionTest {
fun `Given an enum class with no whitespace leaf containing a newline between the first and last enum entry`() {
val code =
"""
enum class Shape {
FOO, FOOBAR, BAR
}
enum class Shape { FOO, FOOBAR, BAR }
""".trimIndent()
val enumEntries =
code
.transformAst(::toEnumClassBodySequence)
.filter { it.elementType == ENUM_ENTRY }
val enumEntries = code.transformAst(::toEnumClassBodySequence)

val actual = hasNewLineInClosedRange(enumEntries.first(), enumEntries.last())

Expand All @@ -129,12 +154,16 @@ class ASTNodeExtensionTest {
fun `Given a range of nodes starting with a whitespace leaf containing a newline but other whitespace leaves not containing a newline`() {
val code =
"""
enum class Shape {
FOO, FOOBAR, BAR } // Malformed on purpose for test
enum class Shape
{ FOO, FOOBAR, BAR } // Malformed on purpose for test
""".trimIndent()
val enumClassBody = code.transformAst(::toEnumClassBodySequence)
val enumClass = code.transformAst(::toEnumClassSequence)

val actual = hasNewLineInClosedRange(enumClassBody.first(), enumClassBody.last())
val actual =
hasNewLineInClosedRange(
enumClass.first { it.isWhiteSpaceWithNewline() },
enumClass.last(),
)

assertThat(actual).isTrue
}
Expand Down Expand Up @@ -163,9 +192,13 @@ class ASTNodeExtensionTest {
enum class Shape { FOO, FOOBAR, BAR
} // Malformed on purpose for test
""".trimIndent()
val enumClassBody = code.transformAst(::toEnumClassBodySequence)
val enumBodyClass = code.transformAst(::toEnumClassBodySequence)

val actual = hasNewLineInClosedRange(enumClassBody.first(), enumClassBody.last())
val actual =
hasNewLineInClosedRange(
enumBodyClass.first(),
enumBodyClass.last { it.isWhiteSpaceWithNewline() },
)

assertThat(actual).isTrue
}
Expand Down Expand Up @@ -601,15 +634,21 @@ class ASTNodeExtensionTest {
?.children()
.orEmpty()

private fun toEnumClassSequence(fileASTNode: FileASTNode) =
fileASTNode
.findChildByType(CLASS)
?.children()
.orEmpty()

/**
* A dummy rule for testing. Optionally the rule can be created with a lambda to be executed for each node visited.
*/
private open class DummyRule(
val block: (node: ASTNode) -> Unit = {},
) : Rule(
ruleId = RuleId("test:dummy-rule"),
about = About(),
) {
ruleId = RuleId("test:dummy-rule"),
about = About(),
) {
override fun beforeVisitChildNodes(
node: ASTNode,
autoCorrect: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public open class InternalRule internal constructor(
override val visitorModifiers: Set<VisitorModifier> = emptySet(),
override val usesEditorConfigProperties: Set<EditorConfigProperty<*>> = emptySet(),
) : Rule(
ruleId = RuleId("internal:$id"),
visitorModifiers = visitorModifiers,
usesEditorConfigProperties = usesEditorConfigProperties,
about = INTERNAL_RULE_ABOUT,
)
ruleId = RuleId("internal:$id"),
visitorModifiers = visitorModifiers,
usesEditorConfigProperties = usesEditorConfigProperties,
about = INTERNAL_RULE_ABOUT,
)
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,9 @@ class KtLintTest {
private open class DummyRule(
val block: (node: ASTNode) -> Unit = {},
) : Rule(
ruleId = RuleId("test:dummy"),
about = About(),
) {
ruleId = RuleId("test:dummy"),
about = About(),
) {
override fun beforeVisitChildNodes(
node: ASTNode,
autoCorrect: Boolean,
Expand Down Expand Up @@ -536,10 +536,10 @@ private class SimpleTestRule(
private val stopTraversalInAfterVisitChildNodes: (ASTNode) -> Boolean = { false },
private val stopTraversalInAfterLastNode: Boolean = false,
) : Rule(
ruleId = ruleId,
about = About(),
visitorModifiers,
) {
ruleId = ruleId,
about = About(),
visitorModifiers,
) {
override fun beforeFirstNode(editorConfig: EditorConfig) {
ruleExecutionCalls.add(RuleExecutionCall(ruleId, BEFORE_FIRST))
if (stopTraversalInBeforeFirstNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,10 @@ class RuleProviderSorterTest {
ruleId: RuleId,
visitorModifiers: Set<VisitorModifier> = emptySet(),
) : Rule(
ruleId = ruleId,
about = About(),
visitorModifiers,
) {
ruleId = ruleId,
about = About(),
visitorModifiers,
) {
constructor(ruleId: RuleId, visitorModifier: VisitorModifier) : this(ruleId, setOf(visitorModifier))

override fun beforeVisitChildNodes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class InternalRuleProvidersFilterTest {
ruleId: RuleId,
visitorModifiers: Set<VisitorModifier> = emptySet(),
) : Rule(
ruleId = ruleId,
about = About(),
visitorModifiers,
) {
ruleId = ruleId,
about = About(),
visitorModifiers,
) {
override fun beforeVisitChildNodes(
node: ASTNode,
autoCorrect: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,10 @@ class RunAfterRuleFilterTest {
ruleId: RuleId,
visitorModifiers: Set<VisitorModifier> = emptySet(),
) : Rule(
ruleId = ruleId,
about = About(),
visitorModifiers,
) {
ruleId = ruleId,
about = About(),
visitorModifiers,
) {
constructor(ruleId: RuleId, visitorModifier: VisitorModifier) : this(ruleId, setOf(visitorModifier))

override fun beforeVisitChildNodes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public open class StandardRule internal constructor(
override val visitorModifiers: Set<VisitorModifier> = emptySet(),
override val usesEditorConfigProperties: Set<EditorConfigProperty<*>> = emptySet(),
) : Rule(
ruleId = RuleId("${RuleSetId.STANDARD.value}:$id"),
visitorModifiers = visitorModifiers,
usesEditorConfigProperties = usesEditorConfigProperties,
about = STANDARD_RULE_ABOUT,
)
ruleId = RuleId("${RuleSetId.STANDARD.value}:$id"),
visitorModifiers = visitorModifiers,
usesEditorConfigProperties = usesEditorConfigProperties,
about = STANDARD_RULE_ABOUT,
)

0 comments on commit b0b93f0

Please sign in to comment.