Skip to content

Commit fe62e8d

Browse files
committed
Improve Diagnostics performance
1 parent 0d8c389 commit fe62e8d

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/DiagnosticCollection.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ async function Diagnostics(document: vscode.TextDocument) {
105105

106106
const diagnostics: vscode.Diagnostic[] = [];
107107

108-
await Promise.all([
108+
const results = await Promise.allSettled([
109109
diagnosticsMismatchingRootScopeName(diagnostics, rootNode, document),
110110
diagnosticsTreeSitterJSONErrors(diagnostics, rootNode),
111111
diagnosticsTreeSitterRegexErrors(diagnostics, trees),
@@ -115,12 +115,18 @@ async function Diagnostics(document: vscode.TextDocument) {
115115
diagnosticsDeadTextMateCode(diagnostics, rootNode),
116116
]);
117117

118+
for (const result of results) {
119+
if (result.status == 'rejected') {
120+
console.warn("JSON TextMate: Diagnostics error\n", result.reason);
121+
}
122+
}
123+
118124
DiagnosticCollection.set(document.uri, diagnostics);
119125
// vscode.window.showInformationMessage(`Diagnostics ${(performance.now() - start).toFixed(3)}ms\n${JSON.stringify(diagnostics)}`);
120126
}
121127

122128

123-
function diagnosticsTreeSitterJSONErrors(diagnostics: vscode.Diagnostic[], rootNode: Node) {
129+
async function diagnosticsTreeSitterJSONErrors(diagnostics: vscode.Diagnostic[], rootNode: Node) {
124130
// vscode.window.showInformationMessage(JSON.stringify("diagnostics JSON"));
125131
// const start = performance.now();
126132
const jsonQuery = `;scm
@@ -168,7 +174,7 @@ function diagnosticsTreeSitterJSONErrors(diagnostics: vscode.Diagnostic[], rootN
168174
// vscode.window.showInformationMessage(`JSON ${(performance.now() - start).toFixed(3)}ms`);
169175
}
170176

171-
function diagnosticsTreeSitterRegexErrors(diagnostics: vscode.Diagnostic[], trees: trees) {
177+
async function diagnosticsTreeSitterRegexErrors(diagnostics: vscode.Diagnostic[], trees: trees) {
172178
// vscode.window.showInformationMessage(JSON.stringify("diagnostics Regex"));
173179
// const start = performance.now();
174180
const regexTrees = trees.regexTrees;
@@ -291,7 +297,7 @@ function diagnosticsTreeSitterRegexErrors(diagnostics: vscode.Diagnostic[], tree
291297
// vscode.window.showInformationMessage(`Regex ${(performance.now() - start).toFixed(3)}ms`);
292298
}
293299

294-
function diagnosticsOnigurumaRegexErrors(diagnostics: vscode.Diagnostic[], trees: trees) {
300+
async function diagnosticsOnigurumaRegexErrors(diagnostics: vscode.Diagnostic[], trees: trees) {
295301
// vscode.window.showInformationMessage(JSON.stringify("diagnostics Regex Oniguruma"));
296302
// const start = performance.now();
297303
const regexNodes = trees.regexNodes;
@@ -369,7 +375,7 @@ function diagnosticsOnigurumaRegexErrors(diagnostics: vscode.Diagnostic[], trees
369375
// vscode.window.showInformationMessage(`Oniguruma ${(performance.now() - start).toFixed(3)}ms`);
370376
}
371377

372-
function diagnosticsBrokenIncludes(diagnostics: vscode.Diagnostic[], rootNode: Node) {
378+
async function diagnosticsBrokenIncludes(diagnostics: vscode.Diagnostic[], rootNode: Node) {
373379
// vscode.window.showInformationMessage(JSON.stringify("diagnostics #includes"))
374380
// const start = performance.now();
375381

@@ -460,7 +466,7 @@ function diagnosticsBrokenIncludes(diagnostics: vscode.Diagnostic[], rootNode: N
460466
repoItems.push(repoCapture.node.text);
461467
}
462468
const distances = wagnerFischer(text, repoItems);
463-
const distance = distances[0].distance;
469+
const distance = distances[0]?.distance;
464470

465471
let message = `Cannot find repo name '${text}'`;
466472
if (closeEnoughQuestionMark(distance, text)) {
@@ -530,13 +536,15 @@ function diagnosticsBrokenIncludes(diagnostics: vscode.Diagnostic[], rootNode: N
530536
// vscode.window.showInformationMessage(`include ${(performance.now() - start).toFixed(3)}ms`);
531537
}
532538

533-
function diagnosticsUnusedRepos(diagnostics: vscode.Diagnostic[], rootNode: Node) {
539+
async function diagnosticsUnusedRepos(diagnostics: vscode.Diagnostic[], rootNode: Node) {
534540
if (ignoreDiagnosticsUnusedRepos) {
535541
return;
536542
}
537-
// vscode.window.showInformationMessage(`diagnostics #includes\n${JSON.stringify(rootNode)}`)
543+
// vscode.window.showInformationMessage(`diagnostics unusedRepos\n${JSON.stringify(rootNode)}`)
538544
// const start = performance.now();
539545

546+
const includeCapturesCache: { [id: number]: QueryCapture[]; } = {};
547+
540548
// should validate all #include first
541549
// but TS too slow
542550
const includeQuery = `;scm
@@ -552,21 +560,14 @@ function diagnosticsUnusedRepos(diagnostics: vscode.Diagnostic[], rootNode: Node
552560
const repoText = repoNode.text;
553561

554562
const repositoryParentNode = repoNode.parent!.parent!.parent!;
555-
const includeCaptures = queryNode(repositoryParentNode, includeQuery);
563+
const repoParentId = repositoryParentNode.id;
556564

557-
let foundInclude = false;
558-
for (const includeCapture of includeCaptures) {
559-
const includeText = includeCapture.node.text;
560-
if (repoText == includeText) {
561-
foundInclude = true;
562-
break;
563-
}
564-
}
565-
if (foundInclude) {
566-
continue;
565+
if (!includeCapturesCache[repoParentId]) {
566+
includeCapturesCache[repoParentId] = queryNode(repositoryParentNode, includeQuery);
567567
}
568568

569-
for (const includeCapture of includeCaptures) {
569+
let foundInclude = false;
570+
for (const includeCapture of includeCapturesCache[repoParentId]) {
570571
const includeText = includeCapture.node.text;
571572
if (repoText == includeText) {
572573
foundInclude = true;
@@ -587,10 +588,10 @@ function diagnosticsUnusedRepos(diagnostics: vscode.Diagnostic[], rootNode: Node
587588
});
588589
}
589590

590-
// vscode.window.showInformationMessage(`include ${(performance.now() - start).toFixed(3)}ms`);
591+
// vscode.window.showInformationMessage(`unusedRepos ${(performance.now() - start).toFixed(3)}ms`);
591592
}
592593

593-
function diagnosticsDeadTextMateCode(diagnostics: vscode.Diagnostic[], rootNode: Node) {
594+
async function diagnosticsDeadTextMateCode(diagnostics: vscode.Diagnostic[], rootNode: Node) {
594595
// vscode.window.showInformationMessage(JSON.stringify("diagnostics TextMate dead"));
595596
// const start = performance.now();
596597

0 commit comments

Comments
 (0)