Skip to content

Commit 45abebb

Browse files
committed
Only error #includes when all error
1 parent bd4a2ba commit 45abebb

File tree

1 file changed

+59
-53
lines changed

1 file changed

+59
-53
lines changed

src/DiagnosticCollection.ts

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as vscode from 'vscode';
22
import * as vscodeOniguruma from 'vscode-oniguruma';
33
import { getTrees, jsonParserLanguage, queryNode, toRange, trueParent } from "./TreeSitter";
44
import { DocumentSelector } from "./extension";
5-
import { Query, QueryOptions } from 'web-tree-sitter';
65
import { unicodeproperties } from "./UNICODE_PROPERTIES";
76

87

@@ -27,32 +26,12 @@ type OnigScanner = vscodeOniguruma.OnigScanner & {
2726
readonly _options: vscodeOniguruma.FindOption[];
2827
};
2928

30-
let repoQuery: Query;
3129

3230
export function initDiagnostics(context: vscode.ExtensionContext) {
3331
// vscode.window.showInformationMessage(JSON.stringify("initDiagnostics"));
3432
const DiagnosticCollection = vscode.languages.createDiagnosticCollection("textmate");
3533
context.subscriptions.push(DiagnosticCollection);
3634

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-
5635
const activeDocuments: {
5736
[uriString: string]: {
5837
edits: vscode.TextDocumentChangeEvent;
@@ -321,27 +300,46 @@ function Diagnostics(document: vscode.TextDocument, Diagnostics: vscode.Diagnost
321300
// vscode.window.showInformationMessage(JSON.stringify("diagnostics #includes"))
322301
// const start = performance.now();
323302

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+
`;
325338

326339
const rootRepoQuery = `(json (repository (repo (key) @rootRepo)))`;
327340
const rootRepoCaptures = queryNode(rootNode, rootRepoQuery);
328341

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))`);
345343
for (const includeCapture of includeCaptures) {
346344
const node = includeCapture.node;
347345
const text = node.text;
@@ -359,20 +357,12 @@ function Diagnostics(document: vscode.TextDocument, Diagnostics: vscode.Diagnost
359357
continue;
360358
}
361359

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;
376366
}
377367
}
378368
if (match) {
@@ -383,11 +373,27 @@ function Diagnostics(document: vscode.TextDocument, Diagnostics: vscode.Diagnost
383373
const diagnostic: vscode.Diagnostic = {
384374
range: range,
385375
message: `'${text}' was not found in a repository.`,
386-
severity: vscode.DiagnosticSeverity.Error,
376+
severity: vscode.DiagnosticSeverity.Warning,
387377
source: 'TextMate',
388378
code: 'include',
389379
};
390380
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;
391397
}
392398
// vscode.window.showInformationMessage(performance.now() - start + "ms");
393399
}

0 commit comments

Comments
 (0)