Skip to content
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Core Grammars:
- enh(json) add json5 support [Kerry Shetline][]
- fix(css) `unicode-range` parsing, issue #4253 [Kerry Shetline][]
- fix(csharp) Support digit separators [te-ing][]
- fix(rust) ensure brackets are balanced within attributes [ewwwin][]

Documentation:

Expand Down Expand Up @@ -54,6 +55,7 @@ CONTRIBUTORS
[Thomas Gorissen]: https://github.com/serrynaimo
[te-ing]: https://github.com/te-ing
[Anthony Martin]: https://github.com/anthony-c-martin
[ewwwin]: https://github.com/ewwwin
[NriotHrreion]: https://github.com/NriotHrreion


Expand Down
28 changes: 24 additions & 4 deletions src/languages/rust.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export default function(hljs) {
IDENT_RE,
regex.lookahead(/\s*\(/))
};
const META_STRING = {
className: 'string',
begin: /"/,
end: /"/,
contains: [ hljs.BACKSLASH_ESCAPE ]
};
const NUMBER_SUFFIX = '([ui](8|16|32|64|128|size)|f(32|64))\?';
const KEYWORDS = [
"abstract",
Expand Down Expand Up @@ -246,12 +252,26 @@ export default function(hljs) {
begin: '#!?\\[',
end: '\\]',
contains: [
META_STRING,
{
className: 'string',
begin: /"/,
end: /"/,
relevance: 0,
variants: [
{
begin: /\(/,
end: /\)/
},
{
begin: /\[/,
end: /\]/
},
{
begin: /\{/,
end: /\}/
}
],
contains: [
hljs.BACKSLASH_ESCAPE
'self',
META_STRING
]
}
]
Expand Down
18 changes: 18 additions & 0 deletions test/markup/rust/attributes.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<span class="hljs-meta">#[foo]</span>
<span class="hljs-meta">#![foo]</span>

<span class="hljs-meta">#[foo(bar)]</span>
<span class="hljs-meta">#[foo = bar]</span>
<span class="hljs-meta">#[foo(<span class="hljs-string">&quot;a \&quot;b\&quot; c&quot;</span>)]</span>
<span class="hljs-meta">#[foo(bar = [a, b, c])]</span>
<span class="hljs-meta">#[foo[bar]]</span>
<span class="hljs-meta">#[foo{bar}]</span>

<span class="hljs-meta">#![no_std]</span>
<span class="hljs-meta">#[doc = <span class="hljs-string">&quot;example&quot;</span>]</span>
<span class="hljs-meta">#[allow(unused, clippy::inline_always)]</span>
<span class="hljs-meta">#[macro_use(foo, bar)]</span>
<span class="hljs-meta">#[link(name = <span class="hljs-string">&quot;CoreFoundation&quot;</span>, kind = <span class="hljs-string">&quot;framework&quot;</span>)]</span>

<span class="hljs-meta">#[rtic::app(device = lm3s6965, dispatchers = [UART0, UART1])]</span>
<span class="hljs-meta">#[task(binds = USB_OTG1, local = [poller])]</span>
18 changes: 18 additions & 0 deletions test/markup/rust/attributes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[foo]
#![foo]

#[foo(bar)]
#[foo = bar]
#[foo("a \"b\" c")]
#[foo(bar = [a, b, c])]
#[foo[bar]]
#[foo{bar}]

#![no_std]
#[doc = "example"]
#[allow(unused, clippy::inline_always)]
#[macro_use(foo, bar)]
#[link(name = "CoreFoundation", kind = "framework")]

#[rtic::app(device = lm3s6965, dispatchers = [UART0, UART1])]
#[task(binds = USB_OTG1, local = [poller])]