@@ -24,17 +24,27 @@ object Formatting {
24
24
object Shown :
25
25
given [A : Show ]: Conversion [A , Shown ] = Show [A ].show(_)
26
26
27
+ extension (s : Shown )
28
+ def runCtxShow (using Context ): Shown = s match
29
+ case cs : CtxShow => cs.run
30
+ case _ => s
31
+
32
+ def toStr (x : Shown )(using Context ): String = x match
33
+ case seq : Seq [? ] => seq.map(toStr).mkString(" [" , " , " , " ]" )
34
+ case res => res.tryToShow
35
+
36
+ import Shown .runCtxShow
37
+
27
38
sealed abstract class Show [- T ]:
28
39
/** Show a value T by returning a "shown" result. */
29
40
def show (x : T ): Shown
30
41
31
42
trait CtxShow :
32
43
def run (using Context ): Shown
33
44
34
- extension (s : Shown )
35
- def ctxShow (using Context ): Shown = s match
36
- case cs : CtxShow => cs.run
37
- case _ => s
45
+ private inline def CtxShow (inline x : Context ?=> Shown ) = new CtxShow { def run (using Context ) = x(using ctx) }
46
+ private def toStr [A : Show ](x : A )(using Context ): String = Shown .toStr(toShown(x))
47
+ private def toShown [A : Show ](x : A )(using Context ): Shown = Show [A ].show(x).runCtxShow
38
48
39
49
/** The base implementation, passing the argument to StringFormatter which will try to `.show` it. */
40
50
object ShowAny extends Show [Any ]:
@@ -55,26 +65,25 @@ object Formatting {
55
65
inline def apply [A ](using inline z : Show [A ]): Show [A ] = z
56
66
57
67
given [X : Show ]: Show [Option [X ]] with
58
- def show (x : Option [X ]) = new CtxShow :
59
- def run ( using Context ) = x.map(show1 )
68
+ def show (x : Option [X ]) =
69
+ CtxShow ( x.map(toStr) )
60
70
end given
61
71
62
72
given [X : Show ]: Show [Seq [X ]] with
63
- def show (x : Seq [X ]) = new CtxShow :
64
- def run (using Context ) = x.map(show1)
73
+ def show (x : Seq [X ]) = CtxShow (x.map(toStr))
65
74
66
75
given [K : Show , V : Show ]: Show [Map [K , V ]] with
67
- def show (x : Map [K , V ]) = new CtxShow :
68
- def run (using Context ) =
69
- x.map((k, v) => s " ${show1(k)} => ${show1(v)}" )
76
+ def show (x : Map [K , V ]) =
77
+ CtxShow (x.map((k, v) => s " ${toStr(k)} => ${toStr(v)}" ))
70
78
end given
71
79
72
80
given [H : Show , T <: Tuple : Show ]: Show [H *: T ] with
73
- def show (x : H *: T ) = new CtxShow :
74
- def run (using Context ) = show1(x.head) *: Show [T ].show(x.tail).ctxShow.asInstanceOf [Tuple ]
81
+ def show (x : H *: T ) =
82
+ CtxShow (toStr(x.head) *: toShown(x.tail).asInstanceOf [Tuple ])
83
+ end given
75
84
76
85
given [X : Show ]: Show [X | Null ] with
77
- def show (x : X | Null ) = if x == null then " null" else Show [ X ].show( x.nn)
86
+ def show (x : X | Null ) = if x == null then " null" else CtxShow (toStr( x.nn) )
78
87
79
88
given Show [FlagSet ] with
80
89
def show (x : FlagSet ) = x.flagsString
@@ -90,7 +99,13 @@ object Formatting {
90
99
case ast.TreeInfo .Impure => " PurityLevel.Impure"
91
100
case ast.TreeInfo .PurePath => " PurityLevel.PurePath"
92
101
case ast.TreeInfo .IdempotentPath => " PurityLevel.IdempotentPath"
93
- case _ => s " PurityLevel( ${x.x}) "
102
+ case _ => s " PurityLevel( ${x.x.toBinaryString}) "
103
+
104
+ given Show [Atoms ] with
105
+ def show (x : Atoms ) = x match
106
+ case Atoms .Unknown => " Unknown"
107
+ case Atoms .Range (lo, hi) => CtxShow (s " Range( ${toStr(lo.toList)}, ${toStr(hi.toList)}) " )
108
+ end given
94
109
95
110
given Show [Showable ] = ShowAny
96
111
given Show [Shown ] = ShowAny
@@ -112,11 +127,6 @@ object Formatting {
112
127
given Show [util.Spans .Span ] = ShowAny
113
128
given Show [tasty.TreeUnpickler # OwnerTree ] = ShowAny
114
129
given Show [typer.ForceDegree .Value ] = ShowAny
115
-
116
- private def show1 [A : Show ](x : A )(using Context ) = show2(Show [A ].show(x).ctxShow)
117
- private def show2 (x : Shown )(using Context ): String = x match
118
- case seq : Seq [? ] => seq.map(show2).mkString(" [" , " , " , " ]" )
119
- case res => res.tryToShow
120
130
end Show
121
131
end ShownDef
122
132
export ShownDef .{ Show , Shown }
@@ -133,7 +143,7 @@ object Formatting {
133
143
class StringFormatter (protected val sc : StringContext ) {
134
144
protected def showArg (arg : Any )(using Context ): String = arg.tryToShow
135
145
136
- private def treatArg (arg : Shown , suffix : String )(using Context ): (String , String ) = arg.ctxShow match {
146
+ private def treatArg (arg : Shown , suffix : String )(using Context ): (String , String ) = arg.runCtxShow match {
137
147
case arg : Seq [? ] if suffix.indexOf('%' ) == 0 && suffix.indexOf('%' , 1 ) != - 1 =>
138
148
val end = suffix.indexOf('%' , 1 )
139
149
val sep = StringContext .processEscapes(suffix.substring(1 , end))
0 commit comments