From 8aee5396871693d2a3ac9665c9ded8fdb2d1350c Mon Sep 17 00:00:00 2001 From: Sha Sha Chu Date: Fri, 5 Jul 2019 09:18:48 -0700 Subject: [PATCH] Fixing bug with extra indentation after lambda arrow (#508) * Fixing bug with extra indentation after lambda arrow Fixes https://github.com/pinterest/ktlint/issues/479 * Adding tests --- .../ruleset/experimental/IndentationRule.kt | 13 ++++++----- .../experimental/IndentationRuleTest.kt | 22 +++++++++++++++++++ .../spec/indent/format-arrow-expected.kt.spec | 5 +++++ .../spec/indent/format-arrow.kt.spec | 5 +++++ 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/IndentationRule.kt b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/IndentationRule.kt index f102aebc04..e8be27ee42 100644 --- a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/IndentationRule.kt +++ b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/IndentationRule.kt @@ -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) + } } } diff --git a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/IndentationRuleTest.kt b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/IndentationRuleTest.kt index c32633d845..0f5fbb337f 100644 --- a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/IndentationRuleTest.kt +++ b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/IndentationRuleTest.kt @@ -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)") + ) + ) + } } diff --git a/ktlint-ruleset-experimental/src/test/resources/spec/indent/format-arrow-expected.kt.spec b/ktlint-ruleset-experimental/src/test/resources/spec/indent/format-arrow-expected.kt.spec index da983689d2..a3ba11cc63 100644 --- a/ktlint-ruleset-experimental/src/test/resources/spec/indent/format-arrow-expected.kt.spec +++ b/ktlint-ruleset-experimental/src/test/resources/spec/indent/format-arrow-expected.kt.spec @@ -23,4 +23,9 @@ fun main() { 2 // second element -> true } + foo.func { + param1, param2 -> + doSomething() + doSomething2() + } } diff --git a/ktlint-ruleset-experimental/src/test/resources/spec/indent/format-arrow.kt.spec b/ktlint-ruleset-experimental/src/test/resources/spec/indent/format-arrow.kt.spec index 5067a8b09a..ac87b97fc2 100644 --- a/ktlint-ruleset-experimental/src/test/resources/spec/indent/format-arrow.kt.spec +++ b/ktlint-ruleset-experimental/src/test/resources/spec/indent/format-arrow.kt.spec @@ -20,4 +20,9 @@ fun main() { 2 // second element -> true } + foo.func { + param1, param2 -> + doSomething() + doSomething2() + } }