Skip to content

fix(rust) ensure brackets are balanced within attributes #4237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 @@ -51,6 +52,7 @@ CONTRIBUTORS
[hbgl]: https://github.com/hbgl
[te-ing]: https://github.com/te-ing
[Anthony Martin]: https://github.com/anthony-c-martin
[ewwwin]: https://github.com/ewwwin

## Version 11.11.1

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])]