Skip to content

Commit

Permalink
C#: More specific class-name highlighting. Fix #1371
Browse files Browse the repository at this point in the history
  • Loading branch information
Golmote committed Mar 28, 2018
1 parent c49b139 commit 0a95f69
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 76 deletions.
53 changes: 41 additions & 12 deletions components/prism-csharp.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,53 @@ Prism.languages.csharp = Prism.languages.extend('clike', {
greedy: true
}
],
'class-name': [
{
// (Foo bar, Bar baz)
pattern: /\b[A-Z]\w*(?:\.\w+)*\b(?=\s+\w+)/,
inside: {
punctuation: /\./
}
},
{
// [Foo]
pattern: /(\[)[A-Z]\w*(?:\.\w+)*\b/,
lookbehind: true,
inside: {
punctuation: /\./
}
},
{
// class Foo : Bar
pattern: /(\b(?:class|interface)\s+[A-Z]\w*(?:\.\w+)*\s*:\s*)[A-Z]\w*(?:\.\w+)*\b/,
lookbehind: true,
inside: {
punctuation: /\./
}
},
{
// class Foo
pattern: /((?:\b(?:class|interface|new)\s+)|(?:catch\s+\())[A-Z]\w*(?:\.\w+)*\b/,
lookbehind: true,
inside: {
punctuation: /\./
}
}
],
'number': /\b0x[\da-f]+\b|(?:\b\d+\.?\d*|\B\.\d+)f?/i
});

