Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir/src/semantics.rs')
| -rw-r--r-- | crates/hir/src/semantics.rs | 49 |
1 files changed, 46 insertions, 3 deletions
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index 6d6a86fc8a..45544559ea 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -151,6 +151,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.expand_attr_macro(item) } + pub fn expand_derive_as_pseudo_attr_macro(&self, attr: &ast::Attr) -> Option<SyntaxNode> { + self.imp.expand_derive_as_pseudo_attr_macro(attr) + } + pub fn resolve_derive_macro(&self, derive: &ast::Attr) -> Option<Vec<Option<Macro>>> { self.imp.resolve_derive_macro(derive) } @@ -185,6 +189,19 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.speculative_expand_attr(actual_macro_call, speculative_args, token_to_map) } + pub fn speculative_expand_derive_as_pseudo_attr_macro( + &self, + actual_macro_call: &ast::Attr, + speculative_args: &ast::Attr, + token_to_map: SyntaxToken, + ) -> Option<(SyntaxNode, SyntaxToken)> { + self.imp.speculative_expand_derive_as_pseudo_attr_macro( + actual_macro_call, + speculative_args, + token_to_map, + ) + } + /// Descend the token into macrocalls to its first mapped counterpart. pub fn descend_into_macros_single(&self, token: SyntaxToken) -> SyntaxToken { self.imp.descend_into_macros_single(token) @@ -438,9 +455,16 @@ impl<'db> SemanticsImpl<'db> { fn expand_attr_macro(&self, item: &ast::Item) -> Option<SyntaxNode> { let src = self.wrap_node_infile(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.parse_or_expand(file_id)?; - Some(node) + self.parse_or_expand(macro_call_id.as_file()) + } + + fn expand_derive_as_pseudo_attr_macro(&self, attr: &ast::Attr) -> Option<SyntaxNode> { + let src = self.wrap_node_infile(attr.clone()); + let adt = attr.syntax().parent().and_then(ast::Adt::cast)?; + let call_id = self.with_ctx(|ctx| { + ctx.attr_to_derive_macro_call(src.with_value(&adt), src).map(|(_, it, _)| it) + })?; + self.parse_or_expand(call_id.as_file()) } fn resolve_derive_macro(&self, attr: &ast::Attr) -> Option<Vec<Option<Macro>>> { @@ -533,6 +557,25 @@ impl<'db> SemanticsImpl<'db> { ) } + fn speculative_expand_derive_as_pseudo_attr_macro( + &self, + actual_macro_call: &ast::Attr, + speculative_args: &ast::Attr, + token_to_map: SyntaxToken, + ) -> Option<(SyntaxNode, SyntaxToken)> { + let attr = self.wrap_node_infile(actual_macro_call.clone()); + let adt = actual_macro_call.syntax().parent().and_then(ast::Adt::cast)?; + let macro_call_id = self.with_ctx(|ctx| { + ctx.attr_to_derive_macro_call(attr.with_value(&adt), attr).map(|(_, it, _)| it) + })?; + hir_expand::db::expand_speculative( + self.db.upcast(), + macro_call_id, + speculative_args.syntax(), + token_to_map, + ) + } + // This might not be the correct way to do this, but it works for now fn descend_node_into_attributes<N: AstNode>(&self, node: N) -> SmallVec<[N; 1]> { let mut res = smallvec![]; |