-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
...t-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/MaxLineLengthRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...andard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/MaxLineLengthRuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
ktlint-ruleset-standard/src/test/resources/spec/max-line-length/lint.kt.spec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |