Skip to content

Commit

Permalink
Add support for ref variable declarations in foreach statement
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeRobich committed Dec 29, 2023
1 parent be24caf commit 52173fe
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
14 changes: 10 additions & 4 deletions grammars/csharp.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -4250,9 +4250,10 @@
<key>match</key>
<string>(?x)
(?:
(\bvar\b)|
(?:(\bref)\s+)?(\bvar\b)| # ref local
(?&lt;type_name&gt;
(?:
(?:ref\s+)? # ref local
(?:
(?:(?&lt;identifier&gt;@?[_[:alpha:]][_[:alnum:]]*)\s*\:\:\s*)? # alias-qualification
(?&lt;name_and_type_args&gt; # identifier + type arguments (if any)
Expand Down Expand Up @@ -4281,9 +4282,14 @@
<key>1</key>
<dict>
<key>name</key>
<string>storage.type.var.cs</string>
<string>storage.modifier.ref.cs</string>
</dict>
<key>2</key>
<dict>
<key>name</key>
<string>storage.type.var.cs</string>
</dict>
<key>3</key>
<dict>
<key>patterns</key>
<array>
Expand All @@ -4293,12 +4299,12 @@
</dict>
</array>
</dict>
<key>7</key>
<key>8</key>
<dict>
<key>name</key>
<string>entity.name.variable.local.cs</string>
</dict>
<key>8</key>
<key>9</key>
<dict>
<key>name</key>
<string>keyword.control.loop.in.cs</string>
Expand Down
11 changes: 7 additions & 4 deletions grammars/csharp.tmLanguage.cson
Original file line number Diff line number Diff line change
Expand Up @@ -2603,9 +2603,10 @@ repository:
match: '''
(?x)
(?:
(\\bvar\\b)|
(?:(\\bref)\\s+)?(\\bvar\\b)| # ref local
(?<type_name>
(?:
(?:ref\\s+)? # ref local
(?:
(?:(?<identifier>@?[_[:alpha:]][_[:alnum:]]*)\\s*\\:\\:\\s*)? # alias-qualification
(?<name_and_type_args> # identifier + type arguments (if any)
Expand All @@ -2632,16 +2633,18 @@ repository:
'''
captures:
"1":
name: "storage.type.var.cs"
name: "storage.modifier.ref.cs"
"2":
name: "storage.type.var.cs"
"3":
patterns: [
{
include: "#type"
}
]
"7":
name: "entity.name.variable.local.cs"
"8":
name: "entity.name.variable.local.cs"
"9":
name: "keyword.control.loop.in.cs"
}
{
Expand Down
20 changes: 11 additions & 9 deletions src/csharp.tmLanguage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1521,9 +1521,10 @@ repository:
- match: |-
(?x)
(?:
(\bvar\b)|
(?:(\bref)\s+)?(\bvar\b)| # ref local
(?<type_name>
(?:
(?:ref\s+)? # ref local
(?:
(?:(?<identifier>@?[_[:alpha:]][_[:alnum:]]*)\s*\:\:\s*)? # alias-qualification
(?<name_and_type_args> # identifier + type arguments (if any)
Expand All @@ -1548,16 +1549,17 @@ repository:
(\g<identifier>)\s+
\b(in)\b
captures:
'1': { name: storage.type.var.cs }
'2':
1: { name: storage.modifier.ref.cs }
2: { name: storage.type.var.cs }
3:
patterns:
- include: '#type'
# '3': ?<identifier> is a sub-expression. It's final value is not considered.
# '4': ?<name_and_type_args> is a sub-expression. It's final value is not considered.
# '5': ?<type_args> is a sub-expression. It's final value is not considered.
# '6': ?<tuple> is a sub-expression. It's final value is not considered.
'7': { name: entity.name.variable.local.cs }
'8': { name: keyword.control.loop.in.cs }
# 4: ?<identifier> is a sub-expression. It's final value is not considered.
# 5: ?<name_and_type_args> is a sub-expression. It's final value is not considered.
# 6: ?<type_args> is a sub-expression. It's final value is not considered.
# 7: ?<tuple> is a sub-expression. It's final value is not considered.
8: { name: entity.name.variable.local.cs }
9: { name: keyword.control.loop.in.cs }
- match: |-
(?x) # match foreach (var (x, y) in ...)
(?:\b(var)\b\s*)?
Expand Down
19 changes: 19 additions & 0 deletions test/statements.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,25 @@ foreach (var s in myList)
Token.Punctuation.CloseBrace,
]);
});

it("foreach loop with ref var (issue #308)", async () => {
const input = Input.InMethod(`
foreach (ref var i in span) {}`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.Keyword.Loop.ForEach,
Token.Punctuation.OpenParen,
Token.Keyword.Modifier.Ref,
Token.Keyword.Definition.Var,
Token.Identifier.LocalName("i"),
Token.Keyword.Loop.In,
Token.Variable.ReadWrite("span"),
Token.Punctuation.CloseParen,
Token.Punctuation.OpenBrace,
Token.Punctuation.CloseBrace,
]);
});
});

describe("While", () => {
Expand Down

0 comments on commit 52173fe

Please sign in to comment.