@@ -12,24 +12,36 @@ use crate::formatting::{
1212#[ derive( Clone , Copy ) ]
1313pub ( crate ) struct PairParts < ' a > {
1414 prefix : & ' a str ,
15+ infix_prefix : & ' a str , /* mainly for pre-infix comments */
1516 infix : & ' a str ,
17+ infix_suffix : & ' a str , /* mainly for post-infix comments */
1618 suffix : & ' a str ,
1719}
1820
1921impl < ' a > PairParts < ' a > {
2022 /// Constructs a new `PairParts`.
21- pub ( crate ) fn new ( prefix : & ' a str , infix : & ' a str , suffix : & ' a str ) -> Self {
22- Self {
23+ pub ( crate ) fn new (
24+ prefix : & ' a str ,
25+ infix_prefix : & ' a str ,
26+ infix : & ' a str ,
27+ infix_suffix : & ' a str ,
28+ suffix : & ' a str ,
29+ ) -> Self {
30+ PairParts {
2331 prefix,
32+ infix_prefix,
2433 infix,
34+ infix_suffix,
2535 suffix,
2636 }
2737 }
2838
2939 pub ( crate ) fn infix ( infix : & ' a str ) -> PairParts < ' a > {
3040 PairParts {
3141 prefix : "" ,
42+ infix_prefix : "" ,
3243 infix,
44+ infix_suffix : "" ,
3345 suffix : "" ,
3446 }
3547 }
@@ -172,22 +184,32 @@ where
172184 RHS : Rewrite ,
173185{
174186 let tab_spaces = context. config . tab_spaces ( ) ;
187+ let infix_result = format ! ( "{}{}" , pp. infix, pp. infix_suffix) ;
188+ let infix_suffix_separator = if pp. infix_suffix . is_empty ( ) { "" } else { " " } ;
189+ let infix_prefix_separator = if pp. infix_prefix . is_empty ( ) { "" } else { " " } ;
175190 let lhs_overhead = match separator_place {
176- SeparatorPlace :: Back => shape. used_width ( ) + pp. prefix . len ( ) + pp. infix . trim_end ( ) . len ( ) ,
191+ SeparatorPlace :: Back => {
192+ shape. used_width ( ) + pp. prefix . len ( ) + pp. infix . trim_end ( ) . len ( ) + pp. infix_prefix . len ( )
193+ }
177194 SeparatorPlace :: Front => shape. used_width ( ) ,
178195 } ;
179196 let lhs_shape = Shape {
180197 width : context. budget ( lhs_overhead) ,
181198 ..shape
182199 } ;
183- let lhs_result = lhs
184- . rewrite ( context, lhs_shape)
185- . map ( |lhs_str| format ! ( "{}{}" , pp. prefix, lhs_str) ) ?;
200+ let lhs_result = lhs. rewrite ( context, lhs_shape) . map ( |lhs_str| {
201+ format ! (
202+ "{}{}{}{}" ,
203+ pp. prefix, lhs_str, infix_prefix_separator, pp. infix_prefix
204+ )
205+ } ) ?;
186206
187207 // Try to put both lhs and rhs on the same line.
188208 let rhs_orig_result = shape
189209 . offset_left ( last_line_width ( & lhs_result) + pp. infix . len ( ) )
190- . and_then ( |s| s. sub_width ( pp. suffix . len ( ) ) )
210+ . and_then ( |s| {
211+ s. sub_width ( pp. suffix . len ( ) + pp. infix_suffix . len ( ) + infix_suffix_separator. len ( ) )
212+ } )
191213 . and_then ( |rhs_shape| rhs. rewrite ( context, rhs_shape) ) ;
192214 if let Some ( ref rhs_result) = rhs_orig_result {
193215 // If the length of the lhs is equal to or shorter than the tab width or
@@ -201,13 +223,13 @@ where
201223 . unwrap_or ( false ) ;
202224 if !rhs_result. contains ( '\n' ) || allow_same_line {
203225 let one_line_width = last_line_width ( & lhs_result)
204- + pp . infix . len ( )
226+ + infix_result . len ( )
205227 + first_line_width ( rhs_result)
206228 + pp. suffix . len ( ) ;
207229 if one_line_width <= shape. width {
208230 return Some ( format ! (
209- "{}{}{}{}" ,
210- lhs_result, pp . infix , rhs_result, pp. suffix
231+ "{}{}{}{}{} " ,
232+ lhs_result, infix_result , infix_suffix_separator , rhs_result, pp. suffix
211233 ) ) ;
212234 }
213235 }
@@ -228,20 +250,45 @@ where
228250 } ;
229251 let infix = match separator_place {
230252 SeparatorPlace :: Back => pp. infix . trim_end ( ) ,
231- SeparatorPlace :: Front => pp. infix . trim_start ( ) ,
253+ SeparatorPlace :: Front => {
254+ if pp. infix_suffix . is_empty ( ) {
255+ pp. infix . trim_start ( )
256+ } else {
257+ pp. infix
258+ }
259+ }
260+ } ;
261+ let infix_suffix = if separator_place == SeparatorPlace :: Front && !pp. infix_suffix . is_empty ( ) {
262+ pp. infix_suffix . trim_start ( )
263+ } else {
264+ pp. infix_suffix
232265 } ;
233266 if separator_place == SeparatorPlace :: Front {
234267 rhs_shape = rhs_shape. offset_left ( infix. len ( ) ) ?;
235268 }
236269 let rhs_result = rhs. rewrite ( context, rhs_shape) ?;
237270 let indent_str = rhs_shape. indent . to_string_with_newline ( context. config ) ;
238- let infix_with_sep = match separator_place {
239- SeparatorPlace :: Back => format ! ( "{}{}" , infix, indent_str) ,
240- SeparatorPlace :: Front => format ! ( "{}{}" , indent_str, infix) ,
271+ let mut infix_with_sep = match separator_place {
272+ SeparatorPlace :: Back => format ! ( "{}{}{}" , infix, infix_suffix. trim_end( ) , indent_str) ,
273+ SeparatorPlace :: Front => format ! (
274+ "{}{}{}{}" ,
275+ indent_str,
276+ infix. trim_start( ) ,
277+ infix_suffix,
278+ infix_suffix_separator
279+ ) ,
280+ } ;
281+ let new_line_width = infix_with_sep. len ( ) - 1 + rhs_result. len ( ) + pp. suffix . len ( ) ;
282+ let rhs_with_sep = if separator_place == SeparatorPlace :: Front && new_line_width > shape. width {
283+ let s: String = String :: from ( infix_with_sep) ;
284+ infix_with_sep = s. trim_end ( ) . to_string ( ) ;
285+ format ! ( "{}{}" , indent_str, rhs_result. trim_start( ) )
286+ } else {
287+ rhs_result
241288 } ;
242289 Some ( format ! (
243290 "{}{}{}{}" ,
244- lhs_result, infix_with_sep, rhs_result , pp. suffix
291+ lhs_result, infix_with_sep, rhs_with_sep , pp. suffix
245292 ) )
246293}
247294
0 commit comments