@@ -156,19 +156,37 @@ object Macros {
156156 ' {new FullName .Machine ($ {Expr (fullName)})}
157157 }
158158
159+ private val filePrefix = " //SOURCECODE_ORIGINAL_FILE_PATH="
160+
161+ private def findOriginalFile (chars : Array [Char ]): Option [String ] = {
162+ new String (chars).linesIterator.find(_.contains(filePrefix)).map(_.split(filePrefix).last)
163+ }
159164 def fileImpl (using Quotes ): Expr [sourcecode.File ] = {
160165 import quotes .reflect ._
161- val file = quotes.reflect.Position .ofMacroExpansion.sourceFile.path
166+ val file = quotes.reflect.Position .ofMacroExpansion.sourceFile.content
167+ .flatMap(s => findOriginalFile(s.toCharArray))
168+ .getOrElse(quotes.reflect.Position .ofMacroExpansion.sourceFile.path)
162169 ' {new sourcecode.File ($ {Expr (file)})}
163170 }
164171
165172 def fileNameImpl (using Quotes ): Expr [sourcecode.FileName ] = {
166- val name = quotes.reflect.Position .ofMacroExpansion.sourceFile.name
173+ val name = quotes.reflect.Position .ofMacroExpansion.sourceFile.content
174+ .flatMap(s => findOriginalFile(s.toCharArray).map(_.split('/' ).last))
175+ .getOrElse(quotes.reflect.Position .ofMacroExpansion.sourceFile.name)
176+
167177 ' {new sourcecode.FileName ($ {Expr (name)})}
168178 }
169179
180+ private val linePrefix = " //SOURCECODE_ORIGINAL_CODE_START_MARKER"
170181 def lineImpl (using Quotes ): Expr [sourcecode.Line ] = {
171- val line = quotes.reflect.Position .ofMacroExpansion.startLine + 1
182+ val offset = quotes.reflect.Position .ofMacroExpansion.sourceFile.content
183+ .iterator
184+ .flatMap(_.linesIterator)
185+ .indexWhere(_.contains(linePrefix)) match {
186+ case - 1 => 0
187+ case n => n
188+ }
189+ val line = quotes.reflect.Position .ofMacroExpansion.startLine + 1 - offset
172190 ' {new sourcecode.Line ($ {Expr (line)})}
173191 }
174192
0 commit comments