@@ -14,19 +14,14 @@ import scala.runtime.quoted._
14
14
object Runners {
15
15
import tpd ._
16
16
17
+ type Run
18
+ type Show
19
+
17
20
implicit def runner [T ]: Runner [T ] = new Runner [T ] {
18
21
19
- def run (expr : Expr [T ]): T = Runners .run(expr, RunSettings ())
22
+ def run (expr : Expr [T ]): T = Runners .run(expr, Settings .run ())
20
23
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())
30
25
31
26
def toConstantOpt (expr : Expr [T ]): Option [T ] = {
32
27
def toConstantOpt (tree : Tree ): Option [T ] = tree match {
@@ -37,21 +32,59 @@ object Runners {
37
32
}
38
33
expr match {
39
34
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() )
41
36
}
42
37
}
43
38
44
39
}
45
40
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 {
47
42
case expr : ConstantExpr [T ] => expr.value
48
43
case _ => new QuoteDriver ().run(expr, settings)
49
44
}
50
45
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
+
57
90
}
0 commit comments