Skip to content

Add .show to Tasty.CaseDef #4609

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 1 commit into from
Jun 6, 2018
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
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,10 @@ object TastyImpl extends scala.tasty.Tasty {

def caseDefClassTag: ClassTag[CaseDef] = implicitly[ClassTag[CaseDef]]

def CaseDefDeco(caseDef: CaseDef): AbstractCaseDef = new AbstractCaseDef {
def show(implicit ctx: Context, s: Show[TastyImpl.this.type]): String = s.showCaseDef(caseDef)
}

object CaseDef extends CaseDefExtractor {
def unapply(x: CaseDef): Option[(Pattern, Option[Term], Term)] = x match {
case x: tpd.CaseDef @unchecked =>
Expand Down
5 changes: 5 additions & 0 deletions library/src/scala/tasty/Tasty.scala
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ abstract class Tasty { tasty =>

implicit def caseDefClassTag: ClassTag[CaseDef]

trait AbstractCaseDef {
def show(implicit ctx: Context, s: Show[tasty.type]): String
}
implicit def CaseDefDeco(caseDef: CaseDef): AbstractCaseDef

val CaseDef: CaseDefExtractor
abstract class CaseDefExtractor {
def unapply(x: CaseDef): Option[(Pattern, Option[Term], Term)]
Expand Down
2 changes: 2 additions & 0 deletions library/src/scala/tasty/util/Show.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ abstract class Show[T <: Tasty with Singleton](val tasty: T) {

def showTree(tree: tasty.Tree)(implicit ctx: tasty.Context): String

def showCaseDef(caseDef: tasty.CaseDef)(implicit ctx: tasty.Context): String

def showTypeOrBoundsTree(tpt: tasty.TypeOrBoundsTree)(implicit ctx: tasty.Context): String

def showTypeOrBounds(tpe: tasty.TypeOrBounds)(implicit ctx: tasty.Context): String
Expand Down
3 changes: 3 additions & 0 deletions library/src/scala/tasty/util/ShowExtractors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
def showTree(tree: Tree)(implicit ctx: Context): String =
new Buffer().visitTree(tree).result()

def showCaseDef(caseDef: CaseDef)(implicit ctx: Context): String =
new Buffer().visitCaseDef(caseDef).result()

def showTypeOrBoundsTree(tpt: TypeOrBoundsTree)(implicit ctx: Context): String =
new Buffer().visitTypeTree(tpt).result()

Expand Down
9 changes: 6 additions & 3 deletions library/src/scala/tasty/util/ShowSourceCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
def showTree(tree: Tree)(implicit ctx: Context): String =
(new Buffer).printTree(tree).result()

def showCaseDef(caseDef: CaseDef)(implicit ctx: Context): String =
(new Buffer).printCaseDef(caseDef).result()

def showTypeOrBoundsTree(tpt: TypeOrBoundsTree)(implicit ctx: Context): String =
(new Buffer).printTypeOrBoundsTree(tpt).result()

Expand Down Expand Up @@ -351,9 +354,9 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
def printCases(cases: List[CaseDef], sep: String): Buffer = {
def printSeparated(list: List[CaseDef]): Unit = list match {
case Nil =>
case x :: Nil => printCase(x)
case x :: Nil => printCaseDef(x)
case x :: xs =>
printCase(x)
printCaseDef(x)
this += sep
printSeparated(xs)
}
Expand Down Expand Up @@ -468,7 +471,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
printTypeTree(tpt)
}

def printCase(caseDef: CaseDef): Buffer = {
def printCaseDef(caseDef: CaseDef): Buffer = {
val CaseDef(pat, guard, body) = caseDef
this += "case "
printPattern(pat)
Expand Down
1 change: 1 addition & 0 deletions tests/run/tasty-custom-show/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ object Macros {
class DummyShow[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty0) {
import tasty._
def showTree(tree: Tree)(implicit ctx: Context): String = "Tree"
def showCaseDef(caseDef: CaseDef)(implicit ctx: Context): String = "CaseDef"
def showTypeOrBoundsTree(tpt: TypeOrBoundsTree)(implicit ctx: Context): String = "TypeOrBoundsTree"
def showTypeOrBounds(tpe: TypeOrBounds)(implicit ctx: Context): String = "TypeOrBounds"
def showConstant(const: Constant)(implicit ctx: Context): String = "Constant"
Expand Down