From ebd607307f132897645bff47795a56332dd9535b Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Tue, 2 Aug 2022 00:30:13 +0300 Subject: [PATCH] Newlines: add forceBeforeAssign --- docs/configuration.md | 13 +++++++++++++ .../main/scala/org/scalafmt/config/Newlines.scala | 2 ++ .../src/test/resources/newlines/source_unfold.stat | 7 +++++++ 3 files changed, 22 insertions(+) diff --git a/docs/configuration.md b/docs/configuration.md index 4231b682e2..9a247c511a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1746,6 +1746,19 @@ class A { } ``` +### `newlines.forceBeforeAssign` + +> Since v3.5.9 + +```scala mdoc:defaults +newlines.forceBeforeAssign +``` + +This parameter takes precedence over +[`newlines.forceBeforeMultilineAssign`](#newlinesforcebeforemultilineassign) +and uses the same values. The difference is, the rule forces a newline before a +matching assignment expression whether or not it can be formatted on a single line. + ### `newlines.beforeTypeBounds` > Since v3.0.0 diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/Newlines.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/Newlines.scala index 6d0de7ae60..c8ec03a5d7 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/Newlines.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/Newlines.scala @@ -180,6 +180,8 @@ case class Newlines( @annotation.ExtraName("usingParamListModifierPrefer") implicitParamListModifierPrefer: Option[BeforeAfter] = None, alwaysBeforeElseAfterCurlyIf: Boolean = false, + forceBeforeAssign: ForceBeforeMultilineAssign = + ForceBeforeMultilineAssign.never, private val forceBeforeMultilineAssign: Option[ForceBeforeMultilineAssign] = None, @annotation.DeprecatedName( diff --git a/scalafmt-tests/src/test/resources/newlines/source_unfold.stat b/scalafmt-tests/src/test/resources/newlines/source_unfold.stat index 550a2f92a7..4a429f3553 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_unfold.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_unfold.stat @@ -6121,3 +6121,10 @@ class Foo() { a + b }.foo } +<<< #3271 +newlines.forceBeforeAssign = def +=== +def sum(a: Int, b: Int): Int = + a + b +>>> +def sum(a: Int, b: Int): Int = a + b