-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add tasty Show type class and us in Tasty.XYZ.show #4577
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package scala.tasty.util | ||
|
||
import scala.tasty.Tasty | ||
|
||
abstract class Show[T <: Tasty with Singleton](val tasty: T) { | ||
|
||
def showTree(tree: tasty.Tree)(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 | ||
|
||
def showConstant(const: tasty.Constant)(implicit ctx: tasty.Context): String | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,22 +2,19 @@ package scala.tasty.util | |
|
||
import scala.tasty.Tasty | ||
|
||
class TastyPrinter[T <: Tasty with Singleton](val tasty: T) { | ||
class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hope the name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||
import tasty._ | ||
|
||
def stringOfTree(tree: Tree)(implicit ctx: Context): String = | ||
def showTree(tree: Tree)(implicit ctx: Context): String = | ||
new Buffer().visitTree(tree).result() | ||
|
||
def stringOfTypeTree(tree: TypeOrBoundsTree)(implicit ctx: Context): String = | ||
new Buffer().visitTypeTree(tree).result() | ||
def showTypeOrBoundsTree(tpt: TypeOrBoundsTree)(implicit ctx: Context): String = | ||
new Buffer().visitTypeTree(tpt).result() | ||
|
||
def stringOfType(tpe: TypeOrBounds)(implicit ctx: Context): String = | ||
def showTypeOrBounds(tpe: TypeOrBounds)(implicit ctx: Context): String = | ||
new Buffer().visitType(tpe).result() | ||
|
||
def stringOfModifier(mod: Modifier)(implicit ctx: Context): String = | ||
new Buffer().visitModifier(mod).result() | ||
|
||
def stringOfConstant(const: Constant)(implicit ctx: Context): String = | ||
def showConstant(const: Constant)(implicit ctx: Context): String = | ||
new Buffer().visitConstant(const).result() | ||
|
||
private class Buffer(implicit ctx: Context) { self => | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
foo | ||
Tree | ||
|
||
bar | ||
Tree | ||
|
||
bar2 | ||
Tree | ||
|
||
foo2 | ||
Tree | ||
|
||
baz | ||
Tree | ||
|
||
baz2 | ||
Tree | ||
|
||
<init> | ||
Tree | ||
|
||
b | ||
Tree | ||
|
||
b2 | ||
Tree | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import scala.quoted._ | ||
import dotty.tools.dotc.quoted.Toolbox._ | ||
|
||
import scala.tasty.Universe | ||
import scala.tasty.Tasty | ||
import scala.tasty.util.{TreeTraverser, Show} | ||
|
||
object Macros { | ||
|
||
implicit inline def printOwners[T](x: => T): Unit = | ||
~impl('(x))(Universe.compilationUniverse) // FIXME infer Universe.compilationUniverse within top level ~ | ||
|
||
def impl[T](x: Expr[T])(implicit u: Universe): Expr[Unit] = { | ||
import u._ | ||
import u.tasty._ | ||
|
||
|
||
val buff = new StringBuilder | ||
|
||
val output = new TreeTraverser(u.tasty) { | ||
override def traverseTree(tree: Tree)(implicit ctx: Context): Unit = { | ||
// Use custom Show[_] here | ||
implicit val printer = new DummyShow(tasty) | ||
tree match { | ||
case tree @ DefDef(name, _, _, _, _) => | ||
buff.append(name) | ||
buff.append("\n") | ||
buff.append(tree.owner.show) | ||
buff.append("\n\n") | ||
case tree @ ValDef(name, _, _) => | ||
buff.append(name) | ||
buff.append("\n") | ||
buff.append(tree.owner.show) | ||
buff.append("\n\n") | ||
case _ => | ||
} | ||
traverseTreeChildren(tree) | ||
} | ||
} | ||
|
||
val tree = x.toTasty | ||
output.traverseTree(tree) | ||
'(print(~buff.result().toExpr)) | ||
} | ||
|
||
} | ||
|
||
class DummyShow[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty0) { | ||
import tasty._ | ||
def showTree(tree: Tree)(implicit ctx: Context): String = "Tree" | ||
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" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
import Macros._ | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
printOwners { | ||
def foo = { | ||
def bar = 1 | ||
val bar2 = 2 | ||
bar | ||
} | ||
val foo2 = { | ||
def baz = 3 | ||
val baz2 = 4 | ||
baz | ||
} | ||
class A { | ||
type B = Int | ||
def b = 5 | ||
val b2 = 6 | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
def
here makes the member unstable problems, see c9dbecc — I'm not using the API you used, but I'd expect both to work as they usually do.