Skip to content

Commit

Permalink
Lint/format needless blank lines in dot qualified expression (pintere…
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Dingemans committed Mar 11, 2021
1 parent 338a9a4 commit 72caf39
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.pinterest.ktlint.ruleset.standard

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType.CLASS
import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER
import com.pinterest.ktlint.core.ast.ElementType.PRIMARY_CONSTRUCTOR
import com.pinterest.ktlint.core.ast.nextLeaf
Expand Down Expand Up @@ -29,14 +30,15 @@ public class NoConsecutiveBlankLinesRule : Rule("no-consecutive-blank-lines") {
val betweenClassAndPrimaryConstructor = prevNode.elementType == IDENTIFIER &&
prevNode.treeParent.elementType == CLASS &&
node.treeNext.elementType == PRIMARY_CONSTRUCTOR
if (lfcount > 2 || eof || betweenClassAndPrimaryConstructor) {
val inDotQualifiedExpression = node.treeParent.elementType == DOT_QUALIFIED_EXPRESSION && lfcount > 1
if (lfcount > 2 || eof || betweenClassAndPrimaryConstructor || inDotQualifiedExpression) {
val split = text.split("\n")
emit(node.startOffset + split[0].length + split[1].length + 2, "Needless blank line(s)", true)
if (autoCorrect) {
val newText = buildString {
append(split.first())
append("\n")
if (!eof && !betweenClassAndPrimaryConstructor) append("\n")
if (!eof && !betweenClassAndPrimaryConstructor && !inDotQualifiedExpression) append("\n")
append(split.last())
}
(node as LeafPsiElement).rawReplaceWithText(newText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,27 @@ class NoConsecutiveBlankLinesRuleTest {
)
).isEmpty()
}

@Test
fun `should remove line in dot qualified expression`() {
assertThat(
NoConsecutiveBlankLinesRule().format(
"""
fun foo(inputText: String) {
inputText
.toLowerCase()
}
""".trimIndent()
)
).isEqualTo(
"""
fun foo(inputText: String) {
inputText
.toLowerCase()
}
""".trimIndent()
)
}
}

0 comments on commit 72caf39

Please sign in to comment.