Fix syntax highlighting of recursive macros

Add syntax highlighting for the BANG (`!`) token if the parent is `MACRO_CALL`.
This commit is contained in:
Leander Tentrup 2020-06-14 15:40:06 +02:00
parent 017331a53c
commit 06f89e5f3a
3 changed files with 37 additions and 16 deletions

View file

@ -62,6 +62,12 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
} }
} }
<span class="macro">macro_rules!</span> <span class="macro declaration">noop</span> {
($expr:expr) =&gt; {
$expr
}
}
<span class="comment">// comment</span> <span class="comment">// comment</span>
<span class="keyword">fn</span> <span class="function declaration">main</span>() { <span class="keyword">fn</span> <span class="function declaration">main</span>() {
<span class="macro">println!</span>(<span class="string_literal">"Hello, {}!"</span>, <span class="numeric_literal">92</span>); <span class="macro">println!</span>(<span class="string_literal">"Hello, {}!"</span>, <span class="numeric_literal">92</span>);
@ -80,6 +86,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="comment">// Do nothing</span> <span class="comment">// Do nothing</span>
} }
<span class="macro">noop!</span>(<span class="macro">noop</span><span class="macro">!</span>(<span class="numeric_literal">1</span>));
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">x</span> = <span class="numeric_literal">42</span>; <span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">x</span> = <span class="numeric_literal">42</span>;
<span class="keyword">let</span> <span class="variable declaration mutable">y</span> = &<span class="keyword">mut</span> <span class="variable mutable">x</span>; <span class="keyword">let</span> <span class="variable declaration mutable">y</span> = &<span class="keyword">mut</span> <span class="variable mutable">x</span>;
<span class="keyword">let</span> <span class="variable declaration">z</span> = &<span class="variable mutable">y</span>; <span class="keyword">let</span> <span class="variable declaration">z</span> = &<span class="variable mutable">y</span>;

View file

@ -160,9 +160,12 @@ pub(crate) fn highlight(
// Check if macro takes a format string and remember it for highlighting later. // 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 // The macros that accept a format string expand to a compiler builtin macros
// `format_args` and `format_args_nl`. // `format_args` and `format_args_nl`.
if let Some(fmt_macro_call) = parent.parent().and_then(ast::MacroCall::cast) { if let Some(name) = parent
if let Some(name) = .parent()
fmt_macro_call.path().and_then(|p| p.segment()).and_then(|s| s.name_ref()) .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() { match name.text().as_str() {
"format_args" | "format_args_nl" => { "format_args" | "format_args_nl" => {
@ -178,7 +181,6 @@ pub(crate) fn highlight(
_ => {} _ => {}
} }
} }
}
// We only care Name and Name_ref // We only care Name and Name_ref
match (token.kind(), parent.kind()) { match (token.kind(), parent.kind()) {
@ -493,6 +495,9 @@ fn highlight_element(
h |= HighlightModifier::Unsafe; h |= HighlightModifier::Unsafe;
h h
} }
T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => {
Highlight::new(HighlightTag::Macro)
}
k if k.is_keyword() => { k if k.is_keyword() => {
let h = Highlight::new(HighlightTag::Keyword); let h = Highlight::new(HighlightTag::Keyword);

View file

@ -43,6 +43,12 @@ def_fn! {
} }
} }
macro_rules! noop {
($expr:expr) => {
$expr
}
}
// comment // comment
fn main() { fn main() {
println!("Hello, {}!", 92); println!("Hello, {}!", 92);
@ -61,6 +67,8 @@ fn main() {
// Do nothing // Do nothing
} }
noop!(noop!(1));
let mut x = 42; let mut x = 42;
let y = &mut x; let y = &mut x;
let z = &y; let z = &y;