From 06f89e5f3a00d91e84963745af989f1e9a906bb4 Mon Sep 17 00:00:00 2001 From: Leander Tentrup Date: Sun, 14 Jun 2020 15:40:06 +0200 Subject: [PATCH] Fix syntax highlighting of recursive macros Add syntax highlighting for the BANG (`!`) token if the parent is `MACRO_CALL`. --- crates/ra_ide/src/snapshots/highlighting.html | 8 ++++ crates/ra_ide/src/syntax_highlighting.rs | 37 +++++++++++-------- .../ra_ide/src/syntax_highlighting/tests.rs | 8 ++++ 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/crates/ra_ide/src/snapshots/highlighting.html b/crates/ra_ide/src/snapshots/highlighting.html index 33548d43cc..5c2ff6ab5a 100644 --- a/crates/ra_ide/src/snapshots/highlighting.html +++ b/crates/ra_ide/src/snapshots/highlighting.html @@ -62,6 +62,12 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd } } +macro_rules! noop { + ($expr:expr) => { + $expr + } +} + // comment fn main() { println!("Hello, {}!", 92); @@ -80,6 +86,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd // Do nothing } + noop!(noop!(1)); + let mut x = 42; let y = &mut x; let z = &y; diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index ab45c364a8..bbcd52a1c1 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -160,23 +160,25 @@ pub(crate) fn highlight( // Check if macro takes a format string and remember it for highlighting later. // The macros that accept a format string expand to a compiler builtin macros // `format_args` and `format_args_nl`. - if let Some(fmt_macro_call) = parent.parent().and_then(ast::MacroCall::cast) { - if let Some(name) = - fmt_macro_call.path().and_then(|p| p.segment()).and_then(|s| s.name_ref()) - { - match name.text().as_str() { - "format_args" | "format_args_nl" => { - format_string = parent - .children_with_tokens() - .filter(|t| t.kind() != WHITESPACE) - .nth(1) - .filter(|e| { - ast::String::can_cast(e.kind()) - || ast::RawString::can_cast(e.kind()) - }) - } - _ => {} + if let Some(name) = parent + .parent() + .and_then(ast::MacroCall::cast) + .and_then(|mc| mc.path()) + .and_then(|p| p.segment()) + .and_then(|s| s.name_ref()) + { + match name.text().as_str() { + "format_args" | "format_args_nl" => { + format_string = parent + .children_with_tokens() + .filter(|t| t.kind() != WHITESPACE) + .nth(1) + .filter(|e| { + ast::String::can_cast(e.kind()) + || ast::RawString::can_cast(e.kind()) + }) } + _ => {} } } @@ -493,6 +495,9 @@ fn highlight_element( h |= HighlightModifier::Unsafe; h } + T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => { + Highlight::new(HighlightTag::Macro) + } k if k.is_keyword() => { let h = Highlight::new(HighlightTag::Keyword); diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs index 949bf59a0d..070b24f455 100644 --- a/crates/ra_ide/src/syntax_highlighting/tests.rs +++ b/crates/ra_ide/src/syntax_highlighting/tests.rs @@ -43,6 +43,12 @@ def_fn! { } } +macro_rules! noop { + ($expr:expr) => { + $expr + } +} + // comment fn main() { println!("Hello, {}!", 92); @@ -61,6 +67,8 @@ fn main() { // Do nothing } + noop!(noop!(1)); + let mut x = 42; let y = &mut x; let z = &y;