Skip to content

Commit

Permalink
Allow new-line after lambda return type
Browse files Browse the repository at this point in the history
  • Loading branch information
romtsn committed Nov 13, 2019
1 parent 6864374 commit fe5a041
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.pinterest.ktlint.ruleset.standard

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.*
import com.pinterest.ktlint.core.ast.ElementType.AT
import com.pinterest.ktlint.core.ast.ElementType.CLASS_BODY
import com.pinterest.ktlint.core.ast.ElementType.COLONCOLON
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 All @@ -16,11 +18,6 @@ import com.pinterest.ktlint.core.ast.ElementType.RBRACKET
import com.pinterest.ktlint.core.ast.ElementType.RPAR
import com.pinterest.ktlint.core.ast.ElementType.SAFE_ACCESS
import com.pinterest.ktlint.core.ast.ElementType.SEMICOLON
import com.pinterest.ktlint.core.ast.isPartOfString
import com.pinterest.ktlint.core.ast.nextLeaf
import com.pinterest.ktlint.core.ast.prevLeaf
import com.pinterest.ktlint.core.ast.upsertWhitespaceAfterMe
import com.pinterest.ktlint.core.ast.upsertWhitespaceBeforeMe
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
Expand All @@ -39,7 +36,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 +56,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 fe5a041

Please sign in to comment.