Skip to content

Commit

Permalink
Fixing bug with extra indentation after lambda arrow (#508)
Browse files Browse the repository at this point in the history
* Fixing bug with extra indentation after lambda arrow

Fixes #479

* Adding tests
  • Loading branch information
shashachu authored Jul 5, 2019
1 parent ba513d7 commit f4e5358
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -707,11 +707,14 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
}

private fun adjustExpectedIndentAfterArrow(n: ASTNode, ctx: IndentContext) {
val prevBlockLine = ctx.blockOpeningLineStack.peek() ?: -1
if (prevBlockLine != line) {
expectedIndent++
debug { "++after(ARROW) -> $expectedIndent" }
ctx.exitAdjBy(n.treeParent, -1)
// Only adjust indents for arrows inside of when statements. Lambda arrows should not increase indent.
if (n.treeParent?.elementType == WHEN_ENTRY) {
val prevBlockLine = ctx.blockOpeningLineStack.peek() ?: -1
if (prevBlockLine != line) {
expectedIndent++
debug { "++after(ARROW) -> $expectedIndent" }
ctx.exitAdjBy(n.treeParent, -1)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,26 @@ class IndentationRuleTest {
)
).isEmpty()
}

@Test
fun `no indentation after lambda arrow`() {
assertThat(
IndentationRule().lint(
"""
fun bar() {
foo.func {
param1, param2 ->
doSomething()
doSomething2()
}
}
""".trimIndent()
)
).isEqualTo(
listOf(
LintError(line = 4, col = 1, ruleId = "indent", detail = "Unexpected indentation (12) (should be 8)"),
LintError(line = 5, col = 1, ruleId = "indent", detail = "Unexpected indentation (12) (should be 8)")
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ fun main() {
2 // second element
-> true
}
foo.func {
param1, param2 ->
doSomething()
doSomething2()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ fun main() {
2 // second element
-> true
}
foo.func {
param1, param2 ->
doSomething()
doSomething2()
}
}

0 comments on commit f4e5358

Please sign in to comment.