Skip to content
Open
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
10 changes: 10 additions & 0 deletions sourcecode/shared/src/main/scala/sourcecode/SourceContext.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sourcecode

import language.experimental.macros
import scala.reflect.macros.whitebox


object Util{
Expand Down Expand Up @@ -105,6 +106,15 @@ object Pkg extends SourceCompanion[String, Pkg](new Pkg(_)){
def impl(c: Compat.Context): c.Expr[Pkg] = Impls.enclosing[Pkg](c)(_.isPackage)
}

case class Caller[A](value: A)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not keen on this being parametric, it would be best if it was a String to avoid leaking runtime information.

object Caller {
implicit def generate: Caller[_] = macro impl
def impl(c: whitebox.Context): c.Tree = {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like it could work as blackbox

import c.universe._
q"new _root_.sourcecode.Caller(this)"
}
}

case class Text[T](value: T, source: String)
object Text{
implicit def generate[T](v: T): Text[T] = macro Impls.text[T]
Expand Down
16 changes: 16 additions & 0 deletions sourcecode/shared/src/test/scala/sourcecode/CallerTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package sourcecode

/**
* @author 张志豪 (izhangzhihao) <[email protected]>
*/
object CallerTest {
def run(): Unit = {
assert(Foo.call == CallerTest.getClass.getName)
}
}

object Foo {
def call(implicit caller: Caller[_]): String = {
caller.value.getClass.getName
}
}
1 change: 1 addition & 0 deletions sourcecode/shared/src/test/scala/sourcecode/Tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ object Tests{
ManualImplicit()
TextTests()
ArgsTests()
CallerTest.run()

println("================LogExample================")
logExample()
Expand Down