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

Fix indent of explicit constructor #2118

Merged
merged 3 commits into from
Jul 9, 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
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,
)
Loading