From efd24174ab3bdf84b87efb3b568849cde00e1883 Mon Sep 17 00:00:00 2001 From: ewin Date: Mon, 7 Apr 2025 12:17:22 -0400 Subject: [PATCH 1/6] Ensure bracket balance within attributes --- src/languages/rust.js | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/languages/rust.js b/src/languages/rust.js index 13e7be19f2..be280ae3c9 100644 --- a/src/languages/rust.js +++ b/src/languages/rust.js @@ -25,6 +25,33 @@ export default function(hljs) { IDENT_RE, regex.lookahead(/\s*\(/)) }; + const META_STRING = { + className: 'string', + begin: /"/, + end: /"/, + contains: [ hljs.BACKSLASH_ESCAPE ] + }; + const META_DELIM_TREE = { + relevance: 0, + variants: [ + { + begin: /\(/, + end: /\)/, + }, + { + begin: /\[/, + end: /\]/, + }, + { + begin: /\{/, + end: /\}/, + }, + ], + contains: [ + 'self', + META_STRING, + ] + }; const NUMBER_SUFFIX = '([ui](8|16|32|64|128|size)|f(32|64))\?'; const KEYWORDS = [ "abstract", @@ -246,14 +273,8 @@ export default function(hljs) { begin: '#!?\\[', end: '\\]', contains: [ - { - className: 'string', - begin: /"/, - end: /"/, - contains: [ - hljs.BACKSLASH_ESCAPE - ] - } + META_STRING, + META_DELIM_TREE, ] }, { From 2ca8fca1a82a77e07bcbfc7b5a4de98cc4064440 Mon Sep 17 00:00:00 2001 From: ewin Date: Mon, 7 Apr 2025 12:17:44 -0400 Subject: [PATCH 2/6] Add test cases for attribute highlighting --- test/markup/rust/attributes.expect.txt | 18 ++++++++++++++++++ test/markup/rust/attributes.txt | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 test/markup/rust/attributes.expect.txt create mode 100644 test/markup/rust/attributes.txt diff --git a/test/markup/rust/attributes.expect.txt b/test/markup/rust/attributes.expect.txt new file mode 100644 index 0000000000..3b5be79b72 --- /dev/null +++ b/test/markup/rust/attributes.expect.txt @@ -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])] diff --git a/test/markup/rust/attributes.txt b/test/markup/rust/attributes.txt new file mode 100644 index 0000000000..eb7680a785 --- /dev/null +++ b/test/markup/rust/attributes.txt @@ -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])] From 7ca8f5300daf7515ca30b8b76de88e75cd8882e0 Mon Sep 17 00:00:00 2001 From: ewin Date: Mon, 7 Apr 2025 12:23:53 -0400 Subject: [PATCH 3/6] Re-inline single-use mode definition --- src/languages/rust.js | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/languages/rust.js b/src/languages/rust.js index be280ae3c9..9a2eb028ac 100644 --- a/src/languages/rust.js +++ b/src/languages/rust.js @@ -31,27 +31,6 @@ export default function(hljs) { end: /"/, contains: [ hljs.BACKSLASH_ESCAPE ] }; - const META_DELIM_TREE = { - relevance: 0, - variants: [ - { - begin: /\(/, - end: /\)/, - }, - { - begin: /\[/, - end: /\]/, - }, - { - begin: /\{/, - end: /\}/, - }, - ], - contains: [ - 'self', - META_STRING, - ] - }; const NUMBER_SUFFIX = '([ui](8|16|32|64|128|size)|f(32|64))\?'; const KEYWORDS = [ "abstract", @@ -274,7 +253,27 @@ export default function(hljs) { end: '\\]', contains: [ META_STRING, - META_DELIM_TREE, + { + relevance: 0, + variants: [ + { + begin: /\(/, + end: /\)/, + }, + { + begin: /\[/, + end: /\]/, + }, + { + begin: /\{/, + end: /\}/, + }, + ], + contains: [ + 'self', + META_STRING, + ] + }, ] }, { From 0aab1b0e73b03440a5fb07f40353754be77c66de Mon Sep 17 00:00:00 2001 From: ewin Date: Mon, 7 Apr 2025 12:29:19 -0400 Subject: [PATCH 4/6] Update CHANGES.md --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index e0b3e0ab7a..05ace8d1b1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,12 +8,14 @@ Core Grammars: - enh(csp) add missing directives / keywords from MDN (7 more) [Max Liashuk][] - enh(ada) add new `parallel` keyword, allow `[]` for Ada 2022 [Max Reznik][] +- fix(rust) ensure brackets are balanced within attributes [ewwwin][] CONTRIBUTORS [Josh Marchand]: https://github.com/yHSJ [Max Liashuk]: https://github.com/probil [Max Reznik]: https://github.com/reznikmm +[ewwwin]: https://github.com/ewwwin ## Version 11.11.1 From 16ce416c9b3f4b8ffdc7852d6114d2ebc33ba0db Mon Sep 17 00:00:00 2001 From: ewin Date: Thu, 24 Apr 2025 18:09:32 -0400 Subject: [PATCH 5/6] remove trailing commas --- src/languages/rust.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/languages/rust.js b/src/languages/rust.js index 9a2eb028ac..255c964a05 100644 --- a/src/languages/rust.js +++ b/src/languages/rust.js @@ -258,20 +258,20 @@ export default function(hljs) { variants: [ { begin: /\(/, - end: /\)/, + end: /\)/ }, { begin: /\[/, - end: /\]/, + end: /\]/ }, { begin: /\{/, - end: /\}/, - }, + end: /\}/ + } ], contains: [ 'self', - META_STRING, + META_STRING ] }, ] From 9b52109039943a991b6df0aadc00b59c8e6cf27e Mon Sep 17 00:00:00 2001 From: ewin Date: Thu, 24 Apr 2025 18:20:39 -0400 Subject: [PATCH 6/6] fix other lingering trailing comma, weh --- src/languages/rust.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/rust.js b/src/languages/rust.js index 255c964a05..4c665cb69a 100644 --- a/src/languages/rust.js +++ b/src/languages/rust.js @@ -273,7 +273,7 @@ export default function(hljs) { 'self', META_STRING ] - }, + } ] }, {