@@ -41,7 +41,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
41
41
this += " )"
42
42
}
43
43
44
- def inSquareParens (body : => Unit ): Buffer = {
44
+ def inSquare (body : => Unit ): Buffer = {
45
45
this += " ["
46
46
body
47
47
this += " ]"
@@ -126,7 +126,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
126
126
def printParent (parent : Parent ): Unit = parent match {
127
127
case parent @ Term .TypeApply (fun, targs) =>
128
128
printParent(fun)
129
- inSquareParens (printTypeOrBoundsTrees(targs, " , " ))
129
+ inSquare (printTypeOrBoundsTrees(targs, " , " ))
130
130
131
131
case parent @ Term .Apply (fun, args) =>
132
132
printParent(fun)
@@ -215,6 +215,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
215
215
if (flags.isImplicit) this += " implicit "
216
216
if (flags.isOverride) this += " override "
217
217
218
+ printProtectedOrPrivate(vdef)
219
+
218
220
if (flags.isLazy) this += " lazy "
219
221
if (vdef.flags.isMutable) this += " var "
220
222
else this += " val "
@@ -242,12 +244,14 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
242
244
case ddef @ DefDef (name, targs, argss, tpt, rhs) =>
243
245
printDefAnnotations(ddef)
244
246
247
+ val isConstructor = name == " <init>"
248
+
245
249
val flags = ddef.flags
246
250
if (flags.isImplicit) this += " implicit "
247
251
if (flags.isInline) this += " inline "
248
252
if (flags.isOverride) this += " override "
249
253
250
- val isConstructor = name == " <init> "
254
+ printProtectedOrPrivate(ddef)
251
255
252
256
this += " def " += (if (isConstructor) " this" else name)
253
257
printTargsDefs(targs)
@@ -318,7 +322,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
318
322
// type bounds already printed in `fn`
319
323
this
320
324
case _ =>
321
- inSquareParens (printTypeOrBoundsTrees(args, " , " ))
325
+ inSquare (printTypeOrBoundsTrees(args, " , " ))
322
326
}
323
327
324
328
case Term .Super (qual, idOpt) =>
@@ -329,7 +333,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
329
333
this += " super"
330
334
for (id <- idOpt) {
331
335
val Id (name) = id
332
- inSquareParens (this += name)
336
+ inSquare (this += name)
333
337
}
334
338
this
335
339
@@ -549,7 +553,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
549
553
printSeparated(xs)
550
554
}
551
555
552
- inSquareParens (printSeparated(targs))
556
+ inSquare (printSeparated(targs))
553
557
}
554
558
}
555
559
@@ -578,7 +582,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
578
582
this += " , "
579
583
printSeparated(xs)
580
584
}
581
- inSquareParens (printSeparated(tparams))
585
+ inSquare (printSeparated(tparams))
582
586
if (isMember) {
583
587
this += " = "
584
588
printTypeOrBoundsTree(body)
@@ -600,9 +604,9 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
600
604
601
605
def printSeparated (list : List [ValDef ]): Unit = list match {
602
606
case Nil =>
603
- case x :: Nil => printArgDef (x)
607
+ case x :: Nil => printParamDef (x)
604
608
case x :: xs =>
605
- printArgDef (x)
609
+ printParamDef (x)
606
610
this += " , "
607
611
printSeparated(xs)
608
612
}
@@ -623,15 +627,24 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
623
627
this
624
628
}
625
629
626
- def printArgDef (arg : ValDef ): Unit = {
630
+ def printParamDef (arg : ValDef ): Unit = {
627
631
val ValDef (name, tpt, rhs) = arg
628
632
arg.owner match {
629
633
case DefDef (" <init>" , _, _, _, _) =>
630
634
val ClassDef (_, _, _, _, body) = arg.owner.owner
631
635
body.collectFirst {
632
636
case vdef @ ValDef (`name`, _, _) if vdef.flags.isParamAccessor =>
633
- if (! vdef.flags.isLocal && ! vdef.flags.isCaseAcessor)
634
- this += " val " // TODO `var`s
637
+ if (! vdef.flags.isLocal) {
638
+ var printedPrefix = false
639
+ if (vdef.flags.isOverride) {
640
+ this += " override "
641
+ printedPrefix = true
642
+ }
643
+ printedPrefix |= printProtectedOrPrivate(vdef)
644
+ if (vdef.flags.isMutable) this += " var "
645
+ else if (printedPrefix || ! vdef.flags.isCaseAcessor) this += " val "
646
+ else this // val not explicitly needed
647
+ }
635
648
}
636
649
case _ =>
637
650
}
@@ -715,7 +728,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
715
728
case Constant .String (v) => this += '"' += escapedString(v) += '"'
716
729
case Constant .ClassTag (v) =>
717
730
this += " classOf"
718
- inSquareParens (printType(v))
731
+ inSquare (printType(v))
719
732
}
720
733
721
734
def printTypeOrBoundsTree (tpt : TypeOrBoundsTree ): Buffer = tpt match {
@@ -772,7 +785,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
772
785
773
786
case TypeTree .Applied (tpt, args) =>
774
787
printTypeTree(tpt)
775
- inSquareParens (printTypeOrBoundsTrees(args, " , " ))
788
+ inSquare (printTypeOrBoundsTrees(args, " , " ))
776
789
777
790
case TypeTree .Annotated (tpt, annot) =>
778
791
val Annotation (ref, args) = annot
@@ -863,7 +876,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
863
876
this += " _*"
864
877
case _ =>
865
878
printType(tp)
866
- inSquareParens (printTypesOrBounds(args, " , " ))
879
+ inSquare (printTypesOrBounds(args, " , " ))
867
880
}
868
881
869
882
case Type .AnnotatedType (tp, annot) =>
@@ -888,21 +901,14 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
888
901
889
902
case Type .ThisType (tp) =>
890
903
tp match {
891
- case Type .SymRef (cdef @ ClassDef (name, _, _, _, _), prefix) if ! cdef.flags.isObject =>
892
- def printPrefix (prefix : TypeOrBounds ): Unit = prefix match {
893
- case Type .SymRef (ClassDef (name, _, _, _, _), prefix2) =>
894
- printPrefix(prefix2)
895
- this += name += " ."
896
- case _ =>
897
- }
898
- printPrefix(prefix)
899
- this += name
904
+ case Type .SymRef (cdef @ ClassDef (_, _, _, _, _), _) if ! cdef.flags.isObject =>
905
+ printFullClassName(tp)
900
906
this += " .this"
901
907
case _ => printType(tp)
902
908
}
903
909
904
910
case Type .TypeLambda (paramNames, tparams, body) =>
905
- inSquareParens (printMethodicTypeParams(paramNames, tparams))
911
+ inSquare (printMethodicTypeParams(paramNames, tparams))
906
912
this += " => "
907
913
printTypeOrBound(body)
908
914
@@ -959,7 +965,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
959
965
inParens(printMethodicTypeParams(paramNames, params))
960
966
printMethodicType(res)
961
967
case tp @ Type .TypeLambda (paramNames, params, res) =>
962
- inSquareParens (printMethodicTypeParams(paramNames, params))
968
+ inSquare (printMethodicTypeParams(paramNames, params))
963
969
printMethodicType(res)
964
970
case Type .ByNameType (t) =>
965
971
this += " : "
@@ -1038,6 +1044,46 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
1038
1044
printType(hi)
1039
1045
}
1040
1046
1047
+ def printProtectedOrPrivate (definition : Definition ): Boolean = {
1048
+ var prefixWasPrinted = false
1049
+ def printWithin (within : Type ) = within match {
1050
+ case Type .SymRef (PackageDef (name, _), _) => this += name
1051
+ case _ => printFullClassName(within)
1052
+ }
1053
+ if (definition.flags.isProtected) {
1054
+ this += " protected"
1055
+ definition.protectedWithin match {
1056
+ case Some (within) =>
1057
+ inSquare(printWithin(within))
1058
+ case _ =>
1059
+ }
1060
+ prefixWasPrinted = true
1061
+ } else {
1062
+ definition.privateWithin match {
1063
+ case Some (within) =>
1064
+ this += " private"
1065
+ inSquare(printWithin(within))
1066
+ prefixWasPrinted = true
1067
+ case _ =>
1068
+ }
1069
+ }
1070
+ if (prefixWasPrinted)
1071
+ this += " "
1072
+ prefixWasPrinted
1073
+ }
1074
+
1075
+ def printFullClassName (tp : TypeOrBounds ): Unit = {
1076
+ def printClassPrefix (prefix : TypeOrBounds ): Unit = prefix match {
1077
+ case Type .SymRef (ClassDef (name, _, _, _, _), prefix2) =>
1078
+ printClassPrefix(prefix2)
1079
+ this += name += " ."
1080
+ case _ =>
1081
+ }
1082
+ val Type .SymRef (ClassDef (name, _, _, _, _), prefix) = tp
1083
+ printClassPrefix(prefix)
1084
+ this += name
1085
+ }
1086
+
1041
1087
def += (x : Boolean ): this .type = { sb.append(x); this }
1042
1088
def += (x : Byte ): this .type = { sb.append(x); this }
1043
1089
def += (x : Short ): this .type = { sb.append(x); this }
0 commit comments