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="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>);
@ -80,6 +86,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<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="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>;

View file

@ -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);

View file

@ -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;