diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala index 871bc28bea..009044612a 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala @@ -2261,25 +2261,23 @@ class FormatOps( def create(ft: FormatToken, nft: FormatToken)(implicit style: ScalafmtConfig ): Option[OptionalBracesRegion] = { - def forceNL = shouldBreakInOptionalBraces(nft) + def trySplits(expr: Term, finallyp: Option[Term], usesOB: => Boolean) = + if (isTreeMultiStatBlock(expr)) Some(getSplits(ft, expr, true)) + else if (finallyp.exists(isTreeUsingOptionalBraces) || usesOB) + Some(getSplits(ft, expr, shouldBreakInOptionalBraces(nft))) + else None ft.meta.leftOwner match { case t @ Term.Try(expr, _, finallyp) => - def usesOB = isTreeMultiStatBlock(expr) || - isCatchUsingOptionalBraces(t) || - finallyp.exists(isTreeUsingOptionalBraces) Some(new OptionalBracesRegion { def owner = Some(t) def splits = - if (usesOB) Some(getSplits(ft, expr, forceNL)) else None + trySplits(expr, finallyp, isCatchUsingOptionalBraces(t)) def rightBrace = blockLast(expr) }) case t @ Term.TryWithHandler(expr, _, finallyp) => - def usesOB = isTreeMultiStatBlock(expr) || - finallyp.exists(isTreeUsingOptionalBraces) Some(new OptionalBracesRegion { def owner = Some(t) - def splits = - if (usesOB) Some(getSplits(ft, expr, forceNL)) else None + def splits = trySplits(expr, finallyp, false) def rightBrace = blockLast(expr) }) case _ => None diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat index 0ea31add41..be4f7b1c02 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat @@ -4574,3 +4574,19 @@ object a: c: C, d: D, implicit val e: E) {} +<<< #3492 +object Test: + def test = + try + 1; 2 + catch + case _: RuntimeException => 2 + case _: Exception => 3 +>>> +object Test: + def test = + try + 1; 2 + catch + case _: RuntimeException => 2 + case _: Exception => 3 diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat index e7d3969289..36bb28b3d5 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat @@ -4381,3 +4381,19 @@ object a: c: C, d: D, implicit val e: E) {} +<<< #3492 +object Test: + def test = + try + 1; 2 + catch + case _: RuntimeException => 2 + case _: Exception => 3 +>>> +object Test: + def test = + try + 1; 2 + catch + case _: RuntimeException => 2 + case _: Exception => 3 diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat index a9ebf27a11..983f236de5 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat @@ -4615,3 +4615,19 @@ object a: c: C, d: D, implicit val e: E) {} +<<< #3492 +object Test: + def test = + try + 1; 2 + catch + case _: RuntimeException => 2 + case _: Exception => 3 +>>> +object Test: + def test = + try + 1; 2 + catch + case _: RuntimeException => 2 + case _: Exception => 3 diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat index 4ba5c58100..e9b6a345e2 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat @@ -4714,3 +4714,22 @@ object a: c: C, d: D, implicit val e: E) {} +<<< #3492 +object Test: + def test = + try + 1; 2 + catch + case _: RuntimeException => 2 + case _: Exception => 3 +>>> +object Test: + def test = + try + 1; + 2 + catch + case _: RuntimeException => + 2 + case _: Exception => + 3