Skip to content

Commit 82dfb6c

Browse files
authored
Merge pull request #3032 from dotty-staging/topic/harmonize-printers
Remove duplication from `UserFacingPrinter`
2 parents 761e156 + f7e9af9 commit 82dfb6c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+90
-161
lines changed

build.sbt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ val `dotty-bench-bootstrapped` = Build.`dotty-bench-bootstrapped`
1818
val `scala-library` = Build.`scala-library`
1919
val `scala-compiler` = Build.`scala-compiler`
2020
val `scala-reflect` = Build.`scala-reflect`
21-
val `dotty-repl` = Build.`dotty-repl`
2221
val scalap = Build.scalap
2322
val dist = Build.dist
2423
val `dist-bootstrapped` = Build.`dist-bootstrapped`

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,11 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
662662
override protected def keyString(sym: Symbol): String = {
663663
val flags = sym.flagsUNSAFE
664664
if (sym.isType && sym.owner.isTerm) ""
665+
else if (sym.isPackageObject) "package object"
666+
else if (flags.is(Module) && flags.is(Case)) "case object"
667+
else if (sym.isClass && flags.is(Case)) "case class"
668+
else if (flags is Module) "object"
669+
else if (sym.isTerm && !flags.is(Param) && flags.is(Implicit)) "implicit val"
665670
else super.keyString(sym)
666671
}
667672

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package dotty.tools.dotc
2+
package printing
3+
4+
import core._
5+
import Constants.Constant, Contexts.Context, Denotations._, Flags._, Names._
6+
import NameOps._, StdNames._, Decorators._, Scopes.Scope, Types._, Texts._
7+
import SymDenotations.NoDenotation, Symbols.{ Symbol, ClassSymbol, defn }
8+
9+
class UserFacingPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
10+
11+
private[this] def getPkgCls(path: String) =
12+
_ctx.requiredPackage(path).moduleClass.asClass
13+
14+
private lazy val collectionPkg = getPkgCls("scala.collection")
15+
private lazy val immutablePkg = getPkgCls("scala.collection.immutable")
16+
private lazy val scalaPkg = defn.ScalaPackageClass
17+
private lazy val javaLangPkg = defn.JavaLangPackageVal.moduleClass.asClass
18+
19+
def standardPkg(pkgSym: Symbol) = pkgSym match {
20+
case `scalaPkg` | `collectionPkg` | `immutablePkg` | `javaLangPkg` => true
21+
case _ => false
22+
}
23+
24+
def wrappedName(pkgSym: Symbol) =
25+
pkgSym.name.toTermName == nme.EMPTY_PACKAGE ||
26+
pkgSym.name.isReplWrapperName
27+
28+
def wellKnownPkg(pkgSym: Symbol) = standardPkg(pkgSym) || wrappedName(pkgSym)
29+
30+
override protected def keyString(sym: Symbol): String =
31+
if (sym.flagsUNSAFE is Package) "" else super.keyString(sym)
32+
33+
override def nameString(name: Name): String =
34+
if (name.isReplAssignName) name.decode.toString.takeWhile(_ != '$')
35+
else name.decode.toString
36+
37+
override def toText(sym: Symbol): Text =
38+
if (sym.name.isReplAssignName) nameString(sym.name)
39+
else keyString(sym) ~~ nameString(sym.name.stripModuleClassSuffix)
40+
41+
override def dclText(sym: Symbol): Text = toText(sym) ~ {
42+
if (sym.is(Method)) toText(sym.info)
43+
else if (sym.isType && sym.info.isInstanceOf[TypeAlias]) toText(sym.info)
44+
else if (sym.isType || sym.isClass) ""
45+
else ":" ~~ toText(sym.info)
46+
}
47+
48+
override def toText(const: Constant): Text = Str(const.value.toString)
49+
50+
override def toText(tp: Type): Text = tp match {
51+
case ExprType(result) => ":" ~~ toText(result)
52+
case tp: ConstantType => toText(tp.value)
53+
case tp: TypeRef => tp.info match {
54+
case TypeAlias(alias) => toText(alias)
55+
case _ => toText(tp.info)
56+
}
57+
case tp: ClassInfo => {
58+
if (wellKnownPkg(tp.cls.owner)) nameString(tp.cls.name)
59+
else {
60+
def printPkg(sym: ClassSymbol): Text =
61+
if (sym.owner == defn.RootClass || wrappedName(sym.owner))
62+
nameString(sym.name.stripModuleClassSuffix)
63+
else
64+
printPkg(sym.owner.asClass) ~ "." ~ toText(sym)
65+
66+
printPkg(tp.cls.owner.asClass) ~ "." ~ nameString(tp.cls.name)
67+
}
68+
}
69+
case tp => super.toText(tp)
70+
}
71+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ package object printing {
2020
* -Xprint will print `sym.name` instead of `sym.originalName`
2121
*/
2222
val XprintMode = new Key[Unit]
23+
24+
/** @pre `nel` is non-empty list */
25+
private[printing] implicit class ListOps[A](val nel: List[A]) extends AnyVal {
26+
def intersperse(a: A): List[A] =
27+
nel.flatMap(a :: _ :: Nil).tail
28+
}
2329
}

repl/src/dotty/tools/repl/ParseResult.scala renamed to compiler/src/dotty/tools/repl/ParseResult.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ case object Help extends Command {
9595

9696
object ParseResult {
9797

98-
private[this] val CommandExtract = """(:[\S]+)\s*(.*)""".r
98+
@sharable private[this] val CommandExtract = """(:[\S]+)\s*(.*)""".r
9999

100100
/** Extract a `ParseResult` from the string `sourceCode` */
101101
def apply(sourceCode: String)(implicit ctx: Context): ParseResult =

0 commit comments

Comments
 (0)