diff --git a/components/prism-csharp.js b/components/prism-csharp.js index 84a36e63a6..15ce52dc22 100644 --- a/components/prism-csharp.js +++ b/components/prism-csharp.js @@ -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: /[<>(),.:]/ } diff --git a/components/prism-csharp.min.js b/components/prism-csharp.min.js index 00f441b72e..6c5de062ff 100644 --- a/components/prism-csharp.min.js +++ b/components/prism-csharp.min.js @@ -1 +1 @@ -Prism.languages.csharp=Prism.languages.extend("clike",{keyword:/\b(?:abstract|add|alias|as|ascending|async|await|base|bool|break|byte|case|catch|char|checked|class|const|continue|decimal|default|delegate|descending|do|double|dynamic|else|enum|event|explicit|extern|false|finally|fixed|float|for|foreach|from|get|global|goto|group|if|implicit|in|int|interface|internal|into|is|join|let|lock|long|namespace|new|null|object|operator|orderby|out|override|params|partial|private|protected|public|readonly|ref|remove|return|sbyte|sealed|select|set|short|sizeof|stackalloc|static|string|struct|switch|this|throw|true|try|typeof|uint|ulong|unchecked|unsafe|ushort|using|value|var|virtual|void|volatile|where|while|yield)\b/,string:[{pattern:/@("|')(?:\1\1|\\[\s\S]|(?!\1)[^\\])*\1/,greedy:!0},{pattern:/("|')(?:\\.|(?!\1)[^\\\r\n])*?\1/,greedy:!0}],number:/\b0x[\da-f]+\b|(?:\b\d+\.?\d*|\B\.\d+)f?/i}),"Array"!==Prism.util.type(Prism.languages.csharp["class-name"])&&(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",inside:{keyword:Prism.languages.csharp.keyword,punctuation:/[<>(),.:]/}},preprocessor:{pattern:/(^\s*)#.*/m,lookbehind:!0,alias:"property",inside:{directive:{pattern:/(\s*#)\b(?:define|elif|else|endif|endregion|error|if|line|pragma|region|undef|warning)\b/,lookbehind:!0,alias:"keyword"}}}}),Prism.languages.dotnet=Prism.languages.csharp; \ No newline at end of file +Prism.languages.csharp=Prism.languages.extend("clike",{keyword:/\b(?:abstract|add|alias|as|ascending|async|await|base|bool|break|byte|case|catch|char|checked|class|const|continue|decimal|default|delegate|descending|do|double|dynamic|else|enum|event|explicit|extern|false|finally|fixed|float|for|foreach|from|get|global|goto|group|if|implicit|in|int|interface|internal|into|is|join|let|lock|long|namespace|new|null|object|operator|orderby|out|override|params|partial|private|protected|public|readonly|ref|remove|return|sbyte|sealed|select|set|short|sizeof|stackalloc|static|string|struct|switch|this|throw|true|try|typeof|uint|ulong|unchecked|unsafe|ushort|using|value|var|virtual|void|volatile|where|while|yield)\b/,string:[{pattern:/@("|')(?:\1\1|\\[\s\S]|(?!\1)[^\\])*\1/,greedy:!0},{pattern:/("|')(?:\\.|(?!\1)[^\\\r\n])*?\1/,greedy:!0}],"class-name":[{pattern:/\b[A-Z]\w*(?:\.\w+)*\b(?=\s+\w+)/,inside:{punctuation:/\./}},{pattern:/(\[)[A-Z]\w*(?:\.\w+)*\b/,lookbehind:!0,inside:{punctuation:/\./}},{pattern:/(\b(?:class|interface)\s+[A-Z]\w*(?:\.\w+)*\s*:\s*)[A-Z]\w*(?:\.\w+)*\b/,lookbehind:!0,inside:{punctuation:/\./}},{pattern:/((?:\b(?:class|interface|new)\s+)|(?:catch\s+\())[A-Z]\w*(?:\.\w+)*\b/,lookbehind:!0,inside:{punctuation:/\./}}],number:/\b0x[\da-f]+\b|(?:\b\d+\.?\d*|\B\.\d+)f?/i}),Prism.languages.insertBefore("csharp","class-name",{"generic-method":{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:/[<>(),.:]/}},preprocessor:{pattern:/(^\s*)#.*/m,lookbehind:!0,alias:"property",inside:{directive:{pattern:/(\s*#)\b(?:define|elif|else|endif|endregion|error|if|line|pragma|region|undef|warning)\b/,lookbehind:!0,alias:"keyword"}}}}),Prism.languages.dotnet=Prism.languages.csharp; \ No newline at end of file diff --git a/tests/languages/csharp+aspnet/directive_feature.test b/tests/languages/csharp+aspnet/directive_feature.test index 7e5ff3a5e3..f19c2a82c2 100644 --- a/tests/languages/csharp+aspnet/directive_feature.test +++ b/tests/languages/csharp+aspnet/directive_feature.test @@ -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", "%>"] + ]] ] ---------------------------------------------------- diff --git a/tests/languages/csharp/class-name_feature.test b/tests/languages/csharp/class-name_feature.test index 1f8bfa1794..113982b6fe 100644 --- a/tests/languages/csharp/class-name_feature.test +++ b/tests/languages/csharp/class-name_feature.test @@ -1,6 +1,8 @@ class Foo interface BarBaz -Foo.Barbaz +class Foo : Bar +[Foobar] +void Foo(Bar bar, Baz baz) ---------------------------------------------------- @@ -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", ")"] ] ---------------------------------------------------- diff --git a/tests/languages/csharp/generic_feature.test b/tests/languages/csharp/generic_feature.test index 01115cfe6d..05d3f589a9 100644 --- a/tests/languages/csharp/generic_feature.test +++ b/tests/languages/csharp/generic_feature.test @@ -5,17 +5,19 @@ method(); [ ["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", ","], diff --git a/tests/languages/csharp/issue1371.test b/tests/languages/csharp/issue1371.test new file mode 100644 index 0000000000..1b2cb7be99 --- /dev/null +++ b/tests/languages/csharp/issue1371.test @@ -0,0 +1,148 @@ +container.Register(); +container.Register(); +var container = new Container(f => +{ + f.For().Use(); +}); +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 \ No newline at end of file