Skip to content

Commit

Permalink
[C#] Less strict readonly keyword matching
Browse files Browse the repository at this point in the history
This commit scopes `readonly` modifier more lazily to improve
interoperability with `required` keyword.
  • Loading branch information
deathaxe committed Feb 18, 2024
1 parent de94a6c commit 0913c40
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
21 changes: 9 additions & 12 deletions C#/C#.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ contexts:
- include: main

class_declaration:
- match: \b(static|unsafe|abstract|partial|required|sealed)\b
- match: \b(static|unsafe|abstract|partial|readonly|required|sealed)\b
scope: storage.modifier.cs
- match: '{{visibility}}'
scope: storage.modifier.access.cs
Expand All @@ -343,22 +343,19 @@ contexts:
1: keyword.declaration.class.cs
2: entity.name.class.cs
push: [class_signature, data_type_signature]
- match: (?:\b(readonly)\s+)?\b(record)\s+(?:(struct)\s+)?({{name}})
- match: \b(record)\s+(?:(struct)\s+)?({{name}})
captures:
1: storage.modifier.cs
2: keyword.declaration.class.record.cs
3: keyword.declaration.struct.record.cs
4: entity.name.class.cs
1: keyword.declaration.class.record.cs
2: keyword.declaration.struct.record.cs
3: entity.name.class.cs
push: [record_signature, data_type_constraint, record_parameters, data_type_signature]
- match: (?:\b(readonly)\s+)?(?:\b(partial)\s+)?(?:\b(ref)\s+)?\b(struct)\s+({{name}})
- match: (?:\b(ref)\s+)?\b(struct)\s+({{name}})
captures:
1: storage.modifier.cs
2: storage.modifier.cs
3: storage.modifier.cs
4: keyword.declaration.struct.cs
5: entity.name.struct.cs
2: keyword.declaration.struct.cs
3: entity.name.struct.cs
push: [struct_signature, data_type_signature]
- match: '\b(enum)\s+({{name}})\s*(?:(:)\s*(byte|sbyte|short|ushort|int|uint|long|ulong))?'
- match: \b(enum)\s+({{name}})\s*(?:(:)\s*(byte|sbyte|short|ushort|int|uint|long|ulong))?
scope: meta.enum.cs
captures:
1: keyword.declaration.enum.cs
Expand Down
2 changes: 1 addition & 1 deletion C#/tests/syntax_test_C#10.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public record struct Person(string Name);

public readonly record struct Person(string Name);
///^^^ storage.modifier.access
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.record - meta.class.record meta.class.record
/// ^^^^^^^^^^^^^^^^^^^^ meta.class.record - meta.class.record meta.class.record
/// ^^^^^^^^^^^^^ meta.class.record.parameters - meta.class.record meta.class.record
/// ^^^^^^^^ storage.modifier
/// ^^^^^^ keyword.declaration.class.record
Expand Down
8 changes: 5 additions & 3 deletions C#/tests/syntax_test_C#11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ public class C2 {
/// ^^^^^^ storage.type
}

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

public required string FirstName { get; init; }
/// ^^^^^^ storage.modifier.access
Expand Down
2 changes: 1 addition & 1 deletion C#/tests/syntax_test_C#7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ void Foo (in string s, in int x, in Point point)
bytes[2] = 44; // throws IndexOutOfRangeException

public readonly ref struct Span<T>
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.struct
/// ^^^^^^^^^^^^^^^^^^^ meta.struct
/// ^ storage.modifier.access
/// ^^^^^^^^ storage.modifier
/// ^^^ storage.modifier
Expand Down

0 comments on commit 0913c40

Please sign in to comment.