Skip to content

Commit

Permalink
Allow new-line after lambda return type
Browse files Browse the repository at this point in the history
Revert imports
  • Loading branch information
romtsn committed Nov 13, 2019
1 parent 6864374 commit 598937f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.pinterest.ktlint.core.ast.ElementType.COMMA
import com.pinterest.ktlint.core.ast.ElementType.DOT
import com.pinterest.ktlint.core.ast.ElementType.EXCLEXCL
import com.pinterest.ktlint.core.ast.ElementType.FUN
import com.pinterest.ktlint.core.ast.ElementType.LAMBDA_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.LBRACE
import com.pinterest.ktlint.core.ast.ElementType.LBRACKET
import com.pinterest.ktlint.core.ast.ElementType.LPAR
Expand Down Expand Up @@ -39,7 +40,7 @@ class SpacingAroundCurlyRule : Rule("curly-spacing") {
val nextLeaf = node.nextLeaf()
val spacingBefore: Boolean
val spacingAfter: Boolean
if (node.textMatches("{")) {
if (node.elementType == LBRACE) {
spacingBefore =
prevLeaf is PsiWhiteSpace ||
prevLeaf?.elementType == AT ||
Expand All @@ -59,22 +60,17 @@ class SpacingAroundCurlyRule : Rule("curly-spacing") {
prevLeaf.node.treeParent.removeChild(prevLeaf.node)
}
}
if (prevLeaf is PsiWhiteSpace &&
prevLeaf.textContains('\n') &&
(
prevLeaf.prevLeaf()?.let {
it.elementType == RPAR || KtTokens.KEYWORDS.contains(it.elementType)
} == true ||
node.treeParent.elementType == CLASS_BODY ||
prevLeaf.treeParent.elementType == FUN
)
if (prevLeaf is PsiWhiteSpace && prevLeaf.textContains('\n') &&
(prevLeaf.prevLeaf()?.let { it.elementType == RPAR || KtTokens.KEYWORDS.contains(it.elementType) } == true ||
node.treeParent.elementType == CLASS_BODY ||
(prevLeaf.treeParent.elementType == FUN && prevLeaf.treeNext.elementType != LAMBDA_EXPRESSION)) // allow newline for lambda return type
) {
emit(node.startOffset, "Unexpected newline before \"${node.text}\"", true)
if (autoCorrect) {
(prevLeaf as LeafPsiElement).rawReplaceWithText(" ")
}
}
} else if (node.textMatches("}")) {
} else if (node.elementType == RBRACE) {
spacingBefore = prevLeaf is PsiWhiteSpace || prevLeaf?.elementType == LBRACE
spacingAfter = nextLeaf == null || nextLeaf is PsiWhiteSpace || shouldNotToBeSeparatedBySpace(nextLeaf)
if (nextLeaf is PsiWhiteSpace && !nextLeaf.textContains('\n') &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,26 @@ class SpacingAroundCurlyRuleTest {
""".trimIndent()
)
}

@Test
fun `lint new line after lambda return type passes`() {
assertThat(SpacingAroundCurlyRule().lint("""
fun magicNumber1(): () -> Int = { 37 }
fun magicNumber2(): () -> Int =
{ 42 }
""".trimIndent())).isEmpty()
}

@Test
fun `format new line after lambda return type passes`() {
assertThat(SpacingAroundCurlyRule().format("""
fun magicNumber1(): () -> Int = { 37 }
fun magicNumber2(): () -> Int =
{ 42 }
""".trimIndent())).isEqualTo("""
fun magicNumber1(): () -> Int = { 37 }
fun magicNumber2(): () -> Int =
{ 42 }
""".trimIndent())
}
}

0 comments on commit 598937f

Please sign in to comment.