@@ -38,6 +38,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
38
38
def result (): String = sb.result()
39
39
40
40
def lineBreak (): String = " \n " + (" " * indent)
41
+ def doubleLineBreak (): String = " \n\n " + (" " * indent)
41
42
42
43
def printTree (tree : Tree ): Buffer = tree match {
43
44
case tree @ PackageClause (Term .Ident (name), stats) =>
@@ -191,8 +192,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
191
192
case stats =>
192
193
this += " {"
193
194
indented {
194
- this += lineBreak()
195
- printTrees(stats, lineBreak())
195
+ printStats(stats.init, stats.last)
196
196
}
197
197
this += lineBreak() += " }"
198
198
}
@@ -205,8 +205,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
205
205
case stats =>
206
206
this += " {"
207
207
indented {
208
- this += lineBreak()
209
- printTrees(stats, lineBreak())
208
+ printStats(stats.init, stats.last)
210
209
}
211
210
this += lineBreak() += " }"
212
211
}
@@ -333,28 +332,19 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
333
332
this += " )"
334
333
case _ =>
335
334
this += " {"
335
+ val (stats1, expr1) =
336
+ if (isLoopEntryPoint(expr)) (stats.init, stats.last)
337
+ else (stats, expr)
336
338
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)
345
340
}
346
341
this += lineBreak() += " }"
347
342
}
348
343
349
344
case Term .Inlined (call, bindings, expansion) =>
350
- sb.append( " { // inlined" )
345
+ this += " { // inlined"
351
346
indented {
352
- if (! bindings.isEmpty) {
353
- this += lineBreak()
354
- printTrees(bindings, lineBreak())
355
- }
356
- this += lineBreak()
357
- printTree(expansion)
347
+ printStats(bindings, expansion)
358
348
}
359
349
this += lineBreak() += " }"
360
350
@@ -406,6 +396,30 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
406
396
407
397
}
408
398
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
+
409
423
def printTrees (trees : List [Tree ], sep : String ): Buffer = {
410
424
def printSeparated (list : List [Tree ]): Unit = list match {
411
425
case Nil =>
@@ -609,14 +623,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
609
623
}
610
624
this += " =>"
611
625
indented {
612
- this += lineBreak()
613
626
body match {
614
627
case Term .Block (stats, expr) =>
615
- printTrees(stats, lineBreak())
616
- if (stats.nonEmpty)
617
- this += lineBreak()
618
- printTree(expr)
628
+ printStats(stats, expr)
619
629
case body =>
630
+ this += lineBreak()
620
631
printTree(body)
621
632
}
622
633
}
0 commit comments