Skip to content

Commit

Permalink
Allow breaking before . in a reciever function declaration to avoid…
Browse files Browse the repository at this point in the history
… long lines

Summary:
This fixes:

#268

It fixes it for function only, but I can try to extend into the declareOne method later (it's just more complicated for that)

Reviewed By: cgrushko

Differential Revision: D35047687

fbshipit-source-id: afe6411110becdfa49d14e57cfdfbe19f4d9de87
  • Loading branch information
strulovich authored and facebook-github-bot committed Mar 22, 2022
1 parent a66fbc4 commit f0c6281
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,15 @@ class KotlinInputAstVisitor(
if (name != null || receiverTypeReference != null) {
builder.space()
}
if (receiverTypeReference != null) {
visit(receiverTypeReference)
builder.token(".")
}
if (name != null) {
builder.token(name)
builder.block(ZERO) {
if (receiverTypeReference != null) {
visit(receiverTypeReference)
builder.breakOp(Doc.FillMode.INDEPENDENT, "", expressionBreakIndent)
builder.token(".")
}
if (name != null) {
builder.token(name)
}
}
if (emitParenthesis) {
builder.token("(")
Expand Down
15 changes: 15 additions & 0 deletions core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2790,6 +2790,21 @@ class FormatterTest {
|operator fun Point.component1() = x
|""".trimMargin())

@Test
fun `handle extension methods with very long names`() =
assertFormatted(
"""
|------------------------------------------
|fun LongReceiverNameThatRequiresBreaking
| .doIt() {}
|
|fun LongButNotTooLong.doIt(
| n: Int,
| f: Float
|) {}
|""".trimMargin(),
deduceMaxWidth = true)

@Test
fun `handle extension properties`() =
assertFormatted(
Expand Down

0 comments on commit f0c6281

Please sign in to comment.