Skip to content

Commit

Permalink
Made max-line-length rule skip package/import directives (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
shyiko committed Jul 24, 2017
1 parent 395ebc7 commit 1cd7077
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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.KtImportDirective
import org.jetbrains.kotlin.psi.KtPackageDirective
import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespaceAndComments
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
Expand All @@ -28,7 +30,9 @@ class MaxLineLengthRule : Rule("max-line-length") {
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)
if (!el.isPartOf(KtPackageDirective::class) && !el.isPartOf(KtImportDirective::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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.omg.sooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.long
import com.omg.soooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.long
// http://______________________________________________________________________.
fun main() {
// comment padded with spaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaace
Expand All @@ -6,5 +8,5 @@ fun main() {
}

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

0 comments on commit 1cd7077

Please sign in to comment.