@@ -14,19 +14,14 @@ import scala.runtime.quoted._
1414object Runners {
1515 import tpd ._
1616
17+ type Run
18+ type Show
19+
1720 implicit def runner [T ]: Runner [T ] = new Runner [T ] {
1821
19- def run (expr : Expr [T ]): T = Runners .run(expr, RunSettings ())
22+ def run (expr : Expr [T ]): T = Runners .run(expr, Settings .run ())
2023
21- def show (expr : Expr [T ]): String = expr match {
22- case expr : ConstantExpr [T ] =>
23- implicit val ctx = new QuoteDriver ().initCtx
24- ctx.settings.color.update(" never" )
25- val printer = new RefinedPrinter (ctx)
26- if (expr.value == BoxedUnit .UNIT ) " ()"
27- else printer.toText(Literal (Constant (expr.value))).mkString(Int .MaxValue , false )
28- case _ => new QuoteDriver ().show(expr)
29- }
24+ def show (expr : Expr [T ]): String = Runners .show(expr, Settings .show())
3025
3126 def toConstantOpt (expr : Expr [T ]): Option [T ] = {
3227 def toConstantOpt (tree : Tree ): Option [T ] = tree match {
@@ -37,21 +32,59 @@ object Runners {
3732 }
3833 expr match {
3934 case expr : ConstantExpr [T ] => Some (expr.value)
40- case _ => new QuoteDriver ().withTree(expr, (tree, _) => toConstantOpt(tree))
35+ case _ => new QuoteDriver ().withTree(expr, (tree, _) => toConstantOpt(tree), Settings .run() )
4136 }
4237 }
4338
4439 }
4540
46- def run [T ](expr : Expr [T ], settings : RunSettings ): T = expr match {
41+ def run [T ](expr : Expr [T ], settings : Settings [ Run ] ): T = expr match {
4742 case expr : ConstantExpr [T ] => expr.value
4843 case _ => new QuoteDriver ().run(expr, settings)
4944 }
5045
51- case class RunSettings (
52- /** Enable optimisation when compiling the quoted code */
53- optimise : Boolean = false ,
54- /** Output directory for the copiled quote. If set to None the output will be in memory */
55- outDir : Option [String ] = None
56- )
46+ def show [T ](expr : Expr [T ], settings : Settings [Show ]): String = expr match {
47+ case expr : ConstantExpr [T ] =>
48+ implicit val ctx = new QuoteDriver ().initCtx
49+ if (settings.compilerArgs.contains(" -color:never" ))
50+ ctx.settings.color.update(" never" )
51+ val printer = new RefinedPrinter (ctx)
52+ if (expr.value == BoxedUnit .UNIT ) " ()"
53+ else printer.toText(Literal (Constant (expr.value))).mkString(Int .MaxValue , false )
54+ case _ => new QuoteDriver ().show(expr, settings)
55+ }
56+
57+ class Settings [T ] private (val outDir : Option [String ], val compilerArgs : List [String ])
58+
59+ object Settings {
60+
61+ /** Quote run settings
62+ * @param optimise Enable optimisation when compiling the quoted code
63+ * @param outDir Output directory for the compiled quote. If set to None the output will be in memory
64+ * @param compilerArgs Compiler arguments. Use only if you know what you are doing.
65+ */
66+ def run (
67+ optimise : Boolean = false ,
68+ outDir : Option [String ] = None ,
69+ compilerArgs : List [String ] = Nil
70+ ): Settings [Run ] = {
71+ var compilerArgs1 = compilerArgs
72+ if (optimise) compilerArgs1 = " -optimise" :: compilerArgs1
73+ new Settings (outDir, compilerArgs1)
74+ }
75+
76+ /** Quote show settings
77+ * @param compilerArgs Compiler arguments. Use only if you know what you are doing.
78+ */
79+ def show (
80+ color : Boolean = false ,
81+ compilerArgs : List [String ] = Nil
82+ ): Settings [Show ] = {
83+ var compilerArgs1 = compilerArgs
84+ compilerArgs1 = s " -color: ${if (color) " always" else " never" }" :: compilerArgs1
85+ new Settings (None , compilerArgs1)
86+ }
87+
88+ }
89+
5790}
0 commit comments