Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for lambda attributes #313

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion grammars/csharp.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@
<key>attribute-section</key>
<dict>
<key>begin</key>
<string>(\[)(assembly|module|field|event|method|param|property|return|type)?(\:)?</string>
<string>(\[)(assembly|module|event|field|method|param|property|return|type|typevar)?(\:)?</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
Expand Down Expand Up @@ -2725,12 +2725,38 @@
<key>include</key>
<string>#ref-modifier</string>
</dict>
<dict>
<key>include</key>
<string>#lambda-expression</string>
</dict>
<dict>
<key>include</key>
<string>#expression</string>
</dict>
</array>
</dict>
<key>lambda-expression</key>
<dict>
<key>begin</key>
<string>(?=(?:\[.*\].*)?\(.*\).*=&gt;)</string>
<key>end</key>
<string>(?=;)</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>#attribute-section</string>
</dict>
<dict>
<key>include</key>
<string>#parenthesized-parameter-list</string>
</dict>
<dict>
<key>include</key>
<string>#expression-body</string>
</dict>
</array>
</dict>
<key>expression-body</key>
<dict>
<key>begin</key>
Expand Down Expand Up @@ -7270,6 +7296,10 @@
<key>include</key>
<string>#parameter</string>
</dict>
<dict>
<key>include</key>
<string>#parameter-identifier</string>
</dict>
<dict>
<key>include</key>
<string>#punctuation-comma</string>
Expand Down Expand Up @@ -7333,6 +7363,13 @@
</dict>
</dict>
</dict>
<key>parameter-identifier</key>
<dict>
<key>name</key>
<string>entity.name.variable.parameter.cs</string>
<key>match</key>
<string>(?&lt;!\.)@?\b[_[:alpha:]][_[:alnum:]]*\b(?!\.)</string>
</dict>
<key>argument-list</key>
<dict>
<key>begin</key>
Expand Down
25 changes: 24 additions & 1 deletion grammars/csharp.tmLanguage.cson
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ repository:
}
]
"attribute-section":
begin: "(\\[)(assembly|module|field|event|method|param|property|return|type)?(\\:)?"
begin: "(\\[)(assembly|module|event|field|method|param|property|return|type|typevar)?(\\:)?"
beginCaptures:
"1":
name: "punctuation.squarebracket.open.cs"
Expand Down Expand Up @@ -1738,10 +1738,27 @@ repository:
{
include: "#ref-modifier"
}
{
include: "#lambda-expression"
}
{
include: "#expression"
}
]
"lambda-expression":
begin: "(?=(?:\\[.*\\].*)?\\(.*\\).*=>)"
end: "(?=;)"
patterns: [
{
include: "#attribute-section"
}
{
include: "#parenthesized-parameter-list"
}
{
include: "#expression-body"
}
]
"expression-body":
begin: "=>"
beginCaptures:
Expand Down Expand Up @@ -4383,6 +4400,9 @@ repository:
{
include: "#parameter"
}
{
include: "#parameter-identifier"
}
{
include: "#punctuation-comma"
}
Expand Down Expand Up @@ -4430,6 +4450,9 @@ repository:
]
"7":
name: "entity.name.variable.parameter.cs"
"parameter-identifier":
name: "entity.name.variable.parameter.cs"
match: "(?<!\\.)@?\\b[_[:alpha:]][_[:alnum:]]*\\b(?!\\.)"
"argument-list":
begin: "\\("
beginCaptures:
Expand Down
16 changes: 15 additions & 1 deletion src/csharp.tmLanguage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ repository:
- include: '#operator-assignment'

attribute-section:
begin: (\[)(assembly|module|field|event|method|param|property|return|type)?(\:)?
begin: (\[)(assembly|module|event|field|method|param|property|return|type|typevar)?(\:)?
beginCaptures:
'1': { name: punctuation.squarebracket.open.cs }
'2': { name: keyword.other.attribute-specifier.cs }
Expand Down Expand Up @@ -1024,8 +1024,17 @@ repository:
end: (?=[,\)\];}])
patterns:
- include: '#ref-modifier'
- include: '#lambda-expression'
- include: '#expression'

lambda-expression:
begin: (?=(?:\[.*\].*)?\(.*\).*=>)
end: (?=;)
patterns:
- include: '#attribute-section'
- include: '#parenthesized-parameter-list'
- include: '#expression-body'

expression-body:
begin: =>
beginCaptures:
Expand Down Expand Up @@ -2823,6 +2832,7 @@ repository:
- include: '#comment'
- include: '#attribute-section'
- include: '#parameter'
- include: '#lambda-parameter'
- include: '#punctuation-comma'
- include: '#variable-initializer'

