@@ -1079,74 +1079,75 @@ impl Rewrite for UseSegment {
10791079impl Rewrite for UseTree {
10801080 // This does NOT format attributes and visibility or add a trailing `;`.
10811081 fn rewrite ( & self , context : & RewriteContext < ' _ > , mut shape : Shape ) -> Option < String > {
1082+ fn proceed (
1083+ context : & RewriteContext < ' _ > ,
1084+ shape : & Shape ,
1085+ curr_segment : & UseSegment ,
1086+ curr_segment_is_allow_overflow : bool ,
1087+ next_segment : Option < & & UseSegment > ,
1088+ ) -> Option < ( String , Shape ) > {
1089+ let mut rewritten_segment = curr_segment. rewrite ( context, shape. clone ( ) ) ?;
1090+ if next_segment. is_some ( ) {
1091+ rewritten_segment. push_str ( "::" ) ;
1092+ }
1093+ let reserved_room_for_brace = match next_segment. map ( |s| & s. kind ) {
1094+ Some ( UseSegmentKind :: List ( _) ) => "{" . len ( ) ,
1095+ _ => 0 ,
1096+ } ;
1097+ let next_shape = if matches ! ( & curr_segment. kind, UseSegmentKind :: List ( _) ) {
1098+ // This is the last segment and we won't use `next_shape`. Return `shape`
1099+ // unchanged.
1100+ shape. clone ( )
1101+ } else if curr_segment_is_allow_overflow {
1102+ // If the segment follows `use ` or newline, force to consume the segment with
1103+ // overflow.
1104+
1105+ let s = shape. offset_left_maybe_overflow ( rewritten_segment. len ( ) ) ;
1106+ if s. width == 0 {
1107+ // We have to to commit current segment in this line. Make a room for next
1108+ // round.
1109+ s. add_width ( reserved_room_for_brace)
1110+ } else {
1111+ s. clone ( )
1112+ }
1113+ } else {
1114+ let ret = shape. offset_left ( rewritten_segment. len ( ) ) ?;
1115+ // Check that there is a room for the next "{". If not, return null for retry with
1116+ // newline.
1117+ ret. offset_left ( reserved_room_for_brace) ?;
1118+ ret
1119+ } ;
1120+ Some ( ( rewritten_segment, next_shape) )
1121+ }
1122+
10821123 let shape_top_level = shape. clone ( ) ;
10831124 let mut result = String :: with_capacity ( 256 ) ;
10841125 let mut is_first = true ;
1085- let mut prev_is_allow_overflow = false ;
10861126 let mut iter = self . path . iter ( ) . peekable ( ) ;
10871127 while let Some ( segment) = iter. next ( ) {
1088- // Try stacking the next segment, e.g. `use prev::next_segment::...` and
1089- // `use prev::{next, segment}`.
1090- let can_stack_with_constraint = ( || {
1091- // If the segment follows `use ` or `{`, force to consume the segment with overflow.
1092- if is_first {
1093- let mut chunk = segment. rewrite ( context, shape. infinite_width ( ) ) ?;
1094- let next_shape = if iter. peek ( ) . is_some ( ) {
1095- chunk. push_str ( "::" ) ;
1096- shape. offset_left_maybe_overflow ( chunk. len ( ) )
1097- } else {
1098- shape. clone ( )
1099- } ;
1100- Some ( ( chunk, next_shape) )
1101- } else {
1102- // If the segment follows `use ` or newline, allow overflow by "{".
1103- let s = if prev_is_allow_overflow
1104- && matches ! ( segment. kind, UseSegmentKind :: List ( _) )
1105- {
1106- shape. add_width ( 1 )
1107- } else {
1108- shape. clone ( )
1109- } ;
1110- let mut chunk = segment. rewrite ( context, s) ?;
1111- let next_shape = match iter. peek ( ) . map ( |s| & s. kind ) {
1112- Some ( UseSegmentKind :: List ( _) ) => {
1113- chunk. push_str ( "::" ) ;
1114- let ret = shape. offset_left ( chunk. len ( ) ) ?;
1115- // Ensure that there is a room for the next "{".
1116- ret. offset_left ( 1 ) ?;
1117- ret
1118- }
1119- Some ( _) => {
1120- chunk. push_str ( "::" ) ;
1121- shape. offset_left ( chunk. len ( ) ) ?
1122- }
1123- None => shape. clone ( ) ,
1124- } ;
1125- Some ( ( chunk, next_shape) )
1128+ let allow_overflow = is_first;
1129+ is_first = false ;
1130+ match proceed ( context, & shape, segment, allow_overflow, iter. peek ( ) ) {
1131+ Some ( ( rewritten_segment, next_shape) ) => {
1132+ result. push_str ( & rewritten_segment) ;
1133+ shape = next_shape;
1134+ continue ;
11261135 }
1127- } ) ( ) ;
1128- match can_stack_with_constraint {
1129- Some ( ( chunk, next_shape) ) => {
1130- result. push_str ( & chunk) ;
1136+ None => ( ) ,
1137+ }
1138+ // If the first `proceed()` failed, retry with newline.
1139+ result. push_str ( "\n " ) ;
1140+ result. push_str ( & " " . repeat ( shape. indent . block_indent + 4 ) ) ;
1141+ shape = shape_top_level. clone ( ) ;
1142+ let allow_overflow = true ;
1143+ match proceed ( context, & shape, segment, allow_overflow, iter. peek ( ) ) {
1144+ Some ( ( rewritten_segment, next_shape) ) => {
1145+ result. push_str ( & rewritten_segment) ;
11311146 shape = next_shape;
1132- prev_is_allow_overflow = is_first;
1133- is_first = false ;
11341147 }
1135- // If the next segment exceeds the given width, continue with newline .
1148+ // Give up to format .
11361149 None => {
1137- let segment_str = segment. rewrite ( context, shape) ?;
1138- let mut chunk = format ! (
1139- "{}{}" ,
1140- " " . repeat( shape. indent. block_indent + 4 ) ,
1141- segment_str
1142- ) ;
1143- if iter. peek ( ) . is_some ( ) {
1144- chunk. push_str ( "::" ) ;
1145- }
1146- result. push_str ( "\n " ) ;
1147- result. push_str ( & chunk) ;
1148- shape = shape_top_level. offset_left_maybe_overflow ( segment_str. len ( ) ) ;
1149- prev_is_allow_overflow = true ;
1150+ return None ;
11501151 }
11511152 }
11521153 }
0 commit comments