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

Added column & position #55

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
26 changes: 23 additions & 3 deletions sourcecode/shared/src/main/scala/sourcecode/SourceContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)(
Expand All @@ -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
Expand Down Expand Up @@ -190,4 +210,4 @@ object Impls{
}.mkString.dropRight(1)
c.Expr[T](q"""${c.prefix}($renderedPath)""")
}
}
}
22 changes: 19 additions & 3 deletions sourcecode/shared/src/test/scala/sourcecode/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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(
Expand All @@ -44,4 +60,4 @@ object Implicits {
}
myLazy
}
}
}