@@ -49,10 +49,38 @@ pub enum AnnNode<'a> {
49
49
}
50
50
51
51
pub trait PpAnn {
52
+ fn nested ( & self , state : & mut State < ' _ > , node : AnnNode < ' _ > ) {
53
+ print_default_nested_ann ( state, node) ;
54
+ }
52
55
fn pre ( & self , _state : & mut State < ' _ > , _node : AnnNode < ' _ > ) { }
53
56
fn post ( & self , _state : & mut State < ' _ > , _node : AnnNode < ' _ > ) { }
54
57
}
55
58
59
+ pub fn print_default_nested_ann ( state : & mut State < ' _ > , node : AnnNode < ' _ > ) {
60
+ match node {
61
+ AnnNode :: Block ( blk) => {
62
+ for ( i, st) in blk. stmts . iter ( ) . enumerate ( ) {
63
+ match & st. kind {
64
+ ast:: StmtKind :: Expr ( expr) if i == blk. stmts . len ( ) - 1 => {
65
+ state. maybe_print_comment ( st. span . lo ( ) ) ;
66
+ state. space_if_not_bol ( ) ;
67
+ state. print_expr_outer_attr_style ( expr, false , FixupContext :: new_stmt ( ) ) ;
68
+ state. maybe_print_trailing_comment ( expr. span , Some ( blk. span . hi ( ) ) ) ;
69
+ }
70
+ _ => state. print_stmt ( st) ,
71
+ }
72
+ }
73
+ }
74
+ AnnNode :: Crate ( _)
75
+ | AnnNode :: Expr ( _)
76
+ | AnnNode :: Ident ( _)
77
+ | AnnNode :: Item ( _)
78
+ | AnnNode :: Name ( _)
79
+ | AnnNode :: Pat ( _)
80
+ | AnnNode :: SubItem ( _) => unimplemented ! ( ) ,
81
+ }
82
+ }
83
+
56
84
struct NoAnn ;
57
85
58
86
impl PpAnn for NoAnn { }
@@ -240,9 +268,29 @@ pub fn print_crate<'a>(
240
268
edition : Edition ,
241
269
g : & AttrIdGenerator ,
242
270
) -> String {
243
- let mut s =
271
+ let s =
244
272
State { s : pp:: Printer :: new ( ) , comments : Some ( Comments :: new ( sm, filename, input) ) , ann } ;
273
+ print_crate_with_state ( s, krate, is_expanded, edition, g)
274
+ }
245
275
276
+ pub fn print_crate_with_erased_comments < ' a > (
277
+ krate : & ast:: Crate ,
278
+ ann : & ' a dyn PpAnn ,
279
+ is_expanded : bool ,
280
+ edition : Edition ,
281
+ g : & AttrIdGenerator ,
282
+ ) -> String {
283
+ let s = State { s : pp:: Printer :: new ( ) , comments : None , ann } ;
284
+ print_crate_with_state ( s, krate, is_expanded, edition, g)
285
+ }
286
+
287
+ fn print_crate_with_state < ' a > (
288
+ mut s : State < ' a > ,
289
+ krate : & ast:: Crate ,
290
+ is_expanded : bool ,
291
+ edition : Edition ,
292
+ g : & AttrIdGenerator ,
293
+ ) -> String {
246
294
if is_expanded && !krate. attrs . iter ( ) . any ( |attr| attr. has_name ( sym:: no_core) ) {
247
295
// We need to print `#![no_std]` (and its feature gate) so that
248
296
// compiling pretty-printed source won't inject libstd again.
@@ -1383,17 +1431,7 @@ impl<'a> State<'a> {
1383
1431
1384
1432
let has_attrs = self . print_inner_attributes ( attrs) ;
1385
1433
1386
- for ( i, st) in blk. stmts . iter ( ) . enumerate ( ) {
1387
- match & st. kind {
1388
- ast:: StmtKind :: Expr ( expr) if i == blk. stmts . len ( ) - 1 => {
1389
- self . maybe_print_comment ( st. span . lo ( ) ) ;
1390
- self . space_if_not_bol ( ) ;
1391
- self . print_expr_outer_attr_style ( expr, false , FixupContext :: new_stmt ( ) ) ;
1392
- self . maybe_print_trailing_comment ( expr. span , Some ( blk. span . hi ( ) ) ) ;
1393
- }
1394
- _ => self . print_stmt ( st) ,
1395
- }
1396
- }
1434
+ self . ann . nested ( self , AnnNode :: Block ( blk) ) ;
1397
1435
1398
1436
let empty = !has_attrs && blk. stmts . is_empty ( ) ;
1399
1437
self . bclose_maybe_open ( blk. span , empty, close_box) ;
0 commit comments