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