Skip to content

Commit 0307000

Browse files
committed
Add .show to Tasty.CaseDef
1 parent ce4633d commit 0307000

File tree

6 files changed

+21
-3
lines changed

6 files changed

+21
-3
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,10 @@ object TastyImpl extends scala.tasty.Tasty {
436436

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

439+
def CaseDefDeco(caseDef: CaseDef): AbstractCaseDef = new AbstractCaseDef {
440+
def show(implicit ctx: Context, s: Show[TastyImpl.this.type]): String = s.showCaseDef(caseDef)
441+
}
442+
439443
object CaseDef extends CaseDefExtractor {
440444
def unapply(x: CaseDef): Option[(Pattern, Option[Term], Term)] = x match {
441445
case x: tpd.CaseDef @unchecked =>

library/src/scala/tasty/Tasty.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,11 @@ abstract class Tasty { tasty =>
305305

306306
implicit def caseDefClassTag: ClassTag[CaseDef]
307307

308+
trait AbstractCaseDef {
309+
def show(implicit ctx: Context, s: Show[tasty.type]): String
310+
}
311+
implicit def CaseDefDeco(caseDef: CaseDef): AbstractCaseDef
312+
308313
val CaseDef: CaseDefExtractor
309314
abstract class CaseDefExtractor {
310315
def unapply(x: CaseDef): Option[(Pattern, Option[Term], Term)]

library/src/scala/tasty/util/Show.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ abstract class Show[T <: Tasty with Singleton](val tasty: T) {
66

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

9+
def showCaseDef(caseDef: tasty.CaseDef)(implicit ctx: tasty.Context): String
10+
911
def showTypeOrBoundsTree(tpt: tasty.TypeOrBoundsTree)(implicit ctx: tasty.Context): String
1012

1113
def showTypeOrBounds(tpe: tasty.TypeOrBounds)(implicit ctx: tasty.Context): String

library/src/scala/tasty/util/ShowExtractors.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
88
def showTree(tree: Tree)(implicit ctx: Context): String =
99
new Buffer().visitTree(tree).result()
1010

11+
def showCaseDef(caseDef: CaseDef)(implicit ctx: Context): String =
12+
new Buffer().visitCaseDef(caseDef).result()
13+
1114
def showTypeOrBoundsTree(tpt: TypeOrBoundsTree)(implicit ctx: Context): String =
1215
new Buffer().visitTypeTree(tpt).result()
1316

library/src/scala/tasty/util/ShowSourceCode.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
77
def showTree(tree: Tree)(implicit ctx: Context): String =
88
(new Buffer).printTree(tree).result()
99

10+
def showCaseDef(caseDef: CaseDef)(implicit ctx: Context): String =
11+
(new Buffer).printCaseDef(caseDef).result()
12+
1013
def showTypeOrBoundsTree(tpt: TypeOrBoundsTree)(implicit ctx: Context): String =
1114
(new Buffer).printTypeOrBoundsTree(tpt).result()
1215

@@ -351,9 +354,9 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
351354
def printCases(cases: List[CaseDef], sep: String): Buffer = {
352355
def printSeparated(list: List[CaseDef]): Unit = list match {
353356
case Nil =>
354-
case x :: Nil => printCase(x)
357+
case x :: Nil => printCaseDef(x)
355358
case x :: xs =>
356-
printCase(x)
359+
printCaseDef(x)
357360
this += sep
358361
printSeparated(xs)
359362
}
@@ -468,7 +471,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
468471
printTypeTree(tpt)
469472
}
470473

471-
def printCase(caseDef: CaseDef): Buffer = {
474+
def printCaseDef(caseDef: CaseDef): Buffer = {
472475
val CaseDef(pat, guard, body) = caseDef
473476
this += "case "
474477
printPattern(pat)

tests/run/tasty-custom-show/quoted_1.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ object Macros {
4848
class DummyShow[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty0) {
4949
import tasty._
5050
def showTree(tree: Tree)(implicit ctx: Context): String = "Tree"
51+
def showCaseDef(caseDef: CaseDef)(implicit ctx: Context): String = "CaseDef"
5152
def showTypeOrBoundsTree(tpt: TypeOrBoundsTree)(implicit ctx: Context): String = "TypeOrBoundsTree"
5253
def showTypeOrBounds(tpe: TypeOrBounds)(implicit ctx: Context): String = "TypeOrBounds"
5354
def showConstant(const: Constant)(implicit ctx: Context): String = "Constant"

0 commit comments

Comments
 (0)