Skip to content

Commit 5b013e5

Browse files
bors[bot]ltentrup
andauthored
Merge #4877
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>
2 parents d0ac2f7 + 06f89e5 commit 5b013e5

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

crates/ra_ide/src/snapshots/highlighting.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@
6262
}
6363
}
6464

65+
<span class="macro">macro_rules!</span> <span class="macro declaration">noop</span> {
66+
($expr:expr) =&gt; {
67+
$expr
68+
}
69+
}
70+
6571
<span class="comment">// comment</span>
6672
<span class="keyword">fn</span> <span class="function declaration">main</span>() {
6773
<span class="macro">println!</span>(<span class="string_literal">"Hello, {}!"</span>, <span class="numeric_literal">92</span>);
@@ -80,6 +86,8 @@
8086
<span class="comment">// Do nothing</span>
8187
}
8288

89+
<span class="macro">noop!</span>(<span class="macro">noop</span><span class="macro">!</span>(<span class="numeric_literal">1</span>));
90+
8391
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">x</span> = <span class="numeric_literal">42</span>;
8492
<span class="keyword">let</span> <span class="variable declaration mutable">y</span> = &<span class="keyword">mut</span> <span class="variable mutable">x</span>;
8593
<span class="keyword">let</span> <span class="variable declaration">z</span> = &<span class="variable mutable">y</span>;

crates/ra_ide/src/syntax_highlighting.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,23 +160,25 @@ pub(crate) fn highlight(
160160
// Check if macro takes a format string and remember it for highlighting later.
161161
// The macros that accept a format string expand to a compiler builtin macros
162162
// `format_args` and `format_args_nl`.
163-
if let Some(fmt_macro_call) = parent.parent().and_then(ast::MacroCall::cast) {
164-
if let Some(name) =
165-
fmt_macro_call.path().and_then(|p| p.segment()).and_then(|s| s.name_ref())
166-
{
167-
match name.text().as_str() {
168-
"format_args" | "format_args_nl" => {
169-
format_string = parent
170-
.children_with_tokens()
171-
.filter(|t| t.kind() != WHITESPACE)
172-
.nth(1)
173-
.filter(|e| {
174-
ast::String::can_cast(e.kind())
175-
|| ast::RawString::can_cast(e.kind())
176-
})
177-
}
178-
_ => {}
163+
if let Some(name) = parent
164+
.parent()
165+
.and_then(ast::MacroCall::cast)
166+
.and_then(|mc| mc.path())
167+
.and_then(|p| p.segment())
168+
.and_then(|s| s.name_ref())
169+
{
170+
match name.text().as_str() {
171+
"format_args" | "format_args_nl" => {
172+
format_string = parent
173+
.children_with_tokens()
174+
.filter(|t| t.kind() != WHITESPACE)
175+
.nth(1)
176+
.filter(|e| {
177+
ast::String::can_cast(e.kind())
178+
|| ast::RawString::can_cast(e.kind())
179+
})
179180
}
181+
_ => {}
180182
}
181183
}
182184

@@ -493,6 +495,9 @@ fn highlight_element(
493495
h |= HighlightModifier::Unsafe;
494496
h
495497
}
498+
T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => {
499+
Highlight::new(HighlightTag::Macro)
500+
}
496501

497502
k if k.is_keyword() => {
498503
let h = Highlight::new(HighlightTag::Keyword);

crates/ra_ide/src/syntax_highlighting/tests.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ def_fn! {
4343
}
4444
}
4545
46+
macro_rules! noop {
47+
($expr:expr) => {
48+
$expr
49+
}
50+
}
51+
4652
// comment
4753
fn main() {
4854
println!("Hello, {}!", 92);
@@ -61,6 +67,8 @@ fn main() {
6167
// Do nothing
6268
}
6369
70+
noop!(noop!(1));
71+
6472
let mut x = 42;
6573
let y = &mut x;
6674
let z = &y;

0 commit comments

Comments
 (0)