@@ -39,9 +39,9 @@ use spanned::Spanned;
39
39
use string:: { rewrite_string, StringFormat } ;
40
40
use types:: { 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 ;
@@ -1388,13 +1388,15 @@ fn rewrite_paren(
1388
1388
debug ! ( "rewrite_paren, shape: {:?}" , shape) ;
1389
1389
1390
1390
// Extract comments within parens.
1391
+ let mut pre_span;
1392
+ let mut post_span;
1391
1393
let mut pre_comment;
1392
1394
let mut post_comment;
1393
1395
let remove_nested_parens = context. config . remove_nested_parens ( ) ;
1394
1396
loop {
1395
1397
// 1 = "(" or ")"
1396
- let pre_span = mk_sp ( span. lo ( ) + BytePos ( 1 ) , subexpr. span . lo ( ) ) ;
1397
- let post_span = mk_sp ( subexpr. span . hi ( ) , span. hi ( ) - BytePos ( 1 ) ) ;
1398
+ pre_span = mk_sp ( span. lo ( ) + BytePos ( 1 ) , subexpr. span . lo ( ) ) ;
1399
+ post_span = mk_sp ( subexpr. span . hi ( ) , span. hi ( ) - BytePos ( 1 ) ) ;
1398
1400
pre_comment = rewrite_missing_comment ( pre_span, shape, context) ?;
1399
1401
post_comment = rewrite_missing_comment ( post_span, shape, context) ?;
1400
1402
@@ -1410,20 +1412,48 @@ fn rewrite_paren(
1410
1412
break ;
1411
1413
}
1412
1414
1413
- // 1 `(`
1414
- let sub_shape = shape. offset_left ( 1 ) . and_then ( |s| s. sub_width ( 1 ) ) ?;
1415
-
1415
+ // 1 = `(` and `)`
1416
+ let sub_shape = shape. offset_left ( 1 ) ?. sub_width ( 1 ) ?;
1416
1417
let subexpr_str = subexpr. rewrite ( context, sub_shape) ?;
1417
- debug ! ( "rewrite_paren, subexpr_str: `{:?}`" , subexpr_str) ;
1418
-
1419
- // 2 = `()`
1420
- if subexpr_str. contains ( '\n' ) || first_line_width ( & subexpr_str) + 2 <= shape. width {
1418
+ let fits_single_line = !pre_comment. contains ( "//" ) && !post_comment. contains ( "//" ) ;
1419
+ if fits_single_line {
1421
1420
Some ( format ! ( "({}{}{})" , pre_comment, & subexpr_str, post_comment) )
1422
1421
} else {
1423
- None
1422
+ rewrite_paren_in_multi_line ( context , subexpr , shape , pre_span , post_span )
1424
1423
}
1425
1424
}
1426
1425
1426
+ fn rewrite_paren_in_multi_line (
1427
+ context : & RewriteContext ,
1428
+ subexpr : & ast:: Expr ,
1429
+ shape : Shape ,
1430
+ pre_span : Span ,
1431
+ post_span : Span ,
1432
+ ) -> Option < String > {
1433
+ let nested_indent = shape. indent . block_indent ( context. config ) ;
1434
+ let nested_shape = Shape :: indented ( nested_indent, context. config ) ;
1435
+ let pre_comment = rewrite_missing_comment ( pre_span, nested_shape, context) ?;
1436
+ let post_comment = rewrite_missing_comment ( post_span, nested_shape, context) ?;
1437
+ let subexpr_str = subexpr. rewrite ( context, nested_shape) ?;
1438
+
1439
+ let mut result = String :: with_capacity ( subexpr_str. len ( ) * 2 ) ;
1440
+ result. push ( '(' ) ;
1441
+ if !pre_comment. is_empty ( ) {
1442
+ result. push_str ( & nested_indent. to_string_with_newline ( context. config ) ) ;
1443
+ result. push_str ( & pre_comment) ;
1444
+ }
1445
+ result. push_str ( & nested_indent. to_string_with_newline ( context. config ) ) ;
1446
+ result. push_str ( & subexpr_str) ;
1447
+ if !post_comment. is_empty ( ) {
1448
+ result. push_str ( & nested_indent. to_string_with_newline ( context. config ) ) ;
1449
+ result. push_str ( & post_comment) ;
1450
+ }
1451
+ result. push_str ( & shape. indent . to_string_with_newline ( context. config ) ) ;
1452
+ result. push ( ')' ) ;
1453
+
1454
+ Some ( result)
1455
+ }
1456
+
1427
1457
fn rewrite_index (
1428
1458
expr : & ast:: Expr ,
1429
1459
index : & ast:: Expr ,
0 commit comments