Skip to content

Commit d6b06a1

Browse files
Document and clean up returns in PlainPrinter/RefinedPrinter
1 parent 8035a85 commit d6b06a1

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import java.lang.Integer.toOctalString
1111
import config.Config.summarizeDepth
1212
import scala.annotation.switch
1313

14+
/**
15+
* Pretty printer with minimal formating. Similar to `.toString` but with proper indentation,
16+
* position and a few more details. Accessible under `-Yplain-printer`.
17+
*/
1418
class PlainPrinter(_ctx: Context) extends Printer {
1519
protected[this] implicit def ctx: Context = _ctx.addMode(Mode.Printing)
1620

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import config.Config
1616
import scala.annotation.switch
1717
import language.implicitConversions
1818

19+
/** Pretty printer used by default with `.show`, generate strings very close to scala source code. */
1920
class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
2021

2122
/** A stack of enclosing DefDef, TypeDef, or ClassDef, or ModuleDefs nodes */
@@ -64,12 +65,13 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
6465

6566
override def toTextRef(tp: SingletonType): Text = controlled {
6667
tp match {
67-
case tp: ThisType =>
68-
if (tp.cls.isAnonymousClass) return "this"
69-
if (tp.cls is ModuleClass) return fullNameString(tp.cls.sourceModule)
68+
case tp: ThisType if tp.cls.isAnonymousClass =>
69+
"this"
70+
case tp: ThisType if tp.cls is ModuleClass =>
71+
fullNameString(tp.cls.sourceModule)
7072
case _ =>
73+
super.toTextRef(tp)
7174
}
72-
super.toTextRef(tp)
7375
}
7476

7577
override def toTextPrefix(tp: Type): Text = controlled {
@@ -113,44 +115,46 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
113115
homogenize(tp) match {
114116
case AppliedType(tycon, args) =>
115117
val cls = tycon.typeSymbol
116-
if (tycon.isRepeatedParam) return toTextLocal(args.head) ~ "*"
117-
if (defn.isFunctionClass(cls)) return toTextFunction(args)
118-
if (defn.isTupleClass(cls)) return toTextTuple(args)
119-
return (toTextLocal(tycon) ~ "[" ~ Text(args map argText, ", ") ~ "]").close
118+
if (tycon.isRepeatedParam) toTextLocal(args.head) ~ "*"
119+
else if (defn.isFunctionClass(cls)) toTextFunction(args)
120+
else if (defn.isTupleClass(cls)) toTextTuple(args)
121+
else (toTextLocal(tycon) ~ "[" ~ Text(args map argText, ", ") ~ "]").close
120122
case tp: TypeRef =>
121123
val hideType = tp.symbol is AliasPreferred
122124
if (hideType && !ctx.phase.erasedTypes && !tp.symbol.isCompleting) {
123125
tp.info match {
124-
case TypeAlias(alias) => return toText(alias)
125-
case _ => if (tp.prefix.isInstanceOf[ThisType]) return nameString(tp.symbol)
126+
case TypeAlias(alias) => toText(alias)
127+
case _ if (tp.prefix.isInstanceOf[ThisType]) => nameString(tp.symbol)
128+
case _ => super.toText(tp)
126129
}
127-
}
128-
else if (tp.symbol.isAnonymousClass && !ctx.settings.uniqid.value)
129-
return toText(tp.info)
130+
} else if (tp.symbol.isAnonymousClass && !ctx.settings.uniqid.value)
131+
toText(tp.info)
132+
else
133+
super.toText(tp)
130134
case ExprType(result) =>
131-
return "=> " ~ toText(result)
135+
"=> " ~ toText(result)
132136
case ErasedValueType(tycon, underlying) =>
133-
return "ErasedValueType(" ~ toText(tycon) ~ ", " ~ toText(underlying) ~ ")"
137+
"ErasedValueType(" ~ toText(tycon) ~ ", " ~ toText(underlying) ~ ")"
134138
case tp: ClassInfo =>
135-
return toTextParents(tp.parentsWithArgs) ~ "{...}"
139+
toTextParents(tp.parentsWithArgs) ~ "{...}"
136140
case JavaArrayType(elemtp) =>
137-
return toText(elemtp) ~ "[]"
141+
toText(elemtp) ~ "[]"
138142
case tp: SelectionProto =>
139-
return "?{ " ~ toText(tp.name) ~ (" " provided !tp.name.decode.last.isLetterOrDigit) ~
143+
"?{ " ~ toText(tp.name) ~ (" " provided !tp.name.decode.last.isLetterOrDigit) ~
140144
": " ~ toText(tp.memberProto) ~ " }"
141145
case tp: ViewProto =>
142-
return toText(tp.argType) ~ " ?=>? " ~ toText(tp.resultType)
146+
toText(tp.argType) ~ " ?=>? " ~ toText(tp.resultType)
143147
case tp @ FunProto(args, resultType, _) =>
144148
val argsText = args match {
145149
case dummyTreeOfType(tp) :: Nil if !(tp isRef defn.NullClass) => "null: " ~ toText(tp)
146150
case _ => toTextGlobal(args, ", ")
147151
}
148-
return "FunProto(" ~ argsText ~ "):" ~ toText(resultType)
152+
"FunProto(" ~ argsText ~ "):" ~ toText(resultType)
149153
case tp: IgnoredProto =>
150-
return "?"
154+
"?"
151155
case _ =>
156+
super.toText(tp)
152157
}
153-
super.toText(tp)
154158
}
155159

156160
def blockText[T >: Untyped](trees: List[Tree[T]]): Text =

0 commit comments

Comments
 (0)