Skip to content

Commit cff5b6d

Browse files
committed
feat: Support all rules in injections
1 parent 7b172e1 commit cff5b6d

10 files changed

Lines changed: 596 additions & 67 deletions

src/DiagnosticCollection.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,11 @@ function diagnosticsBrokenIncludes(diagnostics: Diagnostic[], document: vscode.T
715715
(repo
716716
(key) @nestRepo))
717717
!match !begin !include)
718+
(injection
719+
&patterns (repository
720+
(repo
721+
(key) @nestRepo))
722+
!match !begin)
718723
(capture
719724
&patterns (repository
720725
(repo
@@ -913,12 +918,31 @@ function diagnosticsDeadTextMateCode(diagnostics: Diagnostic[], document: vscode
913918
(repo (include) @include &patterns !begin !match)
914919
(repo [(begin) (while) (end) (beginCaptures) (whileCaptures) (endCaptures) (contentName) (applyEndPatternLast)] @begin &match)
915920
(repo [(captures) @captures (name) @name (name_scopeName) @name] !match !begin)
921+
(repo (captures) @captures !match &begin &while &beginCaptures &whileCaptures)
922+
(repo (captures) @captures !match !while &begin &end &beginCaptures &endCaptures)
923+
(repo (endCaptures) @beginPatterns &while !match !include)
916924
(repo [(while) @while (end) @end (beginCaptures) @beginCaptures (whileCaptures) @whileCaptures (endCaptures) @endCaptures (contentName) @contentName (applyEndPatternLast) @applyEndPatternLast] !begin)
917925
(repo (whileCaptures) @whileCaptures !while)
918926
(repo [(endCaptures) @endCaptures (applyEndPatternLast) @applyEndPatternLast] !end)
919927
(repo (patterns . (key) . ) @patternsEmpty !match !include)
920928
(repo (key) @repoEmpty !match !begin !include !patterns)
921929
930+
(injection (repository) @repository !patterns !include)
931+
(injection (repository) @repository &match)
932+
(injection (repository) @repository &begin !match)
933+
(injection (patterns) @patterns &match)
934+
(injection (include) @include &match)
935+
(injection (include) @include &begin !match)
936+
(injection (include) @include &patterns !begin !match)
937+
(injection [(begin) (while) (end) (beginCaptures) (whileCaptures) (endCaptures) (contentName) (applyEndPatternLast)] @begin &match)
938+
(injection [(captures) @captures (name) @name (name_scopeName) @name] !match !begin)
939+
(injection [(while) @while (end) @end (beginCaptures) @beginCaptures (whileCaptures) @whileCaptures (endCaptures) @endCaptures (contentName) @contentName (applyEndPatternLast) @applyEndPatternLast] !begin)
940+
(injection (whileCaptures) @whileCaptures !while)
941+
(injection [(endCaptures) @endCaptures (applyEndPatternLast) @applyEndPatternLast] !end)
942+
(injection [(begin) (while) (end) (captures) (beginCaptures) (whileCaptures) (endCaptures) (name) (name_scopeName) (contentName) (applyEndPatternLast)] @whileInjection &begin &while !match)
943+
(injection (patterns . (key) . ) @patternsEmpty !match !include)
944+
(injection (key) @repoEmpty !match !begin !include !patterns)
945+
922946
(pattern (repository) @repositoryPatterns !patterns)
923947
(pattern (repository) @repositoryPatterns &match)
924948
(pattern (repository) @repositoryPatterns &begin !match)
@@ -929,6 +953,9 @@ function diagnosticsDeadTextMateCode(diagnostics: Diagnostic[], document: vscode
929953
(pattern [(begin) (while) (end) (beginCaptures) (whileCaptures) (endCaptures) (contentName) (applyEndPatternLast)] @beginPatterns &match)
930954
(pattern [(begin) (while) (end) (beginCaptures) (whileCaptures) (endCaptures) (contentName) (applyEndPatternLast)] @beginPatterns &include !match)
931955
(pattern [(captures) @captures (name) @name (name_scopeName) @name] !match !begin)
956+
(pattern (captures) @captures !match &begin &while &beginCaptures &whileCaptures)
957+
(pattern (captures) @captures !match !while &begin &end &beginCaptures &endCaptures)
958+
(pattern (endCaptures) @beginPatterns &while !match !include)
932959
(pattern [(while) @while (end) @end (beginCaptures) @beginCaptures (whileCaptures) @whileCaptures (endCaptures) @endCaptures (contentName) @contentName (applyEndPatternLast) @applyEndPatternLast] !begin)
933960
(pattern (whileCaptures) @whileCaptures !while)
934961
(pattern [(endCaptures) @endCaptures (applyEndPatternLast) @applyEndPatternLast] !end)
@@ -959,6 +986,7 @@ function diagnosticsDeadTextMateCode(diagnostics: Diagnostic[], document: vscode
959986
begin: `"begin" requires "match" to be absent.`,
960987
beginPatterns: `"begin" requires "match" and "include" to be absent.`,
961988
while: `"while" requires "begin" to be present.`,
989+
whileInjection: `"while" is required to be absent.`,
962990
end: `"end" requires "begin" to be present and "while" to be absent.`,
963991
name: `"name" requires either "match" or "begin" to be present.`,
964992
contentName: `"contentName" requires "begin" to be present.`,
@@ -1132,6 +1160,7 @@ function diagnosticsHints(diagnostics: Diagnostic[], document: vscode.TextDocume
11321160
(repo (name (value) @name (#match? @name " ")) !name_scopeName)
11331161
(pattern (name (value) @name (#match? @name " ")) !name_scopeName)
11341162
(capture (name (value) @name (#match? @name " ")) !name_scopeName)
1163+
(injection (name (value) @name (#match? @name " ")) !name_scopeName)
11351164
(contentName (value) @contentName (#match? @contentName " "))
11361165
`;
11371166
const captures = queryNode(rootNode, query);

src/TextMate.ts

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -287,37 +287,42 @@ export async function tokenizeFile(document: vscode.TextDocument, runTwice?: boo
287287
const startTime = performance.now();
288288
grammar.startTime = startTime;
289289

290-
try {
291-
for (let i = 0; i < document.lineCount; i++) {
292-
// vscode.window.showInformationMessage(JSON.stringify(grammar, stringify));
293-
const line = document.lineAt(i).text;
294-
295-
const lineTokens = grammar.tokenizeLine(line, ruleStack, 15000);
296-
// grammar.rules.pop();
297-
grammar.rules.push(undefined);
298-
ruleStack = lineTokens.ruleStack;
299-
grammar.lines.push(
300-
{
301-
tokens: lineTokens.tokens,
302-
stoppedEarly: lineTokens.stoppedEarly,
303-
time: performance.now() - startTime,
304-
// @ts-expect-error
305-
lastRule: ruleStack.ruleId || 1,
306-
rulesLength: rulesLength,
307-
}
308-
);
309-
rulesLength = grammar.rules.length;
310-
// tokenLineResults.push(
311-
// {
312-
// tokens: lineTokens.tokens,
313-
// ruleStack: structuredClone(lineTokens.ruleStack),
314-
// stoppedEarly: lineTokens.stoppedEarly,
315-
// }
316-
// );
317-
// vscode.window.showInformationMessage(JSON.stringify(ruleStack, stringify));
290+
for (let i = 0; i < document.lineCount; i++) {
291+
// vscode.window.showInformationMessage(JSON.stringify(grammar, stringify));
292+
const line = document.lineAt(i).text;
293+
294+
295+
const lineTokens = tryCatchSync(
296+
() => grammar.tokenizeLine(line, ruleStack.clone(), 15000),
297+
`tokenizeFile: Error parsing line ${i + 1}:`
298+
);
299+
// grammar.rules.pop();
300+
grammar.rules.push(undefined);
301+
302+
if (!lineTokens) {
303+
continue;
318304
}
319-
} catch (error) {
320-
vscode.window.showWarningMessage(`TextMate: CallStack: ${error?.toString() || String(error)}`);
305+
306+
ruleStack = lineTokens.ruleStack;
307+
grammar.lines.push(
308+
{
309+
tokens: lineTokens.tokens,
310+
stoppedEarly: lineTokens.stoppedEarly,
311+
time: performance.now() - startTime,
312+
// @ts-expect-error
313+
lastRule: ruleStack.ruleId || 1,
314+
rulesLength: rulesLength,
315+
}
316+
);
317+
rulesLength = grammar.rules.length;
318+
// tokenLineResults.push(
319+
// {
320+
// tokens: lineTokens.tokens,
321+
// ruleStack: structuredClone(lineTokens.ruleStack),
322+
// stoppedEarly: lineTokens.stoppedEarly,
323+
// }
324+
// );
325+
// vscode.window.showInformationMessage(JSON.stringify(ruleStack, stringify));
321326
}
322327

323328
// vscode.window.showInformationMessage(JSON.stringify(registry, stringify));

src/test/baselines/DiagnosticCollection.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/test/fixtures/DiagnosticCollection.tmLanguage.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
"comment": " diagnosticsMismatchingPackageJSONInfo ",
33
"scopeName": "source.diagnostics",
44
"injectionSelector": "L:source.js -string -comment",
5+
"injections": {
6+
"L:source.diagnostics": {
7+
"begin": "\"",
8+
"while": "^\\s*",
9+
"captures": { "0": { "name": "" } },
10+
"beginCaptures": { "0": { "name": "" } },
11+
"whileCaptures": { "0": { "name": "" } }
12+
}
13+
},
514
"patterns": [
615
{
716
"comment": " diagnosticsTreeSitterJSONErrors "

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,13 +382,7 @@ module.exports = grammar({
382382
),
383383
injection: $ => pair($,
384384
$.injection_string,
385-
object($,
386-
choice(
387-
$.patterns,
388-
$._comments,
389-
$.item,
390-
),
391-
),
385+
$._pattern,
392386
),
393387
injection_string: $ => choice(
394388
repeat1($._injection_scopes),

syntaxes/json.tmLanguage.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,15 @@
398398
"beginCaptures": { "0": { "name": "punctuation.definition.dictionary.begin.json.tm" } },
399399
"endCaptures": { "0": { "name": "punctuation.definition.dictionary.end.json.tm" } },
400400
"patterns": [
401+
{ "include": "#name" },
402+
{ "include": "#name_scopeName" },
403+
{ "include": "#match" },
404+
{ "include": "#include" },
401405
{ "include": "#patterns" },
406+
{ "include": "#captures" },
407+
{ "include": "#repository" },
408+
{ "include": "#applyEndPatternLast" },
409+
{ "include": "#id" },
402410
{ "include": "#dictionary-separator" }
403411
]
404412
},

syntaxes/tests/snaps/json.tmLanguage.json.snap

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5178,6 +5178,70 @@
51785178
# ^ source.json.textmate punctuation.separator.dictionary.key-value.json.tm
51795179
# ^ source.json.textmate
51805180
# ^ source.json.textmate punctuation.definition.array.begin.json.tm
5181+
> { "include": "#name" },
5182+
#^^^^^^^^ source.json.textmate
5183+
# ^ source.json.textmate punctuation.definition.dictionary.begin.json.tm
5184+
# ^ source.json.textmate
5185+
# ^ source.json.textmate punctuation.definition.string.begin.json.tm
5186+
# ^^^^^^^ source.json.textmate keyword.property-name.string.include.json.tm
5187+
# ^ source.json.textmate punctuation.definition.string.end.json.tm
5188+
# ^ source.json.textmate punctuation.separator.dictionary.key-value.json.tm
5189+
# ^ source.json.textmate
5190+
# ^ source.json.textmate punctuation.definition.string.begin.json.tm
5191+
# ^ source.json.textmate regex keyword.json.tm
5192+
# ^^^^ source.json.textmate regex variable.json.tm
5193+
# ^ source.json.textmate punctuation.definition.string.end.json.tm
5194+
# ^ source.json.textmate
5195+
# ^ source.json.textmate punctuation.definition.dictionary.end.json.tm
5196+
# ^ source.json.textmate punctuation.separator.array.json.tm
5197+
> { "include": "#name_scopeName" },
5198+
#^^^^^^^^ source.json.textmate
5199+
# ^ source.json.textmate punctuation.definition.dictionary.begin.json.tm
5200+
# ^ source.json.textmate
5201+
# ^ source.json.textmate punctuation.definition.string.begin.json.tm
5202+
# ^^^^^^^ source.json.textmate keyword.property-name.string.include.json.tm
5203+
# ^ source.json.textmate punctuation.definition.string.end.json.tm
5204+
# ^ source.json.textmate punctuation.separator.dictionary.key-value.json.tm
5205+
# ^ source.json.textmate
5206+
# ^ source.json.textmate punctuation.definition.string.begin.json.tm
5207+
# ^ source.json.textmate regex keyword.json.tm
5208+
# ^^^^^^^^^^^^^^ source.json.textmate regex variable.json.tm
5209+
# ^ source.json.textmate punctuation.definition.string.end.json.tm
5210+
# ^ source.json.textmate
5211+
# ^ source.json.textmate punctuation.definition.dictionary.end.json.tm
5212+
# ^ source.json.textmate punctuation.separator.array.json.tm
5213+
> { "include": "#match" },
5214+
#^^^^^^^^ source.json.textmate
5215+
# ^ source.json.textmate punctuation.definition.dictionary.begin.json.tm
5216+
# ^ source.json.textmate
5217+
# ^ source.json.textmate punctuation.definition.string.begin.json.tm
5218+
# ^^^^^^^ source.json.textmate keyword.property-name.string.include.json.tm
5219+
# ^ source.json.textmate punctuation.definition.string.end.json.tm
5220+
# ^ source.json.textmate punctuation.separator.dictionary.key-value.json.tm
5221+
# ^ source.json.textmate
5222+
# ^ source.json.textmate punctuation.definition.string.begin.json.tm
5223+
# ^ source.json.textmate regex keyword.json.tm
5224+
# ^^^^^ source.json.textmate regex variable.json.tm
5225+
# ^ source.json.textmate punctuation.definition.string.end.json.tm
5226+
# ^ source.json.textmate
5227+
# ^ source.json.textmate punctuation.definition.dictionary.end.json.tm
5228+
# ^ source.json.textmate punctuation.separator.array.json.tm
5229+
> { "include": "#include" },
5230+
#^^^^^^^^ source.json.textmate
5231+
# ^ source.json.textmate punctuation.definition.dictionary.begin.json.tm
5232+
# ^ source.json.textmate
5233+
# ^ source.json.textmate punctuation.definition.string.begin.json.tm
5234+
# ^^^^^^^ source.json.textmate keyword.property-name.string.include.json.tm
5235+
# ^ source.json.textmate punctuation.definition.string.end.json.tm
5236+
# ^ source.json.textmate punctuation.separator.dictionary.key-value.json.tm
5237+
# ^ source.json.textmate
5238+
# ^ source.json.textmate punctuation.definition.string.begin.json.tm
5239+
# ^ source.json.textmate regex keyword.json.tm
5240+
# ^^^^^^^ source.json.textmate regex variable.json.tm
5241+
# ^ source.json.textmate punctuation.definition.string.end.json.tm
5242+
# ^ source.json.textmate
5243+
# ^ source.json.textmate punctuation.definition.dictionary.end.json.tm
5244+
# ^ source.json.textmate punctuation.separator.array.json.tm
51815245
> { "include": "#patterns" },
51825246
#^^^^^^^^ source.json.textmate
51835247
# ^ source.json.textmate punctuation.definition.dictionary.begin.json.tm
@@ -5194,6 +5258,70 @@
51945258
# ^ source.json.textmate
51955259
# ^ source.json.textmate punctuation.definition.dictionary.end.json.tm
51965260
# ^ source.json.textmate punctuation.separator.array.json.tm
5261+
> { "include": "#captures" },
5262+
#^^^^^^^^ source.json.textmate
5263+
# ^ source.json.textmate punctuation.definition.dictionary.begin.json.tm
5264+
# ^ source.json.textmate
5265+
# ^ source.json.textmate punctuation.definition.string.begin.json.tm
5266+
# ^^^^^^^ source.json.textmate keyword.property-name.string.include.json.tm
5267+
# ^ source.json.textmate punctuation.definition.string.end.json.tm
5268+
# ^ source.json.textmate punctuation.separator.dictionary.key-value.json.tm
5269+
# ^ source.json.textmate
5270+
# ^ source.json.textmate punctuation.definition.string.begin.json.tm
5271+
# ^ source.json.textmate regex keyword.json.tm
5272+
# ^^^^^^^^ source.json.textmate regex variable.json.tm
5273+
# ^ source.json.textmate punctuation.definition.string.end.json.tm
5274+
# ^ source.json.textmate
5275+
# ^ source.json.textmate punctuation.definition.dictionary.end.json.tm
5276+
# ^ source.json.textmate punctuation.separator.array.json.tm
5277+
> { "include": "#repository" },
5278+
#^^^^^^^^ source.json.textmate
5279+
# ^ source.json.textmate punctuation.definition.dictionary.begin.json.tm
5280+
# ^ source.json.textmate
5281+
# ^ source.json.textmate punctuation.definition.string.begin.json.tm
5282+
# ^^^^^^^ source.json.textmate keyword.property-name.string.include.json.tm
5283+
# ^ source.json.textmate punctuation.definition.string.end.json.tm
5284+
# ^ source.json.textmate punctuation.separator.dictionary.key-value.json.tm
5285+
# ^ source.json.textmate
5286+
# ^ source.json.textmate punctuation.definition.string.begin.json.tm
5287+
# ^ source.json.textmate regex keyword.json.tm
5288+
# ^^^^^^^^^^ source.json.textmate regex variable.json.tm
5289+
# ^ source.json.textmate punctuation.definition.string.end.json.tm
5290+
# ^ source.json.textmate
5291+
# ^ source.json.textmate punctuation.definition.dictionary.end.json.tm
5292+
# ^ source.json.textmate punctuation.separator.array.json.tm
5293+
> { "include": "#applyEndPatternLast" },
5294+
#^^^^^^^^ source.json.textmate
5295+
# ^ source.json.textmate punctuation.definition.dictionary.begin.json.tm
5296+
# ^ source.json.textmate
5297+
# ^ source.json.textmate punctuation.definition.string.begin.json.tm
5298+
# ^^^^^^^ source.json.textmate keyword.property-name.string.include.json.tm
5299+
# ^ source.json.textmate punctuation.definition.string.end.json.tm
5300+
# ^ source.json.textmate punctuation.separator.dictionary.key-value.json.tm
5301+
# ^ source.json.textmate
5302+
# ^ source.json.textmate punctuation.definition.string.begin.json.tm
5303+
# ^ source.json.textmate regex keyword.json.tm
5304+
# ^^^^^^^^^^^^^^^^^^^ source.json.textmate regex variable.json.tm
5305+
# ^ source.json.textmate punctuation.definition.string.end.json.tm
5306+
# ^ source.json.textmate
5307+
# ^ source.json.textmate punctuation.definition.dictionary.end.json.tm
5308+
# ^ source.json.textmate punctuation.separator.array.json.tm
5309+
> { "include": "#id" },
5310+
#^^^^^^^^ source.json.textmate
5311+
# ^ source.json.textmate punctuation.definition.dictionary.begin.json.tm
5312+
# ^ source.json.textmate
5313+
# ^ source.json.textmate punctuation.definition.string.begin.json.tm
5314+
# ^^^^^^^ source.json.textmate keyword.property-name.string.include.json.tm
5315+
# ^ source.json.textmate punctuation.definition.string.end.json.tm
5316+
# ^ source.json.textmate punctuation.separator.dictionary.key-value.json.tm
5317+
# ^ source.json.textmate
5318+
# ^ source.json.textmate punctuation.definition.string.begin.json.tm
5319+
# ^ source.json.textmate regex keyword.json.tm
5320+
# ^^ source.json.textmate regex variable.json.tm
5321+
# ^ source.json.textmate punctuation.definition.string.end.json.tm
5322+
# ^ source.json.textmate
5323+
# ^ source.json.textmate punctuation.definition.dictionary.end.json.tm
5324+
# ^ source.json.textmate punctuation.separator.array.json.tm
51975325
> { "include": "#dictionary-separator" }
51985326
#^^^^^^^^ source.json.textmate
51995327
# ^ source.json.textmate punctuation.definition.dictionary.begin.json.tm

0 commit comments

Comments
 (0)