@@ -14,18 +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
- printer.toText(Literal (Constant (expr.value))).mkString(Int .MaxValue , false )
27
- case _ => new QuoteDriver ().show(expr)
28
- }
24
+ def show (expr : Expr [T ]): String = Runners .show(expr, Settings .show())
29
25
30
26
def toConstantOpt (expr : Expr [T ]): Option [T ] = {
31
27
def toConstantOpt (tree : Tree ): Option [T ] = tree match {
@@ -36,21 +32,59 @@ object Runners {
36
32
}
37
33
expr match {
38
34
case expr : ConstantExpr [T ] => Some (expr.value)
39
- case _ => new QuoteDriver ().withTree(expr, (tree, _) => toConstantOpt(tree))
35
+ case _ => new QuoteDriver ().withTree(expr, (tree, _) => toConstantOpt(tree), Settings .run() )
40
36
}
41
37
}
42
38
43
39
}
44
40
45
- def run [T ](expr : Expr [T ], settings : RunSettings ): T = expr match {
41
+ def run [T ](expr : Expr [T ], settings : Settings [ Run ] ): T = expr match {
46
42
case expr : ConstantExpr [T ] => expr.value
47
43
case _ => new QuoteDriver ().run(expr, settings)
48
44
}
49
45
50
- case class RunSettings (
51
- /** Enable optimisation when compiling the quoted code */
52
- optimise : Boolean = false ,
53
- /** Output directory for the copiled quote. If set to None the output will be in memory */
54
- outDir : Option [String ] = None
55
- )
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
+ printer.toText(Literal (Constant (expr.value))).mkString(Int .MaxValue , false )
53
+ case _ => new QuoteDriver ().show(expr, settings)
54
+ }
55
+
56
+ class Settings [T ] private (val compilerArgs : List [String ])
57
+
58
+ object Settings {
59
+
60
+ /** Quote run settings
61
+ * @param optimise Enable optimisation when compiling the quoted code
62
+ * @param outDir Output directory for the compiled quote. If set to None the output will be in memory
63
+ * @param compilerArgs Compiler arguments. Use only if you know what you are doing.
64
+ */
65
+ def run (
66
+ optimise : Boolean = false ,
67
+ outDir : Option [String ] = None ,
68
+ compilerArgs : List [String ] = Nil
69
+ ): Settings [Run ] = {
70
+ var compilerArgs1 = compilerArgs
71
+ if (optimise) compilerArgs1 = " -optimise" :: compilerArgs1
72
+ if (outDir.nonEmpty) compilerArgs1 = " -d" :: outDir.get :: compilerArgs1
73
+ new Settings (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 (compilerArgs1)
86
+ }
87
+
88
+ }
89
+
56
90
}
0 commit comments