Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sourcecode.Column #52

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(_)){
Expand Down
8 changes: 7 additions & 1 deletion sourcecode/shared/src/test/scala/sourcecode/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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(
Expand Down