Skip to content

Commit

Permalink
Merge pull request #200 from ntsim/parameter-list-wrapping-with-max-l…
Browse files Browse the repository at this point in the history
…ine-length

Apply `parameter-list-wrapping` when `max_line_length` is exceeded
shyiko authored Apr 30, 2018
2 parents 15b284f + 41559ca commit cb91be5
Showing 2 changed files with 111 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
// <LPAR line indent + indentSize> VALUE_PARAMETER...
Original file line number Diff line number Diff line change
@@ -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(

0 comments on commit cb91be5

Please sign in to comment.