From 1f87a4198956fd3a8b57d01c2bce19481fe2c610 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 21 Nov 2020 12:51:05 +0100 Subject: Add attribute highlight modifier to all tokens inside attributes --- crates/ide/src/syntax_highlighting.rs | 19 ++++++++++++++++--- .../test_data/highlight_doctest.html | 2 +- .../test_data/highlight_unsafe.html | 2 +- .../syntax_highlighting/test_data/highlighting.html | 10 +++++----- 4 files changed, 23 insertions(+), 10 deletions(-) (limited to 'crates/ide/src') diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 1ed77b40b..5150a970c 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -76,6 +76,7 @@ pub(crate) fn highlight( let mut current_macro_call: Option = None; let mut format_string_highlighter = FormatStringHighlighter::default(); let mut macro_rules_highlighter = MacroRulesHighlighter::default(); + let mut inside_attribute = false; // Walk all nodes, keeping track of whether we are inside a macro or not. // If in macro, expand it first and highlight the expanded code. @@ -132,9 +133,12 @@ pub(crate) fn highlight( _ => (), } - // Check for Rust code in documentation match &event { + // Check for Rust code in documentation WalkEvent::Leave(NodeOrToken::Node(node)) => { + if ast::Attr::can_cast(node.kind()) { + inside_attribute = false + } if let Some((doctest, range_mapping, new_comments)) = injection::extract_doc_comments(node) { @@ -146,6 +150,9 @@ pub(crate) fn highlight( ); } } + WalkEvent::Enter(NodeOrToken::Node(node)) if ast::Attr::can_cast(node.kind()) => { + inside_attribute = true + } _ => (), } @@ -188,12 +195,16 @@ pub(crate) fn highlight( } } - if let Some((highlight, binding_hash)) = highlight_element( + if let Some((mut highlight, binding_hash)) = highlight_element( &sema, &mut bindings_shadow_count, syntactic_name_ref_highlighting, element_to_highlight.clone(), ) { + if inside_attribute { + highlight = highlight | HighlightModifier::Attribute; + } + if macro_rules_highlighter.highlight(element_to_highlight.clone()).is_none() { stack.add(HighlightedRange { range, highlight, binding_hash }); } @@ -474,7 +485,9 @@ fn highlight_element( // Highlight references like the definitions they resolve to NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => { - Highlight::from(HighlightTag::Function) | HighlightModifier::Attribute + // even though we track whether we are in an attribute or not we still need this special case + // as otherwise we would emit unresolved references for name refs inside attributes + Highlight::from(HighlightTag::Function) } NAME_REF => { let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap(); diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html index 6be88f856..d79fa6dca 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html @@ -50,7 +50,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd /// # Examples /// /// ``` - /// # #![allow(unused_mut)] + /// # #![allow(unused_mut)] /// let mut foo: Foo = Foo::new(); /// ``` pub const fn new() -> Foo { diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html b/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html index 4b6d6adc9..1d05b7713 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html @@ -54,7 +54,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd static mut global_mut: TypeForStaticMut = TypeForStaticMut { a: 0 }; -#[repr(packed)] +#[repr(packed)] struct Packed { a: u16, } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html index 6a10a9dcd..15fbf2ce3 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html @@ -40,18 +40,18 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd // Needed for function consuming vs normal pub mod marker { - #[lang = "copy"] + #[lang = "copy"] pub trait Copy {} } pub mod ops { - #[lang = "fn_once"] + #[lang = "fn_once"] pub trait FnOnce<Args> {} - #[lang = "fn_mut"] + #[lang = "fn_mut"] pub trait FnMut<Args>: FnOnce<Args> {} - #[lang = "fn"] + #[lang = "fn"] pub trait Fn<Args>: FnMut<Args> {} } @@ -85,7 +85,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd } } -#[derive(Copy)] +#[derive(Copy)] struct FooCopy { x: u32, } -- cgit v1.2.3