Expand Down Expand Up @@ -2865,6 +2875,10 @@ repository:
# '6': ?<tuple> is a sub-expression. It's final value is not considered.
7: { name: entity.name.variable.parameter.cs }

lambda-parameter:
name: entity.name.variable.parameter.cs
match: '(?<!\.)@?\b[_[:alpha:]][_[:alnum:]]*\b(?!\.)'

argument-list:
begin: \(
beginCaptures:
Expand Down
97 changes: 97 additions & 0 deletions test/expressions.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,103 @@ var a = new A // comment
]);
});

it("lambda with attributes (issue #303)", async () => {
const input = Input.InMethod(`
var concat = ([DisallowNull] string a, [DisallowNull] string b) => a + b;
var inc = [return: NotNullIfNotNull(nameof(s))] (int? s) => s.HasValue ? s++ : null;
Func<string?, int?> parse = [ProvidesNullCheck] (s) => (s is not null) ? int.Parse(s) : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have a test with an explicit return type too.

`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.Keyword.Definition.Var,
Token.Identifier.LocalName("concat"),
Token.Operator.Assignment,
Token.Punctuation.OpenParen,
Token.Punctuation.OpenBracket,
Token.Type("DisallowNull"),
Token.Punctuation.CloseBracket,
Token.PrimitiveType.String,
Token.Identifier.ParameterName("a"),
Token.Punctuation.Comma,
Token.Punctuation.OpenBracket,
Token.Type("DisallowNull"),
Token.Punctuation.CloseBracket,
Token.PrimitiveType.String,
Token.Identifier.ParameterName("b"),
Token.Punctuation.CloseParen,
Token.Operator.Arrow,
Token.Variable.ReadWrite("a"),
Token.Operator.Arithmetic.Addition,
Token.Variable.ReadWrite("b"),
Token.Punctuation.Semicolon,

Token.Keyword.Definition.Var,
Token.Identifier.LocalName("inc"),
Token.Operator.Assignment,
Token.Punctuation.OpenBracket,
Token.Keyword.AttributeSpecifier("return"),
Token.Punctuation.Colon,
Token.Type("NotNullIfNotNull"),
Token.Punctuation.OpenParen,
Token.Operator.Expression.NameOf,
Token.Punctuation.OpenParen,
Token.Variable.ReadWrite("s"),
Token.Punctuation.CloseParen,
Token.Punctuation.CloseParen,
Token.Punctuation.CloseBracket,
Token.Punctuation.OpenParen,
Token.PrimitiveType.Int,
Token.Punctuation.QuestionMark,
Token.Identifier.ParameterName("s"),
Token.Punctuation.CloseParen,
Token.Operator.Arrow,
Token.Variable.Object("s"),
Token.Punctuation.Accessor,
Token.Variable.Property("HasValue"),
Token.Operator.Conditional.QuestionMark,
Token.Variable.ReadWrite("s"),
Token.Operator.Increment,
Token.Operator.Conditional.Colon,
Token.Literal.Null,
Token.Punctuation.Semicolon,

Token.Type("Func"),
Token.Punctuation.TypeParameter.Begin,
Token.PrimitiveType.String,
Token.Punctuation.QuestionMark,
Token.Punctuation.Comma,
Token.PrimitiveType.Int,
Token.Punctuation.QuestionMark,
Token.Punctuation.TypeParameter.End,
Token.Identifier.LocalName("parse"),
Token.Operator.Assignment,
Token.Punctuation.OpenBracket,
Token.Type("ProvidesNullCheck"),
Token.Punctuation.CloseBracket,
Token.Punctuation.OpenParen,
Token.Identifier.ParameterName("s"),
Token.Punctuation.CloseParen,
Token.Operator.Arrow,
Token.Punctuation.OpenParen,
Token.Variable.ReadWrite("s"),
Token.Operator.Pattern.Is,
Token.Operator.Pattern.Not,
Token.Literal.Null,
Token.Punctuation.CloseParen,
Token.Operator.Conditional.QuestionMark,
Token.PrimitiveType.Int,
Token.Punctuation.Accessor,
Token.Identifier.MethodName("Parse"),
Token.Punctuation.OpenParen,
Token.Variable.ReadWrite("s"),
Token.Punctuation.CloseParen,
Token.Operator.Conditional.Colon,
Token.Literal.Null,
Token.Punctuation.Semicolon
]);
});

it("async lambda assigned to dotted name (issue #142)", async () => {
const input = Input.InMethod(
`Something.listener = async args => { return true; };`
Expand Down
Loading