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

Add sourceCode to TASTy reflect Position #5870

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -15,5 +15,7 @@ trait PositionOpsImpl extends scala.tasty.reflect.PositionOps with CoreImpl {

def startColumn: Int = pos.startColumn
def endColumn: Int = pos.endColumn

def sourceCode: String = new String(pos.source.content(), pos.start, pos.end - pos.start)
}
}
3 changes: 3 additions & 0 deletions library/src/scala/tasty/reflect/PositionOps.scala
Expand Up @@ -26,6 +26,9 @@ trait PositionOps extends Core {
/** The end column in the source file */
def endColumn: Int

/** Source code within the position */
def sourceCode: String

}
implicit def PositionDeco(pos: Position): PositionAPI

Expand Down
3 changes: 3 additions & 0 deletions tests/run/tasty-original-source.check
@@ -0,0 +1,3 @@
(foo("abc"),abc)
(foo( "def" ),def)
(foo("ghi" /* comment */),ghi)
14 changes: 14 additions & 0 deletions tests/run/tasty-original-source/Macros_1.scala
@@ -0,0 +1,14 @@
import scala.quoted._
import scala.tasty._

object Macros {

implicit inline def withSource(arg: Any): (String, Any) = ~impl('(arg))

private def impl(arg: Expr[Any])(implicit reflect: Reflection): Expr[(String, Any)] = {
import reflect._
val source = arg.unseal.underlyingArgument.pos.sourceCode.toString.toExpr
'(Tuple2(~source, ~arg))
}

}
13 changes: 13 additions & 0 deletions tests/run/tasty-original-source/Test_2.scala
@@ -0,0 +1,13 @@

import Macros._

object Test {
def main(args: Array[String]): Unit = {
println(withSource(foo("abc")))
println(withSource( foo( "def" )))
println(withSource(foo("ghi" /* comment */)))
}

def foo(x: Any): Any = x

}