Skip to content

Commit

Permalink
[C#] support class definitions without a body
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith Hall committed Dec 26, 2024
1 parent be1bdd1 commit 3a5f3fd
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 39 deletions.
74 changes: 38 additions & 36 deletions C#/C#.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,7 @@ contexts:
- match: \s*(;)
captures:
1: punctuation.terminator.statement.cs
pop: true

maybe_terminator:
- include: terminator
- match: (?=\S)
pop: true
pop: 1

delegate_end:
- include: terminator
Expand All @@ -477,6 +472,7 @@ contexts:

class_signature:
- meta_scope: meta.class.cs
- include: terminator
- match: (?=\S)
set: class_body

Expand Down Expand Up @@ -517,21 +513,24 @@ contexts:
data_type_expect_block:
- match: \{
scope: punctuation.section.block.begin.cs
push:
- meta_scope: meta.block.cs
- match: \}
scope: punctuation.section.block.end.cs
pop: 2
- include: attribute
- include: class_declaration
- include: interface_declaration
- include: delegate_declaration
- include: method_declaration
- include: stray_close_bracket
push: data_type_inside_block
- include: terminator
- match: \S*
scope: invalid.illegal.cs
pop: true

data_type_inside_block:
- meta_scope: meta.block.cs
- match: \}
scope: punctuation.section.block.end.cs
pop: 2
- include: attribute
- include: class_declaration
- include: interface_declaration
- include: delegate_declaration
- include: method_declaration
- include: stray_close_bracket

data_type_parameter:
- match: '<'
scope: meta.generic.cs punctuation.definition.generic.begin.cs
Expand All @@ -555,8 +554,7 @@ contexts:
- match: '{{name}}'
scope: entity.other.inherited-class.cs
push:
- maybe_terminator
- maybe_constructor_parameters
- function_call_begin_paren
- include: type

type_constraint_common:
Expand Down Expand Up @@ -800,17 +798,19 @@ contexts:
- include: attribute
- include: lambdas

- match: (?=[^\s\[])
set:
- match: '\s*\b(out|ref|this|params|in)\s+'
captures:
1: storage.modifier.parameter.cs
- match: \s
pop: true
- include: type
- match: (?=[^\s\[;])
set: method_param_type_modifier
- match: (?=\}|\)|>|\]|;)
pop: true

method_param_type_modifier:
- match: '\s*\b(out|ref|this|params|in)\s+'
captures:
1: storage.modifier.parameter.cs
- match: \s
pop: true
- include: type

constructor_prebody:
- meta_scope: meta.method.constructor.cs
- match: ':'
Expand Down Expand Up @@ -1572,15 +1572,17 @@ contexts:
type_tuple:
- match: \(
scope: punctuation.section.sequence.begin.cs
set:
- meta_scope: meta.sequence.tuple.cs
- match: \)
scope: punctuation.section.sequence.end.cs
pop: true
- match: ','
scope: punctuation.separator.sequence.cs
- match: (?=\S)
push: var_declaration_explicit
set: type_tuple_content

type_tuple_content:
- meta_scope: meta.sequence.tuple.cs
- match: \)
scope: punctuation.section.sequence.end.cs
pop: true
- match: ','
scope: punctuation.separator.sequence.cs
- match: (?=\S)
push: var_declaration_explicit

type_common:
- include: namespace_variables
Expand Down
40 changes: 40 additions & 0 deletions C#/tests/syntax_test_C#12.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/// SYNTAX TEST "Packages/C#/C#.sublime-syntax"

public class InvalidException(string _message) : Exception(_message);
///^^^ storage.modifier.access.cs
/// ^^^^^^^^^^^^^^^^^^^^^^ meta.class.cs
/// ^^^^^ keyword.declaration.class.cs
/// ^^^^^^^^^^^^^^^^ entity.name.class.cs
/// ^^^^^^^^^^^^^^^^^ meta.class.constructor.parameters.cs
/// ^ punctuation.section.parameters.begin.cs
/// ^^^^^^ storage.type.cs
/// ^^^^^^^^ variable.parameter.cs
/// ^ punctuation.section.parameters.end.cs
/// ^^^^^^^^^^^^^^^^^^^^^^^ meta.class.cs
/// ^ punctuation.separator.type.cs
/// ^^^^^^^^^ entity.other.inherited-class.cs
/// ^^^^^^^^^^ meta.function-call.cs meta.group.cs
/// ^ punctuation.section.group.begin.cs
/// ^^^^^^^^ variable.other.cs
/// ^ punctuation.section.group.end.cs
/// ^ punctuation.terminator.statement.cs
/// ^ - meta.class

public class NotFoundException(string _message) : Exception(_message);
///^^^ storage.modifier.access.cs
/// ^^^^^^^^^^^^^^^^^^^^^^^ meta.class.cs
/// ^^^^^ keyword.declaration.class.cs
/// ^^^^^^^^^^^^^^^^^ entity.name.class.cs
/// ^^^^^^^^^^^^^^^^^ meta.class.constructor.parameters.cs
/// ^ punctuation.section.parameters.begin.cs
/// ^^^^^^ storage.type.cs
/// ^^^^^^^^ variable.parameter.cs
/// ^ punctuation.section.parameters.end.cs
/// ^^^^^^^^^^^^^^^^^^^^^^^ meta.class.cs
/// ^ punctuation.separator.type.cs
/// ^^^^^^^^^ entity.other.inherited-class.cs
/// ^^^^^^^^^^ meta.function-call.cs meta.group.cs
/// ^ punctuation.section.group.begin.cs
/// ^^^^^^^^ variable.other.cs
/// ^ punctuation.section.group.end.cs
/// ^ punctuation.terminator.statement.cs
6 changes: 3 additions & 3 deletions C#/tests/syntax_test_C#8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ namespace CommonTests.Attributes;
[AttributeUsage(AttributeTargets.Method)]
public class CustomAutoDataAttribute() : AutoDataAttribute(
() =>
///^^^^^^ meta.class.constructor.parameters.cs
///^^^^^^^ meta.class.cs meta.function-call.cs meta.group.cs
/// ^^^^^ meta.function.anonymous.cs meta.group.cs
/// ^ punctuation.section.group.begin.cs
/// ^ meta.group.cs punctuation.section.group.end.cs
Expand All @@ -333,8 +333,8 @@ public class CustomAutoDataAttribute() : AutoDataAttribute(
fixture.Customizations.Add(new DateOnlySpecimenBuilder());
return fixture;
});
///^^^ meta.class.constructor.parameters.cs
///^^^ meta.class.cs meta.function-call.cs meta.group.cs
/// ^ punctuation.section.block.end.cs
/// ^ punctuation.section.parameters.end.cs
/// ^ punctuation.section.group.end.cs
/// ^ meta.class.cs punctuation.terminator.statement.cs

0 comments on commit 3a5f3fd

Please sign in to comment.