Skip to content

Commit 01dc6be

Browse files
committed
update fragment specifiers/allow spaces in metavariable declarations
- Add `pat_param` specifier from 2021 edition - Add `expr_2021` specifier from 2024 edition: https://doc.rust-lang.org/edition-guide/rust-2024/macro-fragment-specifiers.html - This specifier is currently unstable but the name seems to be confirmed: rust-lang/rust#123742 (comment) - Require a word boundary (`\b`) at the end of the specifier - Replace `[A-Za-z0-9_]` with `\w` - Allow spaces before and after the `:` in metavariable declarations - Add tests for the new specifiers and spacing
1 parent c7670c5 commit 01dc6be

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

syntaxes/rust.tmLanguage.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
{
5151
"comment": "macro type metavariables",
5252
"name": "meta.macro.metavariable.type.rust",
53-
"match": "(\\$)((crate)|([A-Z][A-Za-z0-9_]*))((:)(block|expr|ident|item|lifetime|literal|meta|path?|stmt|tt|ty|vis))?",
53+
"match": "(\\$)((crate)|([A-Z]\\w*))(\\s*(:)\\s*(block|expr(?:_2021)?|ident|item|lifetime|literal|meta|pat(?:_param)?|path|stmt|tt|ty|vis)\\b)?",
5454
"captures": {
5555
"1": {
5656
"name": "keyword.operator.macro.dollar.rust"
@@ -77,7 +77,7 @@
7777
{
7878
"comment": "macro metavariables",
7979
"name": "meta.macro.metavariable.rust",
80-
"match": "(\\$)([a-z][A-Za-z0-9_]*)((:)(block|expr|ident|item|lifetime|literal|meta|path?|stmt|tt|ty|vis))?",
80+
"match": "(\\$)([a-z]\\w*)(\\s*(:)\\s*(block|expr(?:_2021)?|ident|item|lifetime|literal|meta|pat(?:_param)?|path|stmt|tt|ty|vis)\\b)?",
8181
"captures": {
8282
"1": {
8383
"name": "keyword.operator.macro.dollar.rust"

syntaxes/rust.tmLanguage.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ patterns:
2929
-
3030
comment: macro type metavariables
3131
name: meta.macro.metavariable.type.rust
32-
match: (\$)((crate)|([A-Z][A-Za-z0-9_]*))((:)(block|expr|ident|item|lifetime|literal|meta|path?|stmt|tt|ty|vis))?
32+
match: (\$)((crate)|([A-Z]\w*))(\s*(:)\s*(block|expr(?:_2021)?|ident|item|lifetime|literal|meta|pat(?:_param)?|path|stmt|tt|ty|vis)\b)?
3333
captures:
3434
1:
3535
name: keyword.operator.macro.dollar.rust
@@ -46,7 +46,7 @@ patterns:
4646
-
4747
comment: macro metavariables
4848
name: meta.macro.metavariable.rust
49-
match: (\$)([a-z][A-Za-z0-9_]*)((:)(block|expr|ident|item|lifetime|literal|meta|path?|stmt|tt|ty|vis))?
49+
match: (\$)([a-z]\w*)(\s*(:)\s*(block|expr(?:_2021)?|ident|item|lifetime|literal|meta|pat(?:_param)?|path|stmt|tt|ty|vis)\b)?
5050
captures:
5151
1:
5252
name: keyword.operator.macro.dollar.rust

test/test.rs

+41
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,44 @@ let x6 = 1.123E-12;
4646
// ^ keyword.operator.exponent.rust
4747
// ^ keyword.operator.exponent.sign.rust
4848
// ^^ constant.numeric.decimal.exponent.mantissa.rust
49+
50+
// macro metavarables
51+
macro_rules! metavariable_test {
52+
($var:tt $Type:ty $var : tt $Type :ty) => {};
53+
// ^^^^^^^ ^^^^^^^^^ meta.macro.metavariable.rust
54+
// ^^^^^^^^ ^^^^^^^^^ meta.macro.metavariable.type.rust
55+
// ^ ^ ^ ^ keyword.operator.macro.dollar.rust
56+
// ^^^ ^^^ variable.other.metavariable.name.rust
57+
// ^^^^ ^^^^ entity.name.type.metavariable.rust
58+
// ^ ^ ^ ^ keyword.operator.key-value.rust
59+
// ^^ ^^ ^^ ^^ variable.other.metavariable.specifier.rust
60+
($var:pat_param $Var:pat_param) => {};
61+
// ^^^^^^^^^^^^^^ meta.macro.metavariable.rust
62+
// ^^^^^^^^^^^^^^ meta.macro.metavariable.type.rust
63+
// ^ ^ keyword.operator.macro.dollar.rust
64+
// ^^^ variable.other.metavariable.name.rust
65+
// ^^^ entity.name.type.metavariable.rust
66+
// ^ ^ keyword.operator.key-value.rust
67+
// ^^^^^^^^^ ^^^^^^^^^ variable.other.metavariable.specifier.rust
68+
($var: expr_2021 $Var: expr_2021) => {};
69+
// ^^^^^^^^^^^^^^^ meta.macro.metavariable.rust
70+
// ^^^^^^^^^^^^^^^ meta.macro.metavariable.type.rust
71+
// ^ ^ keyword.operator.macro.dollar.rust
72+
// ^^^ variable.other.metavariable.name.rust
73+
// ^^^ entity.name.type.metavariable.rust
74+
// ^ ^ keyword.operator.key-value.rust
75+
// ^^^^^^^^^ ^^^^^^^^^ variable.other.metavariable.specifier.rust
76+
() => { $var $Type $crate };
77+
// ^^^^ meta.macro.metavariable.rust
78+
// ^^^^^ ^^^^^^ meta.macro.metavariable.type.rust
79+
// ^ ^ ^ - meta.macro.metavariable.rust meta.macro.metavariable.type.rust
80+
// ^ ^ ^ keyword.operator.macro.dollar.rust
81+
// ^^^ variable.other.metavariable.name.rust
82+
// ^^^^ entity.name.type.metavariable.rust
83+
// ^^^^^ keyword.other.crate.rust
84+
() => { $var: not_a_specifier };
85+
// ^^^^ meta.macro.metavariable.rust
86+
// ^ keyword.operator.macro.dollar.rust
87+
// ^^^ variable.other.metavariable.name.rust
88+
// ^^^^^^^^^^^^^^^^^ - meta.macro.metavariable.rust
89+
}

0 commit comments

Comments
 (0)