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..ac0fda35d 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/assets/syntaxes/Packages/Rust/syntax_test_rust.rs b/crates/bin/docs_rs_web/assets/syntaxes/Packages/Rust/syntax_test_rust.rs index 3ba2ebe02..526a5f2f7 100644 --- a/crates/bin/docs_rs_web/assets/syntaxes/Packages/Rust/syntax_test_rust.rs +++ b/crates/bin/docs_rs_web/assets/syntaxes/Packages/Rust/syntax_test_rust.rs @@ -1148,3 +1148,12 @@ let p_imm: *const u32 = &i as *const u32; type ExampleRawPointer = HashMap<*const i32, Option, BuildHasherDefault>; // ^^^^^^ storage.modifier - invalid // ^^^ storage.type + + +union MyUnion { +// <- keyword.other + f1: u32, +} + +let union = 1; +// ^^^^^ - keyword.other diff --git a/crates/bin/docs_rs_web/src/utils/highlight.rs b/crates/bin/docs_rs_web/src/utils/highlight.rs index 2705b9d9f..053a86228 100644 --- a/crates/bin/docs_rs_web/src/utils/highlight.rs +++ b/crates/bin/docs_rs_web/src/utils/highlight.rs @@ -135,4 +135,30 @@ mod tests { let highlighted = with_lang(Some("toml"), &text, None); assert!(highlighted.starts_with("<p>\n")); } + + #[test] + fn rust_union_is_highlighted_as_keyword() { + let highlighted = with_lang(Some("rust"), "union Foo { field: u8 }\n", None); + assert!(highlighted.contains("union Foo")); + } + + #[test] + fn rust_union_identifier_is_not_highlighted_as_keyword() { + let highlighted = with_lang( + Some("rust"), + "struct X { union: u32 }\nlet x = X { union: 0 };\nprintln!(\"{}\", x.union);\n", + None, + ); + assert!(highlighted.contains("union:")); + assert!(highlighted.contains("x.union")); + assert!(!highlighted.contains("union:")); + assert!(!highlighted.contains(".union")); + } + + #[test] + fn rust_union_macro_name_is_not_highlighted_as_keyword() { + let highlighted = with_lang(Some("rust"), "macro_rules! union { () => {} }\nunion!();\n", None); + assert!(highlighted.contains("macro_rules! union")); + assert!(!highlighted.contains("union!")); + } }