@@ -501,6 +501,7 @@ where
501
501
502
502
impl < C > FmtWithCtx < C > for FunDecl
503
503
where
504
+ C : AstFormatter ,
504
505
// For the signature
505
506
C : for < ' a > SetGenerics < ' a > ,
506
507
for < ' a > <C as SetGenerics < ' a > >:: C : AstFormatter ,
@@ -509,18 +510,15 @@ where
509
510
for < ' a , ' b > <<C as SetGenerics < ' a > >:: C as SetLocals < ' b > >:: C : AstFormatter ,
510
511
{
511
512
fn fmt_with_ctx_and_indent ( & self , tab : & str , ctx : & C ) -> String {
512
- // Update the context
513
- let ctx = & ctx. set_generics ( & self . signature . generics ) ;
514
-
515
- // Unsafe keyword
516
- let unsafe_kw = if self . signature . is_unsafe {
517
- "unsafe " . to_string ( )
513
+ let keyword = if self . signature . is_unsafe {
514
+ "unsafe fn"
518
515
} else {
519
- "" . to_string ( )
516
+ "fn"
520
517
} ;
518
+ let intro = self . item_meta . fmt_item_intro ( ctx, tab, keyword) ;
521
519
522
- // Function name
523
- let name = self . item_meta . name . fmt_with_ctx ( ctx ) ;
520
+ // Update the context
521
+ let ctx = & ctx . set_generics ( & self . signature . generics ) ;
524
522
525
523
// Generic parameters
526
524
let ( params, preds) = self
@@ -558,7 +556,7 @@ where
558
556
} ;
559
557
560
558
// Put everything together
561
- format ! ( "{tab}{unsafe_kw}fn {name }{params}({args}){ret_ty}{preds}{body}" , )
559
+ format ! ( "{intro }{params}({args}){ret_ty}{preds}{body}" , )
562
560
}
563
561
}
564
562
@@ -579,6 +577,8 @@ where
579
577
for < ' a , ' b , ' c > <<C as SetGenerics < ' a > >:: C as SetLocals < ' b > >:: C : AstFormatter ,
580
578
{
581
579
fn fmt_with_ctx_and_indent ( & self , tab : & str , ctx : & C ) -> String {
580
+ let intro = self . item_meta . fmt_item_intro ( ctx, tab, "global" ) ;
581
+
582
582
// Update the context with the generics
583
583
let ctx = & ctx. set_generics ( & self . generics ) ;
584
584
@@ -594,11 +594,10 @@ where
594
594
} ;
595
595
596
596
// Decl name
597
- let name = self . item_meta . name . fmt_with_ctx ( ctx) ;
598
597
let initializer = ctx. format_object ( self . init ) ;
599
598
600
599
// Put everything together
601
- format ! ( "{tab}global {name }{params}: {ty}{preds}{eq_space}= {initializer}()" )
600
+ format ! ( "{intro }{params}: {ty}{preds}{eq_space}= {initializer}()" )
602
601
}
603
602
}
604
603
@@ -616,6 +615,18 @@ impl<C: AstFormatter> FmtWithCtx<C> for ImplElem {
616
615
}
617
616
}
618
617
618
+ impl ItemMeta {
619
+ /// Format the start of an item definition, up to the name.
620
+ pub fn fmt_item_intro < C > ( & self , ctx : & C , tab : & str , keyword : & str ) -> String
621
+ where
622
+ C : AstFormatter ,
623
+ {
624
+ let name = self . name . fmt_with_ctx ( ctx) ;
625
+ let vis = if self . attr_info . public { "pub " } else { "" } ;
626
+ format ! ( "{tab}{vis}{keyword} {name}" )
627
+ }
628
+ }
629
+
619
630
impl < C : AstFormatter > FmtWithCtx < C > for LiteralTy {
620
631
fn fmt_with_ctx ( & self , _ctx : & C ) -> String {
621
632
match self {
@@ -1192,10 +1203,11 @@ impl<C: AstFormatter> FmtWithCtx<C> for TraitClause {
1192
1203
1193
1204
impl < C : AstFormatter > FmtWithCtx < C > for TraitDecl {
1194
1205
fn fmt_with_ctx ( & self , ctx : & C ) -> String {
1206
+ let intro = self . item_meta . fmt_item_intro ( ctx, "" , "trait" ) ;
1207
+
1195
1208
// Update the context
1196
1209
let ctx = & ctx. set_generics ( & self . generics ) ;
1197
1210
1198
- let name = self . item_meta . name . fmt_with_ctx ( ctx) ;
1199
1211
let ( generics, clauses) = self . generics . fmt_with_ctx_with_trait_clauses ( ctx, "" ) ;
1200
1212
1201
1213
let items = {
@@ -1242,7 +1254,7 @@ impl<C: AstFormatter> FmtWithCtx<C> for TraitDecl {
1242
1254
}
1243
1255
} ;
1244
1256
1245
- format ! ( "trait {name }{generics}{clauses}{items}" )
1257
+ format ! ( "{intro }{generics}{clauses}{items}" )
1246
1258
}
1247
1259
}
1248
1260
@@ -1256,10 +1268,11 @@ impl<C: AstFormatter> FmtWithCtx<C> for TraitDeclRef {
1256
1268
1257
1269
impl < C : AstFormatter > FmtWithCtx < C > for TraitImpl {
1258
1270
fn fmt_with_ctx ( & self , ctx : & C ) -> String {
1271
+ let intro = self . item_meta . fmt_item_intro ( ctx, "" , "impl" ) ;
1272
+
1259
1273
// Update the context
1260
1274
let ctx = & ctx. set_generics ( & self . generics ) ;
1261
1275
1262
- let name = self . item_meta . name . fmt_with_ctx ( ctx) ;
1263
1276
let ( generics, clauses) = self . generics . fmt_with_ctx_with_trait_clauses ( ctx, "" ) ;
1264
1277
1265
1278
let items = {
@@ -1306,7 +1319,7 @@ impl<C: AstFormatter> FmtWithCtx<C> for TraitImpl {
1306
1319
} ;
1307
1320
1308
1321
let impl_trait = self . impl_trait . fmt_with_ctx ( ctx) ;
1309
- format ! ( "impl{generics} {name }{generics} : {impl_trait}{clauses}{items}" )
1322
+ format ! ( "{intro }{generics} : {impl_trait}{clauses}{items}" )
1310
1323
}
1311
1324
}
1312
1325
@@ -1437,74 +1450,56 @@ impl<C: AstFormatter> FmtWithCtx<C> for Ty {
1437
1450
1438
1451
impl < C : AstFormatter > FmtWithCtx < C > for TypeDecl {
1439
1452
fn fmt_with_ctx ( & self , ctx : & C ) -> String {
1453
+ let keyword = match & self . kind {
1454
+ TypeDeclKind :: Struct ( ..) => "struct" ,
1455
+ TypeDeclKind :: Union ( ..) => "union" ,
1456
+ TypeDeclKind :: Enum ( ..) => "enum" ,
1457
+ TypeDeclKind :: Alias ( ..) => "type" ,
1458
+ TypeDeclKind :: Opaque | TypeDeclKind :: Error ( ..) => "opaque type" ,
1459
+ } ;
1460
+ let intro = self . item_meta . fmt_item_intro ( ctx, "" , keyword) ;
1461
+
1440
1462
let ctx = & ctx. set_generics ( & self . generics ) ;
1441
1463
1442
1464
let ( params, preds) = self . generics . fmt_with_ctx_with_trait_clauses ( ctx, " " ) ;
1443
1465
// Predicates
1444
- let eq_space = if !self . generics . has_predicates ( ) {
1466
+ let nl_or_space = if !self . generics . has_predicates ( ) {
1445
1467
" " . to_string ( )
1446
1468
} else {
1447
1469
"\n " . to_string ( )
1448
1470
} ;
1449
1471
1450
- match & self . kind {
1472
+ let contents = match & self . kind {
1451
1473
TypeDeclKind :: Struct ( fields) => {
1452
1474
if !fields. is_empty ( ) {
1453
1475
let fields = fields
1454
1476
. iter ( )
1455
1477
. map ( |f| format ! ( "\n {}," , f. fmt_with_ctx( ctx) ) )
1456
1478
. format ( "" ) ;
1457
- format ! (
1458
- "struct {}{params}{preds}{eq_space}=\n {{{fields}\n }}" ,
1459
- self . item_meta. name. fmt_with_ctx( ctx)
1460
- )
1479
+ format ! ( "{nl_or_space}=\n {{{fields}\n }}" )
1461
1480
} else {
1462
- format ! (
1463
- "struct {}{params}{preds}{eq_space}= {{}}" ,
1464
- self . item_meta. name. fmt_with_ctx( ctx)
1465
- )
1481
+ format ! ( "{nl_or_space}= {{}}" )
1466
1482
}
1467
1483
}
1468
1484
TypeDeclKind :: Union ( fields) => {
1469
1485
let fields = fields
1470
1486
. iter ( )
1471
1487
. map ( |f| format ! ( "\n {}," , f. fmt_with_ctx( ctx) ) )
1472
1488
. format ( "" ) ;
1473
- format ! (
1474
- "union {}{params}{preds}{eq_space}=\n {{{fields}\n }}" ,
1475
- self . item_meta. name. fmt_with_ctx( ctx)
1476
- )
1489
+ format ! ( "{nl_or_space}=\n {{{fields}\n }}" )
1477
1490
}
1478
1491
TypeDeclKind :: Enum ( variants) => {
1479
1492
let variants = variants
1480
1493
. iter ( )
1481
1494
. map ( |v| format ! ( "| {}" , v. fmt_with_ctx( ctx) ) )
1482
1495
. format ( "\n " ) ;
1483
- format ! (
1484
- "enum {}{params}{preds}{eq_space}=\n {variants}\n " ,
1485
- self . item_meta. name. fmt_with_ctx( ctx)
1486
- )
1496
+ format ! ( "{nl_or_space}=\n {variants}\n " )
1487
1497
}
1488
- TypeDeclKind :: Alias ( ty) => {
1489
- format ! (
1490
- "type {}{params}{preds} = {}" ,
1491
- self . item_meta. name. fmt_with_ctx( ctx) ,
1492
- ty. fmt_with_ctx( ctx) ,
1493
- )
1494
- }
1495
- TypeDeclKind :: Opaque => {
1496
- format ! (
1497
- "opaque type {}{params}{preds}" ,
1498
- self . item_meta. name. fmt_with_ctx( ctx)
1499
- )
1500
- }
1501
- TypeDeclKind :: Error ( msg) => {
1502
- format ! (
1503
- "opaque type {}{params}{preds} = ERROR({msg})" ,
1504
- self . item_meta. name. fmt_with_ctx( ctx) ,
1505
- )
1506
- }
1507
- }
1498
+ TypeDeclKind :: Alias ( ty) => format ! ( " = {}" , ty. fmt_with_ctx( ctx) ) ,
1499
+ TypeDeclKind :: Opaque => format ! ( "" ) ,
1500
+ TypeDeclKind :: Error ( msg) => format ! ( " = ERROR({msg})" ) ,
1501
+ } ;
1502
+ format ! ( "{intro}{params}{preds}{contents}" )
1508
1503
}
1509
1504
}
1510
1505
0 commit comments