From d1311fc4f89a87172e733ffca19d5f12f8f1c4c2 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 9 Mar 2026 16:53:09 +0100 Subject: [PATCH] Fix syntax highlighting for rust `union` keyword --- .../Packages/Rust/Rust.sublime-syntax | 3 ++ crates/bin/docs_rs_web/src/utils/highlight.rs | 54 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/crates/bin/docs_rs_web/assets/syntaxes/Packages/Rust/Rust.sublime-syntax b/crates/bin/docs_rs_web/assets/syntaxes/Packages/Rust/Rust.sublime-syntax index 79c3a8877..aeca23485 100644 --- a/crates/bin/docs_rs_web/assets/syntaxes/Packages/Rust/Rust.sublime-syntax +++ b/crates/bin/docs_rs_web/assets/syntaxes/Packages/Rust/Rust.sublime-syntax @@ -136,6 +136,9 @@ contexts: - match: \b(crate|extern|use|where)\b scope: keyword.other.rust + - match: \bunion(?=\s+[[:alpha:]_][[:alnum:]_]*\s*)\b + scope: keyword.other.rust + - match: \b(break|else|for|if|loop|match|while|continue)\b scope: keyword.control.rust diff --git a/crates/bin/docs_rs_web/src/utils/highlight.rs b/crates/bin/docs_rs_web/src/utils/highlight.rs index 2705b9d9f..ce39656be 100644 --- a/crates/bin/docs_rs_web/src/utils/highlight.rs +++ b/crates/bin/docs_rs_web/src/utils/highlight.rs @@ -135,4 +135,58 @@ mod tests { let highlighted = with_lang(Some("toml"), &text, None); assert!(highlighted.starts_with("<p>\n")); } + + // "union" should be a keyword only in this case: `union X {}`. + #[test] + fn union_keyword() { + fn highlight_and_check(code: &str, should_contain: &[&str]) { + let highlighted = with_lang(Some("rust"), code, None); + for needle in should_contain { + if !highlighted.contains(needle) { + panic!("{needle:?} isn't contained in {highlighted:?}"); + } + } + } + highlight_and_check( + r#" +struct X { + union: u32, +} +let union = X { union: 0 }; +println!("{}", union.union);"#, + &[ + // "union" as field name + "{\n union: u32 union union: 0 union.union {} } +union!();"#, + &[ + // "union" as macro name + "macro_rules! union", + // Calling the "union" macro. + "union!", + ], + ); + highlight_and_check( + "union X { a: u32 }", + // "union" has "syntax-keyword" in here, so all good. + &["union X { a: T }"#, + // "union" has "syntax-keyword" in here, so all good. + &[ + "union X