@@ -69,32 +69,34 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
69
69
70
70
// FIXME: Intermix attribute and bang! expansions
71
71
// currently we only recursively expand one of the two types
72
- let mut expanded = None ;
73
- let mut name = None ;
74
- let mut kind = SyntaxKind :: ERROR ;
75
- for node in tok . ancestors ( ) {
72
+ let mut anc = tok . ancestors ( ) ;
73
+ let ( name, expanded , kind ) = loop {
74
+ let node = anc . next ( ) ? ;
75
+
76
76
if let Some ( item) = ast:: Item :: cast ( node. clone ( ) ) {
77
77
if let Some ( def) = sema. resolve_attr_macro_call ( & item) {
78
- name = Some ( def. name ( db) . to_string ( ) ) ;
79
- expanded = expand_attr_macro_recur ( & sema, & item) ;
80
- kind = SyntaxKind :: MACRO_ITEMS ;
81
- break ;
78
+ break (
79
+ def. name ( db) . to_string ( ) ,
80
+ expand_attr_macro_recur ( & sema, & item) ?,
81
+ SyntaxKind :: MACRO_ITEMS ,
82
+ ) ;
82
83
}
83
84
}
84
85
if let Some ( mac) = ast:: MacroCall :: cast ( node) {
85
- name = Some ( mac. path ( ) ?. segment ( ) ?. name_ref ( ) ?. to_string ( ) ) ;
86
- expanded = expand_macro_recur ( & sema, & mac) ;
87
- kind = mac. syntax ( ) . parent ( ) . map ( |it| it. kind ( ) ) . unwrap_or ( SyntaxKind :: MACRO_ITEMS ) ;
88
- break ;
86
+ break (
87
+ mac. path ( ) ?. segment ( ) ?. name_ref ( ) ?. to_string ( ) ,
88
+ expand_macro_recur ( & sema, & mac) ?,
89
+ mac. syntax ( ) . parent ( ) . map ( |it| it. kind ( ) ) . unwrap_or ( SyntaxKind :: MACRO_ITEMS ) ,
90
+ ) ;
89
91
}
90
- }
92
+ } ;
91
93
92
94
// FIXME:
93
95
// macro expansion may lose all white space information
94
96
// But we hope someday we can use ra_fmt for that
95
- let expansion = format ( db, kind, position. file_id , expanded? ) ;
97
+ let expansion = format ( db, kind, position. file_id , expanded) ;
96
98
97
- Some ( ExpandedMacro { name : name . unwrap_or_else ( || "???" . to_owned ( ) ) , expansion } )
99
+ Some ( ExpandedMacro { name, expansion } )
98
100
}
99
101
100
102
fn expand_macro_recur (
@@ -188,8 +190,15 @@ fn _format(
188
190
let captured_stdout = String :: from_utf8 ( output. stdout ) . ok ( ) ?;
189
191
190
192
if output. status . success ( ) && !captured_stdout. trim ( ) . is_empty ( ) {
191
- let foo = captured_stdout. replace ( DOLLAR_CRATE_REPLACE , "$crate" ) ;
192
- let trim_indent = stdx:: trim_indent ( foo. trim ( ) . strip_prefix ( prefix) ?. strip_suffix ( suffix) ?) ;
193
+ let output = captured_stdout. replace ( DOLLAR_CRATE_REPLACE , "$crate" ) ;
194
+ let output = output. trim ( ) . strip_prefix ( prefix) ?;
195
+ let output = match kind {
196
+ SyntaxKind :: MACRO_PAT => {
197
+ output. strip_suffix ( suffix) . or_else ( || output. strip_suffix ( ": u32,\n );" ) ) ?
198
+ }
199
+ _ => output. strip_suffix ( suffix) ?,
200
+ } ;
201
+ let trim_indent = stdx:: trim_indent ( output) ;
193
202
tracing:: debug!( "expand_macro: formatting succeeded" ) ;
194
203
Some ( trim_indent)
195
204
} else {
0 commit comments