Skip to content

Commit

Permalink
Fixed no-consecutive-blank-lines not triggering at the end of file (w…
Browse files Browse the repository at this point in the history
…hen exactly 2 blank lines are present) (#108)
  • Loading branch information
shyiko committed Nov 28, 2017
1 parent 0a262d5 commit 3b9bb1b
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import com.github.shyiko.ktlint.core.Rule
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
import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil

class NoConsecutiveBlankLinesRule : Rule("no-consecutive-blank-lines") {

override fun visit(node: ASTNode, autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) {
if (node is PsiWhiteSpace) {
val split = node.getText().split("\n")
if (split.size > 3) {
if (split.size > 3 || split.size == 3 && PsiTreeUtil.nextLeaf(node) == null /* eof */) {
emit(node.startOffset + split[0].length + split[1].length + 2, "Needless blank line(s)", true)
if (autoCorrect) {
(node as LeafPsiElement).rawReplaceWithText("${split.first()}\n\n${split.last()}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ class NoConsecutiveBlankLinesRuleTest {
LintError(5, 1, "no-consecutive-blank-lines", "Needless blank line(s)")))
}

@Test
fun testLintAtTheEndOfFile() {
assertThat(NoConsecutiveBlankLinesRule().lint(
"""
fun main() {
}
""".trimIndent()
)).isEqualTo(listOf(
LintError(4, 1, "no-consecutive-blank-lines", "Needless blank line(s)")))
}

@Test
fun testLintInString() {
assertThat(NoConsecutiveBlankLinesRule().lint(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ class SpacingAroundCommaRuleTest {
.isEqualTo("fun main() { x(1, 3); x(1, 3) }")
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,3 @@ class SpacingAroundKeywordRuleTest {
))
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ class NoVarRuleTest : Spek({
}
}
})

0 comments on commit 3b9bb1b

Please # to comment.