From a3d7e28e810dd0527c82ff50dab4797e44f74628 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sat, 11 Mar 2023 08:16:07 +0100 Subject: [PATCH 1/2] Add test of comma-separated try block --- .../test/resources/scala3/OptionalBraces.stat | 15 +++++++++++++++ .../resources/scala3/OptionalBraces_fold.stat | 15 +++++++++++++++ .../resources/scala3/OptionalBraces_keep.stat | 16 ++++++++++++++++ .../scala3/OptionalBraces_unfold.stat | 19 +++++++++++++++++++ 4 files changed, 65 insertions(+) diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat index 0ea31add41..41f0bcefa3 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat @@ -4574,3 +4574,18 @@ 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 +>>> +test does not parse +object Test: + def test = + try 1; 2 + catch + ^ diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat index e7d3969289..b5cff4dd78 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat @@ -4381,3 +4381,18 @@ 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 +>>> +test does not parse +object Test: + def test = + try 1; 2 + catch + ^ 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 From 80abbeb48abb043145722ba4f6dc1ea0dd1e35bd Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sat, 11 Mar 2023 08:29:29 +0100 Subject: [PATCH 2/2] FormatOps: force break on multi-stat try blocks --- .../scala/org/scalafmt/internal/FormatOps.scala | 16 +++++++--------- .../test/resources/scala3/OptionalBraces.stat | 7 ++++--- .../resources/scala3/OptionalBraces_fold.stat | 7 ++++--- 3 files changed, 15 insertions(+), 15 deletions(-) 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 41f0bcefa3..be4f7b1c02 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat @@ -4583,9 +4583,10 @@ object Test: case _: RuntimeException => 2 case _: Exception => 3 >>> -test does not parse object Test: def test = - try 1; 2 + 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 b5cff4dd78..36bb28b3d5 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat @@ -4390,9 +4390,10 @@ object Test: case _: RuntimeException => 2 case _: Exception => 3 >>> -test does not parse object Test: def test = - try 1; 2 + try + 1; 2 catch - ^ + case _: RuntimeException => 2 + case _: Exception => 3