Skip to content

Commit e04d3df

Browse files
committed
cache based on path rather than sourcefile
1 parent 2d5e9e1 commit e04d3df

File tree

5 files changed

+48
-31
lines changed

5 files changed

+48
-31
lines changed

build.mill

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import mill.scalalib.api.ZincWorkerUtil.isScala3
1010
val dottyCommunityBuildVersion = sys.props.get("dottyVersion").toList
1111

1212
val scalaVersions =
13-
"2.12.16" :: "2.13.8" :: "3.3.1" :: dottyCommunityBuildVersion
13+
"2.12.16" :: "2.13.8" :: "3.3.3" :: dottyCommunityBuildVersion
1414

1515
trait MimaCheck extends Mima {
1616
def mimaPreviousVersions = Seq("0.2.4", "0.2.5", "0.2.6", "0.2.7", "0.2.8", "0.3.0", "0.3.1")
@@ -65,7 +65,7 @@ object sourcecode extends Module {
6565
object js extends Cross[JsSourcecodeModule](scalaVersions)
6666
trait JsSourcecodeModule extends SourcecodeMainModule with ScalaJSModule with SourcecodeModule {
6767

68-
def scalaJSVersion = "1.12.0"
68+
def scalaJSVersion = "1.17.0"
6969
object test extends ScalaJSTests{
7070
def testFramework = ""
7171
}

sourcecode/src-2/sourcecode/Macros.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ object Macros {
133133
.linesIterator
134134
.indexWhere(_.contains(linePrefix)) match{
135135
case -1 => 0
136-
case n => n
136+
case n => n + 1
137137
}
138138
)
139139

sourcecode/src-3/sourcecode/Macros.scala

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sourcecode
22

33
import java.util.concurrent.ConcurrentHashMap
4+
import java.nio.file.{Files, Path}
45
import scala.language.implicitConversions
56
import scala.quoted.*
67

@@ -158,48 +159,45 @@ object Macros {
158159
}
159160

160161
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}
165167
}
166168
def fileImpl(using Quotes): Expr[sourcecode.File] = {
167169
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(_))
173172
.getOrElse(quotes.reflect.Position.ofMacroExpansion.sourceFile.path)
174173
'{new sourcecode.File(${Expr(file)})}
175174
}
176175

177176
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
184182

185183
'{new sourcecode.FileName(${Expr(name)})}
186184
}
187185

188186
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+
}
191198
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(_))
203201
val line = quotes.reflect.Position.ofMacroExpansion.startLine + 1 - offset
204202
'{new sourcecode.Line(${Expr(line)})}
205203
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package sourcecode
2+
3+
object FileNameOffset {
4+
def main() = {
5+
//SOURCECODE_ORIGINAL_FILE_PATH=Hello/World.Test
6+
7+
//SOURCECODE_ORIGINAL_CODE_START_MARKER
8+
assert(sourcecode.Line() == 1)
9+
assert(sourcecode.Line() == 2)
10+
assert(sourcecode.File() == "Hello/World.Test")
11+
assert(sourcecode.FileName() == "World.Test")
12+
}
13+
}
14+
15+
16+
17+
18+

sourcecode/test/src/sourcecode/Tests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ object Tests{
2222
ManualImplicit()
2323
TextTests()
2424
ArgsTests()
25+
FileNameOffset.main()
2526

2627
println("================LogExample================")
2728
logExample()

0 commit comments

Comments
 (0)