Skip to content

Commit 29a37e5

Browse files
committed
C#12 lambda optional and params
1 parent 92b428c commit 29a37e5

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

grammars/csharp.tmLanguage

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7950,6 +7950,10 @@
79507950
<key>include</key>
79517951
<string>#implicit-anonymous-function-parameter</string>
79527952
</dict>
7953+
<dict>
7954+
<key>include</key>
7955+
<string>#default-argument</string>
7956+
</dict>
79537957
<dict>
79547958
<key>include</key>
79557959
<string>#punctuation-comma</string>
@@ -8097,7 +8101,7 @@
80978101
<dict>
80988102
<key>match</key>
80998103
<string>(?x)
8100-
(?:\b(ref|out|in)\b\s*)?
8104+
(?:\b(ref|params|out|in)\b\s*)?
81018105
(?&lt;type_name&gt;
81028106
(?:
81038107
(?:
@@ -8152,6 +8156,28 @@
81528156
<key>name</key>
81538157
<string>entity.name.variable.parameter.cs</string>
81548158
</dict>
8159+
<key>default-argument</key>
8160+
<dict>
8161+
<key>begin</key>
8162+
<string>=</string>
8163+
<key>beginCaptures</key>
8164+
<dict>
8165+
<key>0</key>
8166+
<dict>
8167+
<key>name</key>
8168+
<string>keyword.operator.assignment.cs</string>
8169+
</dict>
8170+
</dict>
8171+
<key>end</key>
8172+
<string>(?=,|\))</string>
8173+
<key>patterns</key>
8174+
<array>
8175+
<dict>
8176+
<key>include</key>
8177+
<string>#expression</string>
8178+
</dict>
8179+
</array>
8180+
</dict>
81558181
<key>type</key>
81568182
<dict>
81578183
<key>patterns</key>

grammars/csharp.tmLanguage.cson

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4801,6 +4801,9 @@ repository:
48014801
{
48024802
include: "#implicit-anonymous-function-parameter"
48034803
}
4804+
{
4805+
include: "#default-argument"
4806+
}
48044807
{
48054808
include: "#punctuation-comma"
48064809
}
@@ -4891,7 +4894,7 @@ repository:
48914894
"explicit-anonymous-function-parameter":
48924895
match: '''
48934896
(?x)
4894-
(?:\\b(ref|out|in)\\b\\s*)?
4897+
(?:\\b(ref|params|out|in)\\b\\s*)?
48954898
(?<type_name>
48964899
(?:
48974900
(?:
@@ -4930,6 +4933,17 @@ repository:
49304933
"implicit-anonymous-function-parameter":
49314934
match: "\\@?[_[:alpha:]][_[:alnum:]]*\\b"
49324935
name: "entity.name.variable.parameter.cs"
4936+
"default-argument":
4937+
begin: "="
4938+
beginCaptures:
4939+
"0":
4940+
name: "keyword.operator.assignment.cs"
4941+
end: "(?=,|\\))"
4942+
patterns: [
4943+
{
4944+
include: "#expression"
4945+
}
4946+
]
49334947
type:
49344948
patterns: [
49354949
{

src/csharp.tmLanguage.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3124,6 +3124,7 @@ repository:
31243124
- include: '#comment'
31253125
- include: '#explicit-anonymous-function-parameter'
31263126
- include: '#implicit-anonymous-function-parameter'
3127+
- include: '#default-argument'
31273128
- include: '#punctuation-comma'
31283129
5: { name: punctuation.parenthesis.close.cs }
31293130
6: { name: keyword.operator.arrow.cs }
@@ -3169,7 +3170,7 @@ repository:
31693170
explicit-anonymous-function-parameter:
31703171
match: |-
31713172
(?x)
3172-
(?:\b(ref|out|in)\b\s*)?
3173+
(?:\b(ref|params|out|in)\b\s*)?
31733174
(?<type_name>
31743175
(?:
31753176
(?:
@@ -3208,6 +3209,14 @@ repository:
32083209
match: \@?[_[:alpha:]][_[:alnum:]]*\b
32093210
name: entity.name.variable.parameter.cs
32103211

3212+
default-argument:
3213+
begin: =
3214+
beginCaptures:
3215+
0: { name: keyword.operator.assignment.cs }
3216+
end: (?=,|\))
3217+
patterns:
3218+
- include: '#expression'
3219+
32113220
type:
32123221
patterns:
32133222
- include: '#comment'

test/expressions.tests.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,49 @@ _ = delegate /*comment*/ () /*comment*/ { };`);
12681268
Token.Punctuation.Semicolon,
12691269
]);
12701270
});
1271+
1272+
it("lambda expression optional parameter (issue #294)", async () => {
1273+
const input = Input.InMethod(`_ = (int addTo = 2) => addTo + 1;`);
1274+
const tokens = await tokenize(input);
1275+
1276+
tokens.should.deep.equal([
1277+
Token.Variable.ReadWrite("_"),
1278+
Token.Operator.Assignment,
1279+
Token.Punctuation.OpenParen,
1280+
Token.PrimitiveType.Int,
1281+
Token.Identifier.ParameterName("addTo"),
1282+
Token.Operator.Assignment,
1283+
Token.Literal.Numeric.Decimal("2"),
1284+
Token.Punctuation.CloseParen,
1285+
Token.Operator.Arrow,
1286+
Token.Variable.ReadWrite("addTo"),
1287+
Token.Operator.Arithmetic.Addition,
1288+
Token.Literal.Numeric.Decimal("1"),
1289+
Token.Punctuation.Semicolon,
1290+
]);
1291+
});
1292+
1293+
it("lambda expression params array parameter (issue #294)", async () => {
1294+
const input = Input.InMethod(`_ = (params object[] arr) => arr.Length;`);
1295+
const tokens = await tokenize(input);
1296+
1297+
tokens.should.deep.equal([
1298+
Token.Variable.ReadWrite("_"),
1299+
Token.Operator.Assignment,
1300+
Token.Punctuation.OpenParen,
1301+
Token.Keyword.Modifier.Params,
1302+
Token.PrimitiveType.Object,
1303+
Token.Punctuation.OpenBracket,
1304+
Token.Punctuation.CloseBracket,
1305+
Token.Identifier.ParameterName("arr"),
1306+
Token.Punctuation.CloseParen,
1307+
Token.Operator.Arrow,
1308+
Token.Variable.Object("arr"),
1309+
Token.Punctuation.Accessor,
1310+
Token.Variable.Property("Length"),
1311+
Token.Punctuation.Semicolon,
1312+
]);
1313+
});
12711314
});
12721315

12731316
describe("Anonymous Objects", () => {

0 commit comments

Comments
 (0)