diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/ParameterListWrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/ParameterListWrappingRule.kt index dc75a4ae06..3117727bca 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/ParameterListWrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/ParameterListWrappingRule.kt @@ -15,6 +15,7 @@ import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes class ParameterListWrappingRule : Rule("parameter-list-wrapping") { private var indentSize = -1 + private var maxLineLength = -1 override fun visit( node: ASTNode, @@ -24,6 +25,7 @@ class ParameterListWrappingRule : Rule("parameter-list-wrapping") { if (node.elementType == KtStubElementTypes.FILE) { val ec = EditorConfig.from(node as FileASTNode) indentSize = ec.indentSize + maxLineLength = ec.maxLineLength return } if (indentSize <= 0) { @@ -37,8 +39,12 @@ class ParameterListWrappingRule : Rule("parameter-list-wrapping") { // - maxLineLength exceeded (and separating parameters with \n would actually help) // in addition, "(" and ")" must be on separates line if any of the parameters are (otherwise on the same) val putParametersOnSeparateLines = node.textContains('\n') - // maxLineLength > 0 && node.lineLength() > maxLineLength - if (putParametersOnSeparateLines) { + + val maxLineLengthExceeded = + if (maxLineLength > -1) (node.startOffset + node.textLength) > maxLineLength + else false + + if (putParametersOnSeparateLines || maxLineLengthExceeded) { // aiming for // ... LPAR // VALUE_PARAMETER... diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/ParameterListWrappingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/ParameterListWrappingRuleTest.kt index 428af330ee..d333544368 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/ParameterListWrappingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/ParameterListWrappingRuleTest.kt @@ -27,6 +27,25 @@ class ParameterListWrappingRuleTest { ) } + @Test + fun testLintClassParameterListWhenMaxLineLengthExceeded() { + assertThat( + ParameterListWrappingRule().lint( + """ + class ClassA(paramA: String, paramB: String, paramC: String) + """.trimIndent(), + userData = mapOf("max_line_length" to "10") + ) + ).isEqualTo( + listOf( + LintError(1, 14, "parameter-list-wrapping", "Parameter should be on a separate line (unless all parameters can fit a single line)"), + LintError(1, 30, "parameter-list-wrapping", "Parameter should be on a separate line (unless all parameters can fit a single line)"), + LintError(1, 46, "parameter-list-wrapping", "Parameter should be on a separate line (unless all parameters can fit a single line)"), + LintError(1, 60, "parameter-list-wrapping", """Missing newline before ")"""") + ) + ) + } + @Test fun testLintClassParameterListValid() { assertThat( @@ -73,6 +92,26 @@ class ParameterListWrappingRuleTest { ) } + @Test + fun testFormatClassParameterListWhenMaxLineLengthExceeded() { + assertThat( + ParameterListWrappingRule().format( + """ + class ClassA(paramA: String, paramB: String, paramC: String) + """.trimIndent(), + userData = mapOf("max_line_length" to "10") + ) + ).isEqualTo( + """ + class ClassA( + paramA: String, + paramB: String, + paramC: String + ) + """.trimIndent() + ) + } + @Test fun testLintFunctionParameterList() { assertThat( @@ -93,6 +132,25 @@ class ParameterListWrappingRuleTest { ) } + @Test + fun testLintFunctionParameterListWhenMaxLineLengthExceeded() { + assertThat( + ParameterListWrappingRule().lint( + """ + fun f(a: Any, b: Any, c: Any) { + } + """.trimIndent(), + userData = mapOf("max_line_length" to "10") + )).isEqualTo( + listOf( + LintError(1, 7, "parameter-list-wrapping", "Parameter should be on a separate line (unless all parameters can fit a single line)"), + LintError(1, 15, "parameter-list-wrapping", "Parameter should be on a separate line (unless all parameters can fit a single line)"), + LintError(1, 23, "parameter-list-wrapping", "Parameter should be on a separate line (unless all parameters can fit a single line)"), + LintError(1, 29, "parameter-list-wrapping", """Missing newline before ")"""") + ) + ) + } + @Test fun testFormatFunctionParameterList() { assertThat( @@ -115,6 +173,28 @@ class ParameterListWrappingRuleTest { ) } + @Test + fun testFormatFunctionParameterListWhenMaxLineLengthExceeded() { + assertThat( + ParameterListWrappingRule().format( + """ + fun f(a: Any, b: Any, c: Any) { + } + """.trimIndent(), + userData = mapOf("max_line_length" to "10") + ) + ).isEqualTo( + """ + fun f( + a: Any, + b: Any, + c: Any + ) { + } + """.trimIndent() + ) + } + @Test fun testLambdaParametersAreIgnored() { assertThat( @@ -250,6 +330,29 @@ class ParameterListWrappingRuleTest { ) } + @Test + fun testFormatNestedDeclarationsWhenMaxLineLengthExceeded() { + assertThat( + ParameterListWrappingRule().format( + """ + fun visit(node: ASTNode, autoCorrect: Boolean, emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) {} + """.trimIndent(), + userData = mapOf("max_line_length" to "10") + )).isEqualTo( + """ + fun visit( + node: ASTNode, + autoCorrect: Boolean, + emit: ( + offset: Int, + errorMessage: String, + canBeAutoCorrected: Boolean + ) -> Unit + ) {} + """.trimIndent() + ) + } + @Test fun testFormatNestedDeclarationsValid() { assertThat(