@@ -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)
@@ -60,12 +81,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
60
81
case _ =>
61
82
this += " package "
62
83
printType(name.tpe)
63
- this += " {"
64
- indented {
65
- this += lineBreak()
66
- printTrees(stats1, lineBreak())
67
- }
68
- this += lineBreak() += " }"
84
+ inBlock(printTrees(stats1, lineBreak()))
69
85
}
70
86
71
87
case Import (expr, selectors) =>
@@ -110,15 +126,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
110
126
def printParent (parent : Parent ): Unit = parent match {
111
127
case parent @ Term .TypeApply (fun, targs) =>
112
128
printParent(fun)
113
- this += " ["
114
- printTypeOrBoundsTrees(targs, " , " )
115
- this += " ]"
129
+ inSquareParens(printTypeOrBoundsTrees(targs, " , " ))
116
130
117
131
case parent @ Term .Apply (fun, args) =>
118
132
printParent(fun)
119
- this += " ("
120
- printTrees(args, " , " )
121
- this += " )"
133
+ inParens(printTrees(args, " , " ))
122
134
123
135
case parent @ Term .Select (Term .New (tpt), _, _) =>
124
136
printTypeTree(tpt)
@@ -218,17 +230,14 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
218
230
}
219
231
220
232
case Term .While (cond, body) =>
221
- this += " while ("
222
- printTree(cond)
223
- this += " ) "
233
+ this += " while "
234
+ inParens(printTree(cond)) += " "
224
235
printTree(body)
225
236
226
237
case Term .DoWhile (body, cond) =>
227
238
this += " do "
228
- printTree(body)
229
- this += " while ("
230
- printTree(cond)
231
- this += " )"
239
+ printTree(body) += " while "
240
+ inParens(printTree(cond))
232
241
233
242
case ddef @ DefDef (name, targs, argss, tpt, rhs) =>
234
243
printDefAnnotations(ddef)
@@ -300,9 +309,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
300
309
case _ => args
301
310
}
302
311
303
- this += " ("
304
- printTrees(args1, " , " )
305
- this += " )"
312
+ inParens(printTrees(args1, " , " ))
306
313
307
314
case Term .TypeApply (fn, args) =>
308
315
printTree(fn)
@@ -311,9 +318,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
311
318
// type bounds already printed in `fn`
312
319
this
313
320
case _ =>
314
- this += " ["
315
- printTypeOrBoundsTrees(args, " , " )
316
- this += " ]"
321
+ inSquareParens(printTypeOrBoundsTrees(args, " , " ))
317
322
}
318
323
319
324
case Term .Super (qual, idOpt) =>
@@ -324,7 +329,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
324
329
this += " super"
325
330
for (id <- idOpt) {
326
331
val Id (name) = id
327
- this += " [ " += name += " ] "
332
+ inSquareParens( this += name)
328
333
}
329
334
this
330
335
@@ -333,21 +338,21 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
333
338
case Types .Repeated (_) =>
334
339
printTree(term)
335
340
case _ =>
336
- this += " ("
337
- printTree(term)
338
- this += " : "
339
- def printTypeOrAnnots (tpe : Type ): Unit = tpe match {
340
- case Type .AnnotatedType (tp, annot) if tp == term.tpe =>
341
- printAnnotation(annot)
342
- case Type .AnnotatedType (tp, annot) =>
343
- printTypeOrAnnots(tp)
344
- this += " "
345
- printAnnotation(annot)
346
- case tpe =>
347
- printType(tpe)
341
+ inParens {
342
+ printTree(term)
343
+ this += " : "
344
+ def printTypeOrAnnots (tpe : Type ): Unit = tpe match {
345
+ case Type .AnnotatedType (tp, annot) if tp == term.tpe =>
346
+ printAnnotation(annot)
347
+ case Type .AnnotatedType (tp, annot) =>
348
+ printTypeOrAnnots(tp)
349
+ this += " "
350
+ printAnnotation(annot)
351
+ case tpe =>
352
+ printType(tpe)
353
+ }
354
+ printTypeOrAnnots(tpt.tpe)
348
355
}
349
- printTypeOrAnnots(tpt.tpe)
350
- this += " )"
351
356
}
352
357
353
358
case Term .Assign (lhs, rhs) =>
@@ -365,11 +370,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
365
370
case Term .Lambda (_, _) =>
366
371
// Decompile lambda from { def annon$(...) = ...; closure(annon$, ...)}
367
372
val DefDef (_, _, args :: Nil , _, Some (rhs)) :: Nil = stats
368
- this += " ( "
369
- printArgsDefs(args)
370
- this += " => "
371
- printTree(rhs)
372
- this += " ) "
373
+ inParens {
374
+ printArgsDefs(args)
375
+ this += " => "
376
+ printTree(rhs)
377
+ }
373
378
case _ =>
374
379
this += " {"
375
380
indented {
@@ -390,32 +395,24 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
390
395
this
391
396
392
397
case Term .If (cond, thenp, elsep) =>
393
- this += " if ( "
394
- printTree(cond)
395
- this += " ) "
398
+ this += " if "
399
+ inParens( printTree(cond) )
400
+ this += " "
396
401
printTree(thenp)
397
402
this += " else "
398
403
printTree(elsep)
399
404
400
405
case Term .Match (selector, cases) =>
401
406
printTree(selector)
402
- this += " match {"
403
- indented {
404
- this += lineBreak()
405
- printCases(cases, lineBreak())
406
- }
407
- this += lineBreak() += " }"
407
+ this += " match"
408
+ inBlock(printCases(cases, lineBreak()))
408
409
409
410
case Term .Try (body, cases, finallyOpt) =>
410
411
this += " try "
411
412
printTree(body)
412
413
if (cases.nonEmpty) {
413
- this += " catch {"
414
- indented {
415
- this += lineBreak()
416
- printCases(cases, lineBreak())
417
- }
418
- this += lineBreak() += " }"
414
+ this += " catch"
415
+ inBlock(printCases(cases, lineBreak()))
419
416
}
420
417
finallyOpt match {
421
418
case Some (t) =>
@@ -552,9 +549,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
552
549
printSeparated(xs)
553
550
}
554
551
555
- this += " ["
556
- printSeparated(targs)
557
- this += " ]"
552
+ inSquareParens(printSeparated(targs))
558
553
}
559
554
}
560
555
@@ -583,9 +578,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
583
578
this += " , "
584
579
printSeparated(xs)
585
580
}
586
- this += " ["
587
- printSeparated(tparams)
588
- this += " ]"
581
+ inSquareParens(printSeparated(tparams))
589
582
if (isMember) {
590
583
this += " = "
591
584
printTypeOrBoundsTree(body)
@@ -597,8 +590,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
597
590
}
598
591
}
599
592
600
- def printArgsDefs (args : List [ValDef ]): Unit = {
601
- this += " ("
593
+ def printArgsDefs (args : List [ValDef ]): Unit = inParens {
602
594
args match {
603
595
case Nil =>
604
596
case arg :: _ =>
@@ -616,7 +608,6 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
616
608
}
617
609
618
610
printSeparated(args)
619
- this += " )"
620
611
}
621
612
622
613
def printAnnotations (trees : List [Term ]): Buffer = {
@@ -685,14 +676,10 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
685
676
case Term .TypeApply (Term .Select (extractor, " unapply" | " unapplySeq" , _), _) => printTree(extractor)
686
677
case _ => throw new MatchError (fun.show)
687
678
}
688
- this += " ("
689
- printPatterns(patterns, " , " )
690
- this += " )"
679
+ inParens(printPatterns(patterns, " , " ))
691
680
692
681
case Pattern .Alternative (trees) =>
693
- this += " ("
694
- printPatterns(trees, " | " )
695
- this += " )"
682
+ inParens(printPatterns(trees, " | " ))
696
683
697
684
case Pattern .TypeTest (tpt) =>
698
685
this += " _: "
@@ -716,9 +703,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
716
703
case Constant .Char (v) => this += '\' ' += escapedChar(v) += '\' '
717
704
case Constant .String (v) => this += '"' += escapedString(v) += '"'
718
705
case Constant .ClassTag (v) =>
719
- this += " classOf["
720
- printType(v)
721
- this += " ]"
706
+ this += " classOf"
707
+ inSquareParens(printType(v))
722
708
}
723
709
724
710
def printTypeOrBoundsTree (tpt : TypeOrBoundsTree ): Buffer = tpt match {
@@ -771,18 +757,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
771
757
772
758
case TypeTree .Refined (tpt, refinements) =>
773
759
printTypeTree(tpt)
774
- this += " {"
775
- indented {
776
- this += lineBreak()
777
- printTrees(refinements, " ; " )
778
- }
779
- this += lineBreak() += " }"
760
+ inBlock(printTrees(refinements, " ; " ))
780
761
781
762
case TypeTree .Applied (tpt, args) =>
782
763
printTypeTree(tpt)
783
- this += " ["
784
- printTypeOrBoundsTrees(args, " , " )
785
- this += " ]"
764
+ inSquareParens(printTypeOrBoundsTrees(args, " , " ))
786
765
787
766
case TypeTree .Annotated (tpt, annot) =>
788
767
val Annotation (ref, args) = annot
@@ -873,9 +852,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
873
852
this += " _*"
874
853
case _ =>
875
854
printType(tp)
876
- this += " ["
877
- printTypesOrBounds(args, " , " )
878
- this += " ]"
855
+ inSquareParens(printTypesOrBounds(args, " , " ))
879
856
}
880
857
881
858
case Type .AnnotatedType (tp, annot) =>
@@ -914,9 +891,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
914
891
}
915
892
916
893
case Type .TypeLambda (paramNames, tparams, body) =>
917
- this += " ["
918
- printMethodicTypeParams(paramNames, tparams)
919
- this += " ] => "
894
+ inSquareParens(printMethodicTypeParams(paramNames, tparams))
895
+ this += " => "
920
896
printTypeOrBound(body)
921
897
922
898
case Type .ParamRef (lambda, idx) =>
@@ -948,9 +924,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
948
924
val Annotation (ref, args) = annot
949
925
this += " @"
950
926
printTypeTree(ref)
951
- this += " ("
952
- printTrees(args, " , " )
953
- this += " )"
927
+ inParens(printTrees(args, " , " ))
954
928
}
955
929
956
930
def printDefAnnotations (definition : Definition ): Buffer = {
@@ -971,14 +945,10 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
971
945
def printRefinement (tpe : Type ): Buffer = {
972
946
def printMethodicType (tp : TypeOrBounds ): Unit = tp match {
973
947
case tp @ Type .MethodType (paramNames, params, res) =>
974
- this += " ("
975
- printMethodicTypeParams(paramNames, params)
976
- this += " )"
948
+ inParens(printMethodicTypeParams(paramNames, params))
977
949
printMethodicType(res)
978
950
case tp @ Type .TypeLambda (paramNames, params, res) =>
979
- this += " ["
980
- printMethodicTypeParams(paramNames, params)
981
- this += " ]"
951
+ inSquareParens(printMethodicTypeParams(paramNames, params))
982
952
printMethodicType(res)
983
953
case Type .ByNameType (t) =>
984
954
this += " : "
0 commit comments