diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala index 2ff81a60ee..fedbaaf841 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala @@ -108,10 +108,11 @@ object TreeOps { tree: Tree, )(implicit ftoks: FormatTokens): Seq[Tree] = tree match { case SingleArgInBraces(_, fun: Term.FunctionTerm, _) => fun :: Nil - case b: Term.FunctionTerm if isBlockFunction(b) => b.body :: Nil + case t: Term.FunctionTerm if isBlockFunction(t) => t.body :: Nil case t: Term.EnumeratorsBlock => getEnumStatements(t.enums) - case t: Tree.Block => t.stats - case t: CaseTree if t.body.tokens.nonEmpty => t.body :: Nil + case t @ Tree.Block(s) => + if (!t.parent.is[CaseTree] || getSingleStatExceptEndMarker(s).isEmpty) s + else s.drop(1) case _ => Nil } @@ -600,10 +601,13 @@ object TreeOps { * block contains only a single statement. NB: in FormatWriter, when choosing * to insert or remove end markers, we avoid such borderline cases. */ - def getSingleStatExceptEndMarker(s: Seq[Stat]): Option[Stat] = s.headOption - .filter { _ => - val len2 = s.lengthCompare(2) - len2 < 0 || len2 == 0 && s(1).is[Term.EndMarker] + def getSingleStatExceptEndMarker[A <: Tree](ss: List[A]): Option[A] = + ss match { + case s :: rs if (rs match { + case Nil | (_: Term.EndMarker) :: Nil => true + case _ => false + }) => Some(s) + case _ => None } def getSingleStatExceptEndMarker(t: Tree): Option[Tree] = t match { diff --git a/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat b/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat index 731b841de5..cbec2545ac 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat @@ -1958,9 +1958,8 @@ val strings = Seq( s" $bestEPStr", "Metrics:", s" $metricHeader: ${bestScore.score}" -) ++ otherMetricHeaders.zip(bestScore.otherScores).map { case (h, s) => - s" $h: $s" -} ++ outputPath.toSeq.map { p => +) ++ otherMetricHeaders.zip(bestScore.otherScores) + .map { case (h, s) => s" $h: $s" } ++ outputPath.toSeq.map { p => s"The best variant params can be found in $p" } <<< 8.6: assignment with short expression