From 8d87f9b298f41b8eb1e9fa0481c5092c1c136ef9 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sun, 6 Jun 2021 15:51:05 +0200 Subject: Handle attribute macros in `descend_into_macros` --- crates/hir/src/semantics.rs | 66 +++++++++++++++++++++++-------- crates/hir/src/semantics/source_to_def.rs | 15 +++++-- 2 files changed, 61 insertions(+), 20 deletions(-) (limited to 'crates/hir/src') diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index c7f2c02e4..0d55e4a3e 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -362,25 +362,57 @@ impl<'db> SemanticsImpl<'db> { let token = successors(Some(InFile::new(sa.file_id, token)), |token| { self.db.unwind_if_cancelled(); - let macro_call = token.value.ancestors().find_map(ast::MacroCall::cast)?; - let tt = macro_call.token_tree()?; - if !tt.syntax().text_range().contains_range(token.value.text_range()) { - return None; - } - let file_id = sa.expand(self.db, token.with_value(¯o_call))?; - let token = self - .expansion_info_cache - .borrow_mut() - .entry(file_id) - .or_insert_with(|| file_id.expansion_info(self.db.upcast())) - .as_ref()? - .map_token_down(token.as_ref())?; - - if let Some(parent) = token.value.parent() { - self.cache(find_root(&parent), token.file_id); + + for node in token.value.ancestors() { + match_ast! { + match node { + ast::MacroCall(macro_call) => { + let tt = macro_call.token_tree()?; + if !tt.syntax().text_range().contains_range(token.value.text_range()) { + return None; + } + let file_id = sa.expand(self.db, token.with_value(¯o_call))?; + let token = self + .expansion_info_cache + .borrow_mut() + .entry(file_id) + .or_insert_with(|| file_id.expansion_info(self.db.upcast())) + .as_ref()? + .map_token_down(token.as_ref())?; + + if let Some(parent) = token.value.parent() { + self.cache(find_root(&parent), token.file_id); + } + + return Some(token); + }, + ast::Item(item) => { + match self.with_ctx(|ctx| ctx.item_to_macro_call(token.with_value(item))) { + Some(call_id) => { + let file_id = call_id.as_file(); + let token = self + .expansion_info_cache + .borrow_mut() + .entry(file_id) + .or_insert_with(|| file_id.expansion_info(self.db.upcast())) + .as_ref()? + .map_token_down(token.as_ref())?; + + if let Some(parent) = token.value.parent() { + self.cache(find_root(&parent), token.file_id); + } + + return Some(token); + } + None => {} + } + }, + _ => {} + } + } } - Some(token) + None }) .last() .unwrap(); diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs index 9a5a2255f..22e196196 100644 --- a/crates/hir/src/semantics/source_to_def.rs +++ b/crates/hir/src/semantics/source_to_def.rs @@ -10,7 +10,7 @@ use hir_def::{ ImplId, LifetimeParamId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, VariantId, }; -use hir_expand::{name::AsName, AstId, MacroDefKind}; +use hir_expand::{name::AsName, AstId, MacroCallId, MacroDefKind}; use rustc_hash::FxHashMap; use smallvec::SmallVec; use stdx::impl_from; @@ -145,16 +145,25 @@ impl SourceToDefCtx<'_, '_> { Some((container, label_id)) } + pub(super) fn item_to_macro_call(&mut self, src: InFile) -> Option { + let map = self.dyn_map(src.as_ref())?; + map[keys::ATTR_MACRO].get(&src).copied() + } + fn to_def( &mut self, src: InFile, key: Key, ) -> Option { - let container = self.find_container(src.as_ref().map(|it| it.syntax()))?; + self.dyn_map(src.as_ref())?[key].get(&src).copied() + } + + fn dyn_map(&mut self, src: InFile<&Ast>) -> Option<&DynMap> { + let container = self.find_container(src.map(|it| it.syntax()))?; let db = self.db; let dyn_map = &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db)); - dyn_map[key].get(&src).copied() + Some(dyn_map) } pub(super) fn type_param_to_def(&mut self, src: InFile) -> Option { -- cgit v1.2.3 From 33e747d786e588ab61133fe2c0fb6341826e2cea Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 7 Jun 2021 16:05:36 +0200 Subject: Make "expand macro" command work with attribute macros --- crates/hir/src/semantics.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'crates/hir/src') diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index 0d55e4a3e..920e18208 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -117,6 +117,12 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { pub fn expand(&self, macro_call: &ast::MacroCall) -> Option { self.imp.expand(macro_call) } + + /// If `item` has an attribute macro attached to it, expands it. + pub fn expand_attr_macro(&self, item: &ast::Item) -> Option { + self.imp.expand_attr_macro(item) + } + pub fn speculative_expand( &self, actual_macro_call: &ast::MacroCall, @@ -332,6 +338,16 @@ impl<'db> SemanticsImpl<'db> { Some(node) } + fn expand_attr_macro(&self, item: &ast::Item) -> Option { + let sa = self.analyze(item.syntax()); + let src = InFile::new(sa.file_id, item.clone()); + let macro_call_id = self.with_ctx(|ctx| ctx.item_to_macro_call(src))?; + let file_id = macro_call_id.as_file(); + let node = self.db.parse_or_expand(file_id)?; + self.cache(node.clone(), file_id); + Some(node) + } + fn speculative_expand( &self, actual_macro_call: &ast::MacroCall, -- cgit v1.2.3 From 33be5762e579a9e03288ba27821951ca7db3a68e Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 7 Jun 2021 19:32:28 +0200 Subject: Attempt to track attr macros during highlighting --- crates/hir/src/semantics.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'crates/hir/src') diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index 920e18208..2d08a7704 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -123,6 +123,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.expand_attr_macro(item) } + pub fn is_attr_macro_call(&self, item: &ast::Item) -> bool { + self.imp.is_attr_macro_call(item) + } + pub fn speculative_expand( &self, actual_macro_call: &ast::MacroCall, @@ -348,6 +352,12 @@ impl<'db> SemanticsImpl<'db> { Some(node) } + fn is_attr_macro_call(&self, item: &ast::Item) -> bool { + let sa = self.analyze(item.syntax()); + let src = InFile::new(sa.file_id, item.clone()); + self.with_ctx(|ctx| ctx.item_to_macro_call(src).is_some()) + } + fn speculative_expand( &self, actual_macro_call: &ast::MacroCall, -- cgit v1.2.3