Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Core Grammars:
- enh(dos) add `batch` as an alias, issue #4395 [Hashim Khan][]
- fix(lisp) preserve highlighting after quoted multiplication expressions [arturict][]
- fix(rust) recognize `\\` and `\"` char-literal escapes so highlighting doesn't leak, issue #4351 [Sarath Francis][]
- fix(rust) reuse the string rules inside attributes so raw and byte strings are highlighted there, issue #3817 [Maksim Zhukau][]
- fix(cmake) only highlight standalone numbers, not digits that begin an identifier (e.g. `3rdparty`), issue #4170 [MarkXian][]
- fix(cpp) require a word boundary before numeric literals so digits inside identifiers aren't highlighted as numbers, issue #4231 [Mark Xian][]
- fix(c) only match real `atomic_*` type names, not C11 atomic functions, issue #3837 [MarkXian][]
Expand Down Expand Up @@ -43,6 +44,7 @@ CONTRIBUTORS
[Nicolas Le Cam]: https://github.com/KuSh
[Konstantin Baltsat]: https://github.com/Baltsat
[David Pavlovschii]: https://github.com/davidpavlovschi
[Maksim Zhukau]: https://github.com/MaksZhukov
[Pablo]: https://github.com/MsfPablo
[Jayesh Bhade]: https://github.com/Jaybhade
[Hirse]: https://github.com/Hirse
Expand Down
55 changes: 27 additions & 28 deletions src/languages/rust.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ export default function(hljs) {
"assert_ne!",
"debug_assert_ne!"
];
const QUOTE_STRING = hljs.inherit(hljs.QUOTE_STRING_MODE, {
Comment thread
joshgoebel marked this conversation as resolved.
begin: /b?"/,
illegal: null
});
const RAW_STRING = {
scope: 'string',
// rustc caps raw string delimiters at 255 hashes
begin: /b?r(#{0,255})"(.|\n)*?"\1(?!#)/
};
const CHARACTER = {
scope: 'string',
begin: /b?'/,
end: /'/,
contains: [
{
scope: "char.escape",
match: /\\('|"|\\|\w|x\w{2}|u\w{4}|U\w{8})/
}
Comment on lines +171 to +177

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is CHARACTER meant to match a single character?

]
};
const TYPES = [
"i8",
"i16",
Expand Down Expand Up @@ -197,31 +217,14 @@ export default function(hljs) {
contains: [
hljs.C_LINE_COMMENT_MODE,
hljs.COMMENT('/\\*', '\\*/', { contains: [ 'self' ] }),
hljs.inherit(hljs.QUOTE_STRING_MODE, {
begin: /b?"/,
illegal: null
}),
QUOTE_STRING,
{
scope: 'symbol',
// negative lookahead to avoid matching `'`
begin: /'[a-zA-Z_][a-zA-Z0-9_]*(?!')/
},
{
scope: 'string',
variants: [
{ begin: /b?r(#*)"(.|\n)*?"\1(?!#)/ },
{
begin: /b?'/,
end: /'/,
contains: [
{
scope: "char.escape",
match: /\\('|"|\\|\w|x\w{2}|u\w{4}|U\w{8})/
}
]
}
]
},
RAW_STRING,
CHARACTER,
{
scope: 'number',
variants: [
Expand Down Expand Up @@ -260,14 +263,10 @@ export default function(hljs) {
begin: '#!?\\[',
end: '\\]',
contains: [
{
scope: 'string',
begin: /"/,
end: /"/,
contains: [
hljs.BACKSLASH_ESCAPE
]
}
// attributes hold arbitrary token trees, so any string literal
// that is valid elsewhere is valid here too
QUOTE_STRING,
RAW_STRING
]
},
{
Expand Down
6 changes: 6 additions & 0 deletions test/markup/rust/strings.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@
<span class="hljs-string">br&quot; &quot;</span>;
<span class="hljs-string">br#&quot;hello&quot;#</span>;
<span class="hljs-string">br##&quot;foo&quot;##</span>;
<span class="hljs-string">r################&quot;many hashes&quot;################</span>;

<span class="hljs-meta">#[error(<span class="hljs-string">&quot;\&quot; appears in a string&quot;</span>)]</span>
<span class="hljs-meta">#[doc = <span class="hljs-string">r#&quot;a &quot;quoted&quot; word&quot;#</span>]</span>
<span class="hljs-meta">#[doc = <span class="hljs-string">b&quot;bytes&quot;</span>]</span>
<span class="hljs-meta">#[doc = <span class="hljs-string">r##&quot;multi
line &quot;# raw&quot;##</span>]</span>
<span class="hljs-meta">#[foo(Bar&lt;&#x27;a&gt;)]</span>
6 changes: 6 additions & 0 deletions test/markup/rust/strings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@ r##" "###
br" ";
br#"hello"#;
br##"foo"##;
r################"many hashes"################;

#[error("\" appears in a string")]
#[doc = r#"a "quoted" word"#]
#[doc = b"bytes"]
#[doc = r##"multi
line "# raw"##]
#[foo(Bar<'a>)]