|
1 | 1 | package sourcecode |
2 | 2 |
|
3 | 3 | import java.util.concurrent.ConcurrentHashMap |
| 4 | +import java.nio.file.{Files, Path} |
4 | 5 | import scala.language.implicitConversions |
5 | 6 | import scala.quoted.* |
6 | 7 |
|
@@ -158,48 +159,45 @@ object Macros { |
158 | 159 | } |
159 | 160 |
|
160 | 161 | private val filePrefix = "//SOURCECODE_ORIGINAL_FILE_PATH=" |
161 | | - private val filePrefixCache = |
162 | | - new ConcurrentHashMap[Any, Option[String]]() |
163 | | - private def findOriginalFile(chars: Array[Char]): Option[String] = { |
164 | | - new String(chars).linesIterator.find(_.contains(filePrefix)).map(_.split(filePrefix).last) |
| 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} |
165 | 167 | } |
166 | 168 | def fileImpl(using Quotes): Expr[sourcecode.File] = { |
167 | 169 | import quotes.reflect._ |
168 | | - val file = filePrefixCache.computeIfAbsent( |
169 | | - quotes.reflect.Position.ofMacroExpansion.sourceFile, |
170 | | - _ => quotes.reflect.Position.ofMacroExpansion.sourceFile.content |
171 | | - .flatMap(s => findOriginalFile(s.toCharArray)) |
172 | | - ) |
| 170 | + val jpath = quotes.reflect.Position.ofMacroExpansion.sourceFile.jpath |
| 171 | + val file = filePrefixCache.computeIfAbsent(jpath, findOriginalFile(_)) |
173 | 172 | .getOrElse(quotes.reflect.Position.ofMacroExpansion.sourceFile.path) |
174 | 173 | '{new sourcecode.File(${Expr(file)})} |
175 | 174 | } |
176 | 175 |
|
177 | 176 | def fileNameImpl(using Quotes): Expr[sourcecode.FileName] = { |
178 | | - val name = filePrefixCache.computeIfAbsent( |
179 | | - quotes.reflect.Position.ofMacroExpansion.sourceFile, |
180 | | - _ => quotes.reflect.Position.ofMacroExpansion.sourceFile.content |
181 | | - .flatMap(s => findOriginalFile(s.toCharArray).map(_.split('/').last) |
182 | | - ).flatMap(s => findOriginalFile(s.toCharArray))) |
183 | | - .getOrElse(quotes.reflect.Position.ofMacroExpansion.sourceFile.name) |
| 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 | + |
| 181 | + val name = file.split('/').last |
184 | 182 |
|
185 | 183 | '{new sourcecode.FileName(${Expr(name)})} |
186 | 184 | } |
187 | 185 |
|
188 | 186 | private val linePrefix = "//SOURCECODE_ORIGINAL_CODE_START_MARKER" |
189 | | - private val linePrefixCache = |
190 | | - new ConcurrentHashMap[Any, Int]() |
| 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 |
| 192 | + .indexWhere(_.contains(linePrefix)) match { |
| 193 | + case -1 => 0 |
| 194 | + case n => n + 1 |
| 195 | + } |
| 196 | + catch { _ => 0 } |
| 197 | + } |
191 | 198 | def lineImpl(using Quotes): Expr[sourcecode.Line] = { |
192 | | - val offset = linePrefixCache.computeIfAbsent( |
193 | | - quotes.reflect.Position.ofMacroExpansion.sourceFile, |
194 | | - _ => |
195 | | - quotes.reflect.Position.ofMacroExpansion.sourceFile.content |
196 | | - .iterator |
197 | | - .flatMap(_.linesIterator) |
198 | | - .indexWhere(_.contains(linePrefix)) match{ |
199 | | - case -1 => 0 |
200 | | - case n => n |
201 | | - } |
202 | | - ) |
| 199 | + val jpath = quotes.reflect.Position.ofMacroExpansion.sourceFile.jpath |
| 200 | + val offset = linePrefixCache.computeIfAbsent(jpath, findLineNumber(_)) |
203 | 201 | val line = quotes.reflect.Position.ofMacroExpansion.startLine + 1 - offset |
204 | 202 | '{new sourcecode.Line(${Expr(line)})} |
205 | 203 | } |
|
0 commit comments