diff --git a/CHANGELOG.md b/CHANGELOG.md index 7198c79836..7f27522f05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added ### Fixed +- Fix false positive with delegated properties (`indent`) ([#1189](https://github.com/pinterest/ktlint/issues/1189)) - Fix false positive with lambda argument in super type entry (`indent`) ([#1188](https://github.com/pinterest/ktlint/issues/1188)) ### Changed diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRule.kt index 177db8e628..fc0919421f 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRule.kt @@ -476,10 +476,10 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast { // ]}) should decrease expectedIndent by 1 val blockLine = ctx.blockOpeningLineStack.pop() val prevBlockLine = ctx.blockOpeningLineStack.peek() ?: -1 - val pairedLeftBrace = n.pairedLeftBrace() - if (prevBlockLine != blockLine && !pairedLeftBrace.isAfterLambdaArgumentOnSameLine()) { + val pairedLeft = n.pairedLeft() + if (prevBlockLine != blockLine && !pairedLeft.isAfterLambdaArgumentOnSameLine()) { expectedIndent-- - val byKeywordOnSameLine = pairedLeftBrace?.prevLeafOnSameLine(BY_KEYWORD) + val byKeywordOnSameLine = pairedLeft?.prevLeafOnSameLine(BY_KEYWORD) if (byKeywordOnSameLine != null && byKeywordOnSameLine.prevLeaf()?.isWhiteSpaceWithNewline() == true && !byKeywordOnSameLine.isPartOf(DELEGATED_SUPER_TYPE_ENTRY) @@ -1104,13 +1104,19 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast { private fun ASTNode.isPartOfTypeConstraint() = isPartOf(TYPE_CONSTRAINT_LIST) || nextLeaf()?.elementType == WHERE_KEYWORD - private fun ASTNode.pairedLeftBrace(): ASTNode? { - if (elementType != RBRACE) return null + private fun ASTNode.pairedLeft(): ASTNode? { + val rightType = elementType + val leftType = when (rightType) { + RPAR -> LPAR + RBRACE -> LBRACE + RBRACKET -> LBRACKET + else -> return null + } var node: ASTNode? = prevLeaf() while (node != null) { node = when (node.elementType) { - LBRACE -> return node - RBRACE -> node.treeParent + leftType -> return node + rightType -> node.treeParent else -> node.prevLeaf() } } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRuleTest.kt index e70950d56b..f34968017e 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRuleTest.kt @@ -691,6 +691,45 @@ internal class IndentationRuleTest { ).isEmpty() } + @Test + fun `lint property delegate is indented properly 4`() { + assertThat( + IndentationRule().lint( + """ + fun lazyList() = lazy { mutableListOf() } + + class Test { + val list: List + by lazyList() + + val aVar = 0 + } + """.trimIndent() + ) + ).isEmpty() + } + + @Test + fun `lint property delegate is indented properly 5`() { + assertThat( + IndentationRule().lint( + """ + fun lazyList(a: Int, b: Int) = lazy { mutableListOf() } + + class Test { + val list: List + by lazyList( + 1, + 2 + ) + + val aVar = 0 + } + """.trimIndent() + ) + ).isEmpty() + } + @Test fun `lint delegation 1`() { assertThat(