Skip to content

Commit

Permalink
Fix false positive for delegated properties with a lambda argument (`…
Browse files Browse the repository at this point in the history
…indent`)
  • Loading branch information
t-kameyama authored and Tapchicoma committed Oct 5, 2021
1 parent f574c93 commit 686a60a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- Fix trailing spaces not formatted inside block comments (`no-trailing-spaces`) ([#1197](https://github.com/pinterest/ktlint/issues/1197))
- Do not check for `.idea` folder presence when using `applyToIDEA` globally ([#1186](https://github.com/pinterest/ktlint/issues/1186))
- Remove spaces before primary constructor (`paren-spacing`) ([#1207](https://github.com/pinterest/ktlint/issues/1207))
- Fix false positive for delegated properties with a lambda argument (`indent`) ([#1210](https://github.com/pinterest/ktlint/issues/1210))

### Changed
- Support absolute paths for globs ([#1131](https://github.com/pinterest/ktlint/issues/1131))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import com.pinterest.ktlint.core.ast.children
import com.pinterest.ktlint.core.ast.isPartOf
import com.pinterest.ktlint.core.ast.isPartOfComment
import com.pinterest.ktlint.core.ast.isWhiteSpace
import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline
import com.pinterest.ktlint.core.ast.isWhiteSpaceWithoutNewline
import com.pinterest.ktlint.core.ast.nextCodeLeaf
Expand Down Expand Up @@ -482,7 +483,7 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
val byKeywordOnSameLine = pairedLeft?.prevLeafOnSameLine(BY_KEYWORD)
if (byKeywordOnSameLine != null &&
byKeywordOnSameLine.prevLeaf()?.isWhiteSpaceWithNewline() == true &&
!byKeywordOnSameLine.isPartOf(DELEGATED_SUPER_TYPE_ENTRY)
n.leavesOnSameLine(forward = true).all { it.isWhiteSpace() || it.isPartOfComment() }
) expectedIndent--
debug { "--${n.text} -> $expectedIndent" }
}
Expand Down Expand Up @@ -1123,11 +1124,11 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
return null
}

private fun ASTNode.prevLeafOnSameLine(prevLeafType: IElementType): ASTNode? {
return leaves(forward = false)
.takeWhile { !it.isWhiteSpaceWithNewline() }
.firstOrNull { it.elementType == prevLeafType }
}
private fun ASTNode.leavesOnSameLine(forward: Boolean): Sequence<ASTNode> =
leaves(forward = forward).takeWhile { !it.isWhiteSpaceWithNewline() }

private fun ASTNode.prevLeafOnSameLine(prevLeafType: IElementType): ASTNode? =
leavesOnSameLine(forward = false).firstOrNull { it.elementType == prevLeafType }

private fun ASTNode?.isAfterLambdaArgumentOnSameLine(): Boolean {
if (this == null) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,25 @@ internal class IndentationRuleTest {
).isEmpty()
}

// https://github.com/pinterest/ktlint/issues/1210
@Test
fun `lint delegated properties with a lambda argument`() {
assertThat(
IndentationRule().lint(
"""
import kotlin.properties.Delegates
class Test {
private var test
by Delegates.vetoable("") { _, old, new ->
true
}
}
""".trimIndent()
)
).isEmpty()
}

@Test
fun `lint delegation 1`() {
assertThat(
Expand Down

0 comments on commit 686a60a

Please sign in to comment.