Skip to content

Commit

Permalink
Apply parameter-list-wrapping when max_line_length is exceeded
Browse files Browse the repository at this point in the history
Resolves pinterest#96

This commit applies `parameter-list-wrapping` and its associated
linting/formatting when `max_line_length` is exceeded by a function or
class declaration due to its parameter list being too long. It will also
apply for nested function declarations.
  • Loading branch information
ntsim committed Apr 30, 2018
1 parent 4e58236 commit 76bc6f8
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand All @@ -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...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 76bc6f8

Please sign in to comment.