Skip to content
Merged
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
6 changes: 5 additions & 1 deletion pygments/lexers/dotnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,15 @@ class CSharpLexer(RegexLexer):
'while', 'get', 'set', 'new', 'partial', 'yield', 'add',
'remove', 'value', 'alias', 'ascending', 'descending',
'from', 'group', 'into', 'orderby', 'select', 'thenby',
'where', 'join', 'equals', 'file', 'record', 'allows',
'where', 'join', 'equals', 'record', 'allows',
'and', 'init', 'managed', 'nameof', 'nint', 'not',
'notnull', 'nuint', 'or', 'scoped', 'unmanaged', 'when',
'with'
), suffix=r'\b'), Keyword),
# version 1: assumes that 'file' is the only contextual keyword
# that is a class modifier
(r'(file)(\s+)(record|class|abstract|enum|new|sealed|static)\b',
bygroups(Keyword, Whitespace, Keyword)),
(r'(global)(::)', bygroups(Keyword, Punctuation)),
(r'(bool|byte|char|decimal|double|dynamic|float|int|long|object|'
r'sbyte|short|string|uint|ulong|ushort|var)\b\??', Keyword.Type),
Expand Down
141 changes: 141 additions & 0 deletions tests/snippets/csharp/test_file_keyword.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
---input---
file class X { }
file record X(int file);
file abstract class X { }
file sealed class X { }
file static class X { }
file enum X { file }
file new record X(int file);
int file(Func<int> file) => file();
int file = Program.file(file => 42);

---tokens---
'file' Keyword
' ' Text.Whitespace
'class' Keyword
' ' Text.Whitespace
'X' Name
' ' Text.Whitespace
'{' Punctuation
' ' Text.Whitespace
'}' Punctuation
'\n' Text.Whitespace

'file' Keyword
' ' Text.Whitespace
'record' Keyword
' ' Text.Whitespace
'X' Name.Function
'(' Punctuation
'int' Keyword.Type
' ' Text.Whitespace
'file' Name
')' Punctuation
';' Punctuation
'\n' Text.Whitespace

'file' Keyword
' ' Text.Whitespace
'abstract' Keyword
' ' Text.Whitespace
'class' Keyword
' ' Text.Whitespace
'X' Name.Class
' ' Text.Whitespace
'{' Punctuation
' ' Text.Whitespace
'}' Punctuation
'\n' Text.Whitespace

'file' Keyword
' ' Text.Whitespace
'sealed' Keyword
' ' Text.Whitespace
'class' Keyword
' ' Text.Whitespace
'X' Name.Class
' ' Text.Whitespace
'{' Punctuation
' ' Text.Whitespace
'}' Punctuation
'\n' Text.Whitespace

'file' Keyword
' ' Text.Whitespace
'static' Keyword
' ' Text.Whitespace
'class' Keyword
' ' Text.Whitespace
'X' Name.Class
' ' Text.Whitespace
'{' Punctuation
' ' Text.Whitespace
'}' Punctuation
'\n' Text.Whitespace

'file' Keyword
' ' Text.Whitespace
'enum' Keyword
' ' Text.Whitespace
'X' Name
' ' Text.Whitespace
'{' Punctuation
' ' Text.Whitespace
'file' Name
' ' Text.Whitespace
'}' Punctuation
'\n' Text.Whitespace

'file' Keyword
' ' Text.Whitespace
'new' Keyword
' ' Text.Whitespace
'record' Keyword
' ' Text.Whitespace
'X' Name.Function
'(' Punctuation
'int' Keyword.Type
' ' Text.Whitespace
'file' Name
')' Punctuation
';' Punctuation
'\n' Text.Whitespace

'int' Keyword.Type
' ' Text.Whitespace
'file' Name.Function
'(' Punctuation
'Func' Name
'<' Operator
'int' Keyword.Type
'>' Operator
' ' Text.Whitespace
'file' Name
')' Punctuation
' ' Text.Whitespace
'=>' Operator
' ' Text.Whitespace
'file' Name
'(' Punctuation
')' Punctuation
';' Punctuation
'\n' Text.Whitespace

'int' Keyword.Type
' ' Text.Whitespace
'file' Name
' ' Text.Whitespace
'=' Operator
' ' Text.Whitespace
'Program' Name
'.' Punctuation
'file' Name
'(' Punctuation
'file' Name
' ' Text.Whitespace
'=>' Operator
' ' Text.Whitespace
'42' Literal.Number.Integer
')' Punctuation
';' Punctuation
'\n' Text.Whitespace