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

Allow new-line after lambda return type #643

Merged
merged 1 commit into from
Nov 17, 2019
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 @@ -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())
}
}