Skip to content

Add reflect SourceFile.{getJPath,name,path} #13177

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

Merged
merged 1 commit into from
Aug 17, 2021
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
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
16 changes: 15 additions & 1 deletion library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4151,9 +4151,23 @@ 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.
*
* It does not necessarily point to a path in the filesystem, it could be the path of a virtual file.
* Use `getJPath` to get paths to the filesystem.
*/
def path: String

/** Content of this source file */
def content: Option[String]
end extension
Expand Down
7 changes: 7 additions & 0 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@ import com.typesafe.tools.mima.core.ProblemFilters._

object MiMaFilters {
val Library: Seq[ProblemFilter] = Seq(
// 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"),
)
}
2 changes: 1 addition & 1 deletion scaladoc/src/dotty/tools/scaladoc/tasty/SymOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object SymOps:
else None

def source =
val path = sym.pos.map(_.sourceFile.jpath).filter(_ != null).map(_.toAbsolutePath)
val path = sym.pos.flatMap(_.sourceFile.getJPath).map(_.toAbsolutePath)
path.map(TastyMemberSource(_, sym.pos.get.startLine))

//TODO: Retrieve string that will match scaladoc anchors
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