if (Prism.util.type(Prism.languages.csharp['class-name']) !== 'Array') {
Prism.languages.csharp['class-name'] = [Prism.languages.csharp['class-name']];
}
Prism.languages.csharp['class-name'].push({
pattern: /\b[A-Z]\w*(?:\.\w+)*\b(?!\()/,
inside: {
punctuation: /\./
}
});

Prism.languages.insertBefore('csharp', 'class-name', {
'generic-method': {
pattern: /[a-z0-9_]+\s*<[^>\r\n]+?>\s*(?=\()/i,
alias: 'function',
pattern: /\w+\s*<[^>\r\n]+?>\s*(?=\()/,
inside: {
function: /^\w+/,
'class-name': {
pattern: /\b[A-Z]\w*(?:\.\w+)*\b/,
inside: {
punctuation: /\./
}
},
keyword: Prism.languages.csharp.keyword,
punctuation: /[<>(),.:]/
}
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.

100 changes: 47 additions & 53 deletions tests/languages/csharp+aspnet/directive_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,59 @@
[
["directive tag", [
["directive tag", "<%:"],
["class-name", [
"Page",
["punctuation", "."],
"Title"
]],
" Page",
["punctuation", "."],
"Title ",
["directive tag", "%>"]
]],

["directive tag", [
["directive tag", "<%#:"],
["class-name", [
"Item",
["punctuation", "."],
"ProductID"
]],
["directive tag", "%>"]
]],
["directive tag", "<%#:"],
"Item",
["punctuation", "."],
"ProductID",
["directive tag", "%>"]
]],

["tag", [
["tag", [
["punctuation", "<"],
"a"
]],
["attr-name", [
"href"
]],
["attr-value", [
["punctuation", "="],
["punctuation", "\""],
"ProductDetails.aspx?productID=",
["directive tag", [
["directive tag", "<%#:"],
["class-name", [
"Item",
["punctuation", "."],
"ProductID"
]],
["directive tag", "%>"]
]],
["punctuation", "\""]
]],
["punctuation", ">"]
]],
["tag", [
["tag", [
["punctuation", "<"],
"a"
]],
["attr-name", [
"href"
]],
["attr-value", [
["punctuation", "="],
["punctuation", "\""],
"ProductDetails.aspx?productID=",
["directive tag", [
["directive tag", "<%#:"],
"Item",
["punctuation", "."],
"ProductID",
["directive tag", "%>"]
]],
["punctuation", "\""]
]],
["punctuation", ">"]
]],

["directive tag", [
["directive tag", "<%"],
["keyword", "if"],
["punctuation", "("],
"foo",
["punctuation", ")"],
["punctuation", "{"],
["directive tag", "%>"]
]],
"\r\n\tfoobar\r\n",
["directive tag", [
["directive tag", "<%"],
["punctuation", "}"],
["directive tag", "%>"]
]]
["directive tag", [
["directive tag", "<%"],
["keyword", "if"],
["punctuation", "("],
"foo",
["punctuation", ")"],
["punctuation", "{"],
["directive tag", "%>"]
]],
"\r\n\tfoobar\r\n",
["directive tag", [
["directive tag", "<%"],
["punctuation", "}"],
["directive tag", "%>"]
]]
]

----------------------------------------------------
Expand Down
25 changes: 19 additions & 6 deletions tests/languages/csharp/class-name_feature.test
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Foo
interface BarBaz
Foo.Barbaz
class Foo : Bar
[Foobar]
void Foo(Bar bar, Baz baz)

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

Expand All @@ -9,11 +11,22 @@ Foo.Barbaz
["class-name", ["Foo"]],
["keyword", "interface"],
["class-name", ["BarBaz"]],
["class-name", [
"Foo",
["punctuation", "."],
"Barbaz"
]]
["keyword", "class"],
["class-name", ["Foo"]],
["punctuation", ":"],
["class-name", ["Bar"]],
["punctuation", "["],
["class-name", ["Foobar"]],
["punctuation", "]"],
["keyword", "void"],
["function", "Foo"],
["punctuation", "("],
["class-name", ["Bar"]],
" bar",
["punctuation", ","],
["class-name", ["Baz"]],
" baz",
["punctuation", ")"]
]

----------------------------------------------------
Expand Down
10 changes: 6 additions & 4 deletions tests/languages/csharp/generic_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ method<int, char>();

[
["keyword", "void"],
["generic-method", [ "method",
["generic-method", [
["function", "method"],
["punctuation", "<"],
"T",
["class-name", ["T"]],
["punctuation", ","],
" U",
["class-name", ["U"]],
["punctuation", ">"]
]],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],
["generic-method", [ "method",
["generic-method", [
["function", "method"],
["punctuation", "<"],
["keyword", "int"],
["punctuation", ","],
Expand Down
148 changes: 148 additions & 0 deletions tests/languages/csharp/issue1371.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
container.Register<Car>();
container.Register<IJuice, Juice>();
var container = new Container(f =>
{
f.For<IFoo>().Use<Foo>();
});
class LandAnimal {
public void Move() => Run(); }
class Dog : LandAnimal {
public new void Move() => Run(); }
class Works : LandAnimal {
public override void Move() => Run(); }
[Required]
[RequiredAttribute()]
[Range(1, 10)]

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

[
"container",
["punctuation", "."],
["generic-method", [
["function", "Register"],
["punctuation", "<"],
["class-name", ["Car"]],
["punctuation", ">"]
]],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],
"\r\ncontainer",
["punctuation", "."],
["generic-method", [
["function", "Register"],
["punctuation", "<"],
["class-name", ["IJuice"]],
["punctuation", ","],
["class-name", ["Juice"]],
["punctuation", ">"]
]],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],
["keyword", "var"],
" container ",
["operator", "="],
["keyword", "new"],
["class-name", ["Container"]],
["punctuation", "("],
"f ",
["operator", "="],
["operator", ">"],
["punctuation", "{"],
"\r\n f",
["punctuation", "."],
["generic-method", [
["function", "For"],
["punctuation", "<"],
["class-name", ["IFoo"]],
["punctuation", ">"]
]],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "."],
["generic-method", [
["function", "Use"],
["punctuation", "<"],
["class-name", ["Foo"]],
["punctuation", ">"]
]],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],
["punctuation", "}"],
["punctuation", ")"],
["punctuation", ";"],
["keyword", "class"],
["class-name", ["LandAnimal"]],
["punctuation", "{"],
["keyword", "public"],
["keyword", "void"],
["function", "Move"],
["punctuation", "("],
["punctuation", ")"],
["operator", "="],
["operator", ">"],
["function", "Run"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],
["punctuation", "}"],
["keyword", "class"],
["class-name", ["Dog"]],
["punctuation", ":"],
["class-name", ["LandAnimal"]],
["punctuation", "{"],
["keyword", "public"],
["keyword", "new"],
["keyword", "void"],
["function", "Move"],
["punctuation", "("],
["punctuation", ")"],
["operator", "="],
["operator", ">"],
["function", "Run"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],
["punctuation", "}"],
["keyword", "class"],
["class-name", ["Works"]],
["punctuation", ":"],
["class-name", ["LandAnimal"]],
["punctuation", "{"],
["keyword", "public"],
["keyword", "override"],
["keyword", "void"],
["function", "Move"],
["punctuation", "("],
["punctuation", ")"],
["operator", "="],
["operator", ">"],
["function", "Run"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],
["punctuation", "}"],
["punctuation", "["],
["class-name", ["Required"]],
["punctuation", "]"],
["punctuation", "["],
["class-name", ["RequiredAttribute"]],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "]"],
["punctuation", "["],
["class-name", ["Range"]],
["punctuation", "("],
["number", "1"],
["punctuation", ","],
["number", "10"],
["punctuation", ")"],
["punctuation", "]"]
]

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

Checks for various cases of class names. See #1371

0 comments on commit 0a95f69

Please sign in to comment.