Skip to content

Commit 02d6ed6

Browse files
committed
Add inParens, inSquareParens and inBlock
1 parent fce7ef8 commit 02d6ed6

File tree

1 file changed

+71
-99
lines changed

1 file changed

+71
-99
lines changed

library/src/scala/tasty/util/ShowSourceCode.scala

Lines changed: 71 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
@@ -51,12 +72,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
5172
if (name == "<empty>") {
5273
printTrees(stats1, lineBreak())
5374
} 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()))
6077
}
6178

6279
case Import(expr, selectors) =>
@@ -97,15 +114,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
97114
def printParent(parent: Parent): Unit = parent match {
98115
case parent @ Term.TypeApply(fun, targs) =>
99116
printParent(fun)
100-
this += "["
101-
printTypeOrBoundsTrees(targs, ", ")
102-
this += "]"
117+
inSquareParens(printTypeOrBoundsTrees(targs, ", "))
103118

104119
case parent @ Term.Apply(fun, args) =>
105120
printParent(fun)
106-
this += "("
107-
printTrees(args, ", ")
108-
this += ")"
121+
inParens(printTrees(args, ", "))
109122

110123
case parent @ Term.Select(Term.New(tpt), _, _) =>
111124
printTypeTree(tpt)
@@ -205,17 +218,14 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
205218
}
206219

207220
case Term.While(cond, body) =>
208-
this += "while ("
209-
printTree(cond)
210-
this += ") "
221+
this += "while "
222+
inParens(printTree(cond)) += " "
211223
printTree(body)
212224

213225
case Term.DoWhile(body, cond) =>
214226
this += "do "
215-
printTree(body)
216-
this += " while ("
217-
printTree(cond)
218-
this += ")"
227+
printTree(body) += " while "
228+
inParens(printTree(cond))
219229

220230
case ddef @ DefDef(name, targs, argss, tpt, rhs) =>
221231
printDefAnnotations(ddef)
@@ -287,9 +297,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
287297
case _ => args
288298
}
289299

290-
this += "("
291-
printTrees(args1, ", ")
292-
this += ")"
300+
inParens(printTrees(args1, ", "))
293301

294302
case Term.TypeApply(fn, args) =>
295303
printTree(fn)
@@ -298,9 +306,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
298306
// type bounds already printed in `fn`
299307
this
300308
case _ =>
301-
this += "["
302-
printTypeOrBoundsTrees(args, ", ")
303-
this += "]"
309+
inSquareParens(printTypeOrBoundsTrees(args, ", "))
304310
}
305311

306312
case Term.Super(qual, idOpt) =>
@@ -311,7 +317,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
311317
this += "super"
312318
for (id <- idOpt) {
313319
val Id(name) = id
314-
this += "[" += name += "]"
320+
inSquareParens(this += name)
315321
}
316322
this
317323

@@ -320,21 +326,21 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
320326
case Types.Repeated(_) =>
321327
printTree(term)
322328
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)
335343
}
336-
printTypeOrAnnots(tpt.tpe)
337-
this += ")"
338344
}
339345

340346
case Term.Assign(lhs, rhs) =>
@@ -352,11 +358,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
352358
case Term.Lambda(_, _) =>
353359
// Decompile lambda from { def annon$(...) = ...; closure(annon$, ...)}
354360
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+
}
360366
case _ =>
361367
this += "{"
362368
indented {
@@ -377,32 +383,24 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
377383
this
378384

379385
case Term.If(cond, thenp, elsep) =>
380-
this += "if ("
381-
printTree(cond)
382-
this += ") "
386+
this += "if "
387+
inParens(printTree(cond))
388+
this += " "
383389
printTree(thenp)
384390
this+= " else "
385391
printTree(elsep)
386392

387393
case Term.Match(selector, cases) =>
388394
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()))
395397

396398
case Term.Try(body, cases, finallyOpt) =>
397399
this += "try "
398400
printTree(body)
399401
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()))
406404
}
407405
finallyOpt match {
408406
case Some(t) =>
@@ -539,9 +537,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
539537
printSeparated(xs)
540538
}
541539

542-
this += "["
543-
printSeparated(targs)
544-
this += "]"
540+
inSquareParens(printSeparated(targs))
545541
}
546542
}
547543

