Skip to content

Commit

Permalink
C#: Added with keyword & improved record support (#2993)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Jul 15, 2021
1 parent 14fdfe3 commit fdd291c
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 87 deletions.
16 changes: 11 additions & 5 deletions components/prism-csharp.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
typeDeclaration: 'class enum interface record struct',
// contextual keywords
// ("var" and "dynamic" are missing because they are used like types)
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',
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*{)',
// all other keywords
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'
};
Expand Down Expand Up @@ -158,7 +158,7 @@
{
// Variable, field and parameter declaration
// (Foo bar, Bar baz, Foo[,,] bay, Foo<Bar, FooBar<Bar>> bax)
pattern: re(/\b<<0>>(?=\s+(?!<<1>>)<<2>>(?:\s*[=,;:{)\]]|\s+(?:in|when)\b))/.source, [typeExpression, nonContextualKeywords, name]),
pattern: re(/\b<<0>>(?=\s+(?!<<1>>|with\s*\{)<<2>>(?:\s*[=,;:{)\]]|\s+(?:in|when)\b))/.source, [typeExpression, nonContextualKeywords, name]),
inside: typeInside
}
],
Expand Down Expand Up @@ -239,18 +239,24 @@
// class Foo<F> : Bar, IList<FooBar>
// where F : Bar, IList<int>
pattern: re(
/\b((?:<<0>>\s+<<1>>|where\s+<<2>>)\s*:\s*)(?:<<3>>|<<4>>)(?:\s*,\s*(?:<<3>>|<<4>>))*(?=\s*(?:where|[{;]|=>|$))/.source,
[typeDeclarationKeywords, genericName, name, typeExpression, keywords.source]
/\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,
[typeDeclarationKeywords, genericName, name, typeExpression, keywords.source, nestedRound, /\bnew\s*\(\s*\)/.source]
),
lookbehind: true,
inside: {
'record-arguments': {
pattern: re(/(^(?!new\s*\()<<0>>\s*)<<1>>/.source, [genericName, nestedRound]),
lookbehind: true,
greedy: true,
inside: Prism.languages.csharp
},
'keyword': keywords,
'class-name': {
pattern: RegExp(typeExpression),
greedy: true,
inside: typeInside
},
'punctuation': /,/
'punctuation': /[,()]/
}
},
'preprocessor': {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-csharp.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions tests/languages/csharp/class-name-declaration_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ class Foo
interface BarBaz
struct Foo
enum Foo
record Foo
class Foo<A, B>
interface Bar<out T>
record Foo<A, B>

record TestData(string Name);

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

["keyword", "record"],
["class-name", ["Foo"]],

["keyword", "class"],
["class-name", [
"Foo",
Expand All @@ -47,6 +54,26 @@ public static RGBColor FromRainbow(Rainbow colorBand) =>
["punctuation", ">"]
]],

["keyword", "record"],
["class-name", [
"Foo",
["punctuation", "<"],
"A",
["punctuation", ","],
" B",
["punctuation", ">"]
]],

["keyword", "record"],
["class-name", ["TestData"]],
["punctuation", "("],
["class-name", [
["keyword", "string"]
]],
" Name",
["punctuation", ")"],
["punctuation", ";"],

["comment", "// not variables"],

["keyword", "public"],
Expand Down
52 changes: 0 additions & 52 deletions tests/languages/csharp/issue2991.test

This file was deleted.

19 changes: 18 additions & 1 deletion tests/languages/csharp/keyword_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ where;
while
yield

// very contextual keywords:
Person person2 = person1 with { FirstName = "John" };

----------------------------------------------------

[
Expand Down Expand Up @@ -219,7 +222,21 @@ yield
["keyword", "when"],
["keyword", "where"], ["punctuation", ";"],
["keyword", "while"],
["keyword", "yield"]
["keyword", "yield"],

["comment", "// very contextual keywords:"],

["class-name", ["Person"]],
" person2 ",
["operator", "="],
" person1 ",
["keyword", "with"],
["punctuation", "{"],
" FirstName ",
["operator", "="],
["string", "\"John\""],
["punctuation", "}"],
["punctuation", ";"]
]

----------------------------------------------------
Expand Down
Loading

0 comments on commit fdd291c

Please sign in to comment.