Skip to content

Move settings to toolbox #4041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions compiler/src/dotty/tools/dotc/quoted/Toolbox.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,26 @@ object Toolbox {
type Run
type Show

implicit def toolbox[T]: Toolbox[T] = new Toolbox[T] {

def run(expr: Expr[T]): T = Toolbox.run(expr, Settings.run())
implicit def toolbox[T](implicit
runSettings: Settings[Run] = Settings.run(),
showSettings: Settings[Show] = Settings.show()
): Toolbox[T] = new Toolbox[T] {

def run(expr: Expr[T]): T = expr match {
case expr: ValueExpr[T] => expr.value
case _ => new QuoteDriver().run(expr, runSettings)
}

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

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

}

def run[T](expr: Expr[T], settings: Settings[Run]): T = expr match {
case expr: ValueExpr[T] => expr.value
case _ => new QuoteDriver().run(expr, settings)
}

def show[T](expr: Expr[T], settings: Settings[Show]): String = expr match {
case expr: ValueExpr[T] =>
implicit val ctx = new QuoteDriver().initCtx
if (settings.compilerArgs.contains("-color:never"))
ctx.settings.color.update("never")
val printer = new RefinedPrinter(ctx)
if (expr.value == BoxedUnit.UNIT) "()"
else printer.toText(Literal(Constant(expr.value))).mkString(Int.MaxValue, false)
case _ => new QuoteDriver().show(expr, settings)
}

class Settings[T] private (val outDir: Option[String], val compilerArgs: List[String])

object Settings {
Expand Down
9 changes: 5 additions & 4 deletions tests/run-with-compiler/quote-run-with-settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ object Test {

Files.deleteIfExists(classFile)

val settings = Settings.run(optimise = true, outDir = Some(outDir.toString))

println(run(expr, settings))
assert(Files.exists(classFile))
{
implicit val settings = Settings.run(optimise = true, outDir = Some(outDir.toString))
println(expr.run)
assert(Files.exists(classFile))
}
}
}