Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply parameter-list-wrapping when max_line_length is exceeded #200

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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