Skip to content

Commit

Permalink
Fix indentation for function types after a newline (pinterest#1022)
Browse files Browse the repository at this point in the history
  • Loading branch information
romtsn authored Dec 23, 2020
1 parent 3591e61 commit 68e65db
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- EditorConfig generation for `import-ordering` ([#1011](https://github.com/pinterest/ktlint/pull/1011))
- Internal error (`no-unused-imports`) ([#996](https://github.com/pinterest/ktlint/issues/996))
- Fix false positive when argument list is after multiline dot-qualified expression (`argument-list-wrapping`) ([#893](https://github.com/pinterest/ktlint/issues/893))
- Fix indentation for function types after a newline (`indent`) ([#918](https://github.com/pinterest/ktlint/issues/918))

### Changed
- Update Gradle shadow plugin to `6.1.0` version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.pinterest.ktlint.core.ast.ElementType.ELSE
import com.pinterest.ktlint.core.ast.ElementType.ELVIS
import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.EQ
import com.pinterest.ktlint.core.ast.ElementType.FUN
import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_LITERAL
import com.pinterest.ktlint.core.ast.ElementType.GT
import com.pinterest.ktlint.core.ast.ElementType.KDOC
Expand Down Expand Up @@ -725,7 +726,12 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
private fun adjustExpectedIndentAfterColon(n: ASTNode, ctx: IndentContext) {
expectedIndent++
debug { "++after(COLON) -> $expectedIndent" }
ctx.exitAdjBy(n.treeParent, -1)
if (n.isPartOf(FUN)) {
val returnType = n.nextCodeSibling()
ctx.exitAdjBy(returnType!!, -1)
} else {
ctx.exitAdjBy(n.treeParent, -1)
}
}

private fun adjustExpectedIndentAfterLparInsideCondition(n: ASTNode, ctx: IndentContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -828,4 +828,25 @@ internal class IndentationRuleTest {
)
).isEmpty()
}

// https://github.com/pinterest/ktlint/issues/918
@Test
fun `lint newline after type reference in functions`() {
assertThat(
IndentationRule().lint(
"""
override fun actionProcessor():
ObservableTransformer<in SomeVeryVeryLongNameOverHereAction, out SomeVeryVeryLongNameOverHereResult> =
ObservableTransformer { actions ->
// ...
}
fun generateGooooooooooooooooogle():
Gooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooogle {
return Gooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooogle()
}
""".trimIndent()
)
).isEmpty()
}
}

0 comments on commit 68e65db

Please sign in to comment.