Skip to content

Commit 88028d4

Browse files
Merge pull request #4041 from dotty-staging/move-settings-to-toolbox
Move settings to toolbox
2 parents ddf2799 + 40bd5da commit 88028d4

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

compiler/src/dotty/tools/dotc/quoted/Toolbox.scala

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,26 @@ object Toolbox {
1717
type Run
1818
type Show
1919

20-
implicit def toolbox[T]: Toolbox[T] = new Toolbox[T] {
21-
22-
def run(expr: Expr[T]): T = Toolbox.run(expr, Settings.run())
20+
implicit def toolbox[T](implicit
21+
runSettings: Settings[Run] = Settings.run(),
22+
showSettings: Settings[Show] = Settings.show()
23+
): Toolbox[T] = new Toolbox[T] {
24+
25+
def run(expr: Expr[T]): T = expr match {
26+
case expr: ValueExpr[T] => expr.value
27+
case _ => new QuoteDriver().run(expr, runSettings)
28+
}
2329

24-
def show(expr: Expr[T]): String = Toolbox.show(expr, Settings.show())
30+
def show(expr: Expr[T]): String = expr match {
31+
case expr: ValueExpr[T] =>
32+
implicit val ctx = new QuoteDriver().initCtx
33+
if (showSettings.compilerArgs.contains("-color:never"))
34+
ctx.settings.color.update("never")
35+
val printer = new RefinedPrinter(ctx)
36+
if (expr.value == BoxedUnit.UNIT) "()"
37+
else printer.toText(Literal(Constant(expr.value))).mkString(Int.MaxValue, false)
38+
case _ => new QuoteDriver().show(expr, showSettings)
39+
}
2540

2641
def toConstantOpt(expr: Expr[T]): Option[T] = {
2742
def toConstantOpt(tree: Tree): Option[T] = tree match {
@@ -38,22 +53,6 @@ object Toolbox {
3853

3954
}
4055

41-
def run[T](expr: Expr[T], settings: Settings[Run]): T = expr match {
42-
case expr: ValueExpr[T] => expr.value
43-
case _ => new QuoteDriver().run(expr, settings)
44-
}
45-
46-
def show[T](expr: Expr[T], settings: Settings[Show]): String = expr match {
47-
case expr: ValueExpr[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-
5756
class Settings[T] private (val outDir: Option[String], val compilerArgs: List[String])
5857

5958
object Settings {

tests/run-with-compiler/quote-run-with-settings.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ object Test {
2121

2222
Files.deleteIfExists(classFile)
2323

24-
val settings = Settings.run(optimise = true, outDir = Some(outDir.toString))
25-
26-
println(run(expr, settings))
27-
assert(Files.exists(classFile))
24+
{
25+
implicit val settings = Settings.run(optimise = true, outDir = Some(outDir.toString))
26+
println(expr.run)
27+
assert(Files.exists(classFile))
28+
}
2829
}
2930
}

0 commit comments

Comments
 (0)