diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/MaxLineLengthRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/MaxLineLengthRule.kt index aa7cca165d..40f88691ea 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/MaxLineLengthRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/MaxLineLengthRule.kt @@ -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: diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/package.kt b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/package.kt index d9e03b1e7b..3eea0ec22b 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/package.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/package.kt @@ -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) = 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) { diff --git a/ktlint-ruleset-standard/src/test/resources/spec/max-line-length/lint.kt.spec b/ktlint-ruleset-standard/src/test/resources/spec/max-line-length/lint.kt.spec index de3ebd6665..0d017cb0ef 100644 --- a/ktlint-ruleset-standard/src/test/resources/spec/max-line-length/lint.kt.spec +++ b/ktlint-ruleset-standard/src/test/resources/spec/max-line-length/lint.kt.spec @@ -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()} + """ + ) } /** @@ -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)