forked from scalameta/metals
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ImplementationProvider.scala
533 lines (486 loc) Β· 15.5 KB
/
ImplementationProvider.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
package scala.meta.internal.implementation
import java.nio.file.Path
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentLinkedQueue
import scala.collection.mutable
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.util.control.NonFatal
import scala.meta.internal.metals.Buffers
import scala.meta.internal.metals.BuildTargets
import scala.meta.internal.metals.DefinitionProvider
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.ReportContext
import scala.meta.internal.metals.ScalaVersionSelector
import scala.meta.internal.metals.SemanticdbFeatureProvider
import scala.meta.internal.mtags.GlobalSymbolIndex
import scala.meta.internal.mtags.Mtags
import scala.meta.internal.mtags.SemanticdbPath
import scala.meta.internal.mtags.Semanticdbs
import scala.meta.internal.mtags.SymbolDefinition
import scala.meta.internal.mtags.{Symbol => MSymbol}
import scala.meta.internal.parsing.Trees
import scala.meta.internal.semanticdb.ClassSignature
import scala.meta.internal.semanticdb.Scala._
import scala.meta.internal.semanticdb.Signature
import scala.meta.internal.semanticdb.SymbolInformation
import scala.meta.internal.semanticdb.SymbolOccurrence
import scala.meta.internal.semanticdb.TextDocument
import scala.meta.internal.semanticdb.TextDocuments
import scala.meta.internal.semanticdb.TypeRef
import scala.meta.internal.semanticdb.TypeSignature
import scala.meta.internal.symtab.GlobalSymbolTable
import scala.meta.io.AbsolutePath
import org.eclipse.lsp4j.Location
import org.eclipse.lsp4j.TextDocumentPositionParams
final class ImplementationProvider(
semanticdbs: Semanticdbs,
workspace: AbsolutePath,
index: GlobalSymbolIndex,
buildTargets: BuildTargets,
buffer: Buffers,
definitionProvider: DefinitionProvider,
trees: Trees,
scalaVersionSelector: ScalaVersionSelector,
)(implicit ec: ExecutionContext, rc: ReportContext)
extends SemanticdbFeatureProvider {
import ImplementationProvider._
private val globalTable = new GlobalClassTable(buildTargets)
private val implementationsInPath =
new ConcurrentHashMap[Path, Map[String, Set[ClassLocation]]]
override def reset(): Unit = {
implementationsInPath.clear()
}
override def onDelete(path: SemanticdbPath): Unit = {
implementationsInPath.remove(path.toNIO)
}
override def onChange(docs: TextDocuments, path: AbsolutePath): Unit = {
if (!path.isJarFileSystem)
implementationsInPath.compute(
path.toNIO,
{ (_, _) => computeInheritance(docs) },
)
}
private def computeInheritance(
documents: TextDocuments
): Map[String, Set[ClassLocation]] = {
val parentImplLocationPairs =
new mutable.ListBuffer[(String, ClassLocation)]
for {
document <- documents.documents
symbolInfo <- document.symbols
} {
if (isClassLike(symbolInfo)) {
parentImplLocationPairs ++= parentsFromSignature(
symbolInfo.symbol,
symbolInfo.signature,
Some(workspace.resolve(document.uri)),
)
}
}
parentImplLocationPairs.groupBy(_._1).map { case (symbol, locations) =>
symbol -> locations.map(_._2).toSet
}
}
def defaultSymbolSearch(
anyWorkspacePath: AbsolutePath,
textDocument: TextDocument,
): String => Option[SymbolInformation] = {
lazy val global =
globalTable.globalSymbolTableFor(anyWorkspacePath)
symbol => {
textDocument.symbols
.find(_.symbol == symbol)
.orElse(findSymbolInformation(symbol))
.orElse(global.flatMap(_.safeInfo(symbol)))
}
}
def implementations(
params: TextDocumentPositionParams
): Future[List[Location]] = {
val source = params.getTextDocument.getUri.toAbsolutePath
val locations = for {
(symbolOccurrence, currentDocument) <-
definitionProvider
.symbolOccurrence(
source,
params.getPosition,
)
.toIterable
} yield {
// 1. Search locally for symbol
// 2. Search inside workspace
// 3. Search classpath via GlobalSymbolTable
val symbolSearch = defaultSymbolSearch(source, currentDocument)
val sym = symbolOccurrence.symbol
val dealiased =
if (sym.desc.isType) dealiasClass(sym, symbolSearch) else sym
val definitionDocument =
if (currentDocument.definesSymbol(dealiased)) {
Some(currentDocument)
} else {
findSemanticDbForSymbol(dealiased)
}
val inheritanceContext = definitionDocument match {
// symbol is not in workspace, we only search classpath for it
case None =>
globalTable.globalContextFor(
source,
implementationsInPath.asScala.toMap,
)
// symbol is in workspace,
// we might need to search different places for related symbols
case Some(_) =>
Some(
InheritanceContext.fromDefinitions(
symbolSearch,
implementationsInPath.asScala.toMap,
)
)
}
symbolLocationsFromContext(
dealiased,
source,
inheritanceContext,
)
}
Future.sequence(locations).map {
_.flatten.toList
}
}
def topMethodParents(
symbol: String,
textDocument: TextDocument,
): Seq[Location] = {
def findClassInfo(owner: String) = {
if (owner.nonEmpty) {
findSymbol(textDocument, owner)
} else {
textDocument.symbols.find { sym =>
sym.signature match {
case sig: ClassSignature =>
sig.declarations.exists(_.symlinks.contains(symbol))
case _ => false
}
}
}
}
val results = for {
currentInfo <- findSymbol(textDocument, symbol)
if !isClassLike(currentInfo)
classInfo <- findClassInfo(symbol.owner)
} yield {
classInfo.signature match {
case sig: ClassSignature =>
methodInParentSignature(sig, currentInfo, sig)
case _ => Nil
}
}
results.getOrElse(Seq.empty)
}
private def methodInParentSignature(
currentClassSig: ClassSignature,
bottomSymbol: SymbolInformation,
bottomClassSig: ClassSignature,
): Seq[Location] = {
currentClassSig.parents.flatMap {
case parentSym: TypeRef =>
val parentTextDocument = findSemanticDbForSymbol(parentSym.symbol)
def search(symbol: String) =
parentTextDocument.flatMap(findSymbol(_, symbol))
search(parentSym.symbol).map(_.signature) match {
case Some(parenClassSig: ClassSignature) =>
val fromParent = methodInParentSignature(
parenClassSig,
bottomSymbol,
bottomClassSig,
)
if (fromParent.isEmpty) {
locationFromClass(
bottomSymbol,
parenClassSig,
search,
parentTextDocument,
)
} else {
fromParent
}
case _ => Nil
}
case _ => Nil
}
}
private def locationFromClass(
bottomSymbolInformation: SymbolInformation,
parentClassSig: ClassSignature,
search: String => Option[SymbolInformation],
parentTextDocument: Option[TextDocument],
): Option[Location] = {
val matchingSymbol = MethodImplementation.findParentSymbol(
bottomSymbolInformation,
parentClassSig,
search,
)
for {
symbol <- matchingSymbol
parentDoc <- parentTextDocument
source = workspace.resolve(parentDoc.uri)
implOccurrence <- findDefOccurrence(
parentDoc,
symbol,
source,
)
range <- implOccurrence.range
distance = buffer.tokenEditDistance(
source,
parentDoc.text,
trees,
)
revised <- distance.toRevised(range.toLsp)
} yield new Location(source.toNIO.toUri().toString(), revised)
}
private def symbolLocationsFromContext(
symbol: String,
source: AbsolutePath,
inheritanceContext: Option[InheritanceContext],
): Future[Seq[Location]] = {
def findImplementationSymbol(
parentSymbolInfo: SymbolInformation,
implDocument: TextDocument,
implReal: ClassLocation,
): Option[String] = {
if (isClassLike(parentSymbolInfo))
Some(implReal.symbol)
else {
val symbolSearch = defaultSymbolSearch(source, implDocument)
MethodImplementation.findInherited(
parentSymbolInfo,
implReal,
symbolSearch,
)
}
}
val allLocations = new ConcurrentLinkedQueue[Location]
def findImplementationLocations(
files: Set[Path],
locationsByFile: Map[Path, Set[ClassLocation]],
parentSymbol: SymbolInformation,
) =
Future {
for {
file <- files
locations = locationsByFile(file)
implPath = AbsolutePath(file)
implDocument <- findSemanticdb(implPath).toIterable
distance = buffer.tokenEditDistance(
implPath,
implDocument.text,
trees,
)
implLocation <- locations
implSymbol <- findImplementationSymbol(
parentSymbol,
implDocument,
implLocation,
)
if !findSymbol(implDocument, implSymbol).exists(
_.kind == SymbolInformation.Kind.TYPE
)
implOccurrence <- findDefOccurrence(
implDocument,
implSymbol,
source,
)
range <- implOccurrence.range
revised <- distance.toRevised(range.toLsp)
} { allLocations.add(new Location(file.toUri.toString, revised)) }
}
lazy val cores = Runtime.getRuntime().availableProcessors()
val splitJobs = for {
classContext <- inheritanceContext.toIterable
parentSymbol <- classContext.findSymbol(symbol).toIterable
symbolClass <- classFromSymbol(parentSymbol, classContext.findSymbol)
locationsByFile = findImplementation(
symbolClass.symbol,
classContext,
source.toNIO,
)
files <- locationsByFile.keySet.grouped(
Math.max(locationsByFile.size / cores, 1)
)
} yield findImplementationLocations(files, locationsByFile, parentSymbol)
Future.sequence(splitJobs).map { _ =>
allLocations.asScala.toSeq
}
}
private def findSemanticdb(fileSource: AbsolutePath): Option[TextDocument] = {
if (fileSource.isJarFileSystem)
None
else
semanticdbs
.textDocument(fileSource)
.documentIncludingStale
}
private def findImplementation(
symbol: String,
classContext: InheritanceContext,
file: Path,
): Map[Path, Set[ClassLocation]] = {
def loop(symbol: String, currentPath: Option[Path]): Set[ClassLocation] = {
val directImplementations =
classContext.getLocations(symbol).filterNot { loc =>
// we are not interested in local symbols from outside the workspace
(loc.symbol.isLocal && loc.file.isEmpty) ||
// for local symbols, inheritance should only be picked up in the same file
// otherwise there can be a name collision between files
// local1' is from file A, local2 extends local1''
// but both local2 and local1'' are from file B
// clearly, local2 shouldn't be considered for local1'
(symbol.isLocal && loc.symbol.isLocal && loc.file != currentPath)
}
directImplementations ++ directImplementations
.flatMap { loc => loop(loc.symbol, loc.file) }
}
loop(symbol, Some(file)).groupBy(_.file).collect {
case (Some(path), locs) =>
path -> locs
}
}
private def findSymbolInformation(
symbol: String
): Option[SymbolInformation] = {
findSemanticDbForSymbol(symbol).flatMap(findSymbol(_, symbol))
}
def findSemanticDbWithPathForSymbol(
symbol: String
): Option[TextDocumentWithPath] = {
for {
symbolDefinition <- findSymbolDefinition(symbol)
document <- findSemanticdb(symbolDefinition.path)
} yield TextDocumentWithPath(document, symbolDefinition.path)
}
private def findSymbolDefinition(symbol: String): Option[SymbolDefinition] = {
index.definition(MSymbol(symbol))
}
private def findSemanticDbForSymbol(symbol: String): Option[TextDocument] = {
for {
symbolDefinition <- findSymbolDefinition(symbol)
document <- findSemanticdb(symbolDefinition.path)
} yield {
document
}
}
private def classFromSymbol(
info: SymbolInformation,
findSymbol: String => Option[SymbolInformation],
): Iterable[SymbolInformation] = {
val classInfo = if (isClassLike(info)) {
Some(info)
} else {
findSymbol(info.symbol.owner)
.filter(info => isClassLike(info))
}
classInfo.map(inf => dealiasClass(inf, findSymbol))
}
private def findDefOccurrence(
semanticDb: TextDocument,
symbol: String,
source: AbsolutePath,
): Option[SymbolOccurrence] = {
def isDefinitionOccurrence(occ: SymbolOccurrence) =
occ.role.isDefinition && occ.symbol == symbol
semanticDb.occurrences
.find(isDefinitionOccurrence)
.orElse(
Mtags
.allToplevels(source.toInput, scalaVersionSelector.getDialect(source))
.occurrences
.find(isDefinitionOccurrence)
)
}
}
object ImplementationProvider {
implicit class XtensionGlobalSymbolTable(symtab: GlobalSymbolTable) {
def safeInfo(symbol: String): Option[SymbolInformation] =
try {
symtab.info(symbol)
} catch {
case NonFatal(_) => None
}
}
def dealiasClass(
symbol: String,
findSymbol: String => Option[SymbolInformation],
): String = {
if (symbol.desc.isType) {
findSymbol(symbol)
.map(inf => dealiasClass(inf, findSymbol).symbol)
.getOrElse(symbol)
} else {
symbol
}
}
def dealiasClass(
info: SymbolInformation,
findSymbol: String => Option[SymbolInformation],
): SymbolInformation = {
if (info.isType) {
info.signature match {
case ts: TypeSignature =>
ts.upperBound match {
case tr: TypeRef =>
findSymbol(tr.symbol)
.map(dealiasClass(_, findSymbol))
.getOrElse(info)
case _ =>
info
}
case _ => info
}
} else {
info
}
}
private def findSymbol(
semanticDb: TextDocument,
symbol: String,
): Option[SymbolInformation] = {
semanticDb.symbols
.find(sym => sym.symbol == symbol)
}
def parentsFromSignature(
symbol: String,
signature: Signature,
filePath: Option[AbsolutePath],
): Seq[(String, ClassLocation)] = {
def fromClassSignature(
classSig: ClassSignature
): Seq[(String, ClassLocation)] = {
classSig.parents.collect { case t: TypeRef =>
t.symbol -> ClassLocation(symbol, filePath.map(_.toNIO))
}
}
def fromTypeSignature(typeSig: TypeSignature) = {
typeSig.upperBound match {
case tr: TypeRef =>
Seq(
tr.symbol -> ClassLocation(
symbol,
filePath.map(_.toNIO),
)
)
case _ => Seq.empty
}
}
signature match {
case classSig: ClassSignature =>
fromClassSignature(classSig)
case ts: TypeSignature =>
fromTypeSignature(ts)
case _ =>
Seq.empty
}
}
def isClassLike(info: SymbolInformation): Boolean =
info.isObject || info.isClass || info.isTrait || info.isType || info.isInterface
}