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