@@ -39,9 +39,9 @@ use spanned::Spanned;
39
39
use string:: { rewrite_string, StringFormat } ;
40
40
use types:: { can_be_overflowed_type, rewrite_path, PathContext } ;
41
41
use utils:: {
42
- colon_spaces, contains_skip, count_newlines, first_line_ends_with, first_line_width ,
43
- inner_attributes , last_line_extendable, last_line_width, mk_sp, outer_attributes,
44
- ptr_vec_to_ref_vec , semicolon_for_stmt, wrap_str,
42
+ colon_spaces, contains_skip, count_newlines, first_line_ends_with, inner_attributes ,
43
+ last_line_extendable, last_line_width, mk_sp, outer_attributes, ptr_vec_to_ref_vec ,
44
+ semicolon_for_stmt, wrap_str,
45
45
} ;
46
46
use vertical:: rewrite_with_alignment;
47
47
use visitor:: FmtVisitor ;
@@ -1438,13 +1438,15 @@ fn rewrite_paren(
1438
1438
debug ! ( "rewrite_paren, shape: {:?}" , shape) ;
1439
1439
1440
1440
// Extract comments within parens.
1441
+ let mut pre_span;
1442
+ let mut post_span;
1441
1443
let mut pre_comment;
1442
1444
let mut post_comment;
1443
1445
let remove_nested_parens = context. config . remove_nested_parens ( ) ;
1444
1446
loop {
1445
1447
// 1 = "(" or ")"
1446
- let pre_span = mk_sp ( span. lo ( ) + BytePos ( 1 ) , subexpr. span . lo ( ) ) ;
1447
- let post_span = mk_sp ( subexpr. span . hi ( ) , span. hi ( ) - BytePos ( 1 ) ) ;
1448
+ pre_span = mk_sp ( span. lo ( ) + BytePos ( 1 ) , subexpr. span . lo ( ) ) ;
1449
+ post_span = mk_sp ( subexpr. span . hi ( ) , span. hi ( ) - BytePos ( 1 ) ) ;
1448
1450
pre_comment = rewrite_missing_comment ( pre_span, shape, context) ?;
1449
1451
post_comment = rewrite_missing_comment ( post_span, shape, context) ?;
1450
1452
@@ -1460,20 +1462,48 @@ fn rewrite_paren(
1460
1462
break ;
1461
1463
}
1462
1464
1463
- // 1 `(`
1464
- let sub_shape = shape. offset_left ( 1 ) . and_then ( |s| s. sub_width ( 1 ) ) ?;
1465
-
1465
+ // 1 = `(` and `)`
1466
+ let sub_shape = shape. offset_left ( 1 ) ?. sub_width ( 1 ) ?;
1466
1467
let subexpr_str = subexpr. rewrite ( context, sub_shape) ?;
1467
- debug ! ( "rewrite_paren, subexpr_str: `{:?}`" , subexpr_str) ;
1468
-
1469
- // 2 = `()`
1470
- if subexpr_str. contains ( '\n' ) || first_line_width ( & subexpr_str) + 2 <= shape. width {
1468
+ let fits_single_line = !pre_comment. contains ( "//" ) && !post_comment. contains ( "//" ) ;
1469
+ if fits_single_line {
1471
1470
Some ( format ! ( "({}{}{})" , pre_comment, & subexpr_str, post_comment) )
1472
1471
} else {
1473
- None
1472
+ rewrite_paren_in_multi_line ( context , subexpr , shape , pre_span , post_span )
1474
1473
}
1475
1474
}
1476
1475
1476
+ fn rewrite_paren_in_multi_line (
1477
+ context : & RewriteContext ,
1478
+ subexpr : & ast:: Expr ,
1479
+ shape : Shape ,
1480
+ pre_span : Span ,
1481
+ post_span : Span ,
1482
+ ) -> Option < String > {
1483
+ let nested_indent = shape. indent . block_indent ( context. config ) ;
1484
+ let nested_shape = Shape :: indented ( nested_indent, context. config ) ;
1485
+ let pre_comment = rewrite_missing_comment ( pre_span, nested_shape, context) ?;
1486
+ let post_comment = rewrite_missing_comment ( post_span, nested_shape, context) ?;
1487
+ let subexpr_str = subexpr. rewrite ( context, nested_shape) ?;
1488
+
1489
+ let mut result = String :: with_capacity ( subexpr_str. len ( ) * 2 ) ;
1490
+ result. push ( '(' ) ;
1491
+ if !pre_comment. is_empty ( ) {
1492
+ result. push_str ( & nested_indent. to_string_with_newline ( context. config ) ) ;
1493
+ result. push_str ( & pre_comment) ;
1494
+ }
1495
+ result. push_str ( & nested_indent. to_string_with_newline ( context. config ) ) ;
1496
+ result. push_str ( & subexpr_str) ;
1497
+ if !post_comment. is_empty ( ) {
1498
+ result. push_str ( & nested_indent. to_string_with_newline ( context. config ) ) ;
1499
+ result. push_str ( & post_comment) ;
1500
+ }
1501
+ result. push_str ( & shape. indent . to_string_with_newline ( context. config ) ) ;
1502
+ result. push ( ')' ) ;
1503
+
1504
+ Some ( result)
1505
+ }
1506
+
1477
1507
fn rewrite_index (
1478
1508
expr : & ast:: Expr ,
1479
1509
index : & ast:: Expr ,
0 commit comments