Skip to content

Commit fdd291c

Browse files
C#: Added with keyword & improved record support (#2993)
1 parent 14fdfe3 commit fdd291c

6 files changed

+213
-87
lines changed

components/prism-csharp.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
typeDeclaration: 'class enum interface record struct',
4848
// contextual keywords
4949
// ("var" and "dynamic" are missing because they are used like types)
50-
contextual: 'add alias and ascending async await by descending from(?=\\s*(?:\\w|$)) get global group into init(?=\\s*;) join let nameof not notnull on or orderby partial remove select set unmanaged value when where',
50+
contextual: 'add alias and ascending async await by descending from(?=\\s*(?:\\w|$)) get global group into init(?=\\s*;) join let nameof not notnull on or orderby partial remove select set unmanaged value when where with(?=\\s*{)',
5151
// all other keywords
5252
other: 'abstract as base break case catch checked const continue default delegate do else event explicit extern finally fixed for foreach goto if implicit in internal is lock namespace new null operator out override params private protected public readonly ref return sealed sizeof stackalloc static switch this throw try typeof unchecked unsafe using virtual volatile while yield'
5353
};
@@ -158,7 +158,7 @@
158158
{
159159
// Variable, field and parameter declaration
160160
// (Foo bar, Bar baz, Foo[,,] bay, Foo<Bar, FooBar<Bar>> bax)
161-
pattern: re(/\b<<0>>(?=\s+(?!<<1>>)<<2>>(?:\s*[=,;:{)\]]|\s+(?:in|when)\b))/.source, [typeExpression, nonContextualKeywords, name]),
161+
pattern: re(/\b<<0>>(?=\s+(?!<<1>>|with\s*\{)<<2>>(?:\s*[=,;:{)\]]|\s+(?:in|when)\b))/.source, [typeExpression, nonContextualKeywords, name]),
162162
inside: typeInside
163163
}
164164
],
@@ -239,18 +239,24 @@
239239
// class Foo<F> : Bar, IList<FooBar>
240240
// where F : Bar, IList<int>
241241
pattern: re(
242-
/\b((?:<<0>>\s+<<1>>|where\s+<<2>>)\s*:\s*)(?:<<3>>|<<4>>)(?:\s*,\s*(?:<<3>>|<<4>>))*(?=\s*(?:where|[{;]|=>|$))/.source,
243-
[typeDeclarationKeywords, genericName, name, typeExpression, keywords.source]
242+
/\b((?:<<0>>\s+<<1>>|record\s+<<1>>\s*<<5>>|where\s+<<2>>)\s*:\s*)(?:<<3>>|<<4>>|<<1>>\s*<<5>>|<<6>>)(?:\s*,\s*(?:<<3>>|<<4>>|<<6>>))*(?=\s*(?:where|[{;]|=>|$))/.source,
243+
[typeDeclarationKeywords, genericName, name, typeExpression, keywords.source, nestedRound, /\bnew\s*\(\s*\)/.source]
244244
),
245245
lookbehind: true,
246246
inside: {
247+
'record-arguments': {
248+
pattern: re(/(^(?!new\s*\()<<0>>\s*)<<1>>/.source, [genericName, nestedRound]),
249+
lookbehind: true,
250+
greedy: true,
251+
inside: Prism.languages.csharp
252+
},
247253
'keyword': keywords,
248254
'class-name': {
249255
pattern: RegExp(typeExpression),
250256
greedy: true,
251257
inside: typeInside
252258
},
253-
'punctuation': /,/
259+
'punctuation': /[,()]/
254260
}
255261
},
256262
'preprocessor': {

components/prism-csharp.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/csharp/class-name-declaration_feature.test

+27
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ class Foo
22
interface BarBaz
33
struct Foo
44
enum Foo
5+
record Foo
56
class Foo<A, B>
67
interface Bar<out T>
8+
record Foo<A, B>
9+
10+
record TestData(string Name);
711

812
// not variables
913
public static RGBColor FromRainbow(Rainbow colorBand) =>
@@ -28,6 +32,9 @@ public static RGBColor FromRainbow(Rainbow colorBand) =>
2832
["keyword", "enum"],
2933
["class-name", ["Foo"]],
3034

35+
["keyword", "record"],
36+
["class-name", ["Foo"]],
37+
3138
["keyword", "class"],
3239
["class-name", [
3340
"Foo",
@@ -47,6 +54,26 @@ public static RGBColor FromRainbow(Rainbow colorBand) =>
4754
["punctuation", ">"]
4855
]],
4956

57+
["keyword", "record"],
58+
["class-name", [
59+
"Foo",
60+
["punctuation", "<"],
61+
"A",
62+
["punctuation", ","],
63+
" B",
64+
["punctuation", ">"]
65+
]],
66+
67+
["keyword", "record"],
68+
["class-name", ["TestData"]],
69+
["punctuation", "("],
70+
["class-name", [
71+
["keyword", "string"]
72+
]],
73+
" Name",
74+
["punctuation", ")"],
75+
["punctuation", ";"],
76+
5077
["comment", "// not variables"],
5178

5279
["keyword", "public"],

tests/languages/csharp/issue2991.test

-52
This file was deleted.

tests/languages/csharp/keyword_feature.test

+18-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ where;
108108
while
109109
yield
110110

111+
// very contextual keywords:
112+
Person person2 = person1 with { FirstName = "John" };
113+
111114
----------------------------------------------------
112115

113116
[
@@ -219,7 +222,21 @@ yield
219222
["keyword", "when"],
220223
["keyword", "where"], ["punctuation", ";"],
221224
["keyword", "while"],
222-
["keyword", "yield"]
225+
["keyword", "yield"],
226+
227+
["comment", "// very contextual keywords:"],
228+
229+
["class-name", ["Person"]],
230+
" person2 ",
231+
["operator", "="],
232+
" person1 ",
233+
["keyword", "with"],
234+
["punctuation", "{"],
235+
" FirstName ",
236+
["operator", "="],
237+
["string", "\"John\""],
238+
["punctuation", "}"],
239+
["punctuation", ";"]
223240
]
224241

225242
----------------------------------------------------

0 commit comments

Comments
 (0)