Skip to content

Commit

Permalink
[C#] Add 'required' keyword
Browse files Browse the repository at this point in the history
Resolves #3926

This commit adds `required` modifier which has been added by C#11.

see: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/required
  • Loading branch information
deathaxe committed Feb 18, 2024
1 parent b4decd9 commit de94a6c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
6 changes: 3 additions & 3 deletions C#/C#.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ variables:
brackets_capture: '((\[)(,*)(\]))'
type_suffix_capture: '(\?)?{{brackets_capture}}?(?:\s*(\*))?'

reserved: '(?:abstract|as|base|break|case|catch|checked|class|const|continue|default|delegate|do|else|enum|event|explicit|extern|finally|fixed|for|foreach|goto|if|implicit|in|interface|internal|is|lock|nameof|namespace|new|not|null|operator|out|override|params|private|protected|public|readonly|ref|return|sealed|sizeof|stackalloc|static|string|struct|switch|this|throw|try|typeof|unchecked|unsafe|using|virtual|volatile|while)'
reserved: '(?:abstract|as|base|break|case|catch|checked|class|const|continue|default|delegate|do|else|enum|event|explicit|extern|finally|fixed|for|foreach|goto|if|implicit|in|interface|internal|is|lock|nameof|namespace|new|not|null|operator|out|override|params|private|protected|public|readonly|ref|required|return|sealed|sizeof|stackalloc|static|string|struct|switch|this|throw|try|typeof|unchecked|unsafe|using|virtual|volatile|while)'
name: '(?:@{{reserved}}|@{{base_type}}|@var|@?{{name_normal}})'
namespaced_name: (?:(?:{{name}}{{generic_declaration}}\s*\.\s*)*{{name}}{{generic_declaration}})

Expand Down Expand Up @@ -225,7 +225,7 @@ contexts:
# allows coloration of code outside a class
- match: (?=\S)
push:
- match: (?={{visibility}}|\b(?:class|delegate|interface|namespace|readonly|record|static)\b)
- match: (?={{visibility}}|\b(?:class|delegate|interface|namespace|readonly|record|required|static)\b)
pop: true
- include: line_of_code

Expand Down Expand Up @@ -334,7 +334,7 @@ contexts:
- include: main

class_declaration:
- match: '\b(static|unsafe|abstract|partial|sealed)\b'
- match: \b(static|unsafe|abstract|partial|required|sealed)\b
scope: storage.modifier.cs
- match: '{{visibility}}'
scope: storage.modifier.access.cs
Expand Down
22 changes: 22 additions & 0 deletions C#/tests/syntax_test_C#11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,25 @@ The following example shows how newlines can improve the readability of an expre
/// ^^^^^^ punctuation.definition.string.begin
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - constant
/// ^^^ punctuation.definition.string.end

public class C2 {
/// <- storage.modifier.access
/// ^^^^^ keyword.declaration.class
/// ^^ entity.name.class

public required string FirstName { get; init; }
/// ^^^^^^ storage.modifier.access
/// ^^^^^^^^ storage.modifier
/// ^^^^^^ storage.type
}

public struct S2 {
/// <- storage.modifier.access
/// ^^^^^^ keyword.declaration.struct
/// ^^ entity.name.struct

public required string FirstName { get; init; }
/// ^^^^^^ storage.modifier.access
/// ^^^^^^^^ storage.modifier
/// ^^^^^^ storage.type
}
6 changes: 6 additions & 0 deletions C#/tests/syntax_test_C#7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ class Foo {
///^^^^^^^^ meta.class
/// ^ meta.class.body

public readonly double value;
/// ^^^^^^ storage.modifier.access
/// ^^^^^^^^ storage.modifier
/// ^^^^^^ storage.type
/// ^^^^^ variable.other.member

void Main(string[] args) {
/// ^^^^ storage.type
/// ^^^^^^^^^^^^^^^^^^^^^ meta.method
Expand Down

0 comments on commit de94a6c

Please sign in to comment.