From 309fe4299e2b21c622fe0a9b319ea57347ada7e3 Mon Sep 17 00:00:00 2001 From: johannes karoff Date: Thu, 16 Aug 2018 13:35:47 +0200 Subject: [PATCH] add sourcecode.Column --- README.md | 1 + .../shared/src/main/scala/sourcecode/SourceContext.scala | 9 +++++++++ .../shared/src/test/scala/sourcecode/Implicits.scala | 8 +++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index df53503..5d62a62 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ 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.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..d23e1a7 100644 --- a/sourcecode/shared/src/main/scala/sourcecode/SourceContext.scala +++ b/sourcecode/shared/src/main/scala/sourcecode/SourceContext.scala @@ -84,6 +84,15 @@ 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 Enclosing(value: String) extends SourceValue[String] object Enclosing extends SourceCompanion[String, Enclosing](new Enclosing(_)){ diff --git a/sourcecode/shared/src/test/scala/sourcecode/Implicits.scala b/sourcecode/shared/src/test/scala/sourcecode/Implicits.scala index 16cf7fc..52f2cce 100644 --- a/sourcecode/shared/src/test/scala/sourcecode/Implicits.scala +++ b/sourcecode/shared/src/test/scala/sourcecode/Implicits.scala @@ -20,6 +20,9 @@ object Implicits { val line = implicitly[sourcecode.Line] assert(line.value == 20) + val column = implicitly[sourcecode.Column] + assert(column.value == 28, column.value) + lazy val myLazy = { trait Bar{ val name = implicitly[sourcecode.Name] @@ -32,7 +35,10 @@ 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 == 37) + + val column = implicitly[sourcecode.Column] + assert(column.value == 32, column.value) val enclosing = implicitly[sourcecode.Enclosing] assert(