@@ -570,9 +566,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
570566
this += ", "
571567
printSeparated(xs)
572568
}
573-
this += "["
574-
printSeparated(tparams)
575-
this += "]"
569+
inSquareParens(printSeparated(tparams))
576570
if (isMember) {
577571
this += " = "
578572
printTypeOrBoundsTree(body)
@@ -584,8 +578,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
584578
}
585579
}
586580

587-
def printArgsDefs(args: List[ValDef]): Unit = {
588-
this += "("
581+
def printArgsDefs(args: List[ValDef]): Unit = inParens {
589582
args match {
590583
case Nil =>
591584
case arg :: _ =>
@@ -603,7 +596,6 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
603596
}
604597

605598
printSeparated(args)
606-
this += ")"
607599
}
608600

609601
def printAnnotations(trees: List[Term]): Buffer = {
@@ -672,14 +664,10 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
672664
case Term.TypeApply(Term.Select(extractor, "unapply" | "unapplySeq", _), _) => printTree(extractor)
673665
case _ => throw new MatchError(fun.show)
674666
}
675-
this += "("
676-
printPatterns(patterns, ", ")
677-
this += ")"
667+
inParens(printPatterns(patterns, ", "))
678668

679669
case Pattern.Alternative(trees) =>
680-
this += "("
681-
printPatterns(trees, " | ")
682-
this += ")"
670+
inParens(printPatterns(trees, " | "))
683671

684672
case Pattern.TypeTest(tpt) =>
685673
this += "_: "
@@ -758,18 +746,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
758746

759747
case TypeTree.Refined(tpt, refinements) =>
760748
printTypeTree(tpt)
761-
this += " {"
762-
indented {
763-
this += lineBreak()
764-
printTrees(refinements, "; ")
765-
}
766-
this += lineBreak() += "}"
749+
inBlock(printTrees(refinements, "; "))
767750

768751
case TypeTree.Applied(tpt, args) =>
769752
printTypeTree(tpt)
770-
this += "["
771-
printTypeOrBoundsTrees(args, ", ")
772-
this += "]"
753+
inSquareParens(printTypeOrBoundsTrees(args, ", "))
773754

774755
case TypeTree.Annotated(tpt, annot) =>
775756
val Annotation(ref, args) = annot
@@ -860,9 +841,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
860841
this += "_*"
861842
case _ =>
862843
printType(tp)
863-
this += "["
864-
printTypesOrBounds(args, ", ")
865-
this += "]"
844+
inSquareParens(printTypesOrBounds(args, ", "))
866845
}
867846

868847
case Type.AnnotatedType(tp, annot) =>
@@ -901,9 +880,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
901880
}
902881

903882
case Type.TypeLambda(paramNames, tparams, body) =>
904-
this += "["
905-
printMethodicTypeParams(paramNames, tparams)
906-
this += "] => "
883+
inSquareParens(printMethodicTypeParams(paramNames, tparams))
884+
this += " => "
907885
printTypeOrBound(body)
908886

909887
case Type.ParamRef(lambda, idx) =>
@@ -935,9 +913,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
935913
val Annotation(ref, args) = annot
936914
this += "@"
937915
printTypeTree(ref)
938-
this += "("
939-
printTrees(args, ", ")
940-
this += ")"
916+
inParens(printTrees(args, ", "))
941917
}
942918

943919
def printDefAnnotations(definition: Definition): Buffer = {
@@ -958,14 +934,10 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
958934
def printRefinement(tpe: Type): Buffer = {
959935
def printMethodicType(tp: TypeOrBounds): Unit = tp match {
960936
case tp @ Type.MethodType(paramNames, params, res) =>
961-
this += "("
962-
printMethodicTypeParams(paramNames, params)
963-
this += ")"
937+
inParens(printMethodicTypeParams(paramNames, params))
964938
printMethodicType(res)
965939
case tp @ Type.TypeLambda(paramNames, params, res) =>
966-
this += "["
967-
printMethodicTypeParams(paramNames, params)
968-
this += "]"
940+
inSquareParens(printMethodicTypeParams(paramNames, params))
969941
printMethodicType(res)
970942
case Type.ByNameType(t) =>
971943
this += ": "

0 commit comments

Comments
 (0)