@@ -35,6 +35,27 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
3535 indent -= 1
3636 }
3737
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+
3859 def result (): String = sb.result()
3960
4061 def lineBreak (): String = " \n " + (" " * indent)
@@ -60,12 +81,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
6081 case _ =>
6182 this += " package "
6283 printType(name.tpe)
63- this += " {"
64- indented {
65- this += lineBreak()
66- printTrees(stats1, lineBreak())
67- }
68- this += lineBreak() += " }"
84+ inBlock(printTrees(stats1, lineBreak()))
6985 }
7086
7187 case Import (expr, selectors) =>
@@ -110,15 +126,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
110126 def printParent (parent : Parent ): Unit = parent match {
111127 case parent @ Term .TypeApply (fun, targs) =>
112128 printParent(fun)
113- this += " ["
114- printTypeOrBoundsTrees(targs, " , " )
115- this += " ]"
129+ inSquareParens(printTypeOrBoundsTrees(targs, " , " ))
116130
117131 case parent @ Term .Apply (fun, args) =>
118132 printParent(fun)
119- this += " ("
120- printTrees(args, " , " )
121- this += " )"
133+ inParens(printTrees(args, " , " ))
122134
123135 case parent @ Term .Select (Term .New (tpt), _, _) =>
124136 printTypeTree(tpt)
@@ -218,17 +230,14 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
218230 }
219231
220232 case Term .While (cond, body) =>
221- this += " while ("
222- printTree(cond)
223- this += " ) "
233+ this += " while "
234+ inParens(printTree(cond)) += " "
224235 printTree(body)
225236
226237 case Term .DoWhile (body, cond) =>
227238 this += " do "
228- printTree(body)
229- this += " while ("
230- printTree(cond)
231- this += " )"
239+ printTree(body) += " while "
240+ inParens(printTree(cond))
232241
233242 case ddef @ DefDef (name, targs, argss, tpt, rhs) =>
234243 printDefAnnotations(ddef)
@@ -300,9 +309,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
300309 case _ => args
301310 }
302311
303- this += " ("
304- printTrees(args1, " , " )
305- this += " )"
312+ inParens(printTrees(args1, " , " ))
306313
307314 case Term .TypeApply (fn, args) =>
308315 printTree(fn)
@@ -311,9 +318,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
311318 // type bounds already printed in `fn`
312319 this
313320 case _ =>
314- this += " ["
315- printTypeOrBoundsTrees(args, " , " )
316- this += " ]"
321+ inSquareParens(printTypeOrBoundsTrees(args, " , " ))
317322 }
318323
319324 case Term .Super (qual, idOpt) =>
@@ -324,7 +329,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
324329 this += " super"
325330 for (id <- idOpt) {
326331 val Id (name) = id
327- this += " [ " += name += " ] "
332+ inSquareParens( this += name)
328333 }
329334 this
330335
@@ -333,21 +338,21 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
333338 case Types .Repeated (_) =>
334339 printTree(term)
335340 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)
348355 }
349- printTypeOrAnnots(tpt.tpe)
350- this += " )"
351356 }
352357
353358 case Term .Assign (lhs, rhs) =>
@@ -365,11 +370,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
365370 case Term .Lambda (_, _) =>
366371 // Decompile lambda from { def annon$(...) = ...; closure(annon$, ...)}
367372 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+ }
373378 case _ =>
374379 this += " {"
375380 indented {
@@ -390,32 +395,24 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
390395 this
391396
392397 case Term .If (cond, thenp, elsep) =>
393- this += " if ( "
394- printTree(cond)
395- this += " ) "
398+ this += " if "
399+ inParens( printTree(cond) )
400+ this += " "
396401 printTree(thenp)
397402 this += " else "
398403 printTree(elsep)
399404
400405 case Term .Match (selector, cases) =>
401406 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()))
408409
409410 case Term .Try (body, cases, finallyOpt) =>
410411 this += " try "
411412 printTree(body)
412413 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()))
419416 }
420417 finallyOpt match {
421418 case Some (t) =>
@@ -552,9 +549,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
552549 printSeparated(xs)
553550 }
554551
555- this += " ["
556- printSeparated(targs)
557- this += " ]"
552+ inSquareParens(printSeparated(targs))
558553 }
559554 }
560555
@@ -583,9 +578,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
583578 this += " , "
584579 printSeparated(xs)
585580 }
586- this += " ["
587- printSeparated(tparams)
588- this += " ]"
581+ inSquareParens(printSeparated(tparams))
589582 if (isMember) {
590583 this += " = "
591584 printTypeOrBoundsTree(body)
@@ -597,8 +590,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
597590 }
598591 }
599592
600- def printArgsDefs (args : List [ValDef ]): Unit = {
601- this += " ("
593+ def printArgsDefs (args : List [ValDef ]): Unit = inParens {
602594 args match {
603595 case Nil =>
604596 case arg :: _ =>
@@ -616,7 +608,6 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
616608 }
617609
618610 printSeparated(args)
619- this += " )"
620611 }
621612
622613 def printAnnotations (trees : List [Term ]): Buffer = {
@@ -685,14 +676,10 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
685676 case Term .TypeApply (Term .Select (extractor, " unapply" | " unapplySeq" , _), _) => printTree(extractor)
686677 case _ => throw new MatchError (fun.show)
687678 }
688- this += " ("
689- printPatterns(patterns, " , " )
690- this += " )"
679+ inParens(printPatterns(patterns, " , " ))
691680
692681 case Pattern .Alternative (trees) =>
693- this += " ("
694- printPatterns(trees, " | " )
695- this += " )"
682+ inParens(printPatterns(trees, " | " ))
696683
697684 case Pattern .TypeTest (tpt) =>
698685 this += " _: "
@@ -716,9 +703,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
716703 case Constant .Char (v) => this += '\' ' += escapedChar(v) += '\' '
717704 case Constant .String (v) => this += '"' += escapedString(v) += '"'
718705 case Constant .ClassTag (v) =>
719- this += " classOf["
720- printType(v)
721- this += " ]"
706+ this += " classOf"
707+ inSquareParens(printType(v))
722708 }
723709
724710 def printTypeOrBoundsTree (tpt : TypeOrBoundsTree ): Buffer = tpt match {
@@ -771,18 +757,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
771757
772758 case TypeTree .Refined (tpt, refinements) =>
773759 printTypeTree(tpt)
774- this += " {"
775- indented {
776- this += lineBreak()
777- printTrees(refinements, " ; " )
778- }
779- this += lineBreak() += " }"
760+ inBlock(printTrees(refinements, " ; " ))
780761
781762 case TypeTree .Applied (tpt, args) =>
782763 printTypeTree(tpt)
783- this += " ["
784- printTypeOrBoundsTrees(args, " , " )
785- this += " ]"
764+ inSquareParens(printTypeOrBoundsTrees(args, " , " ))
786765
787766 case TypeTree .Annotated (tpt, annot) =>
788767 val Annotation (ref, args) = annot
@@ -873,9 +852,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
873852 this += " _*"
874853 case _ =>
875854 printType(tp)
876- this += " ["
877- printTypesOrBounds(args, " , " )
878- this += " ]"
855+ inSquareParens(printTypesOrBounds(args, " , " ))
879856 }
880857
881858 case Type .AnnotatedType (tp, annot) =>
@@ -914,9 +891,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
914891 }
915892
916893 case Type .TypeLambda (paramNames, tparams, body) =>
917- this += " ["
918- printMethodicTypeParams(paramNames, tparams)
919- this += " ] => "
894+ inSquareParens(printMethodicTypeParams(paramNames, tparams))
895+ this += " => "
920896 printTypeOrBound(body)
921897
922898 case Type .ParamRef (lambda, idx) =>
@@ -948,9 +924,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
948924 val Annotation (ref, args) = annot
949925 this += " @"
950926 printTypeTree(ref)
951- this += " ("
952- printTrees(args, " , " )
953- this += " )"
927+ inParens(printTrees(args, " , " ))
954928 }
955929
956930 def printDefAnnotations (definition : Definition ): Buffer = {
@@ -971,14 +945,10 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
971945 def printRefinement (tpe : Type ): Buffer = {
972946 def printMethodicType (tp : TypeOrBounds ): Unit = tp match {
973947 case tp @ Type .MethodType (paramNames, params, res) =>
974- this += " ("
975- printMethodicTypeParams(paramNames, params)
976- this += " )"
948+ inParens(printMethodicTypeParams(paramNames, params))
977949 printMethodicType(res)
978950 case tp @ Type .TypeLambda (paramNames, params, res) =>
979- this += " ["
980- printMethodicTypeParams(paramNames, params)
981- this += " ]"
951+ inSquareParens(printMethodicTypeParams(paramNames, params))
982952 printMethodicType(res)
983953 case Type .ByNameType (t) =>
984954 this += " : "
0 commit comments