Skip to content

Remove duplication from UserFacingPrinter #3032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ val `dotty-bench-bootstrapped` = Build.`dotty-bench-bootstrapped`
val `scala-library` = Build.`scala-library`
val `scala-compiler` = Build.`scala-compiler`
val `scala-reflect` = Build.`scala-reflect`
val `dotty-repl` = Build.`dotty-repl`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional to keep the dotty-repl project in Build.scala?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like an oversight when rebasing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Remove it in #3044)

val scalap = Build.scalap
val dist = Build.dist
val `dist-bootstrapped` = Build.`dist-bootstrapped`
Expand Down
5 changes: 5 additions & 0 deletions compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,11 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
override protected def keyString(sym: Symbol): String = {
val flags = sym.flagsUNSAFE
if (sym.isType && sym.owner.isTerm) ""
else if (sym.isPackageObject) "package object"
else if (flags.is(Module) && flags.is(Case)) "case object"
else if (sym.isClass && flags.is(Case)) "case class"
else if (flags is Module) "object"
else if (sym.isTerm && !flags.is(Param) && flags.is(Implicit)) "implicit val"
else super.keyString(sym)
}

Expand Down
71 changes: 71 additions & 0 deletions compiler/src/dotty/tools/dotc/printing/UserFacingPrinter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package dotty.tools.dotc
package printing

import core._
import Constants.Constant, Contexts.Context, Denotations._, Flags._, Names._
import NameOps._, StdNames._, Decorators._, Scopes.Scope, Types._, Texts._
import SymDenotations.NoDenotation, Symbols.{ Symbol, ClassSymbol, defn }

class UserFacingPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you use _ctx because ctx is already defined in RefinedPrinter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and that's what we do in RefinedPrinter when extending plain printer :)


private[this] def getPkgCls(path: String) =
_ctx.requiredPackage(path).moduleClass.asClass

private lazy val collectionPkg = getPkgCls("scala.collection")
private lazy val immutablePkg = getPkgCls("scala.collection.immutable")
private lazy val scalaPkg = defn.ScalaPackageClass
private lazy val javaLangPkg = defn.JavaLangPackageVal.moduleClass.asClass

def standardPkg(pkgSym: Symbol) = pkgSym match {
case `scalaPkg` | `collectionPkg` | `immutablePkg` | `javaLangPkg` => true
case _ => false
}

def wrappedName(pkgSym: Symbol) =
pkgSym.name.toTermName == nme.EMPTY_PACKAGE ||
pkgSym.name.isReplWrapperName

def wellKnownPkg(pkgSym: Symbol) = standardPkg(pkgSym) || wrappedName(pkgSym)

override protected def keyString(sym: Symbol): String =
if (sym.flagsUNSAFE is Package) "" else super.keyString(sym)

override def nameString(name: Name): String =
if (name.isReplAssignName) name.decode.toString.takeWhile(_ != '$')
else name.decode.toString

override def toText(sym: Symbol): Text =
if (sym.name.isReplAssignName) nameString(sym.name)
else keyString(sym) ~~ nameString(sym.name.stripModuleClassSuffix)

override def dclText(sym: Symbol): Text = toText(sym) ~ {
if (sym.is(Method)) toText(sym.info)
else if (sym.isType && sym.info.isInstanceOf[TypeAlias]) toText(sym.info)
else if (sym.isType || sym.isClass) ""
else ":" ~~ toText(sym.info)
}

override def toText(const: Constant): Text = Str(const.value.toString)

