-
-
Notifications
You must be signed in to change notification settings - Fork 959
/
Copy pathdiagnostic.ts
692 lines (617 loc) · 27.9 KB
/
diagnostic.ts
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
'use strict'
import { minimatch } from '../util/node'
import { v4 as uuid } from 'uuid'
import type {
CancellationToken, ClientCapabilities, Diagnostic, DiagnosticOptions, DiagnosticRegistrationOptions, DocumentDiagnosticParams, DocumentDiagnosticReport, DocumentSelector, PreviousResultId, ServerCapabilities, WorkspaceDiagnosticParams, WorkspaceDiagnosticReport, WorkspaceDiagnosticReportPartialResult
} from 'vscode-languageserver-protocol'
import { TextDocument } from 'vscode-languageserver-textdocument'
import { URI } from 'vscode-uri'
import DiagnosticCollection from '../diagnostic/collection'
import languages from '../languages'
import { DiagnosticProvider, ProviderResult, ResultReporter } from '../provider'
import { TextDocumentMatch } from '../types'
import { CancellationError } from '../util/errors'
import { LinkedMap, Touch } from '../util/map'
import { CancellationTokenSource, DiagnosticRefreshRequest, DiagnosticServerCancellationData, DidChangeTextDocumentNotification, DidCloseTextDocumentNotification, DidOpenTextDocumentNotification, DidSaveTextDocumentNotification, Disposable, DocumentDiagnosticReportKind, DocumentDiagnosticRequest, Emitter, RAL, WorkspaceDiagnosticRequest } from '../util/protocol'
import window from '../window'
import workspace from '../workspace'
import { BaseFeature, ensure, FeatureClient, LSPCancellationError, TextDocumentLanguageFeature } from './features'
import { getConditionValue } from '../util'
interface HandleDiagnosticsSignature {
(this: void, uri: string, diagnostics: Diagnostic[]): void
}
export type ProvideDiagnosticSignature = (this: void, document: TextDocument | URI, previousResultId: string | undefined, token: CancellationToken) => ProviderResult<DocumentDiagnosticReport>
export type ProvideWorkspaceDiagnosticSignature = (this: void, resultIds: PreviousResultId[], token: CancellationToken, resultReporter: ResultReporter) => ProviderResult<WorkspaceDiagnosticReport>
export interface DiagnosticProviderMiddleware {
handleDiagnostics?: (this: void, uri: string, diagnostics: Diagnostic[], next: HandleDiagnosticsSignature) => void
provideDiagnostics?: (this: void, document: TextDocument | URI, previousResultId: string | undefined, token: CancellationToken, next: ProvideDiagnosticSignature) => ProviderResult<DocumentDiagnosticReport>
provideWorkspaceDiagnostics?: (this: void, resultIds: PreviousResultId[], token: CancellationToken, resultReporter: ResultReporter, next: ProvideWorkspaceDiagnosticSignature) => ProviderResult<WorkspaceDiagnosticReport>
}
export interface DiagnosticProviderShape {
onDidChangeDiagnosticsEmitter: Emitter<void>
knows: (kind: PullState, textDocument: TextDocument) => boolean
diagnostics: DiagnosticProvider
}
export enum DiagnosticPullMode {
onType = 'onType',
onSave = 'onSave'
}
export interface DiagnosticPullOptions {
/**
* Whether to pull for diagnostics on document change.
*/
onChange?: boolean
/**
* Whether to pull for diagnostics on document save.
*/
onSave?: boolean
/**
* Whether to pull workspace diagnostics.
*/
workspace?: boolean
/**
* Minimatch patterns to match full filepath that should be ignored for pullDiagnostic.
*/
ignored?: string[]
/**
* An optional filter method that is consulted when triggering a
* diagnostic pull during document change or document save.
*
* @param document the document that changes or got save
* @param mode the mode
*/
filter?(document: TextDocumentMatch, mode: DiagnosticPullMode): boolean
}
export interface $DiagnosticPullOptions {
diagnosticPullOptions?: DiagnosticPullOptions
}
enum RequestStateKind {
active = 'open',
reschedule = 'reschedule',
outDated = 'drop'
}
type RequestState = {
state: RequestStateKind.active
document: TextDocument | URI
version: number | undefined
tokenSource: CancellationTokenSource
} | {
state: RequestStateKind.reschedule
document: TextDocument | URI
} | {
state: RequestStateKind.outDated
document: TextDocument | URI
}
interface DocumentPullState {
document: URI
pulledVersion: number | undefined
resultId: string | undefined
}
export enum PullState {
document = 1,
workspace = 2
}
interface Requestor {
pull: (document: TextDocument, cb?: () => void) => void
}
const pullDebounce = getConditionValue(3000, 10)
export class DocumentPullStateTracker {
private readonly documentPullStates: Map<string, DocumentPullState>
private readonly workspacePullStates: Map<string, DocumentPullState>
constructor() {
this.documentPullStates = new Map()
this.workspacePullStates = new Map()
}
public track(kind: PullState, textDocument: TextDocument): DocumentPullState
public track(kind: PullState, uri: URI, version: number | undefined): DocumentPullState
public track(kind: PullState, document: TextDocument | URI, arg1?: number | undefined): DocumentPullState {
const states = kind === PullState.document ? this.documentPullStates : this.workspacePullStates
const [key, uri, version] = document instanceof URI
? [document.toString(), document, arg1 as number | undefined]
: [document.uri.toString(), URI.parse(document.uri), document.version]
let state = states.get(key)
if (state === undefined) {
state = { document: uri, pulledVersion: version, resultId: undefined }
states.set(key, state)
}
return state
}
public update(kind: PullState, textDocument: TextDocument, resultId: string | undefined): void
public update(kind: PullState, uri: URI, version: number | undefined, resultId: string | undefined): void
public update(kind: PullState, document: TextDocument | URI, arg1: string | number | undefined, arg2?: string | undefined): void {
const states = kind === PullState.document ? this.documentPullStates : this.workspacePullStates
const [key, uri, version, resultId] = document instanceof URI
? [document.toString(), document, arg1 as number | undefined, arg2]
: [document.uri, URI.parse(document.uri), document.version, arg1 as string | undefined]
let state = states.get(key)
if (state === undefined) {
state = { document: uri, pulledVersion: version, resultId }
states.set(key, state)
} else {
state.pulledVersion = version
state.resultId = resultId
}
}
public unTrack(kind: PullState, document: TextDocument | URI): void {
const key = document instanceof URI ? document.toString() : document.uri
const states = kind === PullState.document ? this.documentPullStates : this.workspacePullStates
states.delete(key)
}
public tracks(kind: PullState, document: TextDocument | URI): boolean {
const key = document instanceof URI ? document.toString() : document.uri
const states = kind === PullState.document ? this.documentPullStates : this.workspacePullStates
return states.has(key)
}
public trackingDocuments(): string[] {
return Array.from(this.documentPullStates.keys())
}
public getResultId(kind: PullState, document: TextDocument | URI): string | undefined {
const key = document instanceof URI ? document.toString() : document.uri
const states = kind === PullState.document ? this.documentPullStates : this.workspacePullStates
return states.get(key)?.resultId
}
public getAllResultIds(): PreviousResultId[] {
const result: PreviousResultId[] = []
for (let [uri, value] of this.workspacePullStates) {
if (this.documentPullStates.has(uri)) {
value = this.documentPullStates.get(uri)!
}
if (value.resultId !== undefined) {
result.push({ uri, value: value.resultId })
}
}
return result
}
}
export class DiagnosticRequestor extends BaseFeature<DiagnosticProviderMiddleware, $DiagnosticPullOptions> implements Disposable {
private isDisposed: boolean
private enableWorkspace: boolean
private readonly client: FeatureClient<DiagnosticProviderMiddleware, $DiagnosticPullOptions>
private readonly options: DiagnosticRegistrationOptions
public readonly onDidChangeDiagnosticsEmitter: Emitter<void>
public readonly provider: DiagnosticProvider
private readonly diagnostics: DiagnosticCollection
private readonly openRequests: Map<string, RequestState>
private readonly documentStates: DocumentPullStateTracker
private workspaceErrorCounter: number
private workspaceCancellation: CancellationTokenSource | undefined
private workspaceTimeout: Disposable | undefined
public constructor(client: FeatureClient<DiagnosticProviderMiddleware, $DiagnosticPullOptions>, options: DiagnosticRegistrationOptions) {
super(client)
this.client = client
this.options = options
this.enableWorkspace = options.workspaceDiagnostics && this.client.clientOptions.diagnosticPullOptions?.workspace !== false
this.isDisposed = false
this.onDidChangeDiagnosticsEmitter = new Emitter<void>()
this.provider = this.createProvider()
this.diagnostics = languages.createDiagnosticCollection(options.identifier ? options.identifier : client.id)
this.openRequests = new Map()
this.documentStates = new DocumentPullStateTracker()
this.workspaceErrorCounter = 0
}
public knows(kind: PullState, textDocument: TextDocument): boolean {
return this.documentStates.tracks(kind, textDocument)
}
public trackingDocuments(): string[] {
return this.documentStates.trackingDocuments()
}
public forget(kind: PullState, document: TextDocument): void {
this.documentStates.unTrack(kind, document)
}
public pull(document: TextDocument, cb?: () => void): void {
this.pullAsync(document).then(() => {
if (cb) {
cb()
}
}, error => {
this.client.error(`Document pull failed for text document ${document.uri}`, error)
})
}
private async pullAsync(document: TextDocument): Promise<void> {
if (this.isDisposed) return
const uri = document.uri
const version = document.version
const currentRequestState = this.openRequests.get(uri)
const documentState = this.documentStates.track(PullState.document, document)
if (currentRequestState === undefined) {
const tokenSource = new CancellationTokenSource()
this.openRequests.set(uri, { state: RequestStateKind.active, document, version, tokenSource })
let report: DocumentDiagnosticReport | undefined
let afterState: RequestState | undefined
try {
report = await this.provider.provideDiagnostics(document, documentState.resultId, tokenSource.token) ?? { kind: DocumentDiagnosticReportKind.Full, items: [] }
} catch (error) {
if (error instanceof LSPCancellationError && error.data && DiagnosticServerCancellationData.is(error.data) && error.data.retriggerRequest === false) {
afterState = { state: RequestStateKind.outDated, document }
}
if (afterState === undefined && error instanceof CancellationError) {
afterState = { state: RequestStateKind.reschedule, document }
} else {
throw error
}
}
afterState = afterState ?? this.openRequests.get(uri)
if (afterState === undefined) {
// This shouldn't happen. Log it
this.client.error(`Lost request state in diagnostic pull model. Clearing diagnostics for ${uri}`)
this.diagnostics.delete(uri)
return
}
this.openRequests.delete(uri)
const visible = window.visibleTextEditors.some(editor => editor.document.uri === uri)
if (!visible) {
this.documentStates.unTrack(PullState.document, document)
return
}
if (afterState.state === RequestStateKind.outDated) return
// report is only undefined if the request has thrown.
if (report !== undefined) {
if (report.kind === DocumentDiagnosticReportKind.Full) {
this.diagnostics.set(uri, report.items)
}
documentState.pulledVersion = version
documentState.resultId = report.resultId
}
if (afterState.state === RequestStateKind.reschedule) {
this.pull(document)
}
} else {
if (currentRequestState.state === RequestStateKind.active) {
// Cancel the current request and reschedule a new one when the old one returned.
currentRequestState.tokenSource.cancel()
this.openRequests.set(uri, { state: RequestStateKind.reschedule, document: currentRequestState.document })
} else if (currentRequestState.state === RequestStateKind.outDated) {
this.openRequests.set(uri, { state: RequestStateKind.reschedule, document: currentRequestState.document })
}
}
}
public forgetDocument(document: TextDocument): void {
const uri = document.uri
const request = this.openRequests.get(uri)
if (this.enableWorkspace) {
// If we run workspace diagnostic pull a last time for the diagnostics
// and the rely on getting them from the workspace result.
if (request !== undefined) {
this.openRequests.set(uri, { state: RequestStateKind.reschedule, document })
} else {
this.pull(document, () => {
this.forget(PullState.document, document)
})
}
} else {
// We have normal pull or inter file dependencies. In this case we
// clear the diagnostics (to have the same start as after startup).
// We also cancel outstanding requests.
if (request !== undefined) {
if (request.state === RequestStateKind.active) {
request.tokenSource.cancel()
}
this.openRequests.delete(uri)
}
this.diagnostics.delete(uri.toString())
this.forget(PullState.document, document)
}
}
public pullWorkspace(): void {
if (!this.enableWorkspace) return
this.pullWorkspaceAsync().then(() => {
this.workspaceTimeout = RAL().timer.setTimeout(() => {
this.pullWorkspace()
}, pullDebounce)
}, error => {
if (!(error instanceof LSPCancellationError) && !DiagnosticServerCancellationData.is(error.data)) {
this.client.error(`Workspace diagnostic pull failed.`, error)
this.workspaceErrorCounter++
}
if (this.workspaceErrorCounter <= 5) {
this.workspaceTimeout = RAL().timer.setTimeout(() => {
this.pullWorkspace()
}, pullDebounce)
}
})
}
private async pullWorkspaceAsync(): Promise<void> {
if (!this.provider.provideWorkspaceDiagnostics) {
return
}
if (this.workspaceCancellation !== undefined) {
this.workspaceCancellation.cancel()
this.workspaceCancellation = undefined
}
this.workspaceCancellation = new CancellationTokenSource()
const previousResultIds: PreviousResultId[] = this.documentStates.getAllResultIds()
await this.provider.provideWorkspaceDiagnostics(previousResultIds, this.workspaceCancellation.token, chunk => {
if (!chunk || this.isDisposed) {
return
}
for (const item of chunk.items) {
if (item.kind === DocumentDiagnosticReportKind.Full) {
// Favour document pull result over workspace results. So skip if it is tracked
// as a document result.
if (!this.documentStates.tracks(PullState.document, URI.parse(item.uri))) {
this.diagnostics.set(item.uri.toString(), item.items)
}
}
this.documentStates.update(PullState.workspace, URI.parse(item.uri), item.version ?? undefined, item.resultId)
}
})
}
private createProvider(): DiagnosticProvider {
const provider: DiagnosticProvider = {
onDidChangeDiagnostics: this.onDidChangeDiagnosticsEmitter.event,
provideDiagnostics: (document, previousResultId, token) => {
const middleware = this.client.middleware!
const client = this._client
const provideDiagnostics: ProvideDiagnosticSignature = (document, previousResultId, token) => {
const uri = client.code2ProtocolConverter.asUri(document instanceof URI ? document : URI.parse(document.uri))
const params: DocumentDiagnosticParams = {
identifier: this.options.identifier,
textDocument: { uri },
previousResultId
}
return this.sendRequest(DocumentDiagnosticRequest.type, params, token, { kind: DocumentDiagnosticReportKind.Full, items: [] }).then(async result => {
if (result === undefined || result === null || this.isDisposed) {
return { kind: DocumentDiagnosticReportKind.Full, items: [] }
}
// make handleDiagnostics middleware works
if (middleware.handleDiagnostics && result.kind == DocumentDiagnosticReportKind.Full) {
middleware.handleDiagnostics(uri, result.items, (_, diagnostics) => {
result.items = diagnostics
})
}
return result
})
}
return middleware.provideDiagnostics
? middleware.provideDiagnostics(document, previousResultId, token, provideDiagnostics)
: provideDiagnostics(document, previousResultId, token)
}
}
if (this.options.workspaceDiagnostics) {
provider.provideWorkspaceDiagnostics = (resultIds, token, resultReporter): ProviderResult<WorkspaceDiagnosticReport> => {
const provideWorkspaceDiagnostics: ProvideWorkspaceDiagnosticSignature = (resultIds, token): ProviderResult<WorkspaceDiagnosticReport> => {
const partialResultToken = uuid()
const disposable = this.client.onProgress(WorkspaceDiagnosticRequest.partialResult, partialResultToken, partialResult => {
if (partialResult == undefined) {
resultReporter(null)
return
}
resultReporter(partialResult as WorkspaceDiagnosticReportPartialResult)
})
const params: WorkspaceDiagnosticParams & { __token?: string } = {
identifier: this.options.identifier,
previousResultIds: resultIds,
partialResultToken
}
return this.sendRequest(WorkspaceDiagnosticRequest.type, params, token, { items: [] }).then(async (result): Promise<WorkspaceDiagnosticReport> => {
resultReporter(result)
return { items: [] }
}).finally(() => {
disposable.dispose()
})
}
const middleware: DiagnosticProviderMiddleware = this.client.middleware!
return middleware.provideWorkspaceDiagnostics
? middleware.provideWorkspaceDiagnostics(resultIds, token, resultReporter, provideWorkspaceDiagnostics)
: provideWorkspaceDiagnostics(resultIds, token, resultReporter)
}
}
return provider
}
public dispose(): void {
this.isDisposed = true
// Cancel and clear workspace pull if present.
this.workspaceCancellation?.cancel()
this.workspaceTimeout?.dispose()
// Cancel all request and mark open requests as outdated.
for (const request of this.openRequests.values()) {
if (request.state === RequestStateKind.active) {
request.tokenSource.cancel()
}
}
this.openRequests.clear()
}
}
export class BackgroundScheduler implements Disposable {
private readonly diagnosticRequestor: Requestor
private endDocument: TextDocument | undefined
private readonly documents: LinkedMap<string, TextDocument>
private intervalHandle: Disposable | undefined
public constructor(diagnosticRequestor: Requestor) {
this.diagnosticRequestor = diagnosticRequestor
this.documents = new LinkedMap()
}
public add(document: TextDocument): void {
const key = document.uri
if (this.documents.has(key)) return
this.documents.set(key, document, Touch.AsNew)
this.trigger()
}
public remove(document: TextDocument): void {
const key = document.uri
if (this.documents.has(key)) {
this.documents.delete(key)
// Do a last pull
this.diagnosticRequestor.pull(document)
}
// No more documents. Stop background activity.
if (this.documents.size === 0) {
this.stop()
} else if (document.uri === this.endDocument?.uri) {
// Make sure we have a correct last document. It could have
this.endDocument = this.documents.last
}
}
public trigger(): void {
// We have a round running. So simply make sure we run up to the
// last document
if (this.intervalHandle !== undefined) {
this.endDocument = this.documents.last
return
}
this.endDocument = this.documents.last
this.intervalHandle = RAL().timer.setInterval(() => {
const document = this.documents.first
if (document !== undefined) {
const key = document.uri
this.diagnosticRequestor.pull(document)
this.documents.set(key, document, Touch.AsNew)
if (document === this.endDocument) {
this.stop()
}
}
}, 200)
}
public dispose(): void {
this.stop()
this.documents.clear()
}
private stop(): void {
this.intervalHandle?.dispose()
this.intervalHandle = undefined
this.endDocument = undefined
}
}
class DiagnosticFeatureProviderImpl implements DiagnosticProviderShape {
public readonly disposable: Disposable
private readonly diagnosticRequestor: DiagnosticRequestor
private activeTextDocument: TextDocument | undefined
private readonly backgroundScheduler: BackgroundScheduler
constructor(client: FeatureClient<DiagnosticProviderMiddleware, $DiagnosticPullOptions>, options: DiagnosticRegistrationOptions) {
const diagnosticPullOptions = client.clientOptions.diagnosticPullOptions!
const documentSelector = options.documentSelector!
const disposables: Disposable[] = []
const ignored = diagnosticPullOptions.ignored ?? []
const matches = (document: TextDocument): boolean => {
if (workspace.match(documentSelector, document) <= 0) return false
const visible = window.visibleTextEditors.some(editor => editor.document.uri === document.uri)
if (!visible) return false
if (ignored.length > 0 && ignored.some(p => minimatch(URI.parse(document.uri).fsPath, p, { dot: true }))) return false
return true
}
this.diagnosticRequestor = new DiagnosticRequestor(client, options)
this.backgroundScheduler = new BackgroundScheduler(this.diagnosticRequestor)
const addToBackgroundIfNeeded = (document: TextDocument): void => {
if (!matches(document) || !options.interFileDependencies || this.activeTextDocument?.uri === document.uri) return
this.backgroundScheduler.add(document)
}
this.activeTextDocument = window.activeTextEditor?.document.textDocument
window.onDidChangeActiveTextEditor(editor => {
const oldActive = this.activeTextDocument
let textDocument = this.activeTextDocument = editor?.document.textDocument
if (oldActive !== undefined) {
addToBackgroundIfNeeded(oldActive)
}
if (textDocument != null) this.backgroundScheduler.remove(textDocument)
}, null, disposables)
// We always pull on open.
const openFeature = client.getFeature(DidOpenTextDocumentNotification.method)
disposables.push(openFeature.onNotificationSent(event => {
const textDocument = event.original
if (matches(textDocument)) {
this.diagnosticRequestor.pull(textDocument, () => { addToBackgroundIfNeeded(textDocument) })
}
}))
const shouldPull = (textDocument: TextDocument, mode: DiagnosticPullMode): boolean => {
if (diagnosticPullOptions.filter && diagnosticPullOptions.filter(textDocument, mode)) return false
if (!this.diagnosticRequestor.knows(PullState.document, textDocument)) return false
return true
}
if (diagnosticPullOptions.onChange === true) {
const changeFeature = client.getFeature(DidChangeTextDocumentNotification.method)
disposables.push(changeFeature.onNotificationSent(async event => {
const textDocument = workspace.getDocument(event.original.bufnr).textDocument
if (event.original.contentChanges.length == 0) return
if (shouldPull(textDocument, DiagnosticPullMode.onType)) {
this.diagnosticRequestor.pull(textDocument, () => { this.backgroundScheduler.trigger() })
}
}))
}
if (diagnosticPullOptions.onSave === true) {
const saveFeature = client.getFeature(DidSaveTextDocumentNotification.method)
disposables.push(saveFeature.onNotificationSent(event => {
const textDocument = event.original
if (shouldPull(textDocument, DiagnosticPullMode.onSave)) {
this.diagnosticRequestor.pull(event.original, () => { this.backgroundScheduler.trigger() })
}
}))
}
const closeFeature = client.getFeature(DidCloseTextDocumentNotification.method)
disposables.push(closeFeature.onNotificationSent(event => {
this.cleanUpDocument(event.original)
}))
// We received a did change from the server.
this.diagnosticRequestor.onDidChangeDiagnosticsEmitter.event(() => {
for (const textDocument of workspace.textDocuments) {
if (matches(textDocument)) {
this.diagnosticRequestor.pull(textDocument)
}
}
})
window.onDidChangeVisibleTextEditors(editors => {
const handled: Set<string> = new Set()
const tracking = this.diagnosticRequestor.trackingDocuments()
editors.forEach(editor => {
let { uri, textDocument } = editor.document
if (handled.has(uri)) return
handled.add(uri)
if (matches(textDocument) && !tracking.includes(uri)) {
this.diagnosticRequestor.pull(textDocument, () => { addToBackgroundIfNeeded(textDocument) })
}
})
// cleanUp hidden documents
tracking.forEach(uri => {
if (handled.has(uri)) return
let doc = workspace.getDocument(uri)
if (doc && doc.attached) this.cleanUpDocument(doc.textDocument)
})
}, null, disposables)
// da348dc5-c30a-4515-9d98-31ff3be38d14 is the test UUID to test the middle ware. So don't auto trigger pulls.
if (options.workspaceDiagnostics === true && options.identifier !== 'da348dc5-c30a-4515-9d98-31ff3be38d14') {
this.diagnosticRequestor.pullWorkspace()
}
// disposables.push(languages.registerDiagnosticsProvider(options.documentSelector, this.diagnosticRequestor.provider))
this.disposable = Disposable.create(() => [...disposables, this.backgroundScheduler, this.diagnosticRequestor].forEach(d => d.dispose()))
}
public get onDidChangeDiagnosticsEmitter(): Emitter<void> {
return this.diagnosticRequestor.onDidChangeDiagnosticsEmitter
}
public get diagnostics(): DiagnosticProvider {
return this.diagnosticRequestor.provider
}
public knows(kind: PullState, textDocument: TextDocument): boolean {
return this.diagnosticRequestor.knows(kind, textDocument)
}
private cleanUpDocument(document: TextDocument): void {
if (this.diagnosticRequestor.knows(PullState.document, document)) {
this.diagnosticRequestor.forgetDocument(document)
this.backgroundScheduler.remove(document)
}
}
}
export class DiagnosticFeature extends TextDocumentLanguageFeature<DiagnosticOptions, DiagnosticRegistrationOptions, DiagnosticProviderShape, DiagnosticProviderMiddleware> {
constructor(client: FeatureClient<DiagnosticProviderMiddleware, $DiagnosticPullOptions>) {
super(client, DocumentDiagnosticRequest.type)
}
public fillClientCapabilities(capabilities: ClientCapabilities): void {
let capability = ensure(ensure(capabilities, 'textDocument')!, 'diagnostic')!
capability.dynamicRegistration = true
capability.relatedDocumentSupport = true
ensure(ensure(capabilities, 'workspace')!, 'diagnostics')!.refreshSupport = true
}
public initialize(capabilities: ServerCapabilities, documentSelector: DocumentSelector): void {
const client = this._client
client.onRequest(DiagnosticRefreshRequest.type, async () => {
for (const provider of this.getAllProviders()) {
provider.onDidChangeDiagnosticsEmitter.fire()
}
})
let [id, options] = this.getRegistration(documentSelector, capabilities.diagnosticProvider)
if (!id || !options) return
this.register({ id, registerOptions: options })
}
protected registerLanguageProvider(options: DiagnosticRegistrationOptions): [Disposable, DiagnosticProviderShape] {
const provider = new DiagnosticFeatureProviderImpl(this._client, options)
return [provider.disposable, provider]
}
}