@@ -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,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
205
204
case stats =>
206
205
this += " {"
207
206
indented {
208
- this += lineBreak()
209
- printTrees(stats, lineBreak())
207
+ printStats(stats.init, stats.last)
210
208
}
211
209
this += lineBreak() += " }"
212
210
}
@@ -327,28 +325,19 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
327
325
this += " )"
328
326
case _ =>
329
327
this += " {"
328
+ val (stats1, expr1) =
329
+ if (isLoopEntryPoint(expr)) (stats.init, stats.last)
330
+ else (stats, expr)
330
331
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)
339
333
}
340
334
this += lineBreak() += " }"
341
335
}
342
336
343
337
case Term .Inlined (call, bindings, expansion) =>
344
- sb.append( " { // inlined" )
338
+ this += " { // inlined"
345
339
indented {
346
- if (! bindings.isEmpty) {
347
- this += lineBreak()
348
- printTrees(bindings, lineBreak())
349
- }
350
- this += lineBreak()
351
- printTree(expansion)
340
+ printStats(bindings, expansion)
352
341
}
353
342
this += lineBreak() += " }"
354
343
@@ -400,6 +389,31 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
400
389
401
390
}
402
391
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
+
403
417
def printTrees (trees : List [Tree ], sep : String ): Buffer = {
404
418
def printSeparated (list : List [Tree ]): Unit = list match {
405
419
case Nil =>
@@ -603,14 +617,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
603
617
}
604
618
this += " =>"
605
619
indented {
606
- this += lineBreak()
607
620
body match {
608
621
case Term .Block (stats, expr) =>
609
- printTrees(stats, lineBreak())
610
- if (stats.nonEmpty)
611
- this += lineBreak()
612
- printTree(expr)
622
+ printStats(stats, expr)
613
623
case body =>
624
+ this += lineBreak()
614
625
printTree(body)
615
626
}
616
627
}
0 commit comments