Skip to content

Commit

Permalink
Merge pull request #201 from JelloRanger/jelloranger/parameter-list-w…
Browse files Browse the repository at this point in the history
…rapper-rule-dangling-paren

Fix ParameterListWrappingRule to correctly identify misplaced left parens
  • Loading branch information
shyiko authored May 1, 2018
2 parents 79f5a79 + e7c9161 commit e2aed32
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,21 @@ class ParameterListWrappingRule : Rule("parameter-list-wrapping") {
if (putParametersOnSeparateLines || maxLineLengthExceeded) {
// aiming for
// ... LPAR
// <LPAR line indent + indentSize> VALUE_PARAMETER...
// <LPAR line indent> RPAR
// <line indent + indentSize> VALUE_PARAMETER...
// <line indent> RPAR
val indent = "\n" + node.psi.lineIndent()
val paramIndent = indent + " ".repeat(indentSize) // single indent as recommended by Jetbrains/Google
nextChild@ for (child in node.children()) {
when (child.elementType) {
KtTokens.LPAR -> {
val prevLeaf = child.psi.prevLeaf()!!
if (prevLeaf.elementType == KtTokens.WHITE_SPACE && prevLeaf.textContains('\n')) {
emit(child.startOffset, errorMessage(child), true)
if (autoCorrect) {
prevLeaf.delete()
}
}
}
KtStubElementTypes.VALUE_PARAMETER,
KtTokens.RPAR -> {
var paramInnerIndentAdjustment = 0
Expand Down Expand Up @@ -123,6 +132,7 @@ class ParameterListWrappingRule : Rule("parameter-list-wrapping") {

private fun errorMessage(node: ASTNode) =
when (node.elementType) {
KtTokens.LPAR -> """Unnecessary newline before "(""""
KtStubElementTypes.VALUE_PARAMETER ->
"Parameter should be on a separate line (unless all parameters can fit a single line)"
KtTokens.RPAR -> """Missing newline before ")""""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,92 @@ class ParameterListWrappingRuleTest {
LintError(6, 4, "parameter-list-wrapping", "Unexpected indentation (expected 4, actual 3)")
))
}

@Test
fun testLintClassDanglingLeftParen() {
assertThat(
ParameterListWrappingRule().lint(
"""
class ClassA
(
paramA: String,
paramB: String,
paramC: String
)
""".trimIndent()
)
).isEqualTo(
listOf(
LintError(2, 1, "parameter-list-wrapping", """Unnecessary newline before "("""")
)
)
}

@Test
fun testLintFunctionDanglingLeftParen() {
assertThat(
ParameterListWrappingRule().lint(
"""
fun doSomething
(
paramA: String,
paramB: String,
paramC: String
)
""".trimIndent()
)
).isEqualTo(
listOf(
LintError(2, 1, "parameter-list-wrapping", """Unnecessary newline before "("""")
)
)
}

@Test
fun testFormatClassDanglingLeftParen() {
assertThat(
ParameterListWrappingRule().format(
"""
class ClassA constructor
(
paramA: String,
paramB: String,
paramC: String
)
""".trimIndent()
)
).isEqualTo(
"""
class ClassA constructor(
paramA: String,
paramB: String,
paramC: String
)
""".trimIndent()
)
}

@Test
fun testFormatFunctionDanglingLeftParen() {
assertThat(
ParameterListWrappingRule().format(
"""
fun doSomething
(
paramA: String,
paramB: String,
paramC: String
)
""".trimIndent()
)
).isEqualTo(
"""
fun doSomething(
paramA: String,
paramB: String,
paramC: String
)
""".trimIndent()
)
}
}

0 comments on commit e2aed32

Please # to comment.