Skip to content

Commit efb534d

Browse files
committed
feat: Ignore incompatible regex engine warnings in VSCode Markdown injection grammars
1 parent 9cb5177 commit efb534d

6 files changed

Lines changed: 79 additions & 43 deletions

File tree

src/DiagnosticCollection.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,36 @@ function diagnosticsRegularExpressionErrors(diagnostics: Diagnostic[], document:
418418
const trees = getTrees(document);
419419
const regexNodes = trees.regexNodes;
420420

421+
const isMarkdownCodeblockInjectionRegex = /(\b|_)(markdown|markdownblock|codeblock|inject|injection)(\b|_)/;
422+
let isMarkdownCodeblockInjectionGrammar = 0;
423+
if (isMarkdownCodeblockInjectionRegex.test(document.fileName)) {
424+
isMarkdownCodeblockInjectionGrammar++;
425+
}
426+
const injectionScopesQuery = `;scm
427+
(name_display (value) @name)
428+
(scopeName (value) @scopeName)
429+
(injectionSelector (value (selector) . (scope) @injection))
430+
`;
431+
const injectionCaptures = queryNode(trees.jsonTree.rootNode, injectionScopesQuery);
432+
for (const injectionCapture of injectionCaptures) {
433+
const text = injectionCapture.node.text;
434+
// vscode.window.showInformationMessage(text);
435+
switch (injectionCapture.name) {
436+
case 'name':
437+
case 'scopeName':
438+
if (isMarkdownCodeblockInjectionRegex.test(text)) {
439+
isMarkdownCodeblockInjectionGrammar++;
440+
}
441+
break;
442+
case 'injection':
443+
if (text == 'text.html.markdown'
444+
|| text == 'markup.fenced_code.block.markdown') {
445+
isMarkdownCodeblockInjectionGrammar++;
446+
}
447+
break;
448+
}
449+
}
450+
421451
for (const regexNode of regexNodes.values()) {
422452
const text = regexNode.text;
423453
const key = regexNode.previousNamedSibling;
@@ -744,7 +774,7 @@ function diagnosticsRegularExpressionErrors(diagnostics: Diagnostic[], document:
744774
diagnostics.push({
745775
range: range,
746776
message: `Regex incompatible with TextMate 2.0${timeOnigmo >= 1 ? timeMessageOnigmo : ''} (Onigmo v5.13.5)\n${errorCodeOnigmo}`,
747-
severity: vscode.DiagnosticSeverity.Warning,
777+
severity: isMarkdownCodeblockInjectionGrammar > 1 ? vscode.DiagnosticSeverity.Hint : vscode.DiagnosticSeverity.Warning,
748778
source: 'TextMate',
749779
code: 'Onigmo',
750780
});
@@ -754,7 +784,7 @@ function diagnosticsRegularExpressionErrors(diagnostics: Diagnostic[], document:
754784
diagnostics.push({
755785
range: range,
756786
message: `Regex incompatible with Github-Linguist${timePCRE >= 1 ? timeMessagePCRE : ''} (PCRE v8.36)\n${errorCodePCRE}`,
757-
severity: vscode.DiagnosticSeverity.Warning,
787+
severity: isMarkdownCodeblockInjectionGrammar > 1 ? vscode.DiagnosticSeverity.Hint : vscode.DiagnosticSeverity.Warning,
758788
source: 'TextMate',
759789
code: 'PCRE',
760790
});
@@ -764,7 +794,7 @@ function diagnosticsRegularExpressionErrors(diagnostics: Diagnostic[], document:
764794
diagnostics.push({
765795
range: range,
766796
message: `Regex incompatible with Shiki${timeES >= 1 ? timeMessageES : ''} (oniguruma-to-es)\n${errorCodeES}`,
767-
severity: vscode.DiagnosticSeverity.Warning,
797+
severity: isMarkdownCodeblockInjectionGrammar > 1 ? vscode.DiagnosticSeverity.Hint : vscode.DiagnosticSeverity.Warning,
768798
source: 'TextMate',
769799
code: 'ES',
770800
});
@@ -1358,7 +1388,7 @@ function diagnosticsScopePostfix(diagnostics: Diagnostic[], document: vscode.Tex
13581388
if (scope.endsWith(candidatePostfix)) {
13591389
if (candidatePostfixes.scopes[scope] == 1
13601390
&& scope.length - candidatePostfix.length > 3) {
1361-
const spellingSuggestion = getSpellingSuggestion(scope, commonScopes, 1.99);
1391+
const spellingSuggestion = getSpellingSuggestion(scope, commonScopes.filter(scope => scope.endsWith(candidatePostfix)), 1.99);
13621392
if (spellingSuggestion) {
13631393
diagnostics.push({
13641394
range,
@@ -1428,7 +1458,7 @@ function diagnosticsScopePostfix(diagnostics: Diagnostic[], document: vscode.Tex
14281458
}
14291459

14301460
diagnostics.push({
1431-
range,
1461+
range: new vscode.Range(range.end, range.end),
14321462
message: `ScopeName '${scope}' missing postfix '.${candidatePostfixes.candidatePostfixes[0]}'`,
14331463
severity: diagnosticSeverity,
14341464
source: 'TextMate',

src/Providers/CompletionItemProvider.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ export function findCandidateScopePostfixes(rootNode: webTreeSitter.Node, positi
898898
for (let index = 0; index < scopePostfixParts.length; index++) {
899899
const scopePostfix = scopePostfixParts.slice(index).join('.');
900900
candidateScopePostfixes[scopePostfix] ??= 0;
901-
candidateScopePostfixes[scopePostfix]++;
901+
candidateScopePostfixes[scopePostfix] += scopeCapture.name == 'injectionScope' ? 0.5 : 1;
902902
}
903903

904904
if (scopeCapture.name == 'includeScope') {
@@ -944,7 +944,10 @@ export function findCandidateScopePostfixes(rootNode: webTreeSitter.Node, positi
944944
return 0;
945945
}
946946
)
947-
.filter(candidate => candidate[1] > 3)
947+
.filter(
948+
(candidate, index, candidates) =>
949+
candidate[1] > 3 /* || candidate[1] >= candidates[4][1] */
950+
)
948951
.filter(
949952
(candidate, index, candidates) =>
950953
!candidates[0][0].endsWith('.' + candidate[0])
@@ -955,7 +958,7 @@ export function findCandidateScopePostfixes(rootNode: webTreeSitter.Node, positi
955958
candidatePostfixes.push(rootScopeNamePostfixParts?.slice(1).join('.'));
956959
}
957960

958-
// vscode.window.showInformationMessage(`candidateScopePostfixes ${(performance.now() - start).toFixed(3)}ms ${JSON.stringify(candidatePostfixes)}`);
961+
// vscode.window.showInformationMessage(`candidateScopePostfixes ${(performance.now() - start).toFixed(3)}ms\n${JSON.stringify(candidatePostfixes)}\n${JSON.stringify(candidateScopePostfixes)}`);
959962

960963
return {
961964
cursorScope,

src/test/baselines/CodeActionsProvider.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"diagnostics": [
3939
{
4040
"startLineNumber": 57,
41-
"startColumn": 20,
41+
"startColumn": 78,
4242
"endLineNumber": 0,
4343
"endColumn": 78,
4444
"message": "ScopeName 'TextMate.2 0 only. maybe GitHub. scopeName removes warning' missing postfix '.diagnostics'",
@@ -54,7 +54,7 @@
5454
"text": ".diagnostics",
5555
"range": {
5656
"startLineNumber": 0,
57-
"startColumn": 20,
57+
"startColumn": 78,
5858
"endLineNumber": 0,
5959
"endColumn": 78
6060
}
@@ -72,7 +72,7 @@
7272
"diagnostics": [
7373
{
7474
"startLineNumber": 1,
75-
"startColumn": 15,
75+
"startColumn": 31,
7676
"endLineNumber": 0,
7777
"endColumn": 31,
7878
"message": "ScopeName 'string.scope.one' missing postfix '.diagnostics'",
@@ -88,7 +88,7 @@
8888
"text": ".diagnostics",
8989
"range": {
9090
"startLineNumber": 0,
91-
"startColumn": 15,
91+
"startColumn": 31,
9292
"endLineNumber": 0,
9393
"endColumn": 31
9494
}
@@ -106,7 +106,7 @@
106106
"diagnostics": [
107107
{
108108
"startLineNumber": 0,
109-
"startColumn": 32,
109+
"startColumn": 49,
110110
"endLineNumber": 0,
111111
"endColumn": 49,
112112
"message": "ScopeName 'comment.scope.two' missing postfix '.diagnostics'",
@@ -122,7 +122,7 @@
122122
"text": ".diagnostics",
123123
"range": {
124124
"startLineNumber": 0,
125-
"startColumn": 32,
125+
"startColumn": 49,
126126
"endLineNumber": 0,
127127
"endColumn": 49
128128
}
@@ -242,7 +242,7 @@
242242
"diagnostics": [
243243
{
244244
"startLineNumber": 5,
245-
"startColumn": 13,
245+
"startColumn": 23,
246246
"endLineNumber": 0,
247247
"endColumn": 23,
248248
"message": "ScopeName 'string.end' missing postfix '.diagnostics'",
@@ -258,7 +258,7 @@
258258
"text": ".diagnostics",
259259
"range": {
260260
"startLineNumber": 0,
261-
"startColumn": 13,
261+
"startColumn": 23,
262262
"endLineNumber": 0,
263263
"endColumn": 23
264264
}
@@ -276,7 +276,7 @@
276276
"diagnostics": [
277277
{
278278
"startLineNumber": 1,
279-
"startColumn": 13,
279+
"startColumn": 23,
280280
"endLineNumber": 0,
281281
"endColumn": 23,
282282
"message": "ScopeName 'string.two' missing postfix '.diagnostics'",
@@ -292,7 +292,7 @@
292292
"text": ".diagnostics",
293293
"range": {
294294
"startLineNumber": 0,
295-
"startColumn": 13,
295+
"startColumn": 23,
296296
"endLineNumber": 0,
297297
"endColumn": 23
298298
}
@@ -412,7 +412,7 @@
412412
"diagnostics": [
413413
{
414414
"startLineNumber": 1,
415-
"startColumn": 13,
415+
"startColumn": 44,
416416
"endLineNumber": 0,
417417
"endColumn": 44,
418418
"message": "ScopeName 'token.debug-token.trailing.dot.' missing postfix '.diagnostics'",
@@ -428,7 +428,7 @@
428428
"text": ".diagnostics",
429429
"range": {
430430
"startLineNumber": 0,
431-
"startColumn": 13,
431+
"startColumn": 44,
432432
"endLineNumber": 0,
433433
"endColumn": 44
434434
}

src/test/baselines/CompletionItemProvider.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92368,15 +92368,15 @@
9236892368
{
9236992369
"label": {
9237092370
"label": "string.quoted.double.lang",
92371-
"detail": "uageId"
92371+
"detail": ".completion"
9237292372
},
9237392373
"extensionId": {
9237492374
"value": "RedCMD.tmlanguage-syntax-highlighter",
9237592375
"_lower": "redcmd.tmlanguage-syntax-highlighter"
9237692376
},
9237792377
"kind": 18,
9237892378
"sortText": " string.quoted.double.lang",
92379-
"insertText": "string.quoted.double.languageId",
92379+
"insertText": "string.quoted.double.lang.completion",
9238092380
"range": {
9238192381
"startLineNumber": 0,
9238292382
"startColumn": 13,

src/test/baselines/DiagnosticCollection.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,7 +1693,7 @@
16931693
"range": [
16941694
{
16951695
"line": 0,
1696-
"character": 19
1696+
"character": 77
16971697
},
16981698
{
16991699
"line": 0,
@@ -1709,7 +1709,7 @@
17091709
"range": [
17101710
{
17111711
"line": 1,
1712-
"character": 14
1712+
"character": 30
17131713
},
17141714
{
17151715
"line": 0,
@@ -1725,7 +1725,7 @@
17251725
"range": [
17261726
{
17271727
"line": 0,
1728-
"character": 31
1728+
"character": 48
17291729
},
17301730
{
17311731
"line": 0,
@@ -2444,7 +2444,7 @@
24442444
"range": [
24452445
{
24462446
"line": 0,
2447-
"character": 12
2447+
"character": 22
24482448
},
24492449
{
24502450
"line": 0,
@@ -2476,7 +2476,7 @@
24762476
"range": [
24772477
{
24782478
"line": 0,
2479-
"character": 12
2479+
"character": 22
24802480
},
24812481
{
24822482
"line": 0,
@@ -2732,7 +2732,7 @@
27322732
"range": [
27332733
{
27342734
"line": 0,
2735-
"character": 12
2735+
"character": 43
27362736
},
27372737
{
27382738
"line": 0,

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -384,25 +384,28 @@ module.exports = grammar({
384384
$.injection_string,
385385
$._pattern,
386386
),
387-
injection_string: $ => choice(
388-
seq(
389-
optional($._injection_whitespace),
390-
$._injection_selector,
391-
repeat($._injection_scopes),
392-
repeat(
393-
seq(
394-
',',
395-
optional(
396-
seq(
397-
optional($._injection_whitespace),
398-
$._injection_selector,
399-
),
387+
injection_string: $ => seq(
388+
choice(
389+
seq(
390+
optional($._injection_whitespace),
391+
$._injection_selector,
392+
repeat($._injection_scopes),
393+
394+
),
395+
repeat1($._injection_scopes),
396+
),
397+
repeat(
398+
seq(
399+
',',
400+
optional(
401+
seq(
402+
optional($._injection_whitespace),
403+
$._injection_selector,
400404
),
401-
repeat($._injection_scopes),
402405
),
406+
repeat($._injection_scopes),
403407
),
404408
),
405-
repeat1($._injection_scopes),
406409
),
407410
_injection_selector: $ => choice(
408411
alias(

0 commit comments

Comments
 (0)