Skip to content

Commit

Permalink
Fixed #262 - skip max-line-length check inside multi-line strings
Browse files Browse the repository at this point in the history
  • Loading branch information
shyiko committed Jul 30, 2018
1 parent 84d8931 commit ac0429a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MaxLineLengthRule : Rule("max-line-length"), Rule.Modifier.Last {
for (line in lines) {
if (line.length > maxLineLength) {
val el = node.psi.findElementAt(offset + line.length - 1)!!
if (!el.isPartOf(KDoc::class)) {
if (!el.isPartOf(KDoc::class) && !el.isPartOfMultiLineString()) {
if (!el.isPartOf(PsiComment::class)) {
if (!el.isPartOf(KtPackageDirective::class) && !el.isPartOf(KtImportDirective::class)) {
// fixme:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.psi.KtStringTemplateEntry
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
import kotlin.reflect.KClass

internal fun PsiElement.isPartOf(clazz: KClass<out PsiElement>) = getNonStrictParentOfType(clazz.java) != null
internal fun PsiElement.isPartOfString() = isPartOf(KtStringTemplateEntry::class)
internal fun PsiElement.isPartOfMultiLineString(): Boolean {
val stringTemplate = getNonStrictParentOfType(KtStringTemplateExpression::class.java)
return stringTemplate != null &&
stringTemplate.firstChild.textMatches("\"\"\"") &&
stringTemplate.children.any { it.textMatches("\n") }
}
internal fun PsiElement.prevLeaf(): PsiElement? = PsiTreeUtil.prevLeaf(this)
internal fun PsiElement.nextLeaf(): PsiElement? = PsiTreeUtil.nextLeaf(this)
internal fun ASTNode.visit(cb: (node: ASTNode) -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ fun main() {
// comment padded with spaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaace
println("__________________________________________________________________")
println("") // too looooooooooooooooooooooooooooooooooooooooooooooooooooooong
println("8_____________${"$"}_____________________________________\n_______")
println(
"""10________________${"$"}_____________________________________________"""
)
println(
"""
14___________________${"$"}_____________________________________________"""
)
val long = """
17______________________________________________________________________________.
19
"""
println(
"""
23______________________________________________________________________________.$v
24______________________________________________________________________________.${f()}
"""
)
}

/**
Expand All @@ -14,3 +33,5 @@ fun main() {
// expect
// 6:1:Exceeded max line length (80)
// 7:1:Exceeded max line length (80)
// 8:1:Exceeded max line length (80)
// 10:1:Exceeded max line length (80)

0 comments on commit ac0429a

Please sign in to comment.