Skip to content

Commit

Permalink
[C#] Add support for highlighting GeneratedRegex attributes (#4121)
Browse files Browse the repository at this point in the history
Co-authored-by: Keith Hall <[email protected]>
  • Loading branch information
keith-hall and Keith Hall authored Dec 28, 2024
1 parent be1bdd1 commit c7cd5a7
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
25 changes: 25 additions & 0 deletions C#/C#.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,11 @@ contexts:
attribute_in:
- meta_scope: meta.annotation.cs
- include: namespace_variables
- match: (GeneratedRegex)\s*(\()
captures:
1: variable.annotation.cs
2: meta.group.cs punctuation.section.group.begin.cs
set: [attribute_arguments, arguments, regex_string]
- match: '({{name}})\s*(\()'
captures:
1: variable.annotation.cs
Expand Down Expand Up @@ -2440,3 +2445,23 @@ contexts:
- include: comments_in
- match: '[\w\s]+|.'
scope: text.documentation.cs

regex_string:
- match: '"'
scope: punctuation.definition.string.begin.cs
embed: scope:source.regexp
embed_scope: meta.string.cs meta.regexp.cs
escape: '"'
escape_captures:
0: punctuation.definition.string.end.cs
pop: 1
- match: '@"'
scope: punctuation.definition.string.begin.cs
embed: scope:source.regexp
embed_scope: meta.string.cs meta.regexp.cs
escape: '"'
escape_captures:
0: punctuation.definition.string.end.cs
pop: 1
- match: (?=\S)
pop: 1
70 changes: 69 additions & 1 deletion C#/tests/syntax_test_C#11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,72 @@ public readonly struct Distance(double dx, double dy)
/// ^^^^^^ storage.type
/// ^^^^ variable.parameter
/// ^ punctuation.section.parameters.end
/// ^ punctuation.terminator.statement
/// ^ punctuation.terminator.statement


class SomeClass
{
// https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-source-generators#source-generation
[GeneratedRegex("abc|def", RegexOptions.IgnoreCase, "en-US")]
///^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.body.cs meta.block.cs
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.annotation.cs
/// ^ punctuation.definition.annotation.begin.cs
/// ^^^^^^^^^^^^^^ variable.annotation.cs
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.group.cs
/// ^ punctuation.section.group.begin.cs
/// ^ punctuation.definition.string.begin.cs
/// ^^^^^^^ meta.string.cs meta.regexp.cs source.regexp meta.mode.basic.regexp
/// ^ keyword.operator.alternation.regexp
/// ^ punctuation.definition.string.end.cs
/// ^ punctuation.separator.argument.cs
/// ^^^^^^^^^^^^ variable.other.cs
/// ^ punctuation.accessor.dot.cs
/// ^^^^^^^^^^ variable.other.cs
/// ^ punctuation.separator.argument.cs
/// ^^^^^^^ meta.string.cs string.quoted.double.cs
/// ^ punctuation.definition.string.begin.cs
/// ^ punctuation.definition.string.end.cs
/// ^ punctuation.section.group.end.cs
/// ^ punctuation.definition.annotation.end.cs
private static partial Regex AbcOrDefGeneratedRegex();
///^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.body.cs meta.block.cs
/// ^^^^^^^ storage.modifier.access.cs
/// ^^^^^^ storage.modifier.cs
/// ^^^^^^^ storage.modifier.cs
/// ^^^^^ support.type.cs
/// ^^^^^^^^^^^^^^^^^^^^^^ meta.method.cs entity.name.function.cs
/// ^^ meta.method.parameters.cs
/// ^ punctuation.section.parameters.begin.cs
/// ^ punctuation.section.parameters.end.cs
/// ^ meta.method.cs punctuation.terminator.statement.cs

[GeneratedRegex(@"^some(?<named_capture>[^/]{1,100})$")]
///^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.body.cs meta.block.cs
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.annotation.cs
/// ^ punctuation.definition.annotation.begin.cs
/// ^^^^^^^^^^^^^^ variable.annotation.cs
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.group.cs
/// ^ punctuation.section.group.begin.cs
/// ^^ punctuation.definition.string.begin.cs
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.cs meta.regexp.cs source.regexp
/// ^^^^^ meta.mode.basic.regexp
/// ^ keyword.control.anchor.regexp
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.group.regexp meta.mode.basic.regexp
/// ^ punctuation.section.group.begin.regexp
/// ^^^^^^^^^^^^^^^^ keyword.other.named-capture-group.regexp
/// ^ punctuation.definition.capture-group-name.begin.regexp
/// ^^^^^^^^^^^^^ entity.name.capture-group.regexp
/// ^ punctuation.definition.capture-group-name.end.regexp
/// ^^^^ meta.set.regexp
/// ^ punctuation.definition.set.begin.regexp
/// ^ keyword.operator.logical.regexp
/// ^ punctuation.definition.set.end.regexp
/// ^^^^^^^ keyword.operator.quantifier.regexp
/// ^ punctuation.section.group.end.regexp
/// ^ meta.mode.basic.regexp keyword.control.anchor.regexp
/// ^ punctuation.definition.string.end.cs
/// ^ punctuation.section.group.end.cs
/// ^ punctuation.definition.annotation.end.cs
private static partial Regex SomeRegex();

}

0 comments on commit c7cd5a7

Please sign in to comment.