Skip to content

Commit 553e2b3

Browse files
committed
[experiment] currentCtx should return the context for the last ran unit
1 parent 569d236 commit 553e2b3

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

mtags/src/main/scala-3/scala/meta/internal/pc/MetalsDriver.scala

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,40 @@ class MetalsDriver(
1717
override val settings: List[String]
1818
) extends InteractiveDriver(settings):
1919

20+
private val contextCache: TrieMap[URI, Context] = TrieMap.empty
21+
private var lastCompiledURI: Option[URI] = None
22+
2023
private def alreadyCompiled(uri: URI, content: Array[Char]): Boolean =
2124
compilationUnits.get(uri) match
2225
case Some(unit) if ju.Arrays.equals(unit.source.content(), content) =>
2326
true
2427
case _ => false
2528

29+
override def currentCtx: Context =
30+
(for
31+
uri <-
32+
// lastCompiledURI is initialized as null (!?), it might be a bug in scala3
33+
if lastCompiledURI != null then lastCompiledURI
34+
else None
35+
ctx <- contextCache.get(uri)
36+
yield ctx) match
37+
case Some(ctx) => ctx
38+
case None => super.currentCtx
39+
2640
override def run(uri: URI, source: SourceFile): List[Diagnostic] =
41+
lastCompiledURI = Some(uri)
2742
if alreadyCompiled(uri, source.content) then Nil
28-
else super.run(uri, source)
43+
else
44+
val diags = super.run(uri, source)
45+
contextCache.put(uri, currentCtx)
46+
diags
2947

3048
override def run(uri: URI, sourceCode: String): List[Diagnostic] =
31-
val contentHash = MD5.compute(sourceCode)
49+
lastCompiledURI = Some(uri)
3250
if alreadyCompiled(uri, sourceCode.toCharArray()) then Nil
33-
else super.run(uri, sourceCode)
51+
else
52+
val diags = super.run(uri, sourceCode)
53+
contextCache.put(uri, currentCtx)
54+
diags
3455

3556
end MetalsDriver

0 commit comments

Comments
 (0)