Skip to content

Commit e751cf2

Browse files
committed
Add support for scopeName
1 parent 1a6b76c commit e751cf2

File tree

6 files changed

+132
-7
lines changed

6 files changed

+132
-7
lines changed

src/DiagnosticCollection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ async function diagnosticsDeadTextMateCode(diagnostics: vscode.Diagnostic[], roo
880880
(repo (include) @include &begin !match)
881881
(repo (include) @include &patterns !begin !match)
882882
(repo [(begin) (while) (end) (beginCaptures) (whileCaptures) (endCaptures) (contentName) (applyEndPatternLast)] @begin &match)
883-
(repo [(captures) @captures (name) @name] !match !begin)
883+
(repo [(captures) @captures (name) @name (name_scopeName) @name] !match !begin)
884884
(repo [(while) @while (end) @end (beginCaptures) @beginCaptures (whileCaptures) @whileCaptures (endCaptures) @endCaptures (contentName) @contentName (applyEndPatternLast) @applyEndPatternLast] !begin)
885885
(repo (whileCaptures) @whileCaptures !while)
886886
(repo [(endCaptures) @endCaptures (applyEndPatternLast) @applyEndPatternLast] !end)
@@ -894,7 +894,7 @@ async function diagnosticsDeadTextMateCode(diagnostics: vscode.Diagnostic[], roo
894894
(pattern (match) @match &include)
895895
(pattern [(begin) (while) (end) (beginCaptures) (whileCaptures) (endCaptures) (contentName) (applyEndPatternLast)] @beginPatterns &match)
896896
(pattern [(begin) (while) (end) (beginCaptures) (whileCaptures) (endCaptures) (contentName) (applyEndPatternLast)] @beginPatterns &include !match)
897-
(pattern [(captures) @captures (name) @name] !match !begin)
897+
(pattern [(captures) @captures (name) @name (name_scopeName) @name] !match !begin)
898898
(pattern [(while) @while (end) @end (beginCaptures) @beginCaptures (whileCaptures) @whileCaptures (endCaptures) @endCaptures (contentName) @contentName (applyEndPatternLast) @applyEndPatternLast] !begin)
899899
(pattern (whileCaptures) @whileCaptures !while)
900900
(pattern [(endCaptures) @endCaptures (applyEndPatternLast) @applyEndPatternLast] !end)

src/Providers/CompletionItemProvider.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,12 @@ export const CompletionItemProvider: vscode.CompletionItemProvider = {
8080
(name (value (scope (replace_capture) @replace_capture)))
8181
(name (value (scope) @scope))
8282
(name (value) @scope_new)
83+
(contentName (value (scope (replace_capture) @replace_capture)))
8384
(contentName (value (scope) @scope))
8485
(contentName (value) @scope_new)
86+
(name_scopeName (value (scope (replace_capture) @replace_capture)))
87+
(name_scopeName (value (scope) @scope))
88+
(name_scopeName (value) @scope_new)
8589
(regex) @regex
8690
`;
8791
const cursorCapture = queryNode(rootNode, cursorQuery, point);

src/Providers/DocumentSymbolProvider.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export const SymbolKind: { [key: string]: vscode.SymbolKind; } = {
5858
'scopeName': vscode.SymbolKind.Variable,
5959
'name': vscode.SymbolKind.String,
6060
'name_display': vscode.SymbolKind.String,
61+
'name_scopeName': vscode.SymbolKind.String,
6162

6263
'version': vscode.SymbolKind.String,
6364
'schema': vscode.SymbolKind.String,
@@ -183,9 +184,12 @@ function newDocumentSymbol(node: webTreeSitter.Node): vscode.DocumentSymbol {
183184
}
184185
text = index.toString();
185186
break;
186-
case 'name':
187+
case 'name_display':
187188
text = 'name';
188189
break;
190+
case 'name_scopeName':
191+
text = 'scopeName';
192+
break;
189193
case 'value':
190194
text = node.text;
191195
break;

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

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ module.exports = grammar({
111111
),
112112
$.name,
113113
$.contentName,
114+
field(
115+
'name_scopeName',
116+
$.name_scopeName,
117+
),
114118
field(
115119
'match',
116120
$.match,
@@ -278,12 +282,12 @@ module.exports = grammar({
278282
choice(
279283
$.replace_capture,
280284
/\\[^\x00-\x1F ]/,
281-
/[^\\\x00-\x1F $"]+/,
282-
/\$/,
285+
/[^\\\x00-\x1F "]/,
283286
),
284287
),
285288
),
286289
),
290+
// https://github.com/microsoft/vscode-textmate/blob/main/src/utils.ts#L57-L91
287291
replace_capture: $ => token(
288292
choice(
289293
seq(
@@ -296,7 +300,60 @@ module.exports = grammar({
296300
':/',
297301
choice(
298302
'downcase',
299-
'upcase'
303+
'upcase',
304+
),
305+
'}',
306+
),
307+
),
308+
),
309+
name_scopeName: $ => pair($,
310+
"scopeName",
311+
$._scopes_scopeName,
312+
),
313+
_scopes_scopeName: $ => string($,
314+
alias(
315+
choice(
316+
$._scope_scopeName,
317+
$._forceStringNode,
318+
),
319+
$.value,
320+
),
321+
),
322+
_scope_scopeName: $ => fieldAlias($,
323+
"scope",
324+
prec.right(
325+
repeat1(
326+
choice(
327+
alias(
328+
$.replace_capture_scopeName,
329+
$.replace_capture,
330+
),
331+
/\\[^\x00-\x1F]/,
332+
/[^\\\x00-\x1F"]/,
333+
),
334+
),
335+
),
336+
),
337+
// https://github.com/textmate/textmate/blob/master/Frameworks/regexp/src/parser.cc#L129-L211
338+
replace_capture_scopeName: $ => token(
339+
choice(
340+
seq(
341+
'$',
342+
/\d+/,
343+
),
344+
seq(
345+
'${',
346+
/\d+/,
347+
optional(
348+
seq(
349+
':',
350+
repeat(
351+
choice(
352+
/\\[^\x00-\x1F }]/,
353+
/[^\\\x00-\x1F }"]/,
354+
),
355+
),
356+
),
300357
),
301358
'}',
302359
),

syntaxes/json.tmLanguage.json

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@
621621
"endCaptures": { "0": { "name": "punctuation.definition.dictionary.end.json.tm" } },
622622
"patterns": [
623623
{ "include": "#name" },
624+
{ "include": "#name_scopeName" },
624625
{ "include": "#match" },
625626
{ "include": "#include" },
626627
{ "include": "#patterns" },
@@ -751,6 +752,52 @@
751752
"beginCaptures": { "0": { "name": "punctuation.definition.string.begin.json.tm" } },
752753
"endCaptures": { "0": { "name": "punctuation.definition.string.end.json.tm" } },
753754
"name": "$1$2",
755+
"patterns": [
756+
{
757+
"comment": "TextMate 2.0 does not split scopes on spaces in their grammar files. But does so in their theme files.",
758+
"match": "\\G(?=(?>[^\\\\\" ]++|\\\\[^ ])+ )([^ ]+)(?=\\.)",
759+
"captures": {
760+
"1": {
761+
"name": "$1",
762+
"patterns": [
763+
{ "include": "#escape-characters" },
764+
{ "include": "#name_scopes" }
765+
]
766+
}
767+
}
768+
},
769+
{ "include": "#escape-characters" },
770+
{ "include": "#name_scopes" }
771+
]
772+
},
773+
{ "include": "#json-value" }
774+
]
775+
},
776+
{ "include": "#dictionary-separator" }
777+
]
778+
},
779+
"name_scopeName": {
780+
"comment": "\"scopeName\": \"...\"",
781+
"begin": "(\")(scopeName)(\")",
782+
"end": "(?<=\")(?!\\G)|(?=[},])",
783+
"beginCaptures": {
784+
"1": { "name": "punctuation.definition.string.begin.json.tm" },
785+
"2": { "name": "keyword.property-name.string.$2.json.tm" },
786+
"3": { "name": "punctuation.definition.string.end.json.tm" }
787+
},
788+
"patterns": [
789+
{
790+
"begin": ":",
791+
"end": "(?<=\")|(?=[},])",
792+
"beginCaptures": { "0": { "name": "punctuation.separator.dictionary.key-value.json.tm" } },
793+
"patterns": [
794+
{
795+
"comment": "TextMate 2.0 does not split scopes on spaces in their grammar files. But does so in their theme files.",
796+
"begin": "\"(?=(?=(?>[^\\\\\" ]++|\\\\[^ ])+ )([^ ]+)\\.|((?>[^\\\\\" ]++|\\\\[^ ])+)\")|\"",
797+
"end": "\"",
798+
"beginCaptures": { "0": { "name": "punctuation.definition.string.begin.json.tm" } },
799+
"endCaptures": { "0": { "name": "punctuation.definition.string.end.json.tm" } },
800+
"name": "$1$2",
754801
"patterns": [
755802
{ "include": "#escape-characters" },
756803
{ "include": "#name_scopes" }
@@ -939,6 +986,7 @@
939986
"patterns": [
940987
{ "comment": "All normal \"patterns\" rules are allowed here. With the exception of \"include\". But only when they are paired with \"patterns\"" },
941988
{ "include": "#name" },
989+
{ "include": "#name_scopeName" },
942990
{ "include": "#match" },
943991
{ "include": "#patterns" },
944992
{ "include": "#captures" },
@@ -979,6 +1027,7 @@
9791027
"patterns": [
9801028
{ "comment": "All normal \"patterns\" rules are allowed here. With the exception of \"include\". But only when they are paired with \"patterns\"" },
9811029
{ "include": "#name" },
1030+
{ "include": "#name_scopeName" },
9821031
{ "include": "#match" },
9831032
{ "include": "#patterns" },
9841033
{ "include": "#captures" },
@@ -1411,4 +1460,4 @@
14111460
]
14121461
}
14131462
}
1414-
}
1463+
}

vscode.tmLanguage.schema.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@
343343
"applyEndPatternLast": { "$ref": "#/definitions/applyEndPatternLast" },
344344
"name": { "$ref": "#/definitions/name" },
345345
"contentName": { "$ref": "#/definitions/contentName" },
346+
"scopeName": { "$ref": "#/definitions/scopeName" },
346347
"include": { "$ref": "#/definitions/include" },
347348
"patterns": { "$ref": "#/definitions/patterns" },
348349
"repository": { "$ref": "#/definitions/repository" },
@@ -1001,6 +1002,16 @@
10011002
"markdownDescription": "Same as `\"name\"`, but only applies to inside a `\"begin\"` - `\"end\"`/`\"while\"` region",
10021003
"$ref": "#/definitions/name-content"
10031004
},
1005+
"scopeName": {
1006+
"markdownDescription": "A single scopeName to be assigned to the provided token. Overrides `\"name\"`. \n_Not currently supported by VSCode_",
1007+
"anyOf": [
1008+
{
1009+
"type": "string",
1010+
"pattern": "^([\\w\\-.]|\\$\\d+|\\${\\d+(:[^}]*)?})*$"
1011+
},
1012+
{ "$ref": "#/definitions/name-content" }
1013+
]
1014+
},
10041015
"name-content": {
10051016
"type": "string",
10061017
"anyOf": [

0 commit comments

Comments
 (0)