-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add multiline-loop to complement multiline-if-else
- Loading branch information
Showing
4 changed files
with
368 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
...standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MultiLineLoopRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package com.pinterest.ktlint.ruleset.standard.rules | ||
|
||
import com.pinterest.ktlint.rule.engine.core.api.ElementType.BLOCK | ||
import com.pinterest.ktlint.rule.engine.core.api.ElementType.BODY | ||
import com.pinterest.ktlint.rule.engine.core.api.ElementType.DO_KEYWORD | ||
import com.pinterest.ktlint.rule.engine.core.api.ElementType.LBRACE | ||
import com.pinterest.ktlint.rule.engine.core.api.ElementType.RBRACE | ||
import com.pinterest.ktlint.rule.engine.core.api.ElementType.RPAR | ||
import com.pinterest.ktlint.rule.engine.core.api.IndentConfig | ||
import com.pinterest.ktlint.rule.engine.core.api.RuleId | ||
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint | ||
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.EXPERIMENTAL | ||
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfig | ||
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_SIZE_PROPERTY | ||
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_STYLE_PROPERTY | ||
import com.pinterest.ktlint.rule.engine.core.api.indent | ||
import com.pinterest.ktlint.rule.engine.core.api.isPartOfComment | ||
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpace | ||
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithoutNewline | ||
import com.pinterest.ktlint.rule.engine.core.api.nextSibling | ||
import com.pinterest.ktlint.rule.engine.core.api.upsertWhitespaceBeforeMe | ||
import com.pinterest.ktlint.ruleset.standard.StandardRule | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement | ||
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl | ||
import org.jetbrains.kotlin.psi.KtBlockExpression | ||
import org.jetbrains.kotlin.psi.psiUtil.leaves | ||
|
||
/** | ||
* https://developer.android.com/kotlin/style-guide#braces | ||
*/ | ||
@SinceKtlint("1.0", EXPERIMENTAL) | ||
public class MultiLineLoopRule : | ||
StandardRule( | ||
id = "multiline-loop", | ||
usesEditorConfigProperties = | ||
setOf( | ||
INDENT_SIZE_PROPERTY, | ||
INDENT_STYLE_PROPERTY, | ||
), | ||
) { | ||
private var indentConfig = IndentConfig.DEFAULT_INDENT_CONFIG | ||
|
||
override fun beforeFirstNode(editorConfig: EditorConfig) { | ||
indentConfig = | ||
IndentConfig( | ||
indentStyle = editorConfig[INDENT_STYLE_PROPERTY], | ||
tabWidth = editorConfig[INDENT_SIZE_PROPERTY], | ||
) | ||
} | ||
|
||
override fun beforeVisitChildNodes( | ||
node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit, | ||
) { | ||
if (node.elementType != BODY) { | ||
return | ||
} | ||
|
||
// Ignore when already wrapped in a block | ||
if (node.firstChildNode?.elementType == BLOCK) { | ||
return | ||
} | ||
|
||
if (!node.treePrev.textContains('\n')) { | ||
if (!node.treeParent.textContains('\n')) { | ||
// Allow single line loop statements as long as they are really simple (e.g. do not contain newlines) | ||
// for (...) <statement> | ||
// while (...) <statement> | ||
// do <statement> while (...) | ||
return | ||
} | ||
|
||
Unit | ||
} | ||
|
||
emit(node.firstChildNode.startOffset, "Missing { ... }", true) | ||
if (autoCorrect) { | ||
autocorrect(node) | ||
} | ||
} | ||
|
||
private fun autocorrect(node: ASTNode) { | ||
val prevLeaves = | ||
node | ||
.leaves(forward = false) | ||
.takeWhile { it.elementType !in listOf(RPAR, DO_KEYWORD) } | ||
.toList() | ||
.reversed() | ||
val nextLeaves = | ||
node | ||
.leaves(forward = true) | ||
.takeWhile { it.isWhiteSpaceWithoutNewline() || it.isPartOfComment() } | ||
.toList() | ||
.dropLastWhile { it.isWhiteSpaceWithoutNewline() } | ||
|
||
prevLeaves | ||
.firstOrNull() | ||
.takeIf { it.isWhiteSpace() } | ||
?.let { | ||
(it as LeafPsiElement).rawReplaceWithText(" ") | ||
} | ||
KtBlockExpression(null).apply { | ||
val previousChild = node.firstChildNode | ||
node.replaceChild(node.firstChildNode, this) | ||
addChild(LeafPsiElement(LBRACE, "{")) | ||
addChild(PsiWhiteSpaceImpl(indentConfig.childIndentOf(node))) | ||
prevLeaves | ||
.dropWhile { it.isWhiteSpace() } | ||
.forEach(::addChild) | ||
addChild(previousChild) | ||
nextLeaves.forEach(::addChild) | ||
addChild(PsiWhiteSpaceImpl(node.indent())) | ||
addChild(LeafPsiElement(RBRACE, "}")) | ||
} | ||
|
||
// Make sure while starts on same line as newly inserted right brace | ||
if (node.elementType == BODY) { | ||
node | ||
.nextSibling { !it.isPartOfComment() } | ||
?.upsertWhitespaceBeforeMe(" ") | ||
} | ||
} | ||
} | ||
|
||
public val MULTI_LINE_LOOP_RULE_ID: RuleId = MultiLineLoopRule().ruleId |
229 changes: 229 additions & 0 deletions
229
...dard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MultiLineLoopRuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
package com.pinterest.ktlint.ruleset.standard.rules | ||
|
||
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule | ||
import com.pinterest.ktlint.test.LintViolation | ||
import org.junit.jupiter.api.Test | ||
|
||
class MultiLineLoopRuleTest { | ||
private val multiLineLoopRuleAssertThat = assertThatRule { MultiLineLoopRule() } | ||
|
||
@Test | ||
fun `Given loop statements with curly braces on single line`() { | ||
val code = | ||
""" | ||
fun foo() { | ||
for (c in "Hello World") { return 0 } | ||
while (true) { return 0 } | ||
do { return 0 } while (true) | ||
} | ||
""".trimIndent() | ||
multiLineLoopRuleAssertThat(code).hasNoLintViolations() | ||
} | ||
|
||
@Test | ||
fun `Given loop statements without curly braces on single line`() { | ||
val code = | ||
""" | ||
fun foo() { | ||
for (c in "Hello World") return 0 | ||
while (true) return 0 | ||
do return 0 while (true) | ||
} | ||
""".trimIndent() | ||
multiLineLoopRuleAssertThat(code).hasNoLintViolations() | ||
} | ||
|
||
@Test | ||
fun `Given multiline loop statements with curly braces`() { | ||
val code = | ||
""" | ||
fun foo() { | ||
for (c in "Hello World") { | ||
return 0 | ||
} | ||
while (true) { | ||
return 0 | ||
} | ||
do { | ||
return 0 | ||
} while (true) | ||
} | ||
""".trimIndent() | ||
multiLineLoopRuleAssertThat(code).hasNoLintViolations() | ||
} | ||
|
||
@Test | ||
fun `Given multiline loop statements without curly braces`() { | ||
val code = | ||
""" | ||
fun foo() { | ||
for (c in "Hello World") | ||
return 0 | ||
while (true) | ||
return 0 | ||
do | ||
return 0 | ||
while (true) | ||
} | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
fun foo() { | ||
for (c in "Hello World") { | ||
return 0 | ||
} | ||
while (true) { | ||
return 0 | ||
} | ||
do { | ||
return 0 | ||
} while (true) | ||
} | ||
""".trimIndent() | ||
multiLineLoopRuleAssertThat(code) | ||
.hasLintViolations( | ||
LintViolation(3, 9, "Missing { ... }"), | ||
LintViolation(5, 9, "Missing { ... }"), | ||
LintViolation(7, 9, "Missing { ... }"), | ||
) | ||
.isFormattedAs(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `Given deep nested loop statements without curly braces`() { | ||
val code = | ||
""" | ||
fun main() { | ||
for (c in "Hello World") | ||
while (true) | ||
do | ||
return 0 | ||
while (true) | ||
} | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
fun main() { | ||
for (c in "Hello World") { | ||
while (true) { | ||
do { | ||
return 0 | ||
} while (true) | ||
} | ||
} | ||
} | ||
""".trimIndent() | ||
multiLineLoopRuleAssertThat(code) | ||
.hasLintViolations( | ||
LintViolation(3, 9, "Missing { ... }"), | ||
LintViolation(4, 13, "Missing { ... }"), | ||
LintViolation(5, 17, "Missing { ... }"), | ||
).isFormattedAs(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `Given loop statements inside a lambda`() { | ||
val code = | ||
""" | ||
fun test(s: String?): Int { | ||
val i = s.let { | ||
for (c in s) | ||
1 | ||
while (true) | ||
2 | ||
do | ||
3 | ||
while (true) | ||
} ?: 0 | ||
return i | ||
} | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
fun test(s: String?): Int { | ||
val i = s.let { | ||
for (c in s) { | ||
1 | ||
} | ||
while (true) { | ||
2 | ||
} | ||
do { | ||
3 | ||
} while (true) | ||
} ?: 0 | ||
return i | ||
} | ||
""".trimIndent() | ||
multiLineLoopRuleAssertThat(code) | ||
.hasLintViolations( | ||
LintViolation(4, 13, "Missing { ... }"), | ||
LintViolation(6, 13, "Missing { ... }"), | ||
LintViolation(8, 13, "Missing { ... }"), | ||
).isFormattedAs(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `Given a do-while-statement with do keyword on same line as main statement`() { | ||
val code = | ||
""" | ||
fun foo() { | ||
do return 0 | ||
while (true) | ||
} | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
fun foo() { | ||
do { | ||
return 0 | ||
} while (true) | ||
} | ||
""".trimIndent() | ||
multiLineLoopRuleAssertThat(code) | ||
.hasLintViolations( | ||
LintViolation(2, 8, "Missing { ... }"), | ||
).isFormattedAs(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `Given loop statements with multiline statement starting on same line as loop`() { | ||
val code = | ||
""" | ||
fun foo() { | ||
for (c in "Hello World") 25 | ||
.toString() | ||
while (true) 50 | ||
.toString() | ||
do 75 | ||
.toString() | ||
while (true) | ||
} | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
fun foo() { | ||
for (c in "Hello World") { | ||
25 | ||
.toString() | ||
} | ||
while (true) { | ||
50 | ||
.toString() | ||
} | ||
do { | ||
75 | ||
.toString() | ||
} while (true) | ||
} | ||
""".trimIndent() | ||
multiLineLoopRuleAssertThat(code) | ||
.addAdditionalRuleProvider { IndentationRule() } | ||
.hasLintViolations( | ||
LintViolation(2, 30, "Missing { ... }"), | ||
LintViolation(4, 18, "Missing { ... }"), | ||
LintViolation(6, 8, "Missing { ... }"), | ||
) | ||
.isFormattedAs(formattedCode) | ||
} | ||
} |