@@ -20,6 +20,7 @@ use rustc_span::{BytePos, DUMMY_SP, Ident, Span, Symbol};
20
20
use tracing:: debug;
21
21
22
22
use crate :: Config ;
23
+ use crate :: attr:: rewrite_meta_item_inner_result;
23
24
use crate :: comment:: {
24
25
CharClasses , FindUncommented , FullCodeCharKind , LineClasses , contains_comment,
25
26
} ;
@@ -1513,6 +1514,39 @@ fn format_lazy_static(
1513
1514
Ok ( result)
1514
1515
}
1515
1516
1517
+ fn format_cfg_select_rule (
1518
+ rule : & ast:: MetaItemInner ,
1519
+ context : & RewriteContext < ' _ > ,
1520
+ shape : Shape ,
1521
+ span : Span ,
1522
+ ) -> RewriteResult {
1523
+ let mut result = String :: with_capacity ( 128 ) ;
1524
+
1525
+ // The cfg plus ` => {` should stay within the line length.
1526
+ let rule_shape = shape
1527
+ . sub_width ( " => {" . len ( ) )
1528
+ . max_width_error ( shape. width , span) ?;
1529
+
1530
+ let formatted = rewrite_meta_item_inner_result ( rule, context, rule_shape, true ) ?;
1531
+ result. push_str ( & formatted) ;
1532
+
1533
+ let is_key_value = match rule {
1534
+ ast:: MetaItemInner :: MetaItem ( ref meta_item) => {
1535
+ matches ! ( meta_item. kind, ast:: MetaItemKind :: NameValue ( _) )
1536
+ }
1537
+ _ => false ,
1538
+ } ;
1539
+
1540
+ if is_key_value && formatted. contains ( '\n' ) {
1541
+ result. push_str ( & shape. indent . to_string_with_newline ( context. config ) ) ;
1542
+ result. push_str ( "=>" ) ;
1543
+ } else {
1544
+ result. push_str ( " =>" ) ;
1545
+ }
1546
+
1547
+ Ok ( result)
1548
+ }
1549
+
1516
1550
fn format_cfg_select (
1517
1551
context : & RewriteContext < ' _ > ,
1518
1552
shape : Shape ,
@@ -1529,16 +1563,10 @@ fn format_cfg_select(
1529
1563
. block_indent ( context. config . tab_spaces ( ) )
1530
1564
. with_max_width ( context. config ) ;
1531
1565
1532
- // The cfg plus ` => {` should stay within the line length.
1533
- let rule_shape = shape
1534
- . sub_width ( " => {" . len ( ) )
1535
- . max_width_error ( shape. width , span) ?;
1536
-
1537
1566
result. push_str ( & shape. indent . to_string_with_newline ( context. config ) ) ;
1538
1567
1539
1568
for ( rule, rhs, _) in branches. reachable {
1540
- result. push_str ( & rule. rewrite_result ( context, rule_shape) ?) ;
1541
- result. push_str ( " =>" ) ;
1569
+ result. push_str ( & format_cfg_select_rule ( & rule, context, shape, span) ?) ;
1542
1570
result. push_str ( & format_cfg_select_rhs ( context, shape, rhs) ?) ;
1543
1571
}
1544
1572
@@ -1552,14 +1580,13 @@ fn format_cfg_select(
1552
1580
1553
1581
match lhs {
1554
1582
CfgSelectPredicate :: Cfg ( rule) => {
1555
- result. push_str ( & rule . rewrite_result ( context, rule_shape ) ?) ;
1583
+ result. push_str ( & format_cfg_select_rule ( & rule , context, shape , span ) ?) ;
1556
1584
}
1557
1585
CfgSelectPredicate :: Wildcard ( _) => {
1558
- result. push ( '_' ) ;
1586
+ result. push_str ( "_ =>" ) ;
1559
1587
}
1560
1588
}
1561
1589
1562
- result. push_str ( " =>" ) ;
1563
1590
result. push_str ( & format_cfg_select_rhs ( context, shape, rhs) ?) ;
1564
1591
}
1565
1592
0 commit comments