forked from scalameta/metals
-
Notifications
You must be signed in to change notification settings - Fork 2
/
WorkspaceSymbolProvider.scala
221 lines (201 loc) Β· 6.44 KB
/
WorkspaceSymbolProvider.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
package scala.meta.internal.metals
import java.nio.file.Files
import java.nio.file.Path
import scala.collection.concurrent.TrieMap
import scala.util.control.NonFatal
import scala.meta.internal.mtags.GlobalSymbolIndex
import scala.meta.internal.pc.InterruptException
import scala.meta.io.AbsolutePath
import scala.meta.pc.CancelToken
import scala.meta.pc.SymbolSearch
import scala.meta.pc.SymbolSearchVisitor
import ch.epfl.scala.bsp4j.BuildTargetIdentifier
import org.eclipse.lsp4j.jsonrpc.CancelChecker
import org.eclipse.{lsp4j => l}
/**
* Implements workspace/symbol for both workspace sources and dependency classpath.
*/
final class WorkspaceSymbolProvider(
val workspace: AbsolutePath,
val buildTargets: BuildTargets,
val index: GlobalSymbolIndex,
saveClassFileToDisk: Boolean,
excludedPackageHandler: () => ExcludedPackagesHandler,
bucketSize: Int = CompressedPackageIndex.DefaultBucketSize,
classpathSearchIndexer: ClasspathSearch.Indexer =
ClasspathSearch.Indexer.default,
)(implicit rc: ReportContext) {
val inWorkspace: TrieMap[Path, WorkspaceSymbolsIndex] =
TrieMap.empty[Path, WorkspaceSymbolsIndex]
// symbols for extension methods
val inWorkspaceMethods: TrieMap[Path, Seq[WorkspaceSymbolInformation]] =
TrieMap.empty[Path, Seq[WorkspaceSymbolInformation]]
var inDependencies: ClasspathSearch =
ClasspathSearch.empty
def search(query: String): Seq[l.SymbolInformation] = {
search(query, () => ())
}
def search(query: String, token: CancelChecker): Seq[l.SymbolInformation] = {
if (query.isEmpty) return Nil
try {
searchUnsafe(query, token)
} catch {
case InterruptException() =>
Nil
}
}
def searchExactFrom(
queryString: String,
path: AbsolutePath,
token: CancelToken,
): Seq[l.SymbolInformation] = {
val query = WorkspaceSymbolQuery.exact(queryString)
val visistor =
new WorkspaceSearchVisitor(
workspace,
query,
token,
index,
saveClassFileToDisk,
)
val targetId = buildTargets.inverseSources(path)
search(query, visistor, targetId)
visistor.allResults().filter(_.getName() == queryString)
}
def search(
query: WorkspaceSymbolQuery,
visitor: SymbolSearchVisitor,
target: Option[BuildTargetIdentifier],
): SymbolSearch.Result = {
workspaceSearch(query, visitor, target)
inDependencies.search(query, visitor)
}
def searchMethods(
query: String,
visitor: SymbolSearchVisitor,
target: Option[BuildTargetIdentifier],
): SymbolSearch.Result = {
workspaceMethodSearch(query, visitor, target)
SymbolSearch.Result.COMPLETE
}
def indexClasspath(): Unit = {
try {
indexClasspathUnsafe()
} catch {
case NonFatal(e) =>
scribe.error("failed to index classpath for workspace/symbol", e)
}
}
def didRemove(path: AbsolutePath): Unit = {
inWorkspace.remove(path.toNIO)
}
def didChange(
source: AbsolutePath,
symbols: Seq[WorkspaceSymbolInformation],
methodSymbols: Seq[WorkspaceSymbolInformation],
): Unit = {
val bloom = Fuzzy.bloomFilterSymbolStrings(symbols.map(_.symbol))
inWorkspace(source.toNIO) = WorkspaceSymbolsIndex(bloom, symbols)
// methodSymbols will be searched when we type `qual.x@@`
// where we want to match by prefix-match query.
// Do not index by bloom filter for (extension) method symbols here because
// - currently, we don't index each prefix of the name to bloom filter, so we can't find `incr` by `i`
// if we index it by bloom filter and lookup against it.
// - symbol search will take O(N), if we don't use bloom filter, but
// inWorkspaceMethods stores extension methods only, and the number of symbols (N) are quite limited.
// Therefore, we can expect symbol lookup for extension methods could be fast enough without bloom-filter.
if (methodSymbols.nonEmpty)
inWorkspaceMethods(source.toNIO) = methodSymbols
}
def buildTargetSymbols(
id: BuildTargetIdentifier
): Iterator[WorkspaceSymbolInformation] = {
for {
source <- buildTargets.buildTargetSources(id).iterator
index <- inWorkspace.get(source.toNIO).iterator
sym <- index.symbols.iterator
} yield sym
}
private def indexClasspathUnsafe(): Unit = {
val jars = buildTargets.allWorkspaceJars
inDependencies = classpathSearchIndexer.index(
jars.map(_.toNIO).toSeq,
excludedPackageHandler(),
bucketSize,
)
}
private def workspaceMethodSearch(
query: String,
visitor: SymbolSearchVisitor,
id: Option[BuildTargetIdentifier],
): Unit = {
for {
(path, symbols) <- id match {
case None =>
inWorkspaceMethods.iterator
case Some(target) =>
for {
source <- buildTargets.buildTargetTransitiveSources(target)
symbols <- inWorkspaceMethods.get(source.toNIO)
} yield (source.toNIO, symbols)
}
isDeleted = !Files.isRegularFile(path)
_ = if (isDeleted) inWorkspaceMethods.remove(path)
if !isDeleted
symbol <- symbols
if Fuzzy.matches(query, symbol.symbol)
}
visitor.visitWorkspaceSymbol(
path,
symbol.symbol,
symbol.kind,
symbol.range,
)
}
private def workspaceSearch(
query: WorkspaceSymbolQuery,
visitor: SymbolSearchVisitor,
id: Option[BuildTargetIdentifier],
): Unit = {
for {
(path, index) <- id match {
case None =>
inWorkspace.iterator
case Some(target) =>
for {
source <- buildTargets.buildTargetTransitiveSources(target)
index <- inWorkspace.get(source.toNIO)
} yield (source.toNIO, index)
}
if query.matches(index.bloom)
isDeleted = !Files.isRegularFile(path)
_ = if (isDeleted) inWorkspace.remove(path)
if !isDeleted
symbol <- index.symbols
if query.matches(symbol.symbol)
} {
visitor.visitWorkspaceSymbol(
path,
symbol.symbol,
symbol.kind,
symbol.range,
)
}
}
private def searchUnsafe(
textQuery: String,
token: CancelChecker,
): Seq[l.SymbolInformation] = {
val query = WorkspaceSymbolQuery.fromTextQuery(textQuery)
val visitor =
new WorkspaceSearchVisitor(
workspace,
query,
token,
index,
saveClassFileToDisk,
)
search(query, visitor, None)
visitor.allResults()
}
}