@@ -2327,22 +2327,49 @@ impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Ident>) {
2327
2327
}
2328
2328
}
2329
2329
2330
- impl Clean < Item > for doctree :: Macro {
2330
+ impl Clean < Item > for ( & hir :: MacroDef < ' _ > , Option < Ident > ) {
2331
2331
fn clean ( & self , cx : & DocContext < ' _ > ) -> Item {
2332
- Item :: from_def_id_and_parts (
2333
- self . def_id ,
2334
- Some ( self . name . clean ( cx) ) ,
2335
- MacroItem ( Macro {
2336
- source : format ! (
2337
- "macro_rules! {} {{\n {}}}" ,
2338
- self . name,
2339
- self . matchers
2332
+ let ( item, renamed) = self ;
2333
+ let name = renamed. unwrap_or ( item. ident ) . name ;
2334
+ let tts = item. ast . body . inner_tokens ( ) . trees ( ) . collect :: < Vec < _ > > ( ) ;
2335
+ // Extract the spans of all matchers. They represent the "interface" of the macro.
2336
+ let matchers = tts. chunks ( 4 ) . map ( |arm| arm[ 0 ] . span ( ) ) . collect :: < Vec < _ > > ( ) ;
2337
+ let source = if item. ast . macro_rules {
2338
+ format ! (
2339
+ "macro_rules! {} {{\n {}}}" ,
2340
+ name,
2341
+ matchers
2342
+ . iter( )
2343
+ . map( |span| { format!( " {} => {{ ... }};\n " , span. to_src( cx) ) } )
2344
+ . collect:: <String >( ) ,
2345
+ )
2346
+ } else {
2347
+ let vis = item. vis . clean ( cx) ;
2348
+
2349
+ if matchers. len ( ) <= 1 {
2350
+ format ! (
2351
+ "{}macro {}{} {{\n ...\n }}" ,
2352
+ vis. print_with_space( ) ,
2353
+ name,
2354
+ matchers. iter( ) . map( |span| span. to_src( cx) ) . collect:: <String >( ) ,
2355
+ )
2356
+ } else {
2357
+ format ! (
2358
+ "{}macro {} {{\n {}}}" ,
2359
+ vis. print_with_space( ) ,
2360
+ name,
2361
+ matchers
2340
2362
. iter( )
2341
- . map( |span| { format!( " {} => {{ ... }};\n " , span. to_src( cx) ) } )
2342
- . collect:: <String >( )
2343
- ) ,
2344
- imported_from : self . imported_from . clean ( cx) ,
2345
- } ) ,
2363
+ . map( |span| { format!( " {} => {{ ... }},\n " , span. to_src( cx) ) } )
2364
+ . collect:: <String >( ) ,
2365
+ )
2366
+ }
2367
+ } ;
2368
+
2369
+ Item :: from_hir_id_and_parts (
2370
+ item. hir_id ,
2371
+ Some ( name) ,
2372
+ MacroItem ( Macro { source, imported_from : None } ) ,
2346
2373
cx,
2347
2374
)
2348
2375
}
0 commit comments