-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathDiagnostics.scala
397 lines (359 loc) Β· 12.1 KB
/
Diagnostics.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
package scala.meta.internal.metals
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicReference
import java.{util => ju}
import scala.collection.concurrent.TrieMap
import scala.collection.mutable
import scala.util.Try
import scala.meta.inputs.Input
import scala.meta.internal.metals.JsonParser._
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.PositionSyntax._
import scala.meta.internal.parsing.TokenEditDistance
import scala.meta.internal.parsing.Trees
import scala.meta.io.AbsolutePath
import ch.epfl.scala.bsp4j
import ch.epfl.scala.bsp4j.BuildTargetIdentifier
import org.eclipse.lsp4j.Diagnostic
import org.eclipse.lsp4j.PublishDiagnosticsParams
import org.eclipse.lsp4j.services.LanguageClient
import org.eclipse.{lsp4j => l}
private final case class CompilationStatus(
code: bsp4j.StatusCode,
errors: Int,
)
/**
* Converts diagnostics from the build server and Scalameta parser into LSP diagnostics.
*
* BSP diagnostics have different semantics from LSP diagnostics with regards to how they
* are published. BSP diagnostics can be accumulated when `reset=false`, meaning the client
* (Metals) is responsible for aggregating multiple `build/publishDiagnostics` notifications
* into a single `textDocument/publishDiagnostics` notification.
*
* Another challenge is to consolidate syntax errors on every keystroke with type errors
* on batch compile because positions get stale in batch compile errors as you type new
* syntax errors. To solve this problem we use token edit distance the same way we support
* goto definition in stale buffers.
*/
final class Diagnostics(
buffers: Buffers,
languageClient: LanguageClient,
statistics: StatisticsConfig,
workspace: Option[AbsolutePath],
trees: Trees,
buildTargets: BuildTargets,
downstreamTargets: PreviouslyCompiledDownsteamTargets,
config: MetalsServerConfig,
) {
private val diagnostics =
TrieMap.empty[AbsolutePath, ju.Queue[Diagnostic]]
private val syntaxError =
TrieMap.empty[AbsolutePath, Diagnostic]
private val snapshots =
TrieMap.empty[AbsolutePath, Input.VirtualFile]
private val lastPublished =
new AtomicReference[AbsolutePath]()
private val diagnosticsBuffer =
new ConcurrentLinkedQueue[AbsolutePath]()
private val compileTimer =
TrieMap.empty[BuildTargetIdentifier, Timer]
private val compilationStatus =
TrieMap.empty[BuildTargetIdentifier, CompilationStatus]
def reset(): Unit = {
val keys = diagnostics.keys
diagnostics.clear()
keys.foreach { key => publishDiagnostics(key) }
}
def reset(paths: Seq[AbsolutePath]): Unit =
for (path <- paths if diagnostics.contains(path)) {
diagnostics.remove(path)
publishDiagnostics(path)
}
def resetAmmoniteScripts(): Unit =
for (key <- diagnostics.keys if key.isAmmoniteScript) {
diagnostics.remove(key)
publishDiagnostics(key)
}
def onStartCompileBuildTarget(target: BuildTargetIdentifier): Unit = {
if (statistics.isDiagnostics) {
compileTimer(target) = new Timer(Time.system)
}
}
def onFinishCompileBuildTarget(
report: bsp4j.CompileReport,
statusCode: bsp4j.StatusCode,
): Unit = {
val target = report.getTarget()
// if we use best effort compilation downstream targets
// should get recompiled even if compilation fails
def shouldUnpublishForDownstreamTargets =
!(buildTargets
.scalaTarget(target)
.exists(_.isBestEffort) && config.enableBestEffort)
if (statusCode.isError && shouldUnpublishForDownstreamTargets) {
removeInverseDependenciesDiagnostics(target)
} else {
downstreamTargets.remove(target)
}
publishDiagnosticsBuffer()
compileTimer.remove(target)
val status = CompilationStatus(statusCode, report.getErrors())
compilationStatus.update(target, status)
}
def onSyntaxError(path: AbsolutePath, diags: List[Diagnostic]): Unit = {
diags.headOption match {
case Some(diagnostic) if !workspace.exists(path.isInReadonlyDirectory) =>
syntaxError(path) = diagnostic
publishDiagnostics(path)
case _ =>
onClose(path)
}
}
def onClose(path: AbsolutePath): Unit = {
val diags = if (path.isWorksheet) {
diagnostics.remove(path).toList.flatMap(_.asScala) ++
syntaxError.remove(path)
} else syntaxError.remove(path).toList
diags match {
case Nil =>
() // Do nothing, there was no previous error.
case _ =>
publishDiagnostics(path) // Remove old syntax error.
}
}
def didDelete(path: AbsolutePath): Unit = {
diagnostics.remove(path)
syntaxError.remove(path)
languageClient.publishDiagnostics(
new PublishDiagnosticsParams(
path.toURI.toString(),
ju.Collections.emptyList(),
)
)
}
def didChange(path: AbsolutePath): Unit = {
publishDiagnostics(path)
}
def onBuildPublishDiagnostics(
params: bsp4j.PublishDiagnosticsParams
): Unit = {
val diagnostics = params.getDiagnostics().asScala.map(_.toLsp).toSeq
val publish =
for {
path <- Try(params.getTextDocument.getUri.toAbsolutePath).toOption
if (path.isFile)
} yield onPublishDiagnostics(
path,
diagnostics,
params.getReset(),
)
publish.getOrElse {
scribe.warn(
s"Invalid text document uri received from build server: ${params.getTextDocument.getUri}"
)
diagnostics.map(_.getMessage()).foreach { msg =>
scribe.info(s"BSP server: $msg")
}
}
}
def onPublishDiagnostics(
path: AbsolutePath,
diagnostics: Seq[Diagnostic],
isReset: Boolean,
): Unit = {
val isSamePathAsLastDiagnostic = path == lastPublished.get()
lastPublished.set(path)
val queue = this.diagnostics.getOrElseUpdate(
path,
new ConcurrentLinkedQueue[Diagnostic](),
)
if (isReset) {
queue.clear()
snapshots.remove(path)
}
if (queue.isEmpty && !diagnostics.isEmpty) {
snapshots(path) = path.toInput
}
diagnostics.foreach { diagnostic => queue.add(diagnostic) }
// NOTE(olafur): we buffer up several diagnostics for the same path before forwarding
// them to the editor client. Without buffering, we risk publishing an exponential number
// notifications for a file with N number of diagnostics:
// Notification 1: [1]
// Notification 2: [1, 2]
// Notification 3: [1, 2, 3]
// Notification N: [1, ..., N]
if (isReset || !isSamePathAsLastDiagnostic) {
publishDiagnosticsBuffer()
publishDiagnostics(path, queue)
} else {
diagnosticsBuffer.add(path)
}
}
private def removeInverseDependenciesDiagnostics(
buildTarget: BuildTargetIdentifier
) = {
val inverseDeps =
buildTargets.allInverseDependencies(buildTarget) - buildTarget
val targets = for {
path <- diagnostics.keySet
targets <- buildTargets.sourceBuildTargets(path)
if targets.exists(inverseDeps.apply)
} yield {
diagnostics.remove(path)
publishDiagnostics(path)
targets
}
val targetsSet = targets.flatten.toSet
if (targetsSet.nonEmpty) {
downstreamTargets.addMapping(buildTarget, targetsSet)
}
}
private def publishDiagnostics(path: AbsolutePath): Unit = {
publishDiagnostics(
path,
diagnostics.getOrElse(path, new ju.LinkedList[Diagnostic]()),
)
}
def hasCompilationErrors(buildTarget: BuildTargetIdentifier): Boolean = {
compilationStatus
.get(buildTarget)
.map(status => status.code.isError || status.errors > 0)
.getOrElse(false)
}
def hasSyntaxError(path: AbsolutePath): Boolean =
syntaxError.contains(path)
def hasDiagnosticError(path: AbsolutePath): Boolean = {
val fileDiagnostics = diagnostics
.get(path)
fileDiagnostics match {
case Some(diagnostics) =>
diagnostics.asScala.exists(
_.getSeverity() == l.DiagnosticSeverity.Error
)
case None => false
}
}
private def publishDiagnostics(
path: AbsolutePath,
queue: ju.Queue[Diagnostic],
): Unit = {
if (!path.isFile) return didDelete(path)
val uri = path.toURI.toString
val all = new ju.ArrayList[Diagnostic](queue.size() + 1)
for {
diagnostic <- queue.asScala
freshDiagnostic <- toFreshDiagnostic(path, diagnostic)
} {
all.add(freshDiagnostic)
}
for {
d <- syntaxError.get(path)
// De-duplicate only the most common and basic syntax errors.
isSameMessage = all.asScala.exists(diag =>
diag.getRange() == d.getRange() && diag.getMessage() == d.getMessage()
)
isDuplicate =
d.getMessage.replace("`", "").startsWith("identifier expected but") &&
all.asScala.exists { other =>
other.getMessage
.replace("`", "")
.startsWith("identifier expected") &&
other.getRange().getStart() == d.getRange().getStart()
}
if !isDuplicate && !isSameMessage
} {
all.add(d)
}
languageClient.publishDiagnostics(new PublishDiagnosticsParams(uri, all))
}
private def publishDiagnosticsBuffer(): Unit = {
clearDiagnosticsBuffer().foreach { path => publishDiagnostics(path) }
}
// Adjust positions for type errors for changes in the open buffer.
// Only needed when merging syntax errors with type errors.
private def toFreshDiagnostic(
path: AbsolutePath,
d: Diagnostic,
): Option[Diagnostic] = {
val current = path.toInputFromBuffers(buffers)
val snapshot = snapshots.getOrElse(path, current)
val edit = TokenEditDistance(
snapshot,
current,
trees,
)
edit match {
case Right(edit) =>
val result = edit
.toRevised(
range = d.getRange,
adjustWithinToken = shouldAdjustWithinToken(d),
)
.map { range =>
val ld = new l.Diagnostic(
range,
d.getMessage,
d.getSeverity,
d.getSource,
)
// Scala 3 sets the diagnostic code to -1 for NoExplanation Messages. Ideally
// this will change and we won't need this check in the future, but for now
// let's not forward them.
val isScala3NoExplanationDiag = d.getCode() != null && d
.getCode()
.isLeft() && d.getCode().getLeft() == "-1"
if (!isScala3NoExplanationDiag) ld.setCode(d.getCode())
ld.setTags(d.getTags())
adjustedDiagnosticData(d, edit).map(newData => ld.setData(newData))
ld
}
if (result.isEmpty) {
d.getRange.toMeta(snapshot).foreach { pos =>
val message = pos.formatMessage(
s"stale ${d.getSource} ${d.getSeverity.toString.toLowerCase()}",
d.getMessage,
)
scribe.info(message)
}
}
result
case Left(_) =>
// tokenization error will be shown from scalameta tokenizer
None
}
}
private def clearDiagnosticsBuffer(): Iterable[AbsolutePath] = {
val toPublish = mutable.Set.empty[AbsolutePath]
var path = diagnosticsBuffer.poll()
while (path != null) {
toPublish.add(path)
path = diagnosticsBuffer.poll()
}
toPublish
}
private def adjustedDiagnosticData(
diagnostic: l.Diagnostic,
edit: TokenEditDistance,
): Option[Object] =
diagnostic match {
case ScalacDiagnostic.ScalaDiagnostic(Left(textEdit)) =>
edit
.toRevised(
textEdit,
shouldAdjustWithinToken(diagnostic),
fallbackToNearest = false,
)
.map(_.toJsonObject)
case ScalacDiagnostic.ScalaDiagnostic(Right(scalaDiagnostic)) =>
edit
.toRevised(
scalaDiagnostic,
shouldAdjustWithinToken(diagnostic),
fallbackToNearest = false,
)
.map(_.toJsonObject)
case _ => Some(diagnostic.getData())
}
private def shouldAdjustWithinToken(diagnostic: l.Diagnostic): Boolean =
diagnostic.getSource() == "scala-cli"
}