Skip to content

Commit

Permalink
Add trailing break before brace for Google Style (#308)
Browse files Browse the repository at this point in the history
Summary:
Fix for #245

Pull Request resolved: #308

Reviewed By: cgrushko

Differential Revision: D35506462

Pulled By: strulovich

fbshipit-source-id: 5419130214152e6e083919935e5ae3be0cb47492
  • Loading branch information
bethcutler authored and facebook-github-bot committed Apr 13, 2022
1 parent d8bdf97 commit 1bebf0b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,9 @@ class KotlinInputAstVisitor(
return
}

forEachCommaSeparated(list, hasTrailingComma, wrapInBlock) { visit(it) }
forEachCommaSeparated(list, hasTrailingComma, wrapInBlock, trailingBreak = isGoogleStyle) {
visit(it)
}
if (hasTrailingComma) {
builder.breakOp(Doc.FillMode.UNIFIED, "", expressionBreakNegativeIndent)
}
Expand Down Expand Up @@ -944,6 +946,7 @@ class KotlinInputAstVisitor(
list: Iterable<T>,
hasTrailingComma: Boolean = false,
wrapInBlock: Boolean = true,
trailingBreak: Boolean = false,
function: (T) -> Unit
) {
if (hasTrailingComma) {
Expand Down Expand Up @@ -971,6 +974,9 @@ class KotlinInputAstVisitor(
function(value)
}
}
if (trailingBreak) {
builder.breakOp(Doc.FillMode.UNIFIED, "", expressionBreakNegativeIndent)
}
}

/** Example `a` in `foo(a)`, or `*a`, or `limit = 50` */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,15 +483,15 @@ class GoogleStyleFormatterKtTest {
| something
| is
| PairList<
| String,
| Int>
| String, Int
| >
| )
| doIt(
| something
| as
| PairList<
| String,
| Int>
| String, Int
| >
| )
| println(
| a is Int &&
Expand Down Expand Up @@ -833,4 +833,52 @@ class GoogleStyleFormatterKtTest {
|""".trimMargin(),
formattingOptions = Formatter.GOOGLE_FORMAT,
deduceMaxWidth = true)

@Test
fun `trailing break argument list`() =
assertFormatted(
"""
|-------------------
|fun method() {
| Foo.FooBar(
| longParameter
| )
| Foo.FooBar(
| param1,
| param2
| )
|}
|""".trimMargin(),
formattingOptions = Formatter.GOOGLE_FORMAT,
deduceMaxWidth = true)

@Test
fun `trailing break chains`() =
assertFormatted(
"""
|-------------
|bar(
| FooOpClass
| .doOp(1)
| .doOp(2)
|)
|""".trimMargin(),
formattingOptions = Formatter.GOOGLE_FORMAT,
deduceMaxWidth = true)

@Test
fun `wrapping for long function types`() =
assertFormatted(
"""
|------------------------
|var listener:
| (
| a: String,
| b: String,
| c: String,
| d: String
| ) -> Unit
|""".trimMargin(),
formattingOptions = Formatter.GOOGLE_FORMAT,
deduceMaxWidth = true)
}

0 comments on commit 1bebf0b

Please sign in to comment.