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

Relative paths during macro capture #82

Merged
merged 1 commit into from
Sep 20, 2020
Merged
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
16 changes: 13 additions & 3 deletions modules/codecs/src/weaver/codecs/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,26 @@ package object codecs {
}

implicit val sourceLocationEncoder: Encoder[SourceLocation] =
Encoder.forProduct3[SourceLocation, Option[String], Option[String], Int](
Encoder.forProduct4[SourceLocation,
Option[String],
Option[String],
Option[String],
Int](
"fileName",
"filePath",
"fileRelativePath",
"line"
)(sl => (sl.fileName, sl.filePath, sl.line))
)(sl => (sl.fileName, sl.filePath, sl.fileRelativePath, sl.line))

implicit val sourceLocationDecoder: Decoder[SourceLocation] =
Decoder.forProduct3[SourceLocation, Option[String], Option[String], Int](
Decoder.forProduct4[SourceLocation,
Option[String],
Option[String],
Option[String],
Int](
"fileName",
"filePath",
"fileRelativePath",
"line"
)(SourceLocation.apply)

Expand Down
34 changes: 0 additions & 34 deletions modules/core/src-js/FSCompat.scala

This file was deleted.

18 changes: 0 additions & 18 deletions modules/core/src-jvm/FSCompat.scala

This file was deleted.

4 changes: 2 additions & 2 deletions modules/core/src/weaver/Result.scala
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ object Result {
if (index == 0)
color + prefix + line +
location.fold("")(l =>
s" (${l.bestEffortPath.getOrElse("none")}:${l.line})")
s" (${l.fileRelativePath.getOrElse("none")}:${l.line})")
else
color + prefix + line
}
Expand All @@ -180,7 +180,7 @@ object Result {
if (index == 0)
color + prefix + line +
location.fold("")(l =>
s" (${l.bestEffortPath.getOrElse("none")}:${l.line})")
s" (${l.fileRelativePath.getOrElse("none")}:${l.line})")
else
color + prefix + line
}
Expand Down
20 changes: 12 additions & 8 deletions modules/core/src/weaver/SourceLocation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import scala.util.{Try => STry}
final case class SourceLocation(
fileName: Option[String],
filePath: Option[String],
fileRelativePath: Option[String],
line: Int
) {
def bestEffortPath : Option[String] =
FSCompat.bestEffortPath(fileName, filePath)
}
)

object SourceLocation {
implicit def fromContext: SourceLocation =
Expand All @@ -22,15 +20,21 @@ object SourceLocation {
import c.universe._

def fromContext: Tree = {
val (fileNameExpr, pathExpr, lineExpr) = getSourceLocation
val (fileNameExpr, pathExpr, relPathExpr, lineExpr) = getSourceLocation
val SourceLocationSym = symbolOf[SourceLocation].companion
q"""$SourceLocationSym($fileNameExpr, $pathExpr, $lineExpr)"""
q"""$SourceLocationSym($fileNameExpr, $pathExpr, $relPathExpr, $lineExpr)"""
}

private def getSourceLocation = {
val line = c.Expr[Int](Literal(Constant(c.enclosingPosition.line)))
val pwd = java.nio.file.Paths.get("").toAbsolutePath
val file = STry(Option(c.enclosingPosition.source.file.file)).toOption.flatten
(wrapOption(file.map(_.getName)), wrapOption(file.map(_.getPath)), line)
val path = file.map(_.getPath)
val relativePath = file.map { f =>
pwd.relativize(f.toPath()).toString()
}

val line = c.Expr[Int](Literal(Constant(c.enclosingPosition.line)))
(wrapOption(file.map(_.getName)), wrapOption(path), wrapOption(relativePath), line)
}

private def wrapOption[A](opt: Option[A]): c.Expr[Option[A]] =
Expand Down
33 changes: 0 additions & 33 deletions modules/framework/test/src/FSCompatTest.scala

This file was deleted.

1 change: 1 addition & 0 deletions modules/framework/test/src/Meta.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ object Meta {

implicit val sourceLocation: SourceLocation = SourceLocation(
Some("DogFoodTests.scala"),
None,
Some("src/main/DogFoodTests.scala"),
5)
}
Expand Down
21 changes: 21 additions & 0 deletions modules/framework/test/src/SourceLocationTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package weaver
package framework
package test

object SourceLocationTest extends SimpleIOSuite {

// DO NOT MOVE THIS
val sourceLocation = implicitly[SourceLocation]

test("implicit capture of source location is relativised") {
val name = sourceLocation.fileName
val relPath = sourceLocation.fileRelativePath
val line = sourceLocation.line

expect(name.contains("SourceLocationTest.scala")) &&
expect(relPath.contains(
"modules/framework/test/src/SourceLocationTest.scala")) &&
expect(line == 8)
}

}