diff --git a/README.md b/README.md index df53503..1973ce8 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,8 @@ The kinds of compilation-time data that `sourcecode` provides are: - `sourcecode.File`: full path of the current file where the call occurs - `sourcecode.Line`: current line number +- `sourcecode.Column`: column number within the current line +- `sourcecode.Position`: position where the call occurs including file, line and column - `sourcecode.Name`: the name of the nearest enclosing definition: `val`, `class`, whatever. - `sourcecode.FullName`: the name of the nearest enclosing definition: `val`, diff --git a/sourcecode/shared/src/main/scala/sourcecode/SourceContext.scala b/sourcecode/shared/src/main/scala/sourcecode/SourceContext.scala index 66d67f7..a4c8163 100644 --- a/sourcecode/shared/src/main/scala/sourcecode/SourceContext.scala +++ b/sourcecode/shared/src/main/scala/sourcecode/SourceContext.scala @@ -84,8 +84,29 @@ object Line extends SourceCompanion[Int, Line](new Line(_)){ c.Expr[sourcecode.Line](q"""${c.prefix}($line)""") } } +case class Column(value: Int) extends SourceValue[Int] +object Column extends SourceCompanion[Int, Line](new Line(_)){ + implicit def generate: sourcecode.Column = macro impl + def impl(c: Compat.Context): c.Expr[sourcecode.Column] = { + import c.universe._ + val column = c.enclosingPosition.column + c.Expr[sourcecode.Column](q"""${c.prefix}($column)""") + } +} +case class Position(value: (String, Int, Int)) extends SourceValue[(String, Int, Int)] { + val (file: String, line: Int, column: Int) = value +} +object Position extends SourceCompanion[(String, Int, Int), Position](new Position(_)){ + implicit def generate: sourcecode.Position = macro impl + def impl(c: Compat.Context): c.Expr[sourcecode.Position] = { + import c.universe._ + val file = c.enclosingPosition.source.path + val line = c.enclosingPosition.line + val column = c.enclosingPosition.column + c.Expr[sourcecode.Position](q"""${c.prefix}($file, $line, $column)""") + } +} case class Enclosing(value: String) extends SourceValue[String] - object Enclosing extends SourceCompanion[String, Enclosing](new Enclosing(_)){ implicit def generate: Enclosing = macro impl def impl(c: Compat.Context): c.Expr[Enclosing] = Impls.enclosing[Enclosing](c)( @@ -98,7 +119,6 @@ object Enclosing extends SourceCompanion[String, Enclosing](new Enclosing(_)){ } } - case class Pkg(value: String) extends SourceValue[String] object Pkg extends SourceCompanion[String, Pkg](new Pkg(_)){ implicit def generate: Pkg = macro impl @@ -190,4 +210,4 @@ object Impls{ }.mkString.dropRight(1) c.Expr[T](q"""${c.prefix}($renderedPath)""") } -} +} \ No newline at end of file diff --git a/sourcecode/shared/src/test/scala/sourcecode/Implicits.scala b/sourcecode/shared/src/test/scala/sourcecode/Implicits.scala index 16cf7fc..bc478fa 100644 --- a/sourcecode/shared/src/test/scala/sourcecode/Implicits.scala +++ b/sourcecode/shared/src/test/scala/sourcecode/Implicits.scala @@ -18,7 +18,15 @@ object Implicits { assert(file.value.endsWith("/sourcecode/shared/src/test/scala/sourcecode/Implicits.scala")) val line = implicitly[sourcecode.Line] - assert(line.value == 20) + assert(line.value == 20, line.value) + + val column = implicitly[sourcecode.Column] + assert(column.value == 28, column.value) + + val position = implicitly[sourcecode.Position] + assert(position.file.endsWith("/sourcecode/shared/src/test/scala/sourcecode/Implicits.scala")) + assert(position.line == 26, position.line) + assert(position.column == 30, position.column) lazy val myLazy = { trait Bar{ @@ -32,7 +40,15 @@ object Implicits { assert(file.value.endsWith("/sourcecode/shared/src/test/scala/sourcecode/Implicits.scala")) val line = implicitly[sourcecode.Line] - assert(line.value == 34) + assert(line.value == 42, line.value) + + val column = implicitly[sourcecode.Column] + assert(column.value == 32, column.value) + + val position = implicitly[sourcecode.Position] + assert(position.file.endsWith("/sourcecode/shared/src/test/scala/sourcecode/Implicits.scala")) + assert(position.line == 48, position.line) + assert(position.column == 34, position.column) val enclosing = implicitly[sourcecode.Enclosing] assert( @@ -44,4 +60,4 @@ object Implicits { } myLazy } -} +} \ No newline at end of file