Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -5,6 +5,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 @@ -33,6 +34,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


## Version 11.11.3
Expand Down
54 changes: 26 additions & 28 deletions src/languages/rust.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,25 @@ 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',
begin: /b?r(#*)"(.|\n)*?"\1(?!#)/
Comment thread
joshgoebel marked this conversation as resolved.
Outdated
};
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 +216,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 +262,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
5 changes: 5 additions & 0 deletions test/markup/rust/strings.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@
<span class="hljs-string">br##&quot;foo&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>
5 changes: 5 additions & 0 deletions test/markup/rust/strings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ br#"hello"#;
br##"foo"##;

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