@@ -3,8 +3,7 @@ use std::iter;
33use hir:: Semantics ;
44use ide_db:: RootDatabase ;
55use syntax:: {
6- algo:: find_node_at_offset, ast, ted, AstNode , NodeOrToken , SyntaxKind , SyntaxKind :: * ,
7- SyntaxNode , WalkEvent , T ,
6+ ast, match_ast, ted, AstNode , NodeOrToken , SyntaxKind , SyntaxKind :: * , SyntaxNode , WalkEvent , T ,
87} ;
98
109use crate :: FilePosition ;
@@ -28,16 +27,37 @@ pub struct ExpandedMacro {
2827pub ( crate ) fn expand_macro ( db : & RootDatabase , position : FilePosition ) -> Option < ExpandedMacro > {
2928 let sema = Semantics :: new ( db) ;
3029 let file = sema. parse ( position. file_id ) ;
31- let mac = find_node_at_offset :: < ast:: MacroCall > ( file. syntax ( ) , position. offset ) ?;
32- let name = mac. path ( ) ?. segment ( ) ?. name_ref ( ) ?;
3330
34- let expanded = expand_macro_recur ( & sema, & mac) ?;
31+ let tok = file. syntax ( ) . token_at_offset ( position. offset ) . left_biased ( ) ?;
32+ let mut expanded = None ;
33+ let mut name = None ;
34+ for node in tok. ancestors ( ) {
35+ match_ast ! {
36+ match node {
37+ ast:: MacroCall ( mac) => {
38+ name = Some ( mac. path( ) ?. segment( ) ?. name_ref( ) ?. to_string( ) ) ;
39+ expanded = expand_macro_recur( & sema, & mac) ;
40+ break ;
41+ } ,
42+ ast:: Item ( item) => {
43+ // FIXME: add the macro name
44+ // FIXME: make this recursive too
45+ name = Some ( "?" . to_string( ) ) ;
46+ expanded = sema. expand_attr_macro( & item) ;
47+ if expanded. is_some( ) {
48+ break ;
49+ }
50+ } ,
51+ _ => { }
52+ }
53+ }
54+ }
3555
3656 // FIXME:
3757 // macro expansion may lose all white space information
3858 // But we hope someday we can use ra_fmt for that
39- let expansion = insert_whitespaces ( expanded) ;
40- Some ( ExpandedMacro { name : name. to_string ( ) , expansion } )
59+ let expansion = insert_whitespaces ( expanded? ) ;
60+ Some ( ExpandedMacro { name : name? , expansion } )
4161}
4262
4363fn expand_macro_recur (
0 commit comments