Skip to content

Commit eff5954

Browse files
committed
Add warning for Linguist captures bug
1 parent 5221127 commit eff5954

File tree

5 files changed

+69
-13
lines changed

5 files changed

+69
-13
lines changed

src/DiagnosticCollection.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ async function Diagnostics(document: vscode.TextDocument) {
200200
diagnosticsOnigurumaRegexErrors(diagnostics, trees),
201201
diagnosticsBrokenIncludes(diagnostics, rootNode),
202202
diagnosticsUnusedRepos(diagnostics, rootNode),
203+
diagnosticsLinguistCaptures(diagnostics, rootNode),
203204
diagnosticsDeadTextMateCode(diagnostics, rootNode),
204205
]);
205206

@@ -881,3 +882,42 @@ async function diagnosticsMismatchingRootScopeName(diagnostics: vscode.Diagnosti
881882
}
882883
// vscode.window.showInformationMessage(`scopeName ${(performance.now() - start).toFixed(3)}ms`);
883884
}
885+
886+
async function diagnosticsLinguistCaptures(diagnostics: vscode.Diagnostic[], rootNode: webTreeSitter.Node) {
887+
// vscode.window.showInformationMessage(JSON.stringify("diagnostics (captures)"))
888+
// const start = performance.now();
889+
890+
const capturesQuery = `;scm
891+
(captures "{" (_ (key) @captures))
892+
(beginCaptures "{" (_ (key) @beginCaptures))
893+
(endCaptures "{" (_ (key) @endCaptures))
894+
(whileCaptures "{" (_ (key) @whileCaptures))
895+
(captures "{" (_ "{") @object)
896+
(beginCaptures "{" (_ "{") @object)
897+
(endCaptures "{" (_ "{") @object)
898+
(whileCaptures "{" (_ "{") @object)
899+
`;
900+
const capturesCaptures = queryNode(rootNode, capturesQuery);
901+
// vscode.window.showInformationMessage(`(captures) ${JSON.stringify(capturesCaptures)} ${(performance.now() - start).toFixed(3)}ms`);
902+
for (const captureCapture of capturesCaptures) {
903+
const name = captureCapture.name;
904+
if (name === 'object') {
905+
diagnostics.pop();
906+
continue;
907+
}
908+
909+
const node = captureCapture.node;
910+
911+
const range = toRange(node);
912+
const diagnostic: vscode.Diagnostic = {
913+
range: range,
914+
message: 'Incompatible with Github-Linguist. Only object values are supported within "captures"',
915+
severity: vscode.DiagnosticSeverity.Warning,
916+
source: 'TextMate',
917+
code: 'captures',
918+
};
919+
diagnostics.push(diagnostic);
920+
}
921+
922+
// vscode.window.showInformationMessage(`(captures) ${(performance.now() - start).toFixed(3)}ms`);
923+
}

src/Providers/DocumentSymbolProvider.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,24 @@ export const DocumentSymbolProvider: vscode.DocumentSymbolProvider = {
165165

166166
function newDocumentSymbol(node: webTreeSitter.Node): vscode.DocumentSymbol {
167167
let text = '';
168+
let index = 0;
168169
switch (node.type) {
169-
case 'pattern':
170+
case 'repo': // objects
171+
case 'capture':
172+
const firstChild = node.firstNamedChild;
173+
if (firstChild?.type == 'key') {
174+
text = firstChild.text;
175+
break;
176+
}
177+
index = -1;
178+
case 'pattern': // arrays
170179
case 'injection':
171-
let index = 0;
172180
let sibling: webTreeSitter.Node | null = node;
173181
while (sibling = sibling.previousNamedSibling) {
174182
index++;
175183
}
176184
text = index.toString();
177185
break;
178-
case 'repo':
179-
case 'capture':
180-
text = node.firstNamedChild!.text;
181-
break;
182186
case 'name':
183187
text = 'name';
184188
break;

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export async function activate(context: vscode.ExtensionContext) {
4242
await initTreeSitter(context);
4343
await initOniguruma(context);
4444
initTextMate(context);
45-
initDiagnostics(context);
45+
await initDiagnostics(context);
4646
initCallStackView(context);
4747
initTokenColorCustomizations(context);
4848
initThemeScopes(context);

src/tree-sitter/tree-sitter-json/grammar.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = grammar({
77
name: "jsontm",
88
// word: $ => $._string,
99
extras: $ => [
10-
//$._whitespace,
10+
// $._whitespace,
1111
],
1212
externals: $ => [
1313
$._forceStringNode, // Forces a 0width empty node if it is before a double quote " . Useful when querying the resulting syntax tree
@@ -459,7 +459,10 @@ module.exports = grammar({
459459
),
460460
),
461461
array($,
462-
$._pattern,
462+
alias(
463+
$._pattern,
464+
$.capture,
465+
),
463466
),
464467
),
465468
),
@@ -474,7 +477,10 @@ module.exports = grammar({
474477
),
475478
),
476479
array($,
477-
$._pattern,
480+
alias(
481+
$._pattern,
482+
$.capture,
483+
),
478484
),
479485
),
480486
),
@@ -489,7 +495,10 @@ module.exports = grammar({
489495
),
490496
),
491497
array($,
492-
$._pattern,
498+
alias(
499+
$._pattern,
500+
$.capture,
501+
),
493502
),
494503
),
495504
),
@@ -504,7 +513,10 @@ module.exports = grammar({
504513
),
505514
),
506515
array($,
507-
$._pattern,
516+
alias(
517+
$._pattern,
518+
$.capture,
519+
),
508520
),
509521
),
510522
),

vscode.tmLanguage.schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@
842842
"default": { }
843843
},
844844
"items": {
845-
"markdownDescription": "An array of `\"captures\"` is not officially supported by VSCode TextMate \nBut still happens to work because JavaScript's for-in works with both object (keys) array (indexes)",
845+
"markdownDescription": "An array of `\"captures\"` is not officially supported by VSCode TextMate \nBut still happens to work because JavaScript's for-in works with both object (keys) and array (indexes)\n It is **not** supported by TextMate 2.0 or Github-Linguist",
846846
"$ref": "#/definitions/capture"
847847
},
848848
"defaultSnippets": [

0 commit comments

Comments
 (0)