@@ -34,11 +34,9 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
3434 let mut name = None ;
3535 for node in tok. ancestors ( ) {
3636 if let Some ( item) = ast:: Item :: cast ( node. clone ( ) ) {
37- expanded = sema. expand_attr_macro ( & item) ;
38- if expanded. is_some ( ) {
39- // FIXME: add the macro name
40- // FIXME: make this recursive too
41- name = Some ( "?" . to_string ( ) ) ;
37+ if let Some ( def) = sema. resolve_attr_macro_call ( & item) {
38+ name = def. name ( db) . map ( |name| name. to_string ( ) ) ;
39+ expanded = expand_attr_macro_recur ( & sema, & item) ;
4240 break ;
4341 }
4442 }
@@ -54,20 +52,33 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
5452 // macro expansion may lose all white space information
5553 // But we hope someday we can use ra_fmt for that
5654 let expansion = insert_whitespaces ( expanded?) ;
57- Some ( ExpandedMacro { name : name? , expansion } )
55+ Some ( ExpandedMacro { name : name. unwrap_or_else ( || "???" . to_owned ( ) ) , expansion } )
5856}
5957
6058fn expand_macro_recur (
6159 sema : & Semantics < RootDatabase > ,
6260 macro_call : & ast:: MacroCall ,
6361) -> Option < SyntaxNode > {
6462 let expanded = sema. expand ( macro_call) ?. clone_for_update ( ) ;
63+ expand ( sema, expanded, ast:: MacroCall :: cast, expand_macro_recur)
64+ }
65+
66+ fn expand_attr_macro_recur ( sema : & Semantics < RootDatabase > , item : & ast:: Item ) -> Option < SyntaxNode > {
67+ let expanded = sema. expand_attr_macro ( item) ?. clone_for_update ( ) ;
68+ expand ( sema, expanded, ast:: Item :: cast, expand_attr_macro_recur)
69+ }
6570
66- let children = expanded. descendants ( ) . filter_map ( ast:: MacroCall :: cast) ;
71+ fn expand < T : AstNode > (
72+ sema : & Semantics < RootDatabase > ,
73+ expanded : SyntaxNode ,
74+ f : impl FnMut ( SyntaxNode ) -> Option < T > ,
75+ exp : impl Fn ( & Semantics < RootDatabase > , & T ) -> Option < SyntaxNode > ,
76+ ) -> Option < SyntaxNode > {
77+ let children = expanded. descendants ( ) . filter_map ( f) ;
6778 let mut replacements = Vec :: new ( ) ;
6879
6980 for child in children {
70- if let Some ( new_node) = expand_macro_recur ( sema, & child) {
81+ if let Some ( new_node) = exp ( sema, & child) {
7182 // check if the whole original syntax is replaced
7283 if expanded == * child. syntax ( ) {
7384 return Some ( new_node) ;
0 commit comments