aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide
diff options
context:
space:
mode:
authorLeander Tentrup <[email protected]>2020-06-14 14:40:06 +0100
committerLeander Tentrup <[email protected]>2020-06-15 14:03:13 +0100
commit06f89e5f3a00d91e84963745af989f1e9a906bb4 (patch)
tree6a3d96f58ff9b99066e5b410911b47ce0b28d529 /crates/ra_ide
parent017331a53c1eeaa1253d2829165627bfa27dc124 (diff)
Fix syntax highlighting of recursive macros
Add syntax highlighting for the BANG (`!`) token if the parent is `MACRO_CALL`.
Diffstat (limited to 'crates/ra_ide')
-rw-r--r--crates/ra_ide/src/snapshots/highlighting.html8
-rw-r--r--crates/ra_ide/src/syntax_highlighting.rs37
-rw-r--r--crates/ra_ide/src/syntax_highlighting/tests.rs8
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 33548d43c..5c2ff6ab5 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
62 } 62 }
63} 63}
64 64
65<span class="macro">macro_rules!</span> <span class="macro declaration">noop</span> {
66 ($expr:expr) =&gt; {
67 $expr
68 }
69}
70
65<span class="comment">// comment</span> 71<span class="comment">// comment</span>
66<span class="keyword">fn</span> <span class="function declaration">main</span>() { 72<span class="keyword">fn</span> <span class="function declaration">main</span>() {
67 <span class="macro">println!</span>(<span class="string_literal">"Hello, {}!"</span>, <span class="numeric_literal">92</span>); 73 <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
80 <span class="comment">// Do nothing</span> 86 <span class="comment">// Do nothing</span>
81 } 87 }
82 88
89 <span class="macro">noop!</span>(<span class="macro">noop</span><span class="macro">!</span>(<span class="numeric_literal">1</span>));
90
83 <span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">x</span> = <span class="numeric_literal">42</span>; 91 <span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">x</span> = <span class="numeric_literal">42</span>;
84 <span class="keyword">let</span> <span class="variable declaration mutable">y</span> = &<span class="keyword">mut</span> <span class="variable mutable">x</span>; 92 <span class="keyword">let</span> <span class="variable declaration mutable">y</span> = &<span class="keyword">mut</span> <span class="variable mutable">x</span>;
85 <span class="keyword">let</span> <span class="variable declaration">z</span> = &<span class="variable mutable">y</span>; 93 <span class="keyword">let</span> <span class="variable declaration">z</span> = &<span class="variable mutable">y</span>;
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs
index ab45c364a..bbcd52a1c 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(
160 // Check if macro takes a format string and remember it for highlighting later. 160 // Check if macro takes a format string and remember it for highlighting later.
161 // The macros that accept a format string expand to a compiler builtin macros 161 // The macros that accept a format string expand to a compiler builtin macros
162 // `format_args` and `format_args_nl`. 162 // `format_args` and `format_args_nl`.
163 if let Some(fmt_macro_call) = parent.parent().and_then(ast::MacroCall::cast) { 163 if let Some(name) = parent
164 if let Some(name) = 164 .parent()
165 fmt_macro_call.path().and_then(|p| p.segment()).and_then(|s| s.name_ref()) 165 .and_then(ast::MacroCall::cast)
166 { 166 .and_then(|mc| mc.path())
167 match name.text().as_str() { 167 .and_then(|p| p.segment())
168 "format_args" | "format_args_nl" => { 168 .and_then(|s| s.name_ref())
169 format_string = parent 169 {
170 .children_with_tokens() 170 match name.text().as_str() {
171 .filter(|t| t.kind() != WHITESPACE) 171 "format_args" | "format_args_nl" => {
172 .nth(1) 172 format_string = parent
173 .filter(|e| { 173 .children_with_tokens()
174 ast::String::can_cast(e.kind()) 174 .filter(|t| t.kind() != WHITESPACE)
175 || ast::RawString::can_cast(e.kind()) 175 .nth(1)
176 }) 176 .filter(|e| {
177 } 177 ast::String::can_cast(e.kind())
178 _ => {} 178 || ast::RawString::can_cast(e.kind())
179 })
179 } 180 }
181 _ => {}
180 } 182 }
181 } 183 }
182 184
@@ -493,6 +495,9 @@ fn highlight_element(
493 h |= HighlightModifier::Unsafe; 495 h |= HighlightModifier::Unsafe;
494 h 496 h
495 } 497 }
498 T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => {
499 Highlight::new(HighlightTag::Macro)
500 }
496 501
497 k if k.is_keyword() => { 502 k if k.is_keyword() => {
498 let h = Highlight::new(HighlightTag::Keyword); 503 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 949bf59a0..070b24f45 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! {
43 } 43 }
44} 44}
45 45
46macro_rules! noop {
47 ($expr:expr) => {
48 $expr
49 }
50}
51
46// comment 52// comment
47fn main() { 53fn main() {
48 println!("Hello, {}!", 92); 54 println!("Hello, {}!", 92);
@@ -61,6 +67,8 @@ fn main() {
61 // Do nothing 67 // Do nothing
62 } 68 }
63 69
70 noop!(noop!(1));
71
64 let mut x = 42; 72 let mut x = 42;
65 let y = &mut x; 73 let y = &mut x;
66 let z = &y; 74 let z = &y;