Skip to content

Commit b592fb4

Browse files
authored
[C#] Fix switch case keyword scopes (#4507)
This commit applies scope naming guidelines to switch..case related keywords.
1 parent 8621831 commit b592fb4

6 files changed

Lines changed: 47 additions & 41 deletions

File tree

C#/C#.sublime-syntax

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,10 +1256,9 @@ contexts:
12561256
captures:
12571257
1: keyword.control.conditional.if.cs
12581258
set: [else_block, if_block, if_condition]
1259-
- match: \b(switch)\b
1260-
captures:
1261-
1: keyword.control.flow.switch.cs
1262-
set: [switch_block, if_condition]
1259+
- match: \bswitch\b
1260+
scope: keyword.control.conditional.switch.cs
1261+
set: [switch_block, switch_condition]
12631262
- match: \b(for)\s*(\()(?=(?:{{name}}|var)\s(?!=))
12641263
captures:
12651264
1: keyword.control.loop.for.cs
@@ -1318,7 +1317,7 @@ contexts:
13181317
- match: \b(goto)\s+(case)\b
13191318
captures:
13201319
1: keyword.control.flow.goto.cs
1321-
2: keyword.control.switch.case.cs
1320+
2: keyword.control.conditional.case.cs
13221321
set: line_of_code_in
13231322
- match: '\b(goto)\s+({{name}})\s*(;)'
13241323
captures:
@@ -1480,18 +1479,18 @@ contexts:
14801479
- match: \}
14811480
scope: punctuation.section.block.end.cs
14821481
pop: 1
1483-
- match: '\b(default)\s*(:)'
1482+
- match: \b(default)\s*(:)
14841483
captures:
1485-
1: keyword.control.switch.case.cs
1484+
1: keyword.control.conditional.default.cs
14861485
2: punctuation.separator.case-statement.cs
1487-
- match: '\b(case)\b'
1488-
scope: keyword.control.switch.case.cs
1486+
- match: \bcase\b
1487+
scope: keyword.control.conditional.case.cs
14891488
push:
14901489
- match: '{{name}}(?=\s*:)'
14911490
scope: constant.other.cs
1492-
- match: '\bwhen\b'
1493-
scope: keyword.control.switch.case.when.cs
1494-
- match: '(?={{namespaced_name}}{{type_suffix}}\s+{{name}}\s+when\b)'
1491+
- match: \bwhen\b
1492+
scope: keyword.control.conditional.when.cs
1493+
- match: (?={{namespaced_name}}{{type_suffix}}\s+{{name}}\s+when\b)
14951494
push: var_declaration_explicit
14961495
- match: ':'
14971496
scope: punctuation.separator.case-statement.cs
@@ -1733,7 +1732,7 @@ contexts:
17331732
pop: 1
17341733
- include: type
17351734
- match: \bswitch\b
1736-
scope: keyword.control.flow.cs
1735+
scope: keyword.control.conditional.switch.cs
17371736
push: switch_expression
17381737
- match: '({{reserved}})\b'
17391738
scope: keyword.other.cs
@@ -2156,7 +2155,7 @@ contexts:
21562155
- match: \b_\b
21572156
scope: variable.language.anonymous.cs
21582157
- match: '\bwhen\b'
2159-
scope: keyword.control.switch.case.when.cs
2158+
scope: keyword.control.conditional.when.cs
21602159
- match: \(
21612160
scope: punctuation.section.sequence.begin.cs
21622161
push:

C#/tests/syntax_test_C#11.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ The following example shows how newlines can improve the readability of an expre
565565
*/
566566
string message = $"The usage policy for {safetyScore} is {
567567
safetyScore switch
568-
/// ^^^^^^ meta.string.interpolated source keyword.control.flow
568+
/// ^^^^^^ meta.string.interpolated source keyword.control.conditional.switch
569569
{
570570
> 90 => "Unlimited usage",
571571
> 80 => "General usage, with daily safety check",

C#/tests/syntax_test_C#7.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,10 @@ void Main(string[] args) {
198198

199199
switch (sh) {
200200
case Shape shape when sh.Area == 0:
201-
/// ^^^^ keyword.control.switch.case
201+
/// ^^^^ keyword.control.conditional.case
202202
/// ^^^^^ support.type
203203
/// ^^^^^ variable.other
204-
/// ^^^^ keyword.control.switch.case.when
204+
/// ^^^^ keyword.control.conditional.when
205205
/// ^^ variable.other
206206
/// ^ punctuation.accessor.dot
207207
/// ^^^^ variable.other
@@ -211,23 +211,23 @@ void Main(string[] args) {
211211
Console.WriteLine($"The shape: {sh.GetType().Name} with no dimensions");
212212
break;
213213
case int? example when example == 5:
214-
/// ^^^^ keyword.control.switch.case
214+
/// ^^^^ keyword.control.conditional.case
215215
/// ^^^ storage.type
216216
/// ^ storage.type.nullable
217217
/// ^^^^^^^ variable.other
218-
/// ^^^^ keyword.control.switch.case.when
218+
/// ^^^^ keyword.control.conditional.when
219219
/// ^^^^^^^ variable.other
220220
/// ^^ keyword.operator.comparison
221221
/// ^ meta.number.integer.decimal constant.numeric.value
222222
/// ^ punctuation.separator.case-statement
223223
case Shape<Shape> shape when shape.Area > 0:
224-
/// ^^^^ keyword.control.switch.case
224+
/// ^^^^ keyword.control.conditional.case
225225
/// ^^^^^ support.type
226226
/// ^ punctuation.definition.generic.begin
227227
/// ^^^^^ support.type
228228
/// ^ punctuation.definition.generic.end
229229
/// ^^^^^ variable.other
230-
/// ^^^^ keyword.control.switch.case.when
230+
/// ^^^^ keyword.control.conditional.when
231231
/// ^^^^^ variable.other
232232
/// ^ punctuation.accessor.dot
233233
/// ^^^^ variable.other

C#/tests/syntax_test_C#8.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ interface ILogger
4848
public static decimal CalculateToll(object vehicle) =>
4949
vehicle switch
5050
/// ^^^^^^^ variable.other
51-
/// ^^^^^^ keyword.control.flow
51+
/// ^^^^^^ keyword.control.conditional.switch
5252
{
5353
DeliveryTruck t when t.GrossWeightClass > 5000 => 10.00m + 5.00m,
5454
/// ^^^^^^^^^^^^^ support.type
5555
/// ^ variable.other
56-
/// ^^^^ keyword.control.switch.case.when
56+
/// ^^^^ keyword.control.conditional.when
5757
/// ^ variable.other
5858
/// ^ punctuation.accessor.dot
5959
/// ^^^^^^^^^^^^^^^^ variable.other
@@ -259,7 +259,7 @@ public static string RockPaperScissors(string first, string second)
259259
var (x, y) when x > 0 && y > 0 => Quadrant.One,
260260
/// ^^^ storage.type.variable
261261
/// ^^^^^^ meta.sequence.tuple
262-
/// ^^^^ keyword.control.switch.case.when
262+
/// ^^^^ keyword.control.conditional.when
263263
/// ^ variable.other
264264
/// ^ keyword.operator.comparison
265265
/// ^ constant.numeric.value

C#/tests/syntax_test_C#9.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ public record Student : Person { int ID; }
5151
switch (myValue)
5252
{
5353
case <= 0:
54-
/// ^^^^ keyword.control.switch.case
54+
/// ^^^^ keyword.control.conditional.case
5555
/// ^^ keyword.operator.comparison
5656
/// ^ constant.numeric.value
5757
/// ^ punctuation.separator.case-statement
5858
Console.WriteLine("Less than or equal to 0");
5959
break;
6060
case > 0 and <= 10:
61-
/// ^^^^ keyword.control.switch.case
61+
/// ^^^^ keyword.control.conditional.case
6262
/// ^ keyword.operator.comparison
6363
/// ^ constant.numeric.value
6464
/// ^^^ keyword.operator.logical
@@ -76,7 +76,7 @@ public record Student : Person { int ID; }
7676
Console.WriteLine(myValue);
7777

7878
var message = myValue switch
79-
/// ^^^^^^ keyword.control.flow
79+
/// ^^^^^^ keyword.control.conditional.switch
8080
{
8181
<= 0 => "Less than or equal to 0",
8282
/// ^^ keyword.operator.comparison
@@ -116,7 +116,7 @@ static bool CheckIfCanWalkIntoBank(Bank bank, bool isVip)
116116
/// ^^^^^^^^^^^^^^^^ variable.other
117117
/// ^ punctuation.accessor.dot
118118
/// ^^^^^^^^^^^^^^^^ variable.other
119-
/// ^^^^ keyword.control.switch.case.when
119+
/// ^^^^ keyword.control.conditional.when
120120
/// ^ keyword.operator.logical
121121
/// ^^^^^ variable.other
122122
/// ^^ punctuation.separator.case-expression
@@ -129,7 +129,7 @@ static bool CheckIfCanWalkIntoBank(Bank bank, bool isVip)
129129
return (bank.Status, isVip) switch
130130
/// ^^^^^^ keyword.control.flow.return
131131
/// ^^^^^^^^^^^^^^^^^^^^ meta.sequence.tuple
132-
/// ^^^^^^ keyword.control.flow
132+
/// ^^^^^^ keyword.control.conditional.switch
133133
{
134134
(BankBranchStatus.Open, _) => true,
135135
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.sequence.tuple
@@ -224,7 +224,7 @@ public decimal CalculateToll(object vehicle) =>
224224
Bus b when ((double)b.Riders / (double)b.Capacity) < 0.50 => 5.00m + 2.00m,
225225
/// ^^^ support.type
226226
/// ^ variable.other
227-
/// ^^^^ keyword.control.switch.case.when
227+
/// ^^^^ keyword.control.conditional.when
228228
/// ^^^^^^ meta.cast storage.type
229229
Bus b when ((double)b.Riders / (double)b.Capacity) > 0.90 => 5.00m - 1.00m,
230230
Bus b => 5.00m,
@@ -260,7 +260,7 @@ public decimal CalculateToll(object vehicle) =>
260260
/// ^ punctuation.accessor.dot
261261
/// ^^^^^ variable.other
262262
/// ^ punctuation.section.group.end
263-
/// ^^^^^^ keyword.control.flow
263+
/// ^^^^^^ keyword.control.conditional.switch
264264
{
265265
/// ^ punctuation.section.block.begin
266266
0 => 1.00m,

C#/tests/syntax_test_GeneralStructure.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ public Derived(DateTime exportDate) : base(exportDate) {
370370
/// ^ meta.method meta.block meta.block punctuation.section.block.end
371371

372372
switch (foo) {
373-
/// ^^^^^^ keyword.control.flow.switch
373+
/// ^^^^^^ keyword.control.conditional.switch
374374
/// ^^^^^ meta.group
375375
/// ^ punctuation.section.group.begin
376376
/// ^^^ variable.other
@@ -385,43 +385,43 @@ public Derived(DateTime exportDate) : base(exportDate) {
385385
break;
386386
/// ^ keyword.control
387387
case BLBodyBattleLibrary.ContextType.TapUp:
388-
/// ^^^^ keyword.control.switch.case
388+
/// ^^^^ keyword.control.conditional.case
389389
/// ^^^^^^^^^^^^^^^^^^^ variable.other
390390
/// ^ punctuation.accessor.dot
391391
/// ^^^^^^^^^^^ variable.other
392392
/// ^ punctuation.accessor.dot
393393
/// ^^^^^ constant.other
394394
/// ^ punctuation.separator.case-statement
395395
case BindingFlags.Static:
396-
/// ^^^^ keyword.control.switch.case
396+
/// ^^^^ keyword.control.conditional.case
397397
/// ^^^^^^^^^^^^ variable.other
398398
/// ^ punctuation.accessor.dot
399399
/// ^^^^^^ constant.other
400400
/// ^ punctuation.separator.case-statement
401401
case test:
402-
/// ^^^^ keyword.control.switch.case
402+
/// ^^^^ keyword.control.conditional.case
403403
/// ^^^^ constant.other
404404
/// ^ punctuation.separator.case-statement
405405
case this.test;
406-
/// ^^^^ keyword.control.switch.case
406+
/// ^^^^ keyword.control.conditional.case
407407
/// ^^^^ variable.language.this
408408
/// ^ punctuation.accessor.dot
409409
case 1*2:
410-
/// ^^^^ keyword.control.switch.case
410+
/// ^^^^ keyword.control.conditional.case
411411
/// ^ constant.numeric
412412
/// ^ keyword.operator
413413
/// ^ constant.numeric
414414
/// ^ punctuation.separator.case-statement
415415
case bar("hello"):
416-
/// ^^^^ keyword.control.switch.case
416+
/// ^^^^ keyword.control.conditional.case
417417
/// ^^^ variable.function
418418
/// ^ punctuation.section.group.begin
419419
/// ^^^^^^^ string.quoted.double
420420
/// ^ punctuation.section.group.end
421421
/// ^ punctuation.separator.case-statement
422422
break;
423423
case abc.def:
424-
/// ^^^^ keyword.control.switch.case
424+
/// ^^^^ keyword.control.conditional.case
425425
/// ^^^ variable.other
426426
/// ^ punctuation.accessor.dot
427427
/// ^^^ constant.other
@@ -1157,14 +1157,14 @@ void TestMe () {
11571157
result += 4;
11581158
goto case 'b';
11591159
/// ^^^^ keyword.control.flow.goto
1160-
/// ^^^^ keyword.control.switch.case
1160+
/// ^^^^ keyword.control.conditional.case
11611161
/// ^^^ meta.string string.quoted.single
11621162
/// ^ punctuation.definition.string.begin
11631163
/// ^ constant.character.literal
11641164
/// ^ punctuation.definition.string.end
11651165
/// ^ punctuation.terminator.statement
11661166
case 'b':
1167-
/// ^^^^ keyword.control.switch.case - invalid
1167+
/// ^^^^ keyword.control.conditional.case - invalid
11681168
/// ^^^ meta.string string.quoted.single
11691169
/// ^ punctuation.definition.string.begin
11701170
/// ^ constant.character.literal
@@ -1174,6 +1174,13 @@ void TestMe () {
11741174
case 'c':
11751175
result += 8;
11761176
break;
1177+
1178+
default:
1179+
/// ^^^^^^^ keyword.control.conditional.default
1180+
/// ^ punctuation.separator.case-statement
1181+
break;
1182+
/// ^^^^^ keyword.control.flow.break
1183+
/// ^ punctuation.terminator.statement
11771184
}
11781185

11791186
int foo;

0 commit comments

Comments
 (0)