@@ -35,6 +35,27 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
35
35
indent -= 1
36
36
}
37
37
38
+ def inParens (body : => Unit ): Buffer = {
39
+ this += " ("
40
+ body
41
+ this += " )"
42
+ }
43
+
44
+ def inSquareParens (body : => Unit ): Buffer = {
45
+ this += " ["
46
+ body
47
+ this += " ]"
48
+ }
49
+
50
+ def inBlock (body : => Unit ): Buffer = {
51
+ this += " {"
52
+ indented {
53
+ this += lineBreak()
54
+ body
55
+ }
56
+ this += lineBreak() += " }"
57
+ }
58
+
38
59
def result (): String = sb.result()
39
60
40
61
def lineBreak (): String = " \n " + (" " * indent)
@@ -51,12 +72,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
51
72
if (name == " <empty>" ) {
52
73
printTrees(stats1, lineBreak())
53
74
} else {
54
- this += " package " += name += " {"
55
- indented {
56
- this += lineBreak()
57
- printTrees(stats1, lineBreak())
58
- }
59
- this += lineBreak() += " }"
75
+ this += " package " += name
76
+ inBlock(printTrees(stats1, lineBreak()))
60
77
}
61
78
62
79
case Import (expr, selectors) =>
@@ -97,15 +114,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
97
114
def printParent (parent : Parent ): Unit = parent match {
98
115
case parent @ Term .TypeApply (fun, targs) =>
99
116
printParent(fun)
100
- this += " ["
101
- printTypeOrBoundsTrees(targs, " , " )
102
- this += " ]"
117
+ inSquareParens(printTypeOrBoundsTrees(targs, " , " ))
103
118
104
119
case parent @ Term .Apply (fun, args) =>
105
120
printParent(fun)
106
- this += " ("
107
- printTrees(args, " , " )
108
- this += " )"
121
+ inParens(printTrees(args, " , " ))
109
122
110
123
case parent @ Term .Select (Term .New (tpt), _, _) =>
111
124
printTypeTree(tpt)
@@ -205,17 +218,14 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
205
218
}
206
219
207
220
case Term .While (cond, body) =>
208
- this += " while ("
209
- printTree(cond)
210
- this += " ) "
221
+ this += " while "
222
+ inParens(printTree(cond)) += " "
211
223
printTree(body)
212
224
213
225
case Term .DoWhile (body, cond) =>
214
226
this += " do "
215
- printTree(body)
216
- this += " while ("
217
- printTree(cond)
218
- this += " )"
227
+ printTree(body) += " while "
228
+ inParens(printTree(cond))
219
229
220
230
case ddef @ DefDef (name, targs, argss, tpt, rhs) =>
221
231
printDefAnnotations(ddef)
@@ -287,9 +297,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
287
297
case _ => args
288
298
}
289
299
290
- this += " ("
291
- printTrees(args1, " , " )
292
- this += " )"
300
+ inParens(printTrees(args1, " , " ))
293
301
294
302
case Term .TypeApply (fn, args) =>
295
303
printTree(fn)
@@ -298,9 +306,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
298
306
// type bounds already printed in `fn`
299
307
this
300
308
case _ =>
301
- this += " ["
302
- printTypeOrBoundsTrees(args, " , " )
303
- this += " ]"
309
+ inSquareParens(printTypeOrBoundsTrees(args, " , " ))
304
310
}
305
311
306
312
case Term .Super (qual, idOpt) =>
@@ -311,7 +317,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
311
317
this += " super"
312
318
for (id <- idOpt) {
313
319
val Id (name) = id
314
- this += " [ " += name += " ] "
320
+ inSquareParens( this += name)
315
321
}
316
322
this
317
323
@@ -320,21 +326,21 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
320
326
case Types .Repeated (_) =>
321
327
printTree(term)
322
328
case _ =>
323
- this += " ("
324
- printTree(term)
325
- this += " : "
326
- def printTypeOrAnnots (tpe : Type ): Unit = tpe match {
327
- case Type .AnnotatedType (tp, annot) if tp == term.tpe =>
328
- printAnnotation(annot)
329
- case Type .AnnotatedType (tp, annot) =>
330
- printTypeOrAnnots(tp)
331
- this += " "
332
- printAnnotation(annot)
333
- case tpe =>
334
- printType(tpe)
329
+ inParens {
330
+ printTree(term)
331
+ this += " : "
332
+ def printTypeOrAnnots (tpe : Type ): Unit = tpe match {
333
+ case Type .AnnotatedType (tp, annot) if tp == term.tpe =>
334
+ printAnnotation(annot)
335
+ case Type .AnnotatedType (tp, annot) =>
336
+ printTypeOrAnnots(tp)
337
+ this += " "
338
+ printAnnotation(annot)
339
+ case tpe =>
340
+ printType(tpe)
341
+ }
342
+ printTypeOrAnnots(tpt.tpe)
335
343
}
336
- printTypeOrAnnots(tpt.tpe)
337
- this += " )"
338
344
}
339
345
340
346
case Term .Assign (lhs, rhs) =>
@@ -352,11 +358,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
352
358
case Term .Lambda (_, _) =>
353
359
// Decompile lambda from { def annon$(...) = ...; closure(annon$, ...)}
354
360
val DefDef (_, _, args :: Nil , _, Some (rhs)) :: Nil = stats
355
- this += " ( "
356
- printArgsDefs(args)
357
- this += " => "
358
- printTree(rhs)
359
- this += " ) "
361
+ inParens {
362
+ printArgsDefs(args)
363
+ this += " => "
364
+ printTree(rhs)
365
+ }
360
366
case _ =>
361
367
this += " {"
362
368
indented {
@@ -377,32 +383,24 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
377
383
this
378
384
379
385
case Term .If (cond, thenp, elsep) =>
380
- this += " if ( "
381
- printTree(cond)
382
- this += " ) "
386
+ this += " if "
387
+ inParens( printTree(cond) )
388
+ this += " "
383
389
printTree(thenp)
384
390
this += " else "
385
391
printTree(elsep)
386
392
387
393
case Term .Match (selector, cases) =>
388
394
printTree(selector)
389
- this += " match {"
390
- indented {
391
- this += lineBreak()
392
- printCases(cases, lineBreak())
393
- }
394
- this += lineBreak() += " }"
395
+ this += " match"
396
+ inBlock(printCases(cases, lineBreak()))
395
397
396
398
case Term .Try (body, cases, finallyOpt) =>
397
399
this += " try "
398
400
printTree(body)
399
401
if (cases.nonEmpty) {
400
- this += " catch {"
401
- indented {
402
- this += lineBreak()
403
- printCases(cases, lineBreak())
404
- }
405
- this += lineBreak() += " }"
402
+ this += " catch"
403
+ inBlock(printCases(cases, lineBreak()))
406
404
}
407
405
finallyOpt match {
408
406
case Some (t) =>
@@ -539,9 +537,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
539
537
printSeparated(xs)
540
538
}
541
539
542
- this += " ["
543
- printSeparated(targs)
544
- this += " ]"
540
+ inSquareParens(printSeparated(targs))
545
541
}
546
542
}
547
543
@@ -570,9 +566,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
570
566
this += " , "
571
567
printSeparated(xs)
572
568
}
573
- this += " ["
574
- printSeparated(tparams)
575
- this += " ]"
569
+ inSquareParens(printSeparated(tparams))
576
570
if (isMember) {
577
571
this += " = "
578
572
printTypeOrBoundsTree(body)
@@ -584,8 +578,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
584
578
}
585
579
}
586
580
587
- def printArgsDefs (args : List [ValDef ]): Unit = {
588
- this += " ("
581
+ def printArgsDefs (args : List [ValDef ]): Unit = inParens {
589
582
args match {
590
583
case Nil =>
591
584
case arg :: _ =>
@@ -603,7 +596,6 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
603
596
}
604
597
605
598
printSeparated(args)
606
- this += " )"
607
599
}
608
600
609
601
def printAnnotations (trees : List [Term ]): Buffer = {
@@ -672,14 +664,10 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
672
664
case Term .TypeApply (Term .Select (extractor, " unapply" | " unapplySeq" , _), _) => printTree(extractor)
673
665
case _ => throw new MatchError (fun.show)
674
666
}
675
- this += " ("
676
- printPatterns(patterns, " , " )
677
- this += " )"
667
+ inParens(printPatterns(patterns, " , " ))
678
668
679
669
case Pattern .Alternative (trees) =>
680
- this += " ("
681
- printPatterns(trees, " | " )
682
- this += " )"
670
+ inParens(printPatterns(trees, " | " ))
683
671
684
672
case Pattern .TypeTest (tpt) =>
685
673
this += " _: "
@@ -758,18 +746,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
758
746
759
747
case TypeTree .Refined (tpt, refinements) =>
760
748
printTypeTree(tpt)
761
- this += " {"
762
- indented {
763
- this += lineBreak()
764
- printTrees(refinements, " ; " )
765
- }
766
- this += lineBreak() += " }"
749
+ inBlock(printTrees(refinements, " ; " ))
767
750
768
751
case TypeTree .Applied (tpt, args) =>
769
752
printTypeTree(tpt)
770
- this += " ["
771
- printTypeOrBoundsTrees(args, " , " )
772
- this += " ]"
753
+ inSquareParens(printTypeOrBoundsTrees(args, " , " ))
773
754
774
755
case TypeTree .Annotated (tpt, annot) =>
775
756
val Annotation (ref, args) = annot
@@ -860,9 +841,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
860
841
this += " _*"
861
842
case _ =>
862
843
printType(tp)
863
- this += " ["
864
- printTypesOrBounds(args, " , " )
865
- this += " ]"
844
+ inSquareParens(printTypesOrBounds(args, " , " ))
866
845
}
867
846
868
847
case Type .AnnotatedType (tp, annot) =>
@@ -901,9 +880,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
901
880
}
902
881
903
882
case Type .TypeLambda (paramNames, tparams, body) =>
904
- this += " ["
905
- printMethodicTypeParams(paramNames, tparams)
906
- this += " ] => "
883
+ inSquareParens(printMethodicTypeParams(paramNames, tparams))
884
+ this += " => "
907
885
printTypeOrBound(body)
908
886
909
887
case Type .ParamRef (lambda, idx) =>
@@ -935,9 +913,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
935
913
val Annotation (ref, args) = annot
936
914
this += " @"
937
915
printTypeTree(ref)
938
- this += " ("
939
- printTrees(args, " , " )
940
- this += " )"
916
+ inParens(printTrees(args, " , " ))
941
917
}
942
918
943
919
def printDefAnnotations (definition : Definition ): Buffer = {
@@ -958,14 +934,10 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
958
934
def printRefinement (tpe : Type ): Buffer = {
959
935
def printMethodicType (tp : TypeOrBounds ): Unit = tp match {
960
936
case tp @ Type .MethodType (paramNames, params, res) =>
961
- this += " ("
962
- printMethodicTypeParams(paramNames, params)
963
- this += " )"
937
+ inParens(printMethodicTypeParams(paramNames, params))
964
938
printMethodicType(res)
965
939
case tp @ Type .TypeLambda (paramNames, params, res) =>
966
- this += " ["
967
- printMethodicTypeParams(paramNames, params)
968
- this += " ]"
940
+ inSquareParens(printMethodicTypeParams(paramNames, params))
969
941
printMethodicType(res)
970
942
case Type .ByNameType (t) =>
971
943
this += " : "
0 commit comments