@@ -38,6 +38,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
3838 def result (): String = sb.result()
3939
4040 def lineBreak (): String = " \n " + (" " * indent)
41+ def doubleLineBreak (): String = " \n\n " + (" " * indent)
4142
4243 def printTree (tree : Tree ): Buffer = tree match {
4344 case tree @ PackageClause (Term .Ident (name), stats) =>
@@ -191,8 +192,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
191192 case stats =>
192193 this += " {"
193194 indented {
194- this += lineBreak()
195- printTrees(stats, lineBreak())
195+ printStats(stats.init, stats.last)
196196 }
197197 this += lineBreak() += " }"
198198 }
@@ -205,8 +205,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
205205 case stats =>
206206 this += " {"
207207 indented {
208- this += lineBreak()
209- printTrees(stats, lineBreak())
208+ printStats(stats.init, stats.last)
210209 }
211210 this += lineBreak() += " }"
212211 }
@@ -333,28 +332,19 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
333332 this += " )"
334333 case _ =>
335334 this += " {"
335+ val (stats1, expr1) =
336+ if (isLoopEntryPoint(expr)) (stats.init, stats.last)
337+ else (stats, expr)
336338 indented {
337- if (! stats.isEmpty) {
338- this += lineBreak()
339- printTrees(stats, lineBreak())
340- }
341- if (! isLoopEntryPoint(expr)) {
342- this += lineBreak()
343- printTree(expr)
344- }
339+ printStats(stats1, expr1)
345340 }
346341 this += lineBreak() += " }"
347342 }
348343
349344 case Term .Inlined (call, bindings, expansion) =>
350- sb.append( " { // inlined" )
345+ this += " { // inlined"
351346 indented {
352- if (! bindings.isEmpty) {
353- this += lineBreak()
354- printTrees(bindings, lineBreak())
355- }
356- this += lineBreak()
357- printTree(expansion)
347+ printStats(bindings, expansion)
358348 }
359349 this += lineBreak() += " }"
360350
@@ -406,6 +396,30 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
406396
407397 }
408398
399+ def printStats (stats : List [Tree ], expr : Tree ): Unit = {
400+ def printSeparator (nextStats : List [Tree ]) = {
401+ // Avoid accidental application of opening `{` on next line with a double break
402+ val next = if (nextStats.isEmpty) expr else nextStats.head
403+ next match {
404+ case Term .Block (DefDef (" while$" | " doWhile$" , _, _, _, _) :: Nil , _) => this += lineBreak()
405+ case Term .Block (_, _) => this += doubleLineBreak()
406+ case Term .Inlined (_, _, _) => this += doubleLineBreak()
407+ case _ => this += lineBreak()
408+ }
409+ }
410+ def printSeparated (list : List [Tree ]): Unit = list match {
411+ case Nil =>
412+ printTree(expr)
413+ case x :: xs =>
414+ printTree(x)
415+ printSeparator(xs)
416+ printSeparated(xs)
417+ }
418+
419+ this += lineBreak()
420+ printSeparated(stats)
421+ }
422+
409423 def printTrees (trees : List [Tree ], sep : String ): Buffer = {
410424 def printSeparated (list : List [Tree ]): Unit = list match {
411425 case Nil =>
@@ -609,14 +623,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
609623 }
610624 this += " =>"
611625 indented {
612- this += lineBreak()
613626 body match {
614627 case Term .Block (stats, expr) =>
615- printTrees(stats, lineBreak())
616- if (stats.nonEmpty)
617- this += lineBreak()
618- printTree(expr)
628+ printStats(stats, expr)
619629 case body =>
630+ this += lineBreak()
620631 printTree(body)
621632 }
622633 }
0 commit comments