@@ -191,8 +191,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
191
191
case stats =>
192
192
this += " {"
193
193
indented {
194
- this += lineBreak()
195
- printTrees(stats, lineBreak())
194
+ printStats(stats.init, stats.last)
196
195
}
197
196
this += lineBreak() += " }"
198
197
}
@@ -205,8 +204,9 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
205
204
case stats =>
206
205
this += " {"
207
206
indented {
207
+ printStats(stats.init, stats.last)
208
208
this += lineBreak()
209
- printTrees (stats, lineBreak() )
209
+ printTree (stats.last )
210
210
}
211
211
this += lineBreak() += " }"
212
212
}
@@ -327,28 +327,19 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
327
327
this += " )"
328
328
case _ =>
329
329
this += " {"
330
+ val (stats1, expr1) =
331
+ if (isLoopEntryPoint(expr)) (stats.init, stats.last)
332
+ else (stats, expr)
330
333
indented {
331
- if (! stats.isEmpty) {
332
- this += lineBreak()
333
- printTrees(stats, lineBreak())
334
- }
335
- if (! isLoopEntryPoint(expr)) {
336
- this += lineBreak()
337
- printTree(expr)
338
- }
334
+ printStats(stats1, expr1)
339
335
}
340
336
this += lineBreak() += " }"
341
337
}
342
338
343
339
case Term .Inlined (call, bindings, expansion) =>
344
- sb.append( " { // inlined" )
340
+ this += " { // inlined"
345
341
indented {
346
- if (! bindings.isEmpty) {
347
- this += lineBreak()
348
- printTrees(bindings, lineBreak())
349
- }
350
- this += lineBreak()
351
- printTree(expansion)
342
+ printStats(bindings, expansion)
352
343
}
353
344
this += lineBreak() += " }"
354
345
@@ -400,6 +391,31 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
400
391
401
392
}
402
393
394
+ def printStats (stats : List [Tree ], expr : Tree ): Unit = {
395
+ def printSeparator (nextStats : List [Tree ]) = {
396
+ this += lineBreak()
397
+ // Avoid accidental application of opening `{` on next line with a double break
398
+ val next = if (nextStats.isEmpty) expr else nextStats.head
399
+ next match {
400
+ case Term .Block (DefDef (" while$" | " doWhile$" , _, _, _, _) :: Nil , _) =>
401
+ case Term .Block (_, _) => this += lineBreak()
402
+ case Term .Inlined (_, _, _) => this += lineBreak()
403
+ case _ =>
404
+ }
405
+ }
406
+ def printSeparated (list : List [Tree ]): Unit = list match {
407
+ case Nil =>
408
+ printTree(expr)
409
+ case x :: xs =>
410
+ printTree(x)
411
+ printSeparator(xs)
412
+ printSeparated(xs)
413
+ }
414
+
415
+ this += lineBreak()
416
+ printSeparated(stats)
417
+ }
418
+
403
419
def printTrees (trees : List [Tree ], sep : String ): Buffer = {
404
420
def printSeparated (list : List [Tree ]): Unit = list match {
405
421
case Nil =>
@@ -603,14 +619,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
603
619
}
604
620
this += " =>"
605
621
indented {
606
- this += lineBreak()
607
622
body match {
608
623
case Term .Block (stats, expr) =>
609
- printTrees(stats, lineBreak())
610
- if (stats.nonEmpty)
611
- this += lineBreak()
612
- printTree(expr)
624
+ printStats(stats, expr)
613
625
case body =>
626
+ this += lineBreak()
614
627
printTree(body)
615
628
}
616
629
}
0 commit comments