Skip to content

Commit

Permalink
Add reflect SourceFile.{getJPath,name,path}
Browse files Browse the repository at this point in the history
And deprecate SourceFile.jpath

Fixes scala#13007
  • Loading branch information
nicolasstucki committed Jul 28, 2021
1 parent 23be42c commit fcc9f3b
Show file tree
Hide file tree
Showing 6 changed files with 25 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
12 changes: 11 additions & 1 deletion library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4151,9 +4151,19 @@ 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. */
@deprecated("Use getJPath, name, or path instead of jpath", "3.0.2")
def jpath: java.nio.file.Path

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

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

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

/** Content of this source file */
def content: Option[String]
end extension
Expand Down
8 changes: 8 additions & 0 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,13 @@ object MiMaFilters {

// New APIs marked @experimental in 3.0.2
exclude[MissingClassProblem]("scala.Selectable$WithoutPreciseParameterTypes")

// New APIs that will be introduced in 3.1.0
exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.getJPath")
exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.name")
exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.path")
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.getJPath")
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.name")
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.path")
)
}
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 fcc9f3b

Please sign in to comment.