Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1148,3 +1148,12 @@ let p_imm: *const u32 = &i as *const u32;
type ExampleRawPointer = HashMap<*const i32, Option<i32>, BuildHasherDefault<FnvHasher>>;
// ^^^^^^ storage.modifier - invalid
// ^^^ storage.type


union MyUnion {
// <- keyword.other
f1: u32,
}

let union = 1;
// ^^^^^ - keyword.other
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's wrong.

26 changes: 26 additions & 0 deletions crates/bin/docs_rs_web/src/utils/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,30 @@ mod tests {
let highlighted = with_lang(Some("toml"), &text, None);
assert!(highlighted.starts_with("&lt;p&gt;\n"));
}

#[test]
fn rust_union_is_highlighted_as_keyword() {
let highlighted = with_lang(Some("rust"), "union Foo { field: u8 }\n", None);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add more test cases like the one I provided.

Also:

struct X {
    union: u32,
}

let x = X { union: 0 };
macro_rules! union {
    () => {}
}

union!();

assert!(highlighted.contains("<span class=\"syntax-keyword\">union</span> 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("<span class=\"syntax-keyword\">union</span>:"));
assert!(!highlighted.contains(".<span class=\"syntax-keyword\">union</span>"));
}

#[test]
fn rust_union_macro_name_is_not_highlighted_as_keyword() {
let highlighted = with_lang(Some("rust"), "macro_rules! union { () => {} }\nunion!();\n", None);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

assert!(highlighted.contains("macro_rules! union"));
assert!(!highlighted.contains("<span class=\"syntax-keyword\">union</span>!"));
}
}
Loading