-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
...t-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/ChainWrappingRule.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,70 @@ | ||
package com.github.shyiko.ktlint.ruleset.standard | ||
|
||
import com.github.shyiko.ktlint.core.Rule | ||
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.com.intellij.psi.tree.TokenSet | ||
import org.jetbrains.kotlin.lexer.KtTokens.DOT | ||
import org.jetbrains.kotlin.lexer.KtTokens.ELVIS | ||
import org.jetbrains.kotlin.lexer.KtTokens.SAFE_ACCESS | ||
import org.jetbrains.kotlin.psi.psiUtil.nextLeaf | ||
import org.jetbrains.kotlin.psi.psiUtil.prevLeaf | ||
|
||
class ChainWrappingRule : Rule("chain-wrapping") { | ||
|
||
// *,+,-,/,% position differ in | ||
// https://github.com/yole/kotlin-style-guide/issues/9 | ||
// https://android.github.io/kotlin-guides/style.html#where-to-break | ||
private val sameLineTokens = TokenSet.EMPTY // TokenSet.create(MUL, PLUS, MINUS, DIV, PERC) | ||
private val nextLineTokens = TokenSet.create(DOT, SAFE_ACCESS, ELVIS) | ||
private val noSpaceAroundTokens = TokenSet.create(DOT, SAFE_ACCESS) | ||
|
||
override fun visit(node: ASTNode, autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { | ||
/* | ||
org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement (DOT) | "." | ||
org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) | "\n " | ||
org.jetbrains.kotlin.psi.KtCallExpression (CALL_EXPRESSION) | ||
*/ | ||
val elementType = node.elementType | ||
if (nextLineTokens.contains(elementType)) { | ||
val nextLeaf = node.psi.nextLeaf(true) | ||
if (nextLeaf is PsiWhiteSpaceImpl && nextLeaf.textContains('\n')) { | ||
emit(node.startOffset, "Line must not end with \"${node.text}\"", true) | ||
if (autoCorrect) { | ||
val prevLeaf = node.psi.prevLeaf(true) | ||
if (prevLeaf is PsiWhiteSpaceImpl) { | ||
prevLeaf.rawReplaceWithText(nextLeaf.text) | ||
} else { | ||
(node.psi as LeafPsiElement).rawInsertBeforeMe(PsiWhiteSpaceImpl(nextLeaf.text)) | ||
} | ||
if (noSpaceAroundTokens.contains(elementType)) { | ||
nextLeaf.node.treeParent.removeChild(nextLeaf.node) | ||
} else { | ||
nextLeaf.rawReplaceWithText(" ") | ||
} | ||
} | ||
} | ||
} else | ||
if (sameLineTokens.contains(elementType)) { | ||
val prevLeaf = node.psi.prevLeaf(true) | ||
if (prevLeaf is PsiWhiteSpaceImpl && prevLeaf.textContains('\n')) { | ||
emit(node.startOffset, "Line must not begin with \"${node.text}\"", true) | ||
if (autoCorrect) { | ||
val nextLeaf = node.psi.nextLeaf(true) | ||
if (nextLeaf is PsiWhiteSpaceImpl) { | ||
nextLeaf.rawReplaceWithText(prevLeaf.text) | ||
} else { | ||
(node.psi as LeafPsiElement).rawInsertAfterMe(PsiWhiteSpaceImpl(prevLeaf.text)) | ||
} | ||
if (noSpaceAroundTokens.contains(elementType)) { | ||
prevLeaf.node.treeParent.removeChild(prevLeaf.node) | ||
} else { | ||
prevLeaf.rawReplaceWithText(" ") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
...andard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/ChainWrappingRuleTest.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,14 @@ | ||
package com.github.shyiko.ktlint.ruleset.standard | ||
|
||
import org.testng.annotations.Test | ||
|
||
class ChainWrappingRuleTest { | ||
|
||
@Test | ||
fun testLint() = | ||
testLintUsingResource(ChainWrappingRule()) | ||
|
||
@Test | ||
fun testFormat() = | ||
testFormatUsingResource(ChainWrappingRule()) | ||
} |
9 changes: 9 additions & 0 deletions
9
ktlint-ruleset-standard/src/test/resources/spec/chain-wrapping/format-expected.kt.spec
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,9 @@ | ||
fun main() { | ||
val anchor = owner.firstChild!! | ||
.siblings(forward = true) | ||
.dropWhile { it is PsiComment || it is PsiWhiteSpace } | ||
val s = foo() | ||
?: bar | ||
val s = foo() | ||
?.bar | ||
} |
9 changes: 9 additions & 0 deletions
9
ktlint-ruleset-standard/src/test/resources/spec/chain-wrapping/format.kt.spec
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,9 @@ | ||
fun main() { | ||
val anchor = owner.firstChild!!. | ||
siblings(forward = true). | ||
dropWhile { it is PsiComment || it is PsiWhiteSpace } | ||
val s = foo() ?: | ||
bar | ||
val s = foo()?. | ||
bar | ||
} |
17 changes: 17 additions & 0 deletions
17
ktlint-ruleset-standard/src/test/resources/spec/chain-wrapping/lint.kt.spec
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,17 @@ | ||
fun main() { | ||
val anchor = owner.firstChild!!. | ||
siblings(forward = true). | ||
dropWhile { it is PsiComment || it is PsiWhiteSpace } | ||
val s = foo() ?: | ||
bar | ||
val s = foo()?. | ||
bar | ||
// val s = "foo" | ||
// + "bar" | ||
} | ||
|
||
// expect | ||
// 2:36:Line must not end with "." | ||
// 3:33:Line must not end with "." | ||
// 5:19:Line must not end with "?:" | ||
// 7:18:Line must not end with "?." |