@@ -191,8 +191,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
191191 case stats =>
192192 this += " {"
193193 indented {
194- this += lineBreak()
195- printTrees(stats, lineBreak())
194+ printStats(stats.init, stats.last)
196195 }
197196 this += lineBreak() += " }"
198197 }
@@ -205,8 +204,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
205204 case stats =>
206205 this += " {"
207206 indented {
208- this += lineBreak()
209- printTrees(stats, lineBreak())
207+ printStats(stats.init, stats.last)
210208 }
211209 this += lineBreak() += " }"
212210 }
@@ -327,28 +325,19 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
327325 this += " )"
328326 case _ =>
329327 this += " {"
328+ val (stats1, expr1) =
329+ if (isLoopEntryPoint(expr)) (stats.init, stats.last)
330+ else (stats, expr)
330331 indented {
331- if (! stats.isEmpty) {
332- this += lineBreak()
333- printTrees(stats, lineBreak())
334- }
335- if (! isLoopEntryPoint(expr)) {
336- this += lineBreak()
337- printTree(expr)
338- }
332+ printStats(stats1, expr1)
339333 }
340334 this += lineBreak() += " }"
341335 }
342336
343337 case Term .Inlined (call, bindings, expansion) =>
344- sb.append( " { // inlined" )
338+ this += " { // inlined"
345339 indented {
346- if (! bindings.isEmpty) {
347- this += lineBreak()
348- printTrees(bindings, lineBreak())
349- }
350- this += lineBreak()
351- printTree(expansion)
340+ printStats(bindings, expansion)
352341 }
353342 this += lineBreak() += " }"
354343
@@ -400,6 +389,31 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
400389
401390 }
402391
392+ def printStats (stats : List [Tree ], expr : Tree ): Unit = {
393+ def printSeparator (nextStats : List [Tree ]) = {
394+ this += lineBreak()
395+ // Avoid accidental application of opening `{` on next line with a double break
396+ val next = if (nextStats.isEmpty) expr else nextStats.head
397+ next match {
398+ case Term .Block (DefDef (" while$" | " doWhile$" , _, _, _, _) :: Nil , _) =>
399+ case Term .Block (_, _) => this += lineBreak()
400+ case Term .Inlined (_, _, _) => this += lineBreak()
401+ case _ =>
402+ }
403+ }
404+ def printSeparated (list : List [Tree ]): Unit = list match {
405+ case Nil =>
406+ printTree(expr)
407+ case x :: xs =>
408+ printTree(x)
409+ printSeparator(xs)
410+ printSeparated(xs)
411+ }
412+
413+ this += lineBreak()
414+ printSeparated(stats)
415+ }
416+
403417 def printTrees (trees : List [Tree ], sep : String ): Buffer = {
404418 def printSeparated (list : List [Tree ]): Unit = list match {
405419 case Nil =>
@@ -603,14 +617,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
603617 }
604618 this += " =>"
605619 indented {
606- this += lineBreak()
607620 body match {
608621 case Term .Block (stats, expr) =>
609- printTrees(stats, lineBreak())
610- if (stats.nonEmpty)
611- this += lineBreak()
612- printTree(expr)
622+ printStats(stats, expr)
613623 case body =>
624+ this += lineBreak()
614625 printTree(body)
615626 }
616627 }
0 commit comments