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 =

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import dotc.core.Contexts.Context
44
import dotc.core.Symbols.Symbol
55
import dotc.core.Denotations.Denotation
66
import dotc.reporting.diagnostic.MessageContainer
7+
import dotc.printing.UserFacingPrinter
78

89
import dotc.reporting.{
910
StoreReporter,
@@ -17,11 +18,6 @@ package object repl {
1718
new StoreReporter(null)
1819
with UniqueMessagePositions with HideNonSensicalMessages
1920

20-
private[repl] implicit class ListOps[A](val nel: List[A]) extends AnyVal {
21-
def intersperse(a: A): List[A] =
22-
nel.flatMap(a :: _ :: Nil).tail
23-
}
24-
2521
private[repl] implicit class ShowUser(val s: Symbol) extends AnyVal {
2622
def showUser(implicit ctx: Context): String = {
2723
val printer = new UserFacingPrinter(ctx)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

repl/test-resources/repl/i2554 renamed to compiler/test-resources/repl/i2554

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@ scala> import foo._
44
scala> implicit val shape: Shape[_ <: FlatShapeLevel, Int, Int, _] = null
55
implicit val shape: foo.Shape[_ <: foo.FlatShapeLevel, Int, Int, _] = null
66
scala> def hint = Shape.tuple2Shape(shape, shape)
7-
def hint: foo.Shape[foo.FlatShapeLevel, Tuple2[Int, Int], Tuple2[Int, Int],
8-
Tuple2
9-
[_, _]]
7+
def hint: foo.Shape[foo.FlatShapeLevel, (Int, Int), (Int, Int), ()]
File renamed without changes.
File renamed without changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
scala> val toInt: Any => Int = new { def apply(a: Any) = 1; override def toString() = "<func1>" }
2+
val toInt: Any => Int = <func1>

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class CompilationTests extends ParallelTesting {
205205
compileDir("../compiler/src/dotty/tools/dotc/config", picklingOptions) +
206206
compileDir("../compiler/src/dotty/tools/dotc/parsing", picklingOptions) +
207207
compileDir("../compiler/src/dotty/tools/dotc/printing", picklingOptions) +
208-
compileDir("../repl/src/dotty/tools/repl", picklingOptions) +
208+
compileDir("../compiler/src/dotty/tools/repl", picklingOptions) +
209209
compileDir("../compiler/src/dotty/tools/dotc/rewrite", picklingOptions) +
210210
compileDir("../compiler/src/dotty/tools/dotc/transform", picklingOptions) +
211211
compileDir("../compiler/src/dotty/tools/dotc/typer", picklingOptions) +

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ class ErrorMessagesTests extends ErrorMessagesTest {
365365
@Test def methodDoesNotTakePrameters =
366366
checkMessagesAfter("frontend") {
367367
"""
368-
|object Scope{
368+
|object Scope {
369369
| def foo = ()
370370
| foo()
371371
|}

project/Build.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ object Build {
130130
javaSource in Compile := baseDirectory.value / "src",
131131
javaSource in Test := baseDirectory.value / "test",
132132
resourceDirectory in Compile := baseDirectory.value / "resources",
133+
resourceDirectory in Test := baseDirectory.value / "test-resources",
133134

134135
// Prevent sbt from rewriting our dependencies
135136
ivyScala ~= (_ map (_ copy (overrideScalaVersion = false)))
@@ -407,7 +408,6 @@ object Build {
407408
settings(commonBootstrappedSettings).
408409
settings(dottyDocSettings)
409410

410-
411411
lazy val `dotty-bot` = project.in(file("bot")).
412412
settings(commonScala2Settings).
413413
settings(

repl/src/dotty/tools/repl/UserFacingPrinter.scala

Lines changed: 0 additions & 148 deletions
This file was deleted.

0 commit comments

Comments
 (0)