@@ -39,8 +39,8 @@ use string::{rewrite_string, StringFormat};
3939use types:: { can_be_overflowed_type, rewrite_path, PathContext } ;
4040use utils:: {
4141 colon_spaces, contains_skip, count_newlines, first_line_width, inner_attributes,
42- last_line_extendable, last_line_width, mk_sp, outer_attributes, paren_overhead ,
43- ptr_vec_to_ref_vec , semicolon_for_stmt, wrap_str,
42+ last_line_extendable, last_line_width, mk_sp, outer_attributes, ptr_vec_to_ref_vec ,
43+ semicolon_for_stmt, wrap_str,
4444} ;
4545use vertical:: rewrite_with_alignment;
4646use visitor:: FmtVisitor ;
@@ -223,21 +223,14 @@ pub fn format_expr(
223223 ast:: ExprKind :: Index ( ref expr, ref index) => {
224224 rewrite_index ( & * * expr, & * * index, context, shape)
225225 }
226- ast:: ExprKind :: Repeat ( ref expr, ref repeats) => {
227- let ( lbr, rbr) = if context. config . spaces_within_parens_and_brackets ( ) {
228- ( "[ " , " ]" )
229- } else {
230- ( "[" , "]" )
231- } ;
232- rewrite_pair (
233- & * * expr,
234- & * * repeats,
235- PairParts :: new ( lbr, "; " , rbr) ,
236- context,
237- shape,
238- SeparatorPlace :: Back ,
239- )
240- }
226+ ast:: ExprKind :: Repeat ( ref expr, ref repeats) => rewrite_pair (
227+ & * * expr,
228+ & * * repeats,
229+ PairParts :: new ( "[" , "; " , "]" ) ,
230+ context,
231+ shape,
232+ SeparatorPlace :: Back ,
233+ ) ,
241234 ast:: ExprKind :: Range ( ref lhs, ref rhs, limits) => {
242235 let delim = match limits {
243236 ast:: RangeLimits :: HalfOpen => ".." ,
@@ -1581,27 +1574,15 @@ fn rewrite_paren(
15811574 break ;
15821575 }
15831576
1584- let total_paren_overhead = paren_overhead ( context) ;
1585- let paren_overhead = total_paren_overhead / 2 ;
1586- let sub_shape = shape
1587- . offset_left ( paren_overhead)
1588- . and_then ( |s| s. sub_width ( paren_overhead) ) ?;
1589-
1590- let paren_wrapper = |s : & str | {
1591- if context. config . spaces_within_parens_and_brackets ( ) && !s. is_empty ( ) {
1592- format ! ( "( {}{}{} )" , pre_comment, s, post_comment)
1593- } else {
1594- format ! ( "({}{}{})" , pre_comment, s, post_comment)
1595- }
1596- } ;
1577+ // 1 `(`
1578+ let sub_shape = shape. offset_left ( 1 ) . and_then ( |s| s. sub_width ( 1 ) ) ?;
15971579
15981580 let subexpr_str = subexpr. rewrite ( context, sub_shape) ?;
15991581 debug ! ( "rewrite_paren, subexpr_str: `{:?}`" , subexpr_str) ;
16001582
1601- if subexpr_str. contains ( '\n' )
1602- || first_line_width ( & subexpr_str) + total_paren_overhead <= shape. width
1603- {
1604- Some ( paren_wrapper ( & subexpr_str) )
1583+ // 2 = `()`
1584+ if subexpr_str. contains ( '\n' ) || first_line_width ( & subexpr_str) + 2 <= shape. width {
1585+ Some ( format ! ( "({}{}{})" , pre_comment, & subexpr_str, post_comment) )
16051586 } else {
16061587 None
16071588 }
@@ -1615,54 +1596,44 @@ fn rewrite_index(
16151596) -> Option < String > {
16161597 let expr_str = expr. rewrite ( context, shape) ?;
16171598
1618- let ( lbr, rbr) = if context. config . spaces_within_parens_and_brackets ( ) {
1619- ( "[ " , " ]" )
1620- } else {
1621- ( "[" , "]" )
1622- } ;
1623-
1624- let offset = last_line_width ( & expr_str) + lbr. len ( ) ;
1599+ let offset = last_line_width ( & expr_str) + 1 ;
16251600 let rhs_overhead = shape. rhs_overhead ( context. config ) ;
16261601 let index_shape = if expr_str. contains ( '\n' ) {
16271602 Shape :: legacy ( context. config . max_width ( ) , shape. indent )
16281603 . offset_left ( offset)
1629- . and_then ( |shape| shape. sub_width ( rbr . len ( ) + rhs_overhead) )
1604+ . and_then ( |shape| shape. sub_width ( 1 + rhs_overhead) )
16301605 } else {
1631- shape. visual_indent ( offset) . sub_width ( offset + rbr . len ( ) )
1606+ shape. visual_indent ( offset) . sub_width ( offset + 1 )
16321607 } ;
16331608 let orig_index_rw = index_shape. and_then ( |s| index. rewrite ( context, s) ) ;
16341609
16351610 // Return if index fits in a single line.
16361611 match orig_index_rw {
16371612 Some ( ref index_str) if !index_str. contains ( '\n' ) => {
1638- return Some ( format ! ( "{}{}{}{} " , expr_str, lbr , index_str, rbr ) ) ;
1613+ return Some ( format ! ( "{}[{}] " , expr_str, index_str) ) ;
16391614 }
16401615 _ => ( ) ,
16411616 }
16421617
16431618 // Try putting index on the next line and see if it fits in a single line.
16441619 let indent = shape. indent . block_indent ( context. config ) ;
1645- let index_shape = Shape :: indented ( indent, context. config ) . offset_left ( lbr . len ( ) ) ?;
1646- let index_shape = index_shape. sub_width ( rbr . len ( ) + rhs_overhead) ?;
1620+ let index_shape = Shape :: indented ( indent, context. config ) . offset_left ( 1 ) ?;
1621+ let index_shape = index_shape. sub_width ( 1 + rhs_overhead) ?;
16471622 let new_index_rw = index. rewrite ( context, index_shape) ;
16481623 match ( orig_index_rw, new_index_rw) {
16491624 ( _, Some ( ref new_index_str) ) if !new_index_str. contains ( '\n' ) => Some ( format ! (
1650- "{}{}{}{}{} " ,
1625+ "{}{}[{}] " ,
16511626 expr_str,
16521627 indent. to_string_with_newline( context. config) ,
1653- lbr,
16541628 new_index_str,
1655- rbr
16561629 ) ) ,
16571630 ( None , Some ( ref new_index_str) ) => Some ( format ! (
1658- "{}{}{}{}{} " ,
1631+ "{}{}[{}] " ,
16591632 expr_str,
16601633 indent. to_string_with_newline( context. config) ,
1661- lbr,
16621634 new_index_str,
1663- rbr
16641635 ) ) ,
1665- ( Some ( ref index_str) , _) => Some ( format ! ( "{}{}{}{} " , expr_str, lbr , index_str, rbr ) ) ,
1636+ ( Some ( ref index_str) , _) => Some ( format ! ( "{}[{}] " , expr_str, index_str) ) ,
16661637 _ => None ,
16671638 }
16681639}
@@ -1877,13 +1848,7 @@ where
18771848 . next ( )
18781849 . unwrap ( )
18791850 . rewrite ( context, nested_shape)
1880- . map ( |s| {
1881- if context. config . spaces_within_parens_and_brackets ( ) {
1882- format ! ( "( {}, )" , s)
1883- } else {
1884- format ! ( "({},)" , s)
1885- }
1886- } ) ;
1851+ . map ( |s| format ! ( "({},)" , s) ) ;
18871852 }
18881853
18891854 let list_lo = context. snippet_provider . span_after ( span, "(" ) ;
@@ -1919,11 +1884,7 @@ where
19191884 } ;
19201885 let list_str = write_list ( & item_vec, & fmt) ?;
19211886
1922- if context. config . spaces_within_parens_and_brackets ( ) && !list_str. is_empty ( ) {
1923- Some ( format ! ( "( {} )" , list_str) )
1924- } else {
1925- Some ( format ! ( "({})" , list_str) )
1926- }
1887+ Some ( format ! ( "({})" , list_str) )
19271888}
19281889
19291890pub fn rewrite_tuple < ' a , T > (
0 commit comments