Skip to content

Commit

Permalink
Add parameter list wrapping formatting when max_line_length is exce…
Browse files Browse the repository at this point in the history
…eded

This change applies additional formatting rules when `max_line_length`
is exceeded by a function or class declaration due to a long parameter
list. It will also apply the formatting for nested declarations.

Related issues: pinterest#96
  • Loading branch information
ntsim committed Apr 30, 2018
1 parent 4e58236 commit 8f95cfa
Show file tree
Hide file tree
Showing 2 changed files with 73 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 @@ -73,6 +73,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 Down Expand Up @@ -115,6 +135,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 +292,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 8f95cfa

Please sign in to comment.