@@ -2,7 +2,6 @@ import * as vscode from 'vscode';
2
2
import * as vscodeOniguruma from 'vscode-oniguruma' ;
3
3
import { getTrees , jsonParserLanguage , queryNode , toRange , trueParent } from "./TreeSitter" ;
4
4
import { DocumentSelector } from "./extension" ;
5
- import { Query , QueryOptions } from 'web-tree-sitter' ;
6
5
import { unicodeproperties } from "./UNICODE_PROPERTIES" ;
7
6
8
7
@@ -27,32 +26,12 @@ type OnigScanner = vscodeOniguruma.OnigScanner & {
27
26
readonly _options : vscodeOniguruma . FindOption [ ] ;
28
27
} ;
29
28
30
- let repoQuery : Query ;
31
29
32
30
export function initDiagnostics ( context : vscode . ExtensionContext ) {
33
31
// vscode.window.showInformationMessage(JSON.stringify("initDiagnostics"));
34
32
const DiagnosticCollection = vscode . languages . createDiagnosticCollection ( "textmate" ) ;
35
33
context . subscriptions . push ( DiagnosticCollection ) ;
36
34
37
- const repoQueryString = `
38
- (repo
39
- (include) (repository
40
- (repo
41
- (key) @nestRepo))
42
- !match !begin)
43
- (repo
44
- (patterns) (repository
45
- (repo
46
- (key) @nestRepo))
47
- !match !begin)
48
- (pattern
49
- (patterns) (repository
50
- (repo
51
- (key) @nestRepo))
52
- !match !begin)
53
- ` ;
54
- repoQuery = jsonParserLanguage . query ( repoQueryString ) ;
55
-
56
35
const activeDocuments : {
57
36
[ uriString : string ] : {
58
37
edits : vscode . TextDocumentChangeEvent ;
@@ -321,27 +300,46 @@ function Diagnostics(document: vscode.TextDocument, Diagnostics: vscode.Diagnost
321
300
// vscode.window.showInformationMessage(JSON.stringify("diagnostics #includes"))
322
301
// const start = performance.now();
323
302
324
- const includeCaptures = queryNode ( rootNode , `(include (value !scopeName (ruleName) @include))` ) ;
303
+ let prevParent ;
304
+ let errorCount ;
305
+
306
+ const repoQueryString = `
307
+ (repo
308
+ [(include) (patterns)] (repository
309
+ (repo
310
+ (key) @nestRepo))
311
+ !match !begin)
312
+ (repo
313
+ (repository
314
+ (repo
315
+ (key) @nestRepo)) [(include) (patterns)]
316
+ !match !begin)
317
+ (pattern
318
+ (patterns) (repository
319
+ (repo
320
+ (key) @nestRepo))
321
+ !match !begin)
322
+ (pattern
323
+ (repository
324
+ (repo
325
+ (key) @nestRepo)) (patterns)
326
+ !match !begin)
327
+ (capture
328
+ (patterns) (repository
329
+ (repo
330
+ (key) @nestRepo))
331
+ !match !begin)
332
+ (capture
333
+ (repository
334
+ (repo
335
+ (key) @nestRepo)) (patterns)
336
+ !match !begin)
337
+ ` ;
325
338
326
339
const rootRepoQuery = `(json (repository (repo (key) @rootRepo)))` ;
327
340
const rootRepoCaptures = queryNode ( rootNode , rootRepoQuery ) ;
328
341
329
- // const repoQueryString = `
330
- // ;(json
331
- // ; (repository
332
- // ; (repo
333
- // ; (key) @rootRepo)))
334
- // (repo
335
- // ; [(patterns) (include)] (repository ; causes extra 70ms lag
336
- // (repository
337
- // (repo
338
- // (key) @nestRepo))
339
- // !match !begin)
340
- // `;
341
- // const language = jsonTree.getLanguage();
342
- // const repoQuery = language.query(repoQueryString);
343
- // const queryCaptures = repoQuery.captures(node, startPoint, endPoint || startPoint);
344
-
342
+ const includeCaptures = queryNode ( rootNode , `(include (value !scopeName (ruleName) @include))` ) ;
345
343
for ( const includeCapture of includeCaptures ) {
346
344
const node = includeCapture . node ;
347
345
const text = node . text ;
@@ -359,20 +357,12 @@ function Diagnostics(document: vscode.TextDocument, Diagnostics: vscode.Diagnost
359
357
continue ;
360
358
}
361
359
362
- const queryOptions : QueryOptions = {
363
- startPosition : node . startPosition ,
364
- endPosition : node . endPosition ,
365
- } ;
366
- const repoMatches = repoQuery . matches ( rootNode , queryOptions ) ;
367
- // vscode.window.showInformationMessage(JSON.stringify(repoMatches));
368
- for ( const repoMatch of repoMatches ) {
369
- const repoCaptures = repoMatch . captures ;
370
- for ( const repoCapture of repoCaptures ) {
371
- const repoText = repoCapture . node . text ;
372
- if ( repoText == text ) {
373
- match = true ;
374
- break ;
375
- }
360
+ const repoCaptures = queryNode ( rootNode , repoQueryString , node . startPosition , node . endPosition ) ;
361
+ for ( const repoCapture of repoCaptures ) {
362
+ const repoText = repoCapture . node . text ;
363
+ if ( repoText == text ) {
364
+ match = true ;
365
+ break ;
376
366
}
377
367
}
378
368
if ( match ) {
@@ -383,11 +373,27 @@ function Diagnostics(document: vscode.TextDocument, Diagnostics: vscode.Diagnost
383
373
const diagnostic : vscode . Diagnostic = {
384
374
range : range ,
385
375
message : `'${ text } ' was not found in a repository.` ,
386
- severity : vscode . DiagnosticSeverity . Error ,
376
+ severity : vscode . DiagnosticSeverity . Warning ,
387
377
source : 'TextMate' ,
388
378
code : 'include' ,
389
379
} ;
390
380
diagnostics . push ( diagnostic ) ;
381
+
382
+ // Change `severity` to `Error` if every single `#include` cannot be found inside a `"patterns"` array
383
+ const parent = node . parent . parent . parent . parent ;
384
+ if ( prevParent != parent . id ) {
385
+ errorCount = 0 ;
386
+ }
387
+
388
+ errorCount ++ ;
389
+ if ( parent . namedChildCount - 1 == errorCount ) {
390
+ for ( let index = diagnostics . length - errorCount ; index < diagnostics . length ; index ++ ) {
391
+ const diagnostic = diagnostics [ index ] ;
392
+ diagnostic . severity = vscode . DiagnosticSeverity . Error ;
393
+ diagnostics [ index ] = diagnostic ;
394
+ }
395
+ }
396
+ prevParent = parent . id ;
391
397
}
392
398
// vscode.window.showInformationMessage(performance.now() - start + "ms");
393
399
}
0 commit comments