@@ -159,45 +159,47 @@ object Macros {
159
159
}
160
160
161
161
private val filePrefix = " //SOURCECODE_ORIGINAL_FILE_PATH="
162
- private val filePrefixCache = new ConcurrentHashMap [Path , Option [String ]]()
163
- private def findOriginalFile (jpath : Path ): Option [String ] = {
164
- import collection .JavaConverters ._
165
- try Files .readAllLines(jpath).asScala.find(_.contains(filePrefix)).map(_.split(filePrefix).last)
166
- catch {case _ => None }
162
+ private val filePrefixCache = new ConcurrentHashMap [Any , Option [String ]]()
163
+ private def findOriginalFile (contents : Option [String ]): Option [String ] = {
164
+ contents
165
+ .iterator
166
+ .flatMap(_.linesIterator)
167
+ .find(_.contains(filePrefix))
168
+ .flatMap(_.split(filePrefix).lastOption)
167
169
}
170
+
168
171
def fileImpl (using Quotes ): Expr [sourcecode.File ] = {
169
172
import quotes .reflect ._
170
- val jpath = quotes.reflect.Position .ofMacroExpansion.sourceFile.jpath
171
- val file = filePrefixCache.computeIfAbsent(jpath, findOriginalFile(_ ))
172
- .getOrElse(quotes.reflect. Position .ofMacroExpansion. sourceFile.path)
173
+ val sourceFile = quotes.reflect.Position .ofMacroExpansion.sourceFile
174
+ val file = filePrefixCache.computeIfAbsent(sourceFile, _ => findOriginalFile(sourceFile.content ))
175
+ .getOrElse(sourceFile.path)
173
176
' {new sourcecode.File ($ {Expr (file)})}
174
177
}
175
178
176
179
def fileNameImpl (using Quotes ): Expr [sourcecode.FileName ] = {
177
- val jpath = quotes.reflect.Position .ofMacroExpansion.sourceFile.jpath
178
- val file = filePrefixCache.computeIfAbsent(jpath, findOriginalFile(_ ))
179
- .getOrElse(quotes.reflect. Position .ofMacroExpansion. sourceFile.path)
180
+ val sourceFile = quotes.reflect.Position .ofMacroExpansion.sourceFile
181
+ val file = filePrefixCache.computeIfAbsent(sourceFile, _ => findOriginalFile(sourceFile.content ))
182
+ .getOrElse(sourceFile.path)
180
183
181
184
val name = file.split('/' ).last
182
185
183
186
' {new sourcecode.FileName ($ {Expr (name)})}
184
187
}
185
188
186
189
private val linePrefix = " //SOURCECODE_ORIGINAL_CODE_START_MARKER"
187
- private val linePrefixCache = new ConcurrentHashMap [Path , Int ]()
188
- private def findLineNumber (jpath : Path ) = {
189
- import collection . JavaConverters . _
190
- println( Files .readAllLines(jpath).asScala.zipWithIndex)
191
- try Files .readAllLines(jpath).asScala
190
+ private val linePrefixCache = new ConcurrentHashMap [Any , Int ]()
191
+ private def findLineNumber (contents : Option [ String ] ) = {
192
+ contents
193
+ .iterator
194
+ .flatMap(_.linesIterator)
192
195
.indexWhere(_.contains(linePrefix)) match {
193
196
case - 1 => 0
194
197
case n => n + 1
195
198
}
196
- catch { _ => 0 }
197
199
}
198
200
def lineImpl (using Quotes ): Expr [sourcecode.Line ] = {
199
- val jpath = quotes.reflect.Position .ofMacroExpansion.sourceFile.jpath
200
- val offset = linePrefixCache.computeIfAbsent(jpath, findLineNumber(_ ))
201
+ val sourceFile = quotes.reflect.Position .ofMacroExpansion.sourceFile
202
+ val offset = linePrefixCache.computeIfAbsent(sourceFile, _ => findLineNumber(sourceFile.content ))
201
203
val line = quotes.reflect.Position .ofMacroExpansion.startLine + 1 - offset
202
204
' {new sourcecode.Line ($ {Expr (line)})}
203
205
}
0 commit comments