diff options
author | Beth Cutler <bcutler@google.com> | 2022-04-13 08:17:20 -0700 |
---|---|---|
committer | Facebook GitHub Bot <facebook-github-bot@users.noreply.github.com> | 2022-04-13 08:17:20 -0700 |
commit | 1bebf0bc4677832a2b7004acf42a938baecd565a (patch) | |
tree | e27cc2ea9d6bb1365b1e18b5747047041a98ed1e /core | |
parent | d8bdf97ee409652671e22bb76dc7861c777792c2 (diff) | |
download | ktfmt-1bebf0bc4677832a2b7004acf42a938baecd565a.tar.gz |
Add trailing break before brace for Google Style (#308)
Summary:
Fix for https://github.com/facebookincubator/ktfmt/issues/245
Pull Request resolved: https://github.com/facebookincubator/ktfmt/pull/308
Reviewed By: cgrushko
Differential Revision: D35506462
Pulled By: strulovich
fbshipit-source-id: 5419130214152e6e083919935e5ae3be0cb47492
Diffstat (limited to 'core')
-rw-r--r-- | core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt | 8 | ||||
-rw-r--r-- | core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt | 56 |
2 files changed, 59 insertions, 5 deletions
diff --git a/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt b/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt index b818145..c4633f2 100644 --- a/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt +++ b/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt @@ -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) } @@ -944,6 +946,7 @@ class KotlinInputAstVisitor( list: Iterable<T>, hasTrailingComma: Boolean = false, wrapInBlock: Boolean = true, + trailingBreak: Boolean = false, function: (T) -> Unit ) { if (hasTrailingComma) { @@ -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` */ diff --git a/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt b/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt index caec529..b61cc9f 100644 --- a/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt @@ -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 && @@ -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) } |