Skip to content

Commit 6dd594e

Browse files
authored
Make sourcecode.{File,FileName,Line} magic-comment aware (#178)
User-land implementation of scala/scala3#21919
1 parent 2f5403a commit 6dd594e

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

sourcecode/src-2/sourcecode/Macros.scala

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,38 @@ object Macros {
9494
c.Expr[FullName.Machine](q"""${c.prefix}($fullName)""")
9595
}
9696

97+
private val filePrefix = "//SOURCECODE_ORIGINAL_FILE_PATH="
98+
private def findOriginalFile(chars: Array[Char]): Option[String] = {
99+
new String(chars).linesIterator.find(_.contains(filePrefix)).map(_.split(filePrefix).last)
100+
}
101+
97102
def fileImpl(c: Compat.Context): c.Expr[sourcecode.File] = {
98103
import c.universe._
99-
val file = c.enclosingPosition.source.path
104+
105+
val file = findOriginalFile(c.enclosingPosition.source.content)
106+
.getOrElse(c.enclosingPosition.source.path)
107+
100108
c.Expr[sourcecode.File](q"""${c.prefix}($file)""")
101109
}
102110

103111
def fileNameImpl(c: Compat.Context): c.Expr[sourcecode.FileName] = {
104112
import c.universe._
105-
val fileName = c.enclosingPosition.source.path.split('/').last
113+
val fileName = findOriginalFile(c.enclosingPosition.source.content)
114+
.getOrElse(c.enclosingPosition.source.path)
115+
.split('/').last
106116
c.Expr[sourcecode.FileName](q"""${c.prefix}($fileName)""")
107117
}
108118

119+
private val linePrefix = "//SOURCECODE_ORIGINAL_CODE_START_MARKER"
109120
def lineImpl(c: Compat.Context): c.Expr[sourcecode.Line] = {
110121
import c.universe._
111-
val line = c.enclosingPosition.line
122+
val offset = new String(c.enclosingPosition.source.content)
123+
.linesIterator
124+
.indexWhere(_.contains(linePrefix)) match{
125+
case -1 => 0
126+
case n => n
127+
}
128+
val line = c.enclosingPosition.line - offset
112129
c.Expr[sourcecode.Line](q"""${c.prefix}($line)""")
113130
}
114131

sourcecode/src-3/sourcecode/Macros.scala

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)