Skip to content

Commit f77825b

Browse files
committed
Create ReplPrinter
1 parent b29e4d4 commit f77825b

File tree

5 files changed

+46
-42
lines changed

5 files changed

+46
-42
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ class ScalaSettings extends Settings.SettingGroup {
101101
val YplainPrinter = BooleanSetting("-Yplain-printer", "Pretty-print using a plain printer.")
102102
val YprintSyms = BooleanSetting("-Yprint-syms", "when printing trees print info in symbols instead of corresponding info in trees.")
103103
val YprintDebug = BooleanSetting("-Yprint-debug", "when printing trees, print some extra information useful for debugging.")
104-
val YprintRepl = BooleanSetting("-Yprint-repl", "Print the program removing REPL encodings.")
105104
val YshowPrintErrors = BooleanSetting("-Yshow-print-errors", "don't suppress exceptions thrown during tree printing.")
106105
val YtestPickler = BooleanSetting("-Ytest-pickler", "self-test for pickling functionality; should be used with -Ystop-after:pickler")
107106
val YcheckReentrant = BooleanSetting("-Ycheck-reentrant", "check that compiled program does not contain vars that can be accessed from a global root.")

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

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
4040
private[this] var myCtx: Context = _ctx
4141
private[this] var printPos = ctx.settings.YprintPos.value
4242
private[this] val printLines = ctx.settings.printLines.value
43-
private[this] val YprintRepl = ctx.settings.YprintRepl.value
4443

4544
override protected[this] implicit def ctx: Context = myCtx
4645

@@ -193,8 +192,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
193192
return nameString(tp.symbol)
194193
case _ =>
195194
}
196-
case ExprType(result) =>
197-
return (if (YprintRepl) ": " else "=> ") ~ toText(result)
195+
case tp: ExprType =>
196+
return exprToText(tp)
198197
case ErasedValueType(tycon, underlying) =>
199198
return "ErasedValueType(" ~ toText(tycon) ~ ", " ~ toText(underlying) ~ ")"
200199
case tp: ClassInfo =>
@@ -226,6 +225,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
226225
super.toText(tp)
227226
}
228227

228+
protected def exprToText(tp: ExprType): Text =
229+
"=> " ~ toText(tp.resType)
230+
229231
protected def blockText[T >: Untyped](trees: List[Tree[T]]): Text =
230232
("{" ~ toText(trees, "\n") ~ "}").close
231233

@@ -718,33 +720,12 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
718720
case info: ImportType => return s"import $info.expr.show"
719721
case _ =>
720722
}
721-
if (YprintRepl) {
722-
if (sym.name.isReplAssignName) nameString(sym.name)
723-
else keyString(sym) ~~ nameString(sym.name.stripModuleClassSuffix)
724-
}
725-
else if (sym.is(ModuleClass))
723+
if (sym.is(ModuleClass))
726724
kindString(sym) ~~ (nameString(sym.name.stripModuleClassSuffix) + idString(sym))
727725
else
728726
super.toText(sym)
729727
}
730728

731-
override def toText(const: Constant): Text =
732-
if (!YprintRepl) super.toText(const)
733-
else if (const.tag == StringTag) Str('"' + const.value.toString + '"')
734-
else Str(const.value.toString)
735-
736-
override def dclText(sym: Symbol): Text = {
737-
if (!YprintRepl) super.dclText(sym)
738-
else {
739-
toText(sym) ~ {
740-
if (sym.is(Method)) toText(sym.info)
741-
else if (sym.isType && sym.info.isInstanceOf[TypeAlias]) toText(sym.info)
742-
else if (sym.isType || sym.isClass) ""
743-
else ":" ~~ toText(sym.info)
744-
}
745-
}
746-
}
747-
748729
override def kindString(sym: Symbol) = {
749730
val flags = sym.flagsUNSAFE
750731
if (flags is Package) "package"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dotty.tools.dotc.printing
2+
3+
import dotty.tools.dotc.core.Constants
4+
import dotty.tools.dotc.core.Constants.Constant
5+
import dotty.tools.dotc.core.Contexts._
6+
import dotty.tools.dotc.core.Flags._
7+
import dotty.tools.dotc.core.NameOps._
8+
import dotty.tools.dotc.core.Symbols._
9+
import dotty.tools.dotc.core.Types.{ExprType, TypeAlias}
10+
import dotty.tools.dotc.printing.Texts._
11+
12+
import scala.language.implicitConversions
13+
14+
class ReplPrinter(_ctx: Context) extends DecompilerPrinter(_ctx) {
15+
16+
override protected def exprToText(tp: ExprType): Text =
17+
": " ~ toText(tp.resType)
18+
19+
override def toText(sym: Symbol): Text =
20+
if (sym.name.isReplAssignName) nameString(sym.name)
21+
else keyString(sym) ~~ nameString(sym.name.stripModuleClassSuffix)
22+
23+
override def toText(const: Constant): Text =
24+
if (const.tag == Constants.StringTag) Str('"' + const.value.toString + '"')
25+
else Str(const.value.toString)
26+
27+
override def dclText(sym: Symbol): Text = {
28+
toText(sym) ~ {
29+
if (sym.is(Method)) toText(sym.info)
30+
else if (sym.isType && sym.info.isInstanceOf[TypeAlias]) toText(sym.info)
31+
else if (sym.isType || sym.isClass) ""
32+
else ":" ~~ toText(sym.info)
33+
}
34+
}
35+
}

compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ package typer
44

55
import ast._
66
import core._
7-
import Trees._
87
import Types._, ProtoTypes._, Contexts._, Decorators._, Denotations._, Symbols._
9-
import Applications._, Implicits._, Flags._
8+
import Implicits._, Flags._
109
import util.Positions._
11-
import printing.{Showable, RefinedPrinter}
12-
import scala.collection.mutable
1310
import java.util.regex.Matcher.quoteReplacement
1411
import reporting.diagnostic.Message
1512
import reporting.diagnostic.messages._

compiler/src/dotty/tools/repl/package.scala

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@ package dotty.tools
22

33
import dotc.core.Contexts.Context
44
import dotc.core.Symbols.Symbol
5-
import dotc.core.Denotations.Denotation
65
import dotc.reporting.diagnostic.MessageContainer
7-
import dotc.printing.RefinedPrinter
8-
9-
import dotc.reporting.{
10-
StoreReporter,
11-
UniqueMessagePositions,
12-
HideNonSensicalMessages
13-
}
6+
import dotc.printing.ReplPrinter
7+
import dotc.reporting.{HideNonSensicalMessages, StoreReporter, UniqueMessagePositions}
148

159
package object repl {
1610
/** Create empty outer store reporter */
@@ -20,11 +14,9 @@ package object repl {
2014

2115
private[repl] implicit class ShowUser(val s: Symbol) extends AnyVal {
2216
def showUser(implicit ctx: Context): String = {
23-
val ctx2 = ctx.fresh
24-
ctx2.setSetting(ctx2.settings.YprintRepl, true)
25-
val printer = new RefinedPrinter(ctx2)
17+
val printer = new ReplPrinter(ctx)
2618
val text = printer.dclText(s)
27-
text.mkString(ctx2.settings.pageWidth.value, ctx2.settings.printLines.value)
19+
text.mkString(ctx.settings.pageWidth.value, ctx.settings.printLines.value)
2820
}
2921
}
3022

0 commit comments

Comments
 (0)