Skip to content

Commit 33be576

Browse files
Attempt to track attr macros during highlighting
1 parent 33e747d commit 33be576

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

crates/hir/src/semantics.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
123123
self.imp.expand_attr_macro(item)
124124
}
125125

126+
pub fn is_attr_macro_call(&self, item: &ast::Item) -> bool {
127+
self.imp.is_attr_macro_call(item)
128+
}
129+
126130
pub fn speculative_expand(
127131
&self,
128132
actual_macro_call: &ast::MacroCall,
@@ -348,6 +352,12 @@ impl<'db> SemanticsImpl<'db> {
348352
Some(node)
349353
}
350354

355+
fn is_attr_macro_call(&self, item: &ast::Item) -> bool {
356+
let sa = self.analyze(item.syntax());
357+
let src = InFile::new(sa.file_id, item.clone());
358+
self.with_ctx(|ctx| ctx.item_to_macro_call(src).is_some())
359+
}
360+
351361
fn speculative_expand(
352362
&self,
353363
actual_macro_call: &ast::MacroCall,

crates/ide/src/syntax_highlighting.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ fn traverse(
192192
let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
193193

194194
let mut current_macro_call: Option<ast::MacroCall> = None;
195+
let mut current_attr_macro_call = None;
195196
let mut current_macro: Option<ast::Macro> = None;
196197
let mut macro_highlighter = MacroHighlighter::default();
197198
let mut inside_attribute = false;
@@ -227,6 +228,19 @@ fn traverse(
227228
}
228229
_ => (),
229230
}
231+
match event.clone().map(|it| it.into_node().and_then(ast::Item::cast)) {
232+
WalkEvent::Enter(Some(item)) => {
233+
if sema.is_attr_macro_call(&item) {
234+
current_attr_macro_call = Some(item);
235+
}
236+
}
237+
WalkEvent::Leave(Some(item)) => {
238+
if current_attr_macro_call == Some(item) {
239+
current_attr_macro_call = None;
240+
}
241+
}
242+
_ => (),
243+
}
230244

231245
match event.clone().map(|it| it.into_node().and_then(ast::Macro::cast)) {
232246
WalkEvent::Enter(Some(mac)) => {
@@ -286,6 +300,22 @@ fn traverse(
286300
}
287301
None => token.into(),
288302
}
303+
} else if current_attr_macro_call.is_some() {
304+
let token = match element.clone().into_token() {
305+
Some(it) => it,
306+
_ => continue,
307+
};
308+
let token = sema.descend_into_macros(token.clone());
309+
match token.parent() {
310+
Some(parent) => {
311+
// We only care Name and Name_ref
312+
match (token.kind(), parent.kind()) {
313+
(IDENT, NAME) | (IDENT, NAME_REF) => parent.into(),
314+
_ => token.into(),
315+
}
316+
}
317+
None => token.into(),
318+
}
289319
} else {
290320
element.clone()
291321
};

0 commit comments

Comments
 (0)