aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Pretty <jon.pretty@propensive.com>2018-01-05 16:18:07 +0100
committerGitHub <noreply@github.com>2018-01-05 16:18:07 +0100
commitc0a73e04f9d902e1dbb66182485d7da3ee7097b6 (patch)
tree2243e14792b4e7428177bfea1699db276db6d187
parent235f2ddd5adf5c99cf53d6e6321dbe5e2fd842c2 (diff)
parenteada37b225a081cdff608398d10c3bfe4a3c0e62 (diff)
downloadmagnolia-c0a73e04f9d902e1dbb66182485d7da3ee7097b6.tar.gz
magnolia-c0a73e04f9d902e1dbb66182485d7da3ee7097b6.tar.bz2
magnolia-c0a73e04f9d902e1dbb66182485d7da3ee7097b6.zip
Merge pull request #70 from sirthias/md/debug-annotation
Add support for `magnolia.debug` annotation
-rw-r--r--core/shared/src/main/scala/interface.scala12
-rw-r--r--core/shared/src/main/scala/magnolia.scala8
2 files changed, 19 insertions, 1 deletions
diff --git a/core/shared/src/main/scala/interface.scala b/core/shared/src/main/scala/interface.scala
index 05803f0..51abc22 100644
--- a/core/shared/src/main/scala/interface.scala
+++ b/core/shared/src/main/scala/interface.scala
@@ -188,4 +188,14 @@ final class SealedTrait[Typeclass[_], Type](val typeName: TypeName,
*/
final case class TypeName(owner: String, short: String) {
def full: String = s"$owner.$short"
-} \ No newline at end of file
+}
+
+/**
+ * This annotation can be attached to the implicit `gen` method of a type class companion,
+ * which is implemented by the `Magnolia.gen` macro.
+ * It causes magnolia to dump the macro-generated code to the console during compilation.
+ *
+ * @param typeNamePart If non-empty restricts the output generation to types
+ * whose full name contains the given [[String]]
+ */
+final class debug(typeNamePart: String = "") extends scala.annotation.StaticAnnotation
diff --git a/core/shared/src/main/scala/magnolia.scala b/core/shared/src/main/scala/magnolia.scala
index b61d1c0..b5c196a 100644
--- a/core/shared/src/main/scala/magnolia.scala
+++ b/core/shared/src/main/scala/magnolia.scala
@@ -69,6 +69,10 @@ object Magnolia {
import c.universe._
import internal._
+ val debug = c.macroApplication.symbol.annotations
+ .find(_.tree.tpe <:< typeOf[debug])
+ .flatMap(_.tree.children.tail.collectFirst { case Literal(Constant(s: String)) => s })
+
val magnoliaPkg = q"_root_.magnolia"
val scalaPkg = q"_root_.scala"
@@ -447,6 +451,10 @@ object Magnolia {
if (currentStack.frames.isEmpty) recursionStack = ListMap()
val dereferencedResult = result.map { tree =>
+ if (debug.isDefined && genericType.toString.contains(debug.get)) {
+ c.echo(c.enclosingPosition, s"Magnolia macro expansion for $genericType")
+ c.echo(NoPosition, s"... = ${showCode(tree)}\n\n")
+ }
if (currentStack.frames.isEmpty) c.untypecheck(removeDeferred.transform(tree)) else tree
}