@@ -65,6 +65,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
65
65
printImportSelectors(selectors)
66
66
67
67
case cdef @ ClassDef (name, DefDef (_, targs, argss, _, _), parents, self, stats) =>
68
+ printDefAnnotations(cdef)
69
+
68
70
val flags = cdef.flags
69
71
if (flags.isFinal && ! flags.isObject) this += " final "
70
72
if (flags.isCase) this += " case "
@@ -146,11 +148,14 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
146
148
}
147
149
this
148
150
149
- case tdef@ TypeDef (name, rhs) =>
151
+ case tdef @ TypeDef (name, rhs) =>
152
+ printDefAnnotations(tdef)
150
153
this += " type "
151
154
printTargDef(tdef)
152
155
153
- case vdef@ ValDef (name, tpt, rhs) =>
156
+ case vdef @ ValDef (name, tpt, rhs) =>
157
+ printDefAnnotations(vdef)
158
+
154
159
val flags = vdef.flags
155
160
if (flags.isOverride) this += " override "
156
161
@@ -201,7 +206,9 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
201
206
printTree(cond)
202
207
this += " )"
203
208
204
- case ddef@ DefDef (name, targs, argss, tpt, rhs) =>
209
+ case ddef @ DefDef (name, targs, argss, tpt, rhs) =>
210
+ printDefAnnotations(ddef)
211
+
205
212
val flags = ddef.flags
206
213
if (flags.isOverride) sb.append(" override " )
207
214
@@ -220,8 +227,16 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
220
227
}
221
228
this
222
229
223
- case tree@ Term .Ident (name) =>
224
- printType(tree.tpe)
230
+ case tree @ Term .Ident (name) =>
231
+ tree.tpe match {
232
+ case Type .SymRef (_, Types .EmptyPrefix ()) | Type .TermRef (_, Types .EmptyPrefix ()) => this += name
233
+ case Type .SymRef (_, prefix) =>
234
+ printTypeOrBound(prefix)
235
+ this += " ." += name
236
+ case Type .TermRef (_, prefix) =>
237
+ printTypeOrBound(prefix)
238
+ this += " ." += name
239
+ }
225
240
226
241
case Term .Select (qual, name, sig) =>
227
242
printTree(qual)
@@ -516,6 +531,19 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
516
531
this += " )"
517
532
}
518
533
534
+ def printAnnotations (trees : List [Term ]): Buffer = {
535
+ def printSeparated (list : List [Term ]): Unit = list match {
536
+ case Nil =>
537
+ case x :: Nil => printAnnotation(x)
538
+ case x :: xs =>
539
+ printAnnotation(x)
540
+ this += " "
541
+ printSeparated(xs)
542
+ }
543
+ printSeparated(trees)
544
+ this
545
+ }
546
+
519
547
def printArgDef (arg : ValDef ): Unit = {
520
548
val ValDef (name, tpt, rhs) = arg
521
549
this += name += " : "
@@ -654,9 +682,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
654
682
printTypeTrees(args, " , " )
655
683
this += " ]"
656
684
657
- case TypeTree .Annotated (tpt, annots) =>
685
+ case TypeTree .Annotated (tpt, annot) =>
686
+ val Annotation (ref, args) = annot
658
687
printTypeTree(tpt)
659
- // TODO print annots
688
+ this += " "
689
+ printAnnotation(annot)
660
690
661
691
case TypeTree .And (left, right) =>
662
692
printTypeTree(left)
@@ -692,14 +722,13 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
692
722
693
723
case Type .SymRef (sym, prefix) =>
694
724
prefix match {
695
- case Type . ThisType ( Types .EmptyPackage () | Types . RootPackage () ) =>
725
+ case Types .EmptyPrefix ( ) =>
696
726
case prefix@ Type .SymRef (ClassDef (_, _, _, _, _), _) =>
697
727
printType(prefix)
698
728
this += " #"
699
729
case prefix@ Type () =>
700
730
printType(prefix)
701
731
this += " ."
702
- case prefix@ NoPrefix () =>
703
732
}
704
733
printDefinitionName(sym)
705
734
@@ -734,7 +763,10 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
734
763
this += " ]"
735
764
736
765
case Type .AnnotatedType (tp, annot) =>
766
+ val Annotation (ref, args) = annot
737
767
printType(tp)
768
+ this += " "
769
+ printAnnotation(annot)
738
770
739
771
case Type .AndType (left, right) =>
740
772
printType(left)
@@ -768,13 +800,35 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
768
800
}
769
801
770
802
def printDefinitionName (sym : Definition ): Buffer = sym match {
771
- case ValDef (name, _, _) => this += name
803
+ case ValDef (name, _, _) => this += name += " .type "
772
804
case DefDef (name, _, _, _, _) => this += name
773
805
case ClassDef (name, _, _, _, _) => this += name.stripSuffix(" $" )
774
806
case TypeDef (name, _) => this += name
775
807
case PackageDef (name, _) => this += name
776
808
}
777
809
810
+ def printAnnotation (annot : Term ): Buffer = {
811
+ val Annotation (ref, args) = annot
812
+ this += " @"
813
+ printTypeTree(ref)
814
+ this += " ("
815
+ printTrees(args, " , " )
816
+ this += " )"
817
+ }
818
+
819
+ def printDefAnnotations (definition : Definition ): Buffer = {
820
+ val annots = definition.annots.filter {
821
+ case Annotation (annot, _) =>
822
+ annot.tpe match {
823
+ case Type .TypeRef (_, Type .SymRef (PackageDef (" internal" , _), Type .ThisType (Type .SymRef (PackageDef (" annotation" , _), NoPrefix ())))) => false
824
+ case _ => true
825
+ }
826
+ }
827
+ printAnnotations(annots)
828
+ if (annots.nonEmpty) this += " "
829
+ else this
830
+ }
831
+
778
832
def += (x : Boolean ): this .type = { sb.append(x); this }
779
833
def += (x : Byte ): this .type = { sb.append(x); this }
780
834
def += (x : Short ): this .type = { sb.append(x); this }
@@ -829,6 +883,13 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
829
883
}
830
884
}
831
885
886
+ private object Annotation {
887
+ def unapply (arg : Tree )(implicit ctx : Context ): Option [(TypeTree , List [Term ])] = arg match {
888
+ case Term .Apply (Term .Select (Term .New (annot), " <init>" , _), args) => Some ((annot, args))
889
+ case _ => None
890
+ }
891
+ }
892
+
832
893
// TODO Provide some of these in scala.tasty.Tasty.scala and implement them using checks on symbols for performance
833
894
private object Types {
834
895
@@ -866,6 +927,13 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
866
927
case _ => false
867
928
}
868
929
}
930
+
931
+ object EmptyPrefix {
932
+ def unapply (tpe : TypeOrBounds )(implicit ctx : Context ): Boolean = tpe match {
933
+ case NoPrefix () | Type .ThisType (Types .EmptyPackage () | Types .RootPackage ()) => true
934
+ case _ => false
935
+ }
936
+ }
869
937
}
870
938
871
939
0 commit comments