@@ -49,9 +49,14 @@ class DocumentLinterDisposed extends Error {}
49
49
exports . DocumentLinterDisposed = DocumentLinterDisposed ;
50
50
51
51
class DocumentLinter {
52
- constructor ( document , diagnosticCollection ) {
52
+ // document has the following methods:
53
+ //
54
+ // getText(): string;
55
+ // setDiagnostics(diagnostics: Object[]): void;
56
+ // removeDiagnostics(): void;
57
+ constructor ( document , processFactoryPromise ) {
53
58
this . _document = document ;
54
- this . _diagnosticCollection = diagnosticCollection ;
59
+ this . _processFactoryPromise = processFactoryPromise ;
55
60
this . _state = DocumentLinterState . NO_PARSER ;
56
61
57
62
// Used only in states: CREATING_PARSER
@@ -71,7 +76,7 @@ class DocumentLinter {
71
76
assert . strictEqual ( this . _state , DocumentLinterState . NO_PARSER ) ;
72
77
this . _state = DocumentLinterState . CREATING_PARSER ;
73
78
this . _parserPromise = ( async ( ) => {
74
- let factory = await processFactoryPromise ;
79
+ let factory = await this . _processFactoryPromise ;
75
80
// TODO(strager): Reuse processes across documents.
76
81
let process = await factory . createProcessAsync ( ) ;
77
82
let parser = await process . createDocumentForVSCodeAsync ( ) ;
@@ -118,7 +123,7 @@ class DocumentLinter {
118
123
default :
119
124
throw new Error ( `Unexpected linter state: ${ this . _state } ` ) ;
120
125
}
121
- this . _diagnosticCollection . delete ( this . _document . uri ) ;
126
+ this . _document . removeDiagnostics ( ) ;
122
127
}
123
128
124
129
dispose ( ) {
@@ -185,7 +190,7 @@ class DocumentLinter {
185
190
// END CRITICAL SECTION (no awaiting above)
186
191
187
192
let diags = this . _parser . lint ( ) ;
188
- this . _updateDocumentDiagnostics ( diags ) ;
193
+ this . _document . setDiagnostics ( diags ) ;
189
194
} catch ( e ) {
190
195
// END CRITICAL SECTION (no awaiting above)
191
196
if ( e instanceof ProcessCrashed ) {
@@ -229,7 +234,7 @@ class DocumentLinter {
229
234
// END CRITICAL SECTION (no awaiting above)
230
235
231
236
let diags = this . _parser . lint ( ) ;
232
- this . _updateDocumentDiagnostics ( diags ) ;
237
+ this . _document . setDiagnostics ( diags ) ;
233
238
} catch ( e ) {
234
239
if ( e instanceof ProcessCrashed ) {
235
240
await this . _recoverFromCrashAsync ( e ) ;
@@ -250,7 +255,7 @@ class DocumentLinter {
250
255
let diags ;
251
256
try {
252
257
// TODO(strager): Reuse processes across documents.
253
- let factory = await processFactoryPromise ;
258
+ let factory = await this . _processFactoryPromise ;
254
259
let process = await factory . createProcessAsync ( ) ;
255
260
let parser = await process . createDocumentForVSCodeAsync ( ) ;
256
261
@@ -279,20 +284,51 @@ class DocumentLinter {
279
284
throw e ;
280
285
}
281
286
}
282
- this . _updateDocumentDiagnostics ( diags ) ;
287
+ this . _document . setDiagnostics ( diags ) ;
283
288
} ) ( ) ;
284
289
// END CRITICAL SECTION (no awaiting above)
285
290
await this . _recoveryPromise ;
286
291
}
292
+ }
287
293
288
- _updateDocumentDiagnostics ( qljsDiagnostics ) {
289
- let diagnostics = qljsDiagnostics . map ( ( diag ) =>
290
- this . _qljsDiagnosticToVSCodeDiagnostic ( diag )
294
+ class VSCodeDocumentLinter {
295
+ constructor ( document , diagnosticCollection ) {
296
+ this . _documentLinter = new DocumentLinter (
297
+ {
298
+ getText ( ) {
299
+ return document . getText ( ) ;
300
+ } ,
301
+ setDiagnostics ( qljsDiagnostics ) {
302
+ let vscodeDiagnostics = qljsDiagnostics . map ( ( diag ) =>
303
+ VSCodeDocumentLinter . _qljsDiagnosticToVSCodeDiagnostic ( diag )
304
+ ) ;
305
+ diagnosticCollection . set ( document . uri , vscodeDiagnostics ) ;
306
+ } ,
307
+ removeDiagnostics ( ) {
308
+ diagnosticCollection . delete ( document . uri ) ;
309
+ } ,
310
+ } ,
311
+ processFactoryPromise
291
312
) ;
292
- this . _diagnosticCollection . set ( this . _document . uri , diagnostics ) ;
293
313
}
294
314
295
- _qljsDiagnosticToVSCodeDiagnostic ( diag ) {
315
+ async disposeAsync ( ) {
316
+ await this . _documentLinter . disposeAsync ( ) ;
317
+ }
318
+
319
+ dispose ( ) {
320
+ this . _documentLinter . dispose ( ) ;
321
+ }
322
+
323
+ async editorChangedVisibilityAsync ( ) {
324
+ await this . _documentLinter . editorChangedVisibilityAsync ( ) ;
325
+ }
326
+
327
+ async textChangedAsync ( changes ) {
328
+ await this . _documentLinter . textChangedAsync ( changes ) ;
329
+ }
330
+
331
+ static _qljsDiagnosticToVSCodeDiagnostic ( diag ) {
296
332
let vsCodeSeverity ;
297
333
switch ( diag . severity ) {
298
334
case DiagnosticSeverity . ERROR :
@@ -326,15 +362,15 @@ class DocumentLinterCollection {
326
362
constructor ( diagnosticCollection ) {
327
363
this . _diagnosticCollection = diagnosticCollection ;
328
364
329
- // Mapping from URI string to DocumentLinter .
365
+ // Mapping from URI string to VSCodeDocumentLinter .
330
366
this . _linters = new Map ( ) ;
331
367
}
332
368
333
369
getLinter ( document ) {
334
370
let documentURIString = document . uri . toString ( ) ;
335
371
let linter = this . _linters . get ( documentURIString ) ;
336
372
if ( typeof linter === "undefined" ) {
337
- linter = new DocumentLinter ( document , this . _diagnosticCollection ) ;
373
+ linter = new VSCodeDocumentLinter ( document , this . _diagnosticCollection ) ;
338
374
this . _linters . set ( documentURIString , linter ) ;
339
375
}
340
376
return linter ;
0 commit comments