record source name or full path for settings/tasks depending on whether the enclosing package is the empty package

This commit is contained in:
Mark Harrah 2012-12-02 03:17:20 -05:00
parent a8d0af9464
commit 2f2596c133
1 changed files with 15 additions and 1 deletions

View File

@ -149,13 +149,27 @@ object TaskMacro
import c.universe._
val pos = c.enclosingPosition
if(pos.isDefined && pos.line >= 0 && pos.source != null) {
val name = constant[String](c, pos.source.file.name)
val f = pos.source.file
val name = constant[String](c, settingSource(c, f.path, f.name))
val line = constant[Int](c, pos.line)
reify { sbt.LinePosition(name.splice, line.splice) }
}
else
reify{ sbt.NoPosition }
}
private[this] def settingSource(c: Context, path: String, name: String): String =
{
val ec = c.enclosingClass.symbol
def inEmptyPackage(s: c.Symbol): Boolean =
s != c.universe.NoSymbol && (s.owner == c.mirror.EmptyPackage || s.owner == c.mirror.EmptyPackageClass || inEmptyPackage(s.owner))
if(!ec.isStatic)
name
else if(inEmptyPackage(ec))
path
else
s"(${ec.fullName}) $name"
}
private[this] def constant[T: c.TypeTag](c: Context, t: T): c.Expr[T] = {
import c.universe._
c.Expr[T](Literal(Constant(t)))