diff --git a/src/dotty/tools/dotc/printing/PlainPrinter.scala b/src/dotty/tools/dotc/printing/PlainPrinter.scala index a92095d9bd15..3701e33e9ffd 100644 --- a/src/dotty/tools/dotc/printing/PlainPrinter.scala +++ b/src/dotty/tools/dotc/printing/PlainPrinter.scala @@ -11,6 +11,10 @@ import java.lang.Integer.toOctalString import config.Config.summarizeDepth import scala.annotation.switch +/** + * Pretty printer with minimal formating. Similar to `.toString` but with proper indentation, + * position and a few more details. Accessible under `-Yplain-printer`. + */ class PlainPrinter(_ctx: Context) extends Printer { protected[this] implicit def ctx: Context = _ctx.addMode(Mode.Printing) diff --git a/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/src/dotty/tools/dotc/printing/RefinedPrinter.scala index 4f3a8d272597..5ec823276b12 100644 --- a/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -16,6 +16,7 @@ import config.Config import scala.annotation.switch import language.implicitConversions +/** Pretty printer used by default with `.show`, generate strings very close to scala source code. */ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { /** A stack of enclosing DefDef, TypeDef, or ClassDef, or ModuleDefs nodes */ @@ -64,12 +65,13 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { override def toTextRef(tp: SingletonType): Text = controlled { tp match { - case tp: ThisType => - if (tp.cls.isAnonymousClass) return "this" - if (tp.cls is ModuleClass) return fullNameString(tp.cls.sourceModule) + case tp: ThisType if tp.cls.isAnonymousClass => + "this" + case tp: ThisType if tp.cls is ModuleClass => + fullNameString(tp.cls.sourceModule) case _ => + super.toTextRef(tp) } - super.toTextRef(tp) } override def toTextPrefix(tp: Type): Text = controlled { @@ -113,44 +115,46 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { homogenize(tp) match { case AppliedType(tycon, args) => val cls = tycon.typeSymbol - if (tycon.isRepeatedParam) return toTextLocal(args.head) ~ "*" - if (defn.isFunctionClass(cls)) return toTextFunction(args) - if (defn.isTupleClass(cls)) return toTextTuple(args) - return (toTextLocal(tycon) ~ "[" ~ Text(args map argText, ", ") ~ "]").close + if (tycon.isRepeatedParam) toTextLocal(args.head) ~ "*" + else if (defn.isFunctionClass(cls)) toTextFunction(args) + else if (defn.isTupleClass(cls)) toTextTuple(args) + else (toTextLocal(tycon) ~ "[" ~ Text(args map argText, ", ") ~ "]").close case tp: TypeRef => val hideType = tp.symbol is AliasPreferred if (hideType && !ctx.phase.erasedTypes && !tp.symbol.isCompleting) { tp.info match { - case TypeAlias(alias) => return toText(alias) - case _ => if (tp.prefix.isInstanceOf[ThisType]) return nameString(tp.symbol) + case TypeAlias(alias) => toText(alias) + case _ if (tp.prefix.isInstanceOf[ThisType]) => nameString(tp.symbol) + case _ => super.toText(tp) } - } - else if (tp.symbol.isAnonymousClass && !ctx.settings.uniqid.value) - return toText(tp.info) + } else if (tp.symbol.isAnonymousClass && !ctx.settings.uniqid.value) + toText(tp.info) + else + super.toText(tp) case ExprType(result) => - return "=> " ~ toText(result) + "=> " ~ toText(result) case ErasedValueType(tycon, underlying) => - return "ErasedValueType(" ~ toText(tycon) ~ ", " ~ toText(underlying) ~ ")" + "ErasedValueType(" ~ toText(tycon) ~ ", " ~ toText(underlying) ~ ")" case tp: ClassInfo => - return toTextParents(tp.parentsWithArgs) ~ "{...}" + toTextParents(tp.parentsWithArgs) ~ "{...}" case JavaArrayType(elemtp) => - return toText(elemtp) ~ "[]" + toText(elemtp) ~ "[]" case tp: SelectionProto => - return "?{ " ~ toText(tp.name) ~ (" " provided !tp.name.decode.last.isLetterOrDigit) ~ + "?{ " ~ toText(tp.name) ~ (" " provided !tp.name.decode.last.isLetterOrDigit) ~ ": " ~ toText(tp.memberProto) ~ " }" case tp: ViewProto => - return toText(tp.argType) ~ " ?=>? " ~ toText(tp.resultType) + toText(tp.argType) ~ " ?=>? " ~ toText(tp.resultType) case tp @ FunProto(args, resultType, _) => val argsText = args match { case dummyTreeOfType(tp) :: Nil if !(tp isRef defn.NullClass) => "null: " ~ toText(tp) case _ => toTextGlobal(args, ", ") } - return "FunProto(" ~ argsText ~ "):" ~ toText(resultType) + "FunProto(" ~ argsText ~ "):" ~ toText(resultType) case tp: IgnoredProto => - return "?" + "?" case _ => + super.toText(tp) } - super.toText(tp) } def blockText[T >: Untyped](trees: List[Tree[T]]): Text = @@ -527,7 +531,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { else if (!tree.isDef) txt = ("<" ~ txt ~ ":" ~ toText(tp) ~ ">").close } if (ctx.settings.Yprintpos.value && !tree.isInstanceOf[WithoutTypeOrPos[_]]) - txt = txt ~ "@" ~ tree.pos.toString + txt = txt ~ "@" ~ tree.pos.toString(simplified = true) tree match { case Block(_, _) | Template(_, _, _, _) => txt case _ => txt.close diff --git a/src/dotty/tools/dotc/util/Positions.scala b/src/dotty/tools/dotc/util/Positions.scala index c3890cc9a463..c718db02568a 100644 --- a/src/dotty/tools/dotc/util/Positions.scala +++ b/src/dotty/tools/dotc/util/Positions.scala @@ -108,10 +108,12 @@ object Positions { /** A synthetic copy of this position */ def toSynthetic = if (isSynthetic) this else Position(start, end) - override def toString = { - val (left, right) = if (isSynthetic) ("<", ">") else ("[", "]") + override def toString: String = toString(simplified = false) + + def toString(simplified: Boolean): String = { + val (left, right) = if (simplified || isSynthetic) ("<", ">") else ("[", "]") if (exists) - s"$left$start..${if (point == start) "" else s"$point.."}$end$right" + s"$left$start..${if (simplified || point == start) "" else s"$point.."}$end$right" else s"${left}no position${right}" } diff --git a/test/dotc/tests.scala b/test/dotc/tests.scala index f161fefe3b4b..4975003e64c8 100644 --- a/test/dotc/tests.scala +++ b/test/dotc/tests.scala @@ -28,7 +28,7 @@ class tests extends CompilerTest { else List("-Ycheck:tailrec,resolveSuper,mixin,restoreScopes,labelDef") } - val testPickling = List("-Xprint-types", "-Ytest-pickler", "-Ystop-after:pickler") + val testPickling = List("-Xprint-types", "-Yprintpos", "-Ytest-pickler", "-Ystop-after:pickler") val twice = List("#runs", "2") val staleSymbolError: List[String] = List()