/
BuildTargetClasses.scala
246 lines (219 loc) Β· 7.33 KB
/
BuildTargetClasses.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package scala.meta.internal.metals.debug
import scala.collection.concurrent.TrieMap
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.meta.internal.metals.BatchedFunction
import scala.meta.internal.metals.BuildTargets
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.debug.BuildTargetClasses.Classes
import scala.meta.internal.semanticdb.Scala.Descriptor
import scala.meta.internal.semanticdb.Scala.Symbols
import ch.epfl.scala.{bsp4j => b}
/**
* In-memory index of main class symbols grouped by their enclosing build target
*/
final class BuildTargetClasses(
buildTargets: BuildTargets
)(implicit val ec: ExecutionContext) {
private val index = TrieMap.empty[b.BuildTargetIdentifier, Classes]
private val jvmRunEnvironments
: TrieMap[b.BuildTargetIdentifier, b.JvmEnvironmentItem] =
TrieMap.empty[b.BuildTargetIdentifier, b.JvmEnvironmentItem]
val rebuildIndex: BatchedFunction[b.BuildTargetIdentifier, Unit] =
BatchedFunction.fromFuture(
fetchClasses,
"buildTargetClasses",
default = Some(()),
)
def classesOf(target: b.BuildTargetIdentifier): Classes = {
index.getOrElse(target, new Classes)
}
def invalidate(target: b.BuildTargetIdentifier): Unit = {
index.put(target, new Classes)
}
def clear(): Unit = {
jvmRunEnvironments.clear()
}
def findMainClassByName(
name: String
): List[(b.ScalaMainClass, b.BuildTargetIdentifier)] =
findClassesBy(_.mainClasses.values.find(_.getClassName() == name))
def findTestClassByName(
name: String
): List[(String, b.BuildTargetIdentifier)] =
findClassesBy(
_.testClasses.values
.find(_.fullyQualifiedName == name)
.map(_.fullyQualifiedName)
)
private def findClassesBy[A](
f: Classes => Option[A]
): List[(A, b.BuildTargetIdentifier)] = {
index
.mapValues(f)
.toList
.collect { case (target, Some(clazz)) =>
clazz -> target
}
}
private def fetchClasses(
targets: Seq[b.BuildTargetIdentifier]
): Future[Unit] = {
val distinctTargets = targets.distinct
Future
.traverse(distinctTargets.groupBy(buildTargets.buildServerOf).toSeq) {
case (None, _) =>
Future.successful(())
case (Some(connection), targets0) =>
val targetsList = targets0.asJava
targetsList.forEach(invalidate)
val classes = targets0.map(t => (t, new Classes)).toMap
val updateMainClasses = connection
.mainClasses(new b.ScalaMainClassesParams(targetsList))
.map(cacheMainClasses(classes, _))
// Currently tests are only run using DAP
val updateTestClasses =
if (connection.isDebuggingProvider || connection.isSbt)
connection
.testClasses(new b.ScalaTestClassesParams(targetsList))
.map(cacheTestClasses(classes, _))
else Future.unit
for {
_ <- updateMainClasses
_ <- updateTestClasses
} yield {
classes.foreach { case (id, classes) =>
index.put(id, classes)
}
}
}
.ignoreValue
}
def jvmRunEnvironmentSync(
buildTargetId: b.BuildTargetIdentifier
): Option[b.JvmEnvironmentItem] = jvmRunEnvironments.get(buildTargetId)
def jvmRunEnvironment(
buildTargetId: b.BuildTargetIdentifier
): Future[Option[b.JvmEnvironmentItem]] = {
jvmRunEnvironments.get(buildTargetId) match {
case None =>
buildTargets.buildServerOf(buildTargetId) match {
case None => Future.successful(None)
case Some(connection) =>
connection
.jvmRunEnvironment(
new b.JvmRunEnvironmentParams(List(buildTargetId).asJava)
)
.map { env =>
cacheJvmRunEnvironment(env)
env.getItems().asScala.headOption
}
}
case jvmRunEnv: Some[b.JvmEnvironmentItem] =>
Future.successful(jvmRunEnv)
}
}
private def cacheJvmRunEnvironment(
result: b.JvmRunEnvironmentResult
): Unit = {
for {
item <- result.getItems().asScala
target = item.getTarget
} {
jvmRunEnvironments.put(target, item)
}
}
private def cacheMainClasses(
classes: Map[b.BuildTargetIdentifier, Classes],
result: b.ScalaMainClassesResult,
): Unit = {
for {
item <- result.getItems.asScala
target = item.getTarget
aClass <- item.getClasses.asScala
descriptors = descriptorsForMainClasses(target)
symbol <- symbolFromClassName(
aClass.getClassName,
descriptors,
)
} {
classes(target).mainClasses.put(symbol, aClass)
}
}
private def cacheTestClasses(
classes: Map[b.BuildTargetIdentifier, Classes],
result: b.ScalaTestClassesResult,
): Unit = {
for {
item <- result.getItems.asScala
target = item.getTarget
className <- item.getClasses.asScala
symbol <-
symbolFromClassName(className, List(Descriptor.Term, Descriptor.Type))
} {
// item.getFramework() can return null!
val framework = TestFramework(Option(item.getFramework()))
val testInfo = BuildTargetClasses.TestSymbolInfo(className, framework)
classes(target).testClasses.put(symbol, testInfo)
}
}
private def descriptorsForMainClasses(
buildTarget: b.BuildTargetIdentifier
): List[String => Descriptor] = {
buildTargets.scalaTarget(buildTarget) match {
case Some(_) =>
List(Descriptor.Term)
case None =>
List(Descriptor.Type)
}
}
def symbolFromClassName(
className: String,
descriptors: List[String => Descriptor],
): List[String] = {
import scala.reflect.NameTransformer
val isEmptyPackage = !className.contains(".")
val root =
if (isEmptyPackage) Symbols.EmptyPackage
else Symbols.RootPackage
val names = className.stripSuffix("$").split("\\.")
val prefix = names.dropRight(1).foldLeft(root) { (owner, name) =>
Symbols.Global(owner, Descriptor.Package(NameTransformer.decode(name)))
}
val name = NameTransformer.decode(names.last)
descriptors.map(descriptor => Symbols.Global(prefix, descriptor(name)))
}
def cancel(): Unit = {
rebuildIndex.cancelAll()
}
}
sealed abstract class TestFramework(val canResolveChildren: Boolean)
object TestFramework {
def apply(framework: Option[String]): TestFramework = framework
.map {
case "JUnit" => JUnit4
case "munit" => MUnit
case "ScalaTest" => Scalatest
case "weaver-cats-effect" => WeaverCatsEffect
case _ => Unknown
}
.getOrElse(Unknown)
}
case object JUnit4 extends TestFramework(true)
case object MUnit extends TestFramework(true)
case object Scalatest extends TestFramework(true)
case object WeaverCatsEffect extends TestFramework(true)
case object Unknown extends TestFramework(false)
object BuildTargetClasses {
type Symbol = String
type FullyQualifiedClassName = String
final case class TestSymbolInfo(
fullyQualifiedName: FullyQualifiedClassName,
framework: TestFramework,
)
final class Classes {
val mainClasses = new TrieMap[Symbol, b.ScalaMainClass]()
val testClasses = new TrieMap[Symbol, TestSymbolInfo]()
def isEmpty: Boolean = mainClasses.isEmpty && testClasses.isEmpty
}
}