4877: Fix syntax highlighting of recursive macros r=matklad a=ltentrup

Add syntax highlighting for the BANG (`!`) token if the parent is `MACRO_CALL`.

Before:
<img width="514" alt="before" src="https://user-images.githubusercontent.com/201808/84595030-11f65c00-ae56-11ea-9bb2-b1abe2236990.png">

After:
<img width="516" alt="recursive-macro" src="https://user-images.githubusercontent.com/201808/84594981-d196de00-ae55-11ea-8636-f877d5d795ff.png">


Fixes #4694.

Co-authored-by: Leander Tentrup <leander.tentrup@gmail.com>
This commit is contained in:
bors[bot] 2020-06-15 13:44:46 +00:00 committed by GitHub
commit 5b013e5665
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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,23 +160,25 @@ 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())
match name.text().as_str() { .and_then(|p| p.segment())
"format_args" | "format_args_nl" => { .and_then(|s| s.name_ref())
format_string = parent {
.children_with_tokens() match name.text().as_str() {
.filter(|t| t.kind() != WHITESPACE) "format_args" | "format_args_nl" => {
.nth(1) format_string = parent
.filter(|e| { .children_with_tokens()
ast::String::can_cast(e.kind()) .filter(|t| t.kind() != WHITESPACE)
|| ast::RawString::can_cast(e.kind()) .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 |= 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 @@ fn bar() -> u32 {
} }
} }
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;