Skip to content
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
23 changes: 20 additions & 3 deletions sourcecode/src-2/sourcecode/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,38 @@ object Macros {
c.Expr[FullName.Machine](q"""${c.prefix}($fullName)""")
}

private val filePrefix = "//SOURCECODE_ORIGINAL_FILE_PATH="
private def findOriginalFile(chars: Array[Char]): Option[String] = {
new String(chars).linesIterator.find(_.contains(filePrefix)).map(_.split(filePrefix).last)
}

def fileImpl(c: Compat.Context): c.Expr[sourcecode.File] = {
import c.universe._
val file = c.enclosingPosition.source.path

val file = findOriginalFile(c.enclosingPosition.source.content)
.getOrElse(c.enclosingPosition.source.path)

c.Expr[sourcecode.File](q"""${c.prefix}($file)""")
}

def fileNameImpl(c: Compat.Context): c.Expr[sourcecode.FileName] = {
import c.universe._
val fileName = c.enclosingPosition.source.path.split('/').last
val fileName = findOriginalFile(c.enclosingPosition.source.content)
.getOrElse(c.enclosingPosition.source.path)
.split('/').last
c.Expr[sourcecode.FileName](q"""${c.prefix}($fileName)""")
}

private val linePrefix = "//SOURCECODE_ORIGINAL_CODE_START_MARKER"
def lineImpl(c: Compat.Context): c.Expr[sourcecode.Line] = {
import c.universe._
val line = c.enclosingPosition.line
val offset = new String(c.enclosingPosition.source.content)
.linesIterator
.indexWhere(_.contains(linePrefix)) match{
case -1 => 0
case n => n
}
val line = c.enclosingPosition.line - offset
c.Expr[sourcecode.Line](q"""${c.prefix}($line)""")
}

Expand Down
24 changes: 21 additions & 3 deletions sourcecode/src-3/sourcecode/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,37 @@ object Macros {
'{new FullName.Machine(${Expr(fullName)})}
}

private val filePrefix = "//SOURCECODE_ORIGINAL_FILE_PATH="

private def findOriginalFile(chars: Array[Char]): Option[String] = {
new String(chars).linesIterator.find(_.contains(filePrefix)).map(_.split(filePrefix).last)
}
def fileImpl(using Quotes): Expr[sourcecode.File] = {
import quotes.reflect._
val file = quotes.reflect.Position.ofMacroExpansion.sourceFile.path
val file = quotes.reflect.Position.ofMacroExpansion.sourceFile.content
.flatMap(s => findOriginalFile(s.toCharArray))
.getOrElse(quotes.reflect.Position.ofMacroExpansion.sourceFile.path)
'{new sourcecode.File(${Expr(file)})}
}

def fileNameImpl(using Quotes): Expr[sourcecode.FileName] = {
val name = quotes.reflect.Position.ofMacroExpansion.sourceFile.name
val name = quotes.reflect.Position.ofMacroExpansion.sourceFile.content
.flatMap(s => findOriginalFile(s.toCharArray).map(_.split('/').last))
.getOrElse(quotes.reflect.Position.ofMacroExpansion.sourceFile.name)

'{new sourcecode.FileName(${Expr(name)})}
}

private val linePrefix = "//SOURCECODE_ORIGINAL_CODE_START_MARKER"
def lineImpl(using Quotes): Expr[sourcecode.Line] = {
val line = quotes.reflect.Position.ofMacroExpansion.startLine + 1
val offset = quotes.reflect.Position.ofMacroExpansion.sourceFile.content
.iterator
.flatMap(_.linesIterator)
.indexWhere(_.contains(linePrefix)) match{
case -1 => 0
case n => n
}
val line = quotes.reflect.Position.ofMacroExpansion.startLine + 1 - offset
'{new sourcecode.Line(${Expr(line)})}
}

Expand Down