Skip to content

Commit

Permalink
Added max-line-length rule (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
shyiko committed Jul 23, 2017
1 parent 89955c7 commit 9663c1e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.shyiko.ktlint.ruleset.standard

import com.github.shyiko.ktlint.core.KtLint
import com.github.shyiko.ktlint.core.Rule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiComment
import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespaceAndComments
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes

class MaxLineLengthRule : Rule("max-line-length") {

private var maxLineLength = 0

override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
if (node.elementType == KtStubElementTypes.FILE) {
val editorConfig = node.getUserData(KtLint.EDITOR_CONFIG_USER_DATA_KEY)!!
editorConfig.get("max_line_length")?.toInt()?.apply {
maxLineLength = this
}
if (maxLineLength <= 0) {
return
}
val text = node.text
val lines = text.split("\n")
var offset = 0
lines.forEachIndexed { i, line ->
if (line.length > maxLineLength) {
val el = node.psi.findElementAt(offset + line.length - 1)!!
if (!el.isPartOf(PsiComment::class)) {
emit(offset, "Exceeded max line length ($maxLineLength)", false)
} else {
// if comment is the only thing on the line - fine, otherwise emit an error
val prevLeaf = el.getPrevSiblingIgnoringWhitespaceAndComments(false)
if (prevLeaf != null && prevLeaf.startOffset >= offset) {
emit(offset, "Exceeded max line length ($maxLineLength)", false)
}
}
}
offset += line.length + 1
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.shyiko.ktlint.ruleset.standard

import org.testng.annotations.Test

class MaxLineLengthRuleTest {

@Test
fun testLint() {
testLintUsingResource(MaxLineLengthRule(), userData = mapOf("max_line_length" to "80"))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fun getResourceAsText(path: String) =
.bufferedReader()
.readText()

fun testLintUsingResource(rule: Rule, qualifier: String = "") {
fun testLintUsingResource(rule: Rule, qualifier: String = "", userData: Map<String, String> = emptyMap()) {
val resource = "spec/${rule.id}/lint${if (qualifier.isEmpty()) "" else "-$qualifier"}.kt.spec"
val resourceText = getResourceAsText(resource)
val dividerIndex = resourceText.lastIndexOf("\n// expect\n")
Expand All @@ -29,7 +29,7 @@ fun testLintUsingResource(rule: Rule, qualifier: String = "") {
LintError(it[0].toInt(), it[1].toInt(), rule.id, it[2])
}
}
assertThat(rule.lint(input)).isEqualTo(errors)
assertThat(rule.lint(input, userData)).isEqualTo(errors)
}

fun testFormatUsingResource(rule: Rule, qualifier: String = "") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// http://______________________________________________________________________.
fun main() {
// comment padded with spaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaace
println("__________________________________________________________________")
println("") // too looooooooooooooooooooooooooooooooooooooooooooooooooooooong
}

// expect
// 4:1:Exceeded max line length (80)
// 5:1:Exceeded max line length (80)

0 comments on commit 9663c1e

Please # to comment.