Skip to content

Commit 2d6aa75

Browse files
Merge pull request #4609 from dotty-staging/add-casedef-show
Add .show to Tasty.CaseDef
2 parents 656b96a + 0307000 commit 2d6aa75

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
@@ -438,6 +438,10 @@ object TastyImpl extends scala.tasty.Tasty {
438438

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

441+
def CaseDefDeco(caseDef: CaseDef): AbstractCaseDef = new AbstractCaseDef {
442+
def show(implicit ctx: Context, s: Show[TastyImpl.this.type]): String = s.showCaseDef(caseDef)
443+
}
444+
441445
object CaseDef extends CaseDefExtractor {
442446
def unapply(x: CaseDef): Option[(Pattern, Option[Term], Term)] = x match {
443447
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
@@ -310,6 +310,11 @@ abstract class Tasty { tasty =>
310310

311311
implicit def caseDefClassTag: ClassTag[CaseDef]
312312

313+
trait AbstractCaseDef {
314+
def show(implicit ctx: Context, s: Show[tasty.type]): String
315+
}
316+
implicit def CaseDefDeco(caseDef: CaseDef): AbstractCaseDef
317+
313318
val CaseDef: CaseDefExtractor
314319
abstract class CaseDefExtractor {
315320
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)