Skip to content

Commit

Permalink
Add reflect SourceFile{getJPath,name,path}
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Jul 28, 2021
1 parent 23be42c commit 3056343
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
3 changes: 3 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2747,6 +2747,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
given SourceFileMethods: SourceFileMethods with
extension (self: SourceFile)
def jpath: java.nio.file.Path = self.file.jpath
def getJPath: Option[java.nio.file.Path] = Option(self.file.jpath)
def name: String = self.name
def path: String = self.path
def content: Option[String] =
// TODO detect when we do not have a source and return None
Some(new String(self.content()))
Expand Down
15 changes: 14 additions & 1 deletion library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4151,9 +4151,22 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
/** Extension methods of `SourceFile` */
trait SourceFileMethods {
extension (self: SourceFile)
/** Path to this source file */
/** Path to this source file. May be `null` for virtual files such as in the REPL. */
// TODO: deprecate once getJPath is not experimental
def jpath: java.nio.file.Path

/** Path to this source file. May be `None` for virtual files such as in the REPL. */
@experimental
def getJPath: Option[java.nio.file.Path]

/** Name of the source file */
@experimental
def name: String

/** Path of the source file */
@experimental
def path: String

/** Content of this source file */
def content: Option[String]
end extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ object SourceFiles {

def getThisFileImpl: Macro[String] =
val q = quotes // Quotes is ByName and hence not stable (q stabilizes it)
Expr(q.reflect.SourceFile.current.jpath.getFileName.toString)
Expr(q.reflect.SourceFile.current.name)

}
2 changes: 1 addition & 1 deletion tests/run-macros/tasty-macro-positions/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ object Macros {

def posStr(using Quotes)(pos: quotes.reflect.Position): Expr[String] = {
import quotes.reflect.*
Expr(s"${pos.sourceFile.jpath.getFileName.toString}:[${pos.start}..${pos.end}]")
Expr(s"${pos.sourceFile.name}:[${pos.start}..${pos.end}]")
}
}
2 changes: 1 addition & 1 deletion tests/run-macros/tasty-positioned/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object Positioned {
import quotes.reflect.{Position as Pos, *}
val pos = Pos.ofMacroExpansion

val path = Expr(pos.sourceFile.jpath.toString)
val path = Expr(pos.sourceFile.getJPath.get.toString)
val start = Expr(pos.start)
val end = Expr(pos.end)
val startLine = Expr(pos.startLine)
Expand Down

0 comments on commit 3056343

Please sign in to comment.