@@ -24,17 +24,23 @@ 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
+
27
36
sealed abstract class Show [- T ]:
28
37
/** Show a value T by returning a "shown" result. */
29
38
def show (x : T ): Shown
30
39
31
40
trait CtxShow :
32
41
def run (using Context ): Shown
33
42
34
- extension (s : Shown )
35
- def ctxShow (using Context ): Shown = s match
36
- case cs : CtxShow => cs.run
37
- case _ => s
43
+ import Shown .runCtxShow
38
44
39
45
/** The base implementation, passing the argument to StringFormatter which will try to `.show` it. */
40
46
object ShowAny extends Show [Any ]:
@@ -55,15 +61,21 @@ object Formatting {
55
61
inline def apply [A ](using inline z : Show [A ]): Show [A ] = z
56
62
57
63
given [X : Show ]: Show [Seq [X ]] with
58
- def show (x : Seq [X ]) = new CtxShow :
59
- def run (using Context ) = x.map(show1)
64
+ def show (x : Seq [X ]) = CtxShow (x.map(toStr))
60
65
61
66
given [H : Show , T <: Tuple : Show ]: Show [H *: T ] with
62
- def show (x : H *: T ) = new CtxShow :
63
- def run (using Context ) = show1(x.head) *: Show [T ].show(x.tail).ctxShow.asInstanceOf [Tuple ]
67
+ def show (x : H *: T ) = CtxShow (toStr(x.head) *: toShown(x.tail).asInstanceOf [Tuple ])
64
68
65
69
given [X : Show ]: Show [X | Null ] with
66
- def show (x : X | Null ) = if x == null then " null" else Show [X ].show(x.nn)
70
+ def show (x : X | Null ) = if x == null then " null" else CtxShow (toStr(x.nn))
71
+
72
+ given [X : Show ]: Show [Option [X ]] with
73
+ def show (x : Option [X ]) = x match
74
+ case Some (x) => CtxShow (" Some(" + toStr(x) + " )" )
75
+ case _ => " None"
76
+
77
+ given [K : Show , V : Show ]: Show [Map [K , V ]] with
78
+ def show (x : Map [K , V ]) = CtxShow (x.map((k, v) => toStr(k) + " => " + toStr(v)))
67
79
68
80
given Show [FlagSet ] with
69
81
def show (x : FlagSet ) = x.flagsString
@@ -79,7 +91,12 @@ object Formatting {
79
91
case ast.TreeInfo .Impure => " PurityLevel.Impure"
80
92
case ast.TreeInfo .PurePath => " PurityLevel.PurePath"
81
93
case ast.TreeInfo .IdempotentPath => " PurityLevel.IdempotentPath"
82
- case _ => s " PurityLevel( ${x.x}) "
94
+ case _ => s " PurityLevel( ${x.x.toBinaryString}) "
95
+
96
+ given Show [Atoms ] with
97
+ def show (x : Atoms ) = x match
98
+ case Atoms .Unknown => " Unknown"
99
+ case Atoms .Range (lo, hi) => CtxShow (" Range(" + toStr(lo.toList) + " , " + toStr(hi.toList) + " )" )
83
100
84
101
given Show [Showable ] = ShowAny
85
102
given Show [Shown ] = ShowAny
@@ -92,6 +109,7 @@ object Formatting {
92
109
given Show [Throwable ] = ShowAny
93
110
given Show [StringBuffer ] = ShowAny
94
111
given Show [CompilationUnit ] = ShowAny
112
+ given Show [Periods .Period ] = ShowAny
95
113
given Show [Phases .Phase ] = ShowAny
96
114
given Show [TyperState ] = ShowAny
97
115
given Show [config.ScalaVersion ] = ShowAny
@@ -102,10 +120,9 @@ object Formatting {
102
120
given Show [tasty.TreeUnpickler # OwnerTree ] = ShowAny
103
121
given Show [typer.ForceDegree .Value ] = ShowAny
104
122
105
- private def show1 [A : Show ](x : A )(using Context ) = show2(Show [A ].show(x).ctxShow)
106
- private def show2 (x : Shown )(using Context ): String = x match
107
- case seq : Seq [? ] => seq.map(show2).mkString(" [" , " , " , " ]" )
108
- case res => res.tryToShow
123
+ private inline def CtxShow (inline x : Context ?=> Shown ) = new CtxShow { def run (using Context ) = x(using ctx) }
124
+ private def toStr [A : Show ](x : A )(using Context ): String = Shown .toStr(toShown(x))
125
+ private def toShown [A : Show ](x : A )(using Context ): Shown = Show [A ].show(x).runCtxShow
109
126
end Show
110
127
end ShownDef
111
128
export ShownDef .{ Show , Shown }
@@ -122,7 +139,7 @@ object Formatting {
122
139
class StringFormatter (protected val sc : StringContext ) {
123
140
protected def showArg (arg : Any )(using Context ): String = arg.tryToShow
124
141
125
- private def treatArg (arg : Shown , suffix : String )(using Context ): (String , String ) = arg.ctxShow match {
142
+ private def treatArg (arg : Shown , suffix : String )(using Context ): (String , String ) = arg.runCtxShow match {
126
143
case arg : Seq [? ] if suffix.indexOf('%' ) == 0 && suffix.indexOf('%' , 1 ) != - 1 =>
127
144
val end = suffix.indexOf('%' , 1 )
128
145
val sep = StringContext .processEscapes(suffix.substring(1 , end))
0 commit comments