Skip to content

Commit 105a11a

Browse files
TaraLeijoshgoebel
andauthored
Add raw string literal highlighting for C# 11. (highlightjs#3906)
* Add raw string literal highlighting for C# 11. Fixes issue highlightjs#3868. This fails the tests as the Vala default.txt is recognised now as C#. However, Vala is very close in syntax to C#, and the default.txt also seems to be valid C# so not sure what to do about this. * (dev) prevent autodetect failures from breaking tests [Josh Goebe] We are deemphasizing auto-detect - it was never great. Someone who cares about auto-detect can run the tool, but we will no longer fail the test suite based on any auto-detect issues. --------- Co-authored-by: Josh Goebel <me@joshgoebel.com>
1 parent 607a929 commit 105a11a

File tree

5 files changed

+70
-28
lines changed

5 files changed

+70
-28
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Core Grammars:
1515
- fix(rust) fix for r# raw identifier not being highlighted correctly. [JaeBaek Lee][]
1616
- enh(rust) Adding union to be recognized as a keyword in Rust. [JaeBaek Lee][]
1717
- fix(yaml) fix for yaml with keys having brackets highlighted incorrectly [Aneesh Kulkarni][]
18+
- fix(csharp) add raw string highlighting for C# 11. [Tara][]
1819
- fix(bash) fix # within token being detected as the start of a comment [Felix Uhl][]
1920
- fix(python) fix `or` conflicts with string highlighting [Mohamed Ali][]
2021
- enh(delphi) allow digits to be omitted for hex and binary literals [Jonah Jeleniewski][]
@@ -57,6 +58,7 @@ Themes:
5758
[Kristian Ekenes]: https://github.com/ekenes
5859
[Aneesh Kulkarni]: https://github.com/aneesh98
5960
[Bruno Meneguele]: https://github.com/bmeneg
61+
[Tara]: https://github.com/taralei
6062
[Felix Uhl]: https://github.com/iFreilicht
6163
[nataliia-radina]: https://github.com/Nataliia-Radina
6264
[Robloxian Demo]: https://github.com/RobloxianDemo

src/languages/csharp.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ export default function(hljs) {
164164
],
165165
relevance: 0
166166
};
167+
const RAW_STRING = {
168+
className: 'string',
169+
begin: /"""("*)(?!")(.|\n)*?"""\1/,
170+
relevance: 1
171+
};
167172
const VERBATIM_STRING = {
168173
className: 'string',
169174
begin: '@"',
@@ -229,6 +234,7 @@ export default function(hljs) {
229234
hljs.inherit(hljs.C_BLOCK_COMMENT_MODE, { illegal: /\n/ })
230235
];
231236
const STRING = { variants: [
237+
RAW_STRING,
232238
INTERPOLATED_VERBATIM_STRING,
233239
INTERPOLATED_STRING,
234240
VERBATIM_STRING,

test/detect/index.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,31 @@ function testAutoDetection(language, { detectPath }) {
3131
});
3232
}
3333

34-
describe('hljs.highlightAuto()', () => {
35-
before(async function() {
36-
const thirdPartyPackages = await getThirdPartyPackages();
37-
38-
const languages = hljs.listLanguages();
39-
describe(`hljs.highlightAuto()`, function() {
40-
languages.filter(hljs.autoDetection).forEach((language) => {
41-
const detectPath = detectTestDir(language);
42-
testAutoDetection(language, { detectPath });
43-
});
44-
});
45-
46-
// assumes only one package provides the requested module name
47-
function detectTestDir(name) {
48-
for (let i = 0; i < thirdPartyPackages.length; ++i) {
49-
const pkg = thirdPartyPackages[i];
50-
const idx = pkg.names.indexOf(name);
51-
if (idx !== -1) return pkg.detectTestPaths[idx];
52-
}
53-
return null; // test not found
54-
}
55-
});
56-
57-
it("compiling the grammars", async function() {
58-
const languages = hljs.listLanguages();
59-
languages.forEach(lang => hljs.highlight("", { language: lang} ))
60-
}); // this is also required for the dynamic test generation above to work
61-
});
34+
// describe('hljs.highlightAuto()', () => {
35+
// before(async function() {
36+
// const thirdPartyPackages = await getThirdPartyPackages();
37+
38+
// const languages = hljs.listLanguages();
39+
// describe(`hljs.highlightAuto()`, function() {
40+
// languages.filter(hljs.autoDetection).forEach((language) => {
41+
// const detectPath = detectTestDir(language);
42+
// testAutoDetection(language, { detectPath });
43+
// });
44+
// });
45+
46+
// // assumes only one package provides the requested module name
47+
// function detectTestDir(name) {
48+
// for (let i = 0; i < thirdPartyPackages.length; ++i) {
49+
// const pkg = thirdPartyPackages[i];
50+
// const idx = pkg.names.indexOf(name);
51+
// if (idx !== -1) return pkg.detectTestPaths[idx];
52+
// }
53+
// return null; // test not found
54+
// }
55+
// });
56+
57+
// it("compiling the grammars", async function() {
58+
// const languages = hljs.listLanguages();
59+
// languages.forEach(lang => hljs.highlight("", { language: lang} ))
60+
// }); // this is also required for the dynamic test generation above to work
61+
// });
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<span class="hljs-comment">// String raw literals since C# 11. &lt;https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/raw-string&gt;</span>
2+
3+
<span class="hljs-built_in">string</span> longMessage1 = <span class="hljs-string">&quot;&quot;&quot;&quot;
4+
This is a long message.
5+
It has several lines.
6+
Some are indented
7+
more than others.
8+
Some have &quot;&quot;&quot;quoted text&quot;&quot;&quot; in them. \&quot;
9+
&quot;&quot;&quot;&quot;</span>;
10+
11+
<span class="hljs-built_in">string</span> longMessage2 = <span class="hljs-string">&quot;&quot;&quot;
12+
This is a long message.
13+
It has several lines.
14+
Some are indented
15+
more than others.
16+
Some have &quot;&quot;quoted text&quot;&quot; in them. \&quot;
17+
&quot;&quot;&quot;</span>;

test/markup/csharp/string-raw.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// String raw literals since C# 11. <https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/raw-string>
2+
3+
string longMessage1 = """"
4+
This is a long message.
5+
It has several lines.
6+
Some are indented
7+
more than others.
8+
Some have """quoted text""" in them. \"
9+
"""";
10+
11+
string longMessage2 = """
12+
This is a long message.
13+
It has several lines.
14+
Some are indented
15+
more than others.
16+
Some have ""quoted text"" in them. \"
17+
""";

0 commit comments

Comments
 (0)