Skip to content

Commit f770cd7

Browse files
committed
Add -XprintUser and pretty print lambdas with it
Enable flag by default for the decompiler
1 parent 8ae50fe commit f770cd7

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class ScalaSettings extends Settings.SettingGroup {
6060
val XprintTypes = BooleanSetting("-Xprint-types", "Print tree types (debugging option).")
6161
val XprintDiff = BooleanSetting("-Xprint-diff", "Print changed parts of the tree since last print.")
6262
val XprintDiffDel = BooleanSetting("-Xprint-diff-del", "Print chaged parts of the tree since last print including deleted parts.")
63+
val XprintUser = BooleanSetting("-Xprint-user", "Print the program using user friendly abstractions.")
6364
val Xprompt = BooleanSetting("-Xprompt", "Display a prompt after each error (debugging option).")
6465
val XmainClass = StringSetting("-Xmain-class", "path", "Class for manifest's Main-Class entry (only useful with -d <jar>)", "")
6566
val XnoValueClasses = BooleanSetting("-Xno-value-classes", "Do not use value classes. Helps debugging.")

compiler/src/dotty/tools/dotc/decompiler/Main.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ object Main extends dotc.Driver {
1616
override def setup(args0: Array[String], rootCtx: Context): (List[String], Context) = {
1717
var args = args0.filter(a => a != "-decompile")
1818
args = if (args.contains("-from-tasty")) args else "-from-tasty" +: args
19-
super.setup(args, rootCtx)
19+
super.setup("-Xprint-user" +: args, rootCtx)
2020
}
2121
}

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
421421
keywordStr("if ") ~ toText(cond) ~ (keywordText(" then") provided !cond.isInstanceOf[Parens]) ~~ toText(thenp) ~ optText(elsep)(keywordStr(" else ") ~ _)
422422
}
423423
case Closure(env, ref, target) =>
424-
"closure(" ~ (toTextGlobal(env, ", ") ~ " | " provided env.nonEmpty) ~
425-
toTextGlobal(ref) ~ (":" ~ toText(target) provided !target.isEmpty) ~ ")"
424+
("closure(" ~ (toTextGlobal(env, ", ") ~ " | " provided env.nonEmpty) ~
425+
toTextGlobal(ref) ~ (":" ~ toText(target) provided !target.isEmpty) ~ ")").provided(!ctx.settings.XprintUser.value)
426426
case Match(sel, cases) =>
427427
if (sel.isEmpty) blockText(cases)
428428
else changePrec(GlobalPrec) { toText(sel) ~ keywordStr(" match ") ~ blockText(cases) }
@@ -486,10 +486,11 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
486486
}
487487
case tree @ DefDef(name, tparams, vparamss, tpt, _) =>
488488
dclTextOr {
489-
val prefix = modText(tree.mods, keywordStr("def")) ~~ valDefText(nameIdText(tree))
489+
val printLambda = tree.symbol.isAnonymousFunction && ctx.settings.XprintUser.value
490+
val prefix = modText(tree.mods, keywordStr("def")) ~~ valDefText(nameIdText(tree)) provided (!printLambda)
490491
withEnclosingDef(tree) {
491-
addVparamssText(prefix ~ tparamsText(tparams), vparamss) ~ optAscription(tpt) ~
492-
optText(tree.rhs)(" = " ~ _)
492+
addVparamssText(prefix ~ tparamsText(tparams), vparamss) ~ optAscription(tpt).provided(!printLambda) ~
493+
optText(tree.rhs)((if (printLambda) " => " else " = ") ~ _)
493494
}
494495
}
495496
case tree @ TypeDef(name, rhs) =>

0 commit comments

Comments
 (0)