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 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'crates/ide/src/syntax_highlighting.rs') 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(); -- cgit v1.2.3