override def toText(tp: Type): Text = tp match {
case ExprType(result) => ":" ~~ toText(result)
case tp: ConstantType => toText(tp.value)
case tp: TypeRef => tp.info match {
case TypeAlias(alias) => toText(alias)
case _ => toText(tp.info)
}
case tp: ClassInfo => {
if (wellKnownPkg(tp.cls.owner)) nameString(tp.cls.name)
else {
def printPkg(sym: ClassSymbol): Text =
if (sym.owner == defn.RootClass || wrappedName(sym.owner))
nameString(sym.name.stripModuleClassSuffix)
else
printPkg(sym.owner.asClass) ~ "." ~ toText(sym)

printPkg(tp.cls.owner.asClass) ~ "." ~ nameString(tp.cls.name)
}
}
case tp => super.toText(tp)
}
}
6 changes: 6 additions & 0 deletions compiler/src/dotty/tools/dotc/printing/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ package object printing {
* -Xprint will print `sym.name` instead of `sym.originalName`
*/
val XprintMode = new Key[Unit]

/** @pre `nel` is non-empty list */
private[printing] implicit class ListOps[A](val nel: List[A]) extends AnyVal {
def intersperse(a: A): List[A] =
nel.flatMap(a :: _ :: Nil).tail
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ case object Help extends Command {

object ParseResult {

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

/** Extract a `ParseResult` from the string `sourceCode` */
def apply(sourceCode: String)(implicit ctx: Context): ParseResult =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import dotc.core.Contexts.Context
import dotc.core.Symbols.Symbol
import dotc.core.Denotations.Denotation
import dotc.reporting.diagnostic.MessageContainer
import dotc.printing.UserFacingPrinter

import dotc.reporting.{
StoreReporter,
Expand All @@ -17,11 +18,6 @@ package object repl {
new StoreReporter(null)
with UniqueMessagePositions with HideNonSensicalMessages

private[repl] implicit class ListOps[A](val nel: List[A]) extends AnyVal {
def intersperse(a: A): List[A] =
nel.flatMap(a :: _ :: Nil).tail
}

private[repl] implicit class ShowUser(val s: Symbol) extends AnyVal {
def showUser(implicit ctx: Context): String = {
val printer = new UserFacingPrinter(ctx)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ scala> import foo._
scala> implicit val shape: Shape[_ <: FlatShapeLevel, Int, Int, _] = null
implicit val shape: foo.Shape[_ <: foo.FlatShapeLevel, Int, Int, _] = null
scala> def hint = Shape.tuple2Shape(shape, shape)
def hint: foo.Shape[foo.FlatShapeLevel, Tuple2[Int, Int], Tuple2[Int, Int],
Tuple2
[_, _]]
def hint: foo.Shape[foo.FlatShapeLevel, (Int, Int), (Int, Int), ()]
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions compiler/test-resources/type-printer/functions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scala> val toInt: Any => Int = new { def apply(a: Any) = 1; override def toString() = "<func1>" }
val toInt: Any => Int = <func1>
2 changes: 1 addition & 1 deletion compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class CompilationTests extends ParallelTesting {
compileDir("../compiler/src/dotty/tools/dotc/config", picklingOptions) +
compileDir("../compiler/src/dotty/tools/dotc/parsing", picklingOptions) +
compileDir("../compiler/src/dotty/tools/dotc/printing", picklingOptions) +
compileDir("../repl/src/dotty/tools/repl", picklingOptions) +
compileDir("../compiler/src/dotty/tools/repl", picklingOptions) +
compileDir("../compiler/src/dotty/tools/dotc/rewrite", picklingOptions) +
compileDir("../compiler/src/dotty/tools/dotc/transform", picklingOptions) +
compileDir("../compiler/src/dotty/tools/dotc/typer", picklingOptions) +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class ErrorMessagesTests extends ErrorMessagesTest {
@Test def methodDoesNotTakePrameters =
checkMessagesAfter("frontend") {
"""
|object Scope{
|object Scope {
| def foo = ()
| foo()
|}
Expand Down
2 changes: 1 addition & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ object Build {
javaSource in Compile := baseDirectory.value / "src",
javaSource in Test := baseDirectory.value / "test",
resourceDirectory in Compile := baseDirectory.value / "resources",
resourceDirectory in Test := baseDirectory.value / "test-resources",

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


lazy val `dotty-bot` = project.in(file("bot")).
settings(commonScala2Settings).
settings(
Expand Down
148 changes: 0 additions & 148 deletions repl/src/dotty/tools/repl/UserFacingPrinter.scala

This file was deleted.