Skip to content

Commit 95d0433

Browse files
committed
try to use sourceFile.content again
1 parent e04d3df commit 95d0433

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

sourcecode/src-3/sourcecode/Macros.scala

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -159,45 +159,47 @@ object Macros {
159159
}
160160

161161
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)
167169
}
170+
168171
def fileImpl(using Quotes): Expr[sourcecode.File] = {
169172
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)
173176
'{new sourcecode.File(${Expr(file)})}
174177
}
175178

176179
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)
180183

181184
val name = file.split('/').last
182185

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

186189
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)
192195
.indexWhere(_.contains(linePrefix)) match {
193196
case -1 => 0
194197
case n => n + 1
195198
}
196-
catch { _ => 0 }
197199
}
198200
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))
201203
val line = quotes.reflect.Position.ofMacroExpansion.startLine + 1 - offset
202204
'{new sourcecode.Line(${Expr(line)})}
203205
}

0 commit comments

Comments
 (0)