diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Splits.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Splits.scala index df4dacb33..7a2df41a5 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Splits.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Splits.scala @@ -3377,25 +3377,31 @@ object SplitsAfterYield extends Splits { cfg: ScalafmtConfig, ): Seq[Split] = { import fo._, tokens._, ft._ - leftOwner match { - case t: Term.ForYield => - val lastToken = getLast(t.body) - if ( - t.body.is[Tree.Block] && right.is[T.LeftBrace] && - matchingOptRight(ft).exists(_.idx >= lastToken.idx) - ) Seq(Split(Space, 0)) - else { - val indent = Indent(cfg.indent.main, lastToken, ExpiresOn.After) - if (cfg.newlines.avoidAfterYield && !rightOwner.is[Term.If]) { - val noIndent = !isRightCommentWithBreak(ft) - Seq(Split(Space, 0).withIndent(indent, noIndent)) - } else Seq( - // Either everything fits in one line or break on => - Split(Space, 0).withSingleLine(lastToken), - Split(Newline, 1).withIndent(indent), - ) - } - case _ => Seq.empty + (leftOwner match { + case t: Term.ForYield => Some(t.body) + case _ => None + }).fold(Seq.empty[Split]) { + case b: Term.PartialFunction + if dialect.allowSignificantIndentation && + nextNonComment(ft).right.is[T.KwCase] => + val split = Split(Newline, 0) + .withIndent(cfg.indent.getSignificant, getLast(b), ExpiresOn.After) + Seq(split) + case b: Tree.Block + if right.is[T.LeftBrace] && + matchingOptRight(ft).exists(_.idx >= getLast(b).idx) => + Seq(Split(Space, 0)) + case b => + val lastToken = getLast(b) + val indent = Indent(cfg.indent.main, lastToken, ExpiresOn.After) + if (cfg.newlines.avoidAfterYield && !rightOwner.is[Term.If]) { + val noIndent = !isRightCommentWithBreak(ft) + Seq(Split(Space, 0).withIndent(indent, noIndent)) + } else Seq( + // Either everything fits in one line or break on => + Split(Space, 0).withSingleLine(lastToken), + Split(Newline, 1).withIndent(indent), + ) } } } diff --git a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces.stat b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces.stat index 5a84d3e44..5e186ca2b 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces.stat @@ -8527,3 +8527,12 @@ object a { x = "string" ) } +<<< #4857 +for + foo <- Some(42) +yield + case Some(x) => foo +>>> +for foo <- Some(42) +yield + case Some(x) => foo diff --git a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_fold.stat b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_fold.stat index c37c32871..bd57bebf0 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_fold.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_fold.stat @@ -8190,3 +8190,12 @@ object a { x = "string" ) } +<<< #4857 +for + foo <- Some(42) +yield + case Some(x) => foo +>>> +for foo <- Some(42) +yield + case Some(x) => foo diff --git a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_keep.stat b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_keep.stat index e18a51c99..529b5cb52 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_keep.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_keep.stat @@ -8549,3 +8549,13 @@ object a { x = "string" ) } +<<< #4857 +for + foo <- Some(42) +yield + case Some(x) => foo +>>> +for + foo <- Some(42) +yield + case Some(x) => foo diff --git a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_unfold.stat b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_unfold.stat index 9a9cd1baa..a4409a44e 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_unfold.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_unfold.stat @@ -8877,3 +8877,13 @@ object a { x = "string" ) } +<<< #4857 +for + foo <- Some(42) +yield + case Some(x) => foo +>>> +for foo <- Some(42) +yield + case Some(x) => + foo diff --git a/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala b/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala index fcdba62d9..cb41c9aa9 100644 --- a/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala +++ b/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala @@ -148,7 +148,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions { val explored = Debug.explored.get() logger.debug(s"Total explored: $explored") if (!onlyUnit && !onlyManual) - assertEquals(explored, 2537604, "total explored") + assertEquals(explored, 2537916, "total explored") // TODO(olafur) don't block printing out test results. TestPlatformCompat.executeAndWait(PlatformFileOps.writeFile( FileOps.getPath("target", "index.html"),