Skip to content

Commit

Permalink
Fix indentation of delegated super type call entry
Browse files Browse the repository at this point in the history
Forces the BY keyword in a super type call entry to be indented when preceded by a new line

Closes pinterest#1210
  • Loading branch information
Paul Dingemans committed Dec 11, 2021
1 parent c9fbab9 commit 0acbc72
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
### Fixed
- Fix false positive in rule spacing-between-declarations-with-annotations ([#1281](https://github.com/pinterest/ktlint/issues/1281))
- Fix NoSuchElementException for property accessor (`trailing-comma`) ([#1280](https://github.com/pinterest/ktlint/issues/1280))
- Fix indent of delegated super type entry (`indent`) ([#1210](https://github.com/pinterest/ktlint/issues/1210))

### Changed
- Update Kotlin version to `1.6.0` release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -955,13 +955,14 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
// instead of expected
// val i: Int
// by lazy { 1 }
nextLeafElementType == BY_KEYWORD ->
if (node.isPartOf(DELEGATED_SUPER_TYPE_ENTRY)) {
nextLeafElementType == BY_KEYWORD -> {
if (node.isPartOf(DELEGATED_SUPER_TYPE_ENTRY) && node.text != "\n") {
0
} else {
expectedIndent++
1
}
}
// IDEA quirk:
// var value: DataClass =
// DataClass("too long line")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -952,23 +952,42 @@ internal class IndentationRuleTest {
}

@Test
fun `lint delegation 2`() {
assertThat(
IndentationRule().lint(
"""
interface Foo
fun `lint and format delegation 2`() {
val code =
"""
interface Foo
class Bar(a: Int, b: Int, c: Int) : Foo
class Bar(a: Int, b: Int, c: Int) : Foo
class Test2 : Foo
class Test2 : Foo
by Bar(
a = 1,
b = 2,
c = 3
)
""".trimIndent()
val formattedCode =
"""
interface Foo
class Bar(a: Int, b: Int, c: Int) : Foo
class Test2 : Foo
by Bar(
a = 1,
b = 2,
c = 3
)
""".trimIndent()
)
).isEmpty()
""".trimIndent()

assertThat(IndentationRule().lint(code)).containsExactly(
LintError(6, 1, "indent", "Unexpected indentation (0) (should be 4)"),
LintError(7, 1, "indent", "Unexpected indentation (4) (should be 8)"),
LintError(8, 1, "indent", "Unexpected indentation (4) (should be 8)"),
LintError(9, 1, "indent", "Unexpected indentation (4) (should be 8)"),
LintError(10, 1, "indent", "Unexpected indentation (0) (should be 4)"),
)
assertThat(IndentationRule().format(code)).isEqualTo(formattedCode)
}

@Test
Expand Down Expand Up @@ -1343,6 +1362,24 @@ internal class IndentationRuleTest {
assertThat(IndentationRule().format(codeTabs, INDENT_STYLE_TABS)).isEqualTo(codeTabs)
}

@Test
fun `Issue 1210 - format by`() {
val code =
"""
object ApplicationComponentFactory : ApplicationComponent.Factory
by DaggerApplicationComponent.factory()
""".trimIndent()
val formattedCode =
"""
object ApplicationComponentFactory : ApplicationComponent.Factory
by DaggerApplicationComponent.factory()
""".trimIndent()
// assertThat(IndentationRule().lint(code)).containsExactly(
// LintError(2, 1, "indent", "Unexpected indentation (0) (should be 4)")
// )
assertThat(IndentationRule().format(code)).isEqualTo(formattedCode)
}

private companion object {
const val MULTILINE_STRING_QUOTE = "${'"'}${'"'}${'"'}"
const val TAB = "${'\t'}"
Expand Down

0 comments on commit 0acbc72

Please sign in to comment.