@@ -12,24 +12,36 @@ use crate::formatting::{
12
12
#[ derive( Clone , Copy ) ]
13
13
pub ( crate ) struct PairParts < ' a > {
14
14
prefix : & ' a str ,
15
+ infix_prefix : & ' a str , /* mainly for pre-infix comments */
15
16
infix : & ' a str ,
17
+ infix_suffix : & ' a str , /* mainly for post-infix comments */
16
18
suffix : & ' a str ,
17
19
}
18
20
19
21
impl < ' a > PairParts < ' a > {
20
22
/// 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 {
23
31
prefix,
32
+ infix_prefix,
24
33
infix,
34
+ infix_suffix,
25
35
suffix,
26
36
}
27
37
}
28
38
29
39
pub ( crate ) fn infix ( infix : & ' a str ) -> PairParts < ' a > {
30
40
PairParts {
31
41
prefix : "" ,
42
+ infix_prefix : "" ,
32
43
infix,
44
+ infix_suffix : "" ,
33
45
suffix : "" ,
34
46
}
35
47
}
@@ -172,22 +184,32 @@ where
172
184
RHS : Rewrite ,
173
185
{
174
186
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 { " " } ;
175
190
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
+ }
177
194
SeparatorPlace :: Front => shape. used_width ( ) ,
178
195
} ;
179
196
let lhs_shape = Shape {
180
197
width : context. budget ( lhs_overhead) ,
181
198
..shape
182
199
} ;
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
+ } ) ?;
186
206
187
207
// Try to put both lhs and rhs on the same line.
188
208
let rhs_orig_result = shape
189
209
. 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
+ } )
191
213
. and_then ( |rhs_shape| rhs. rewrite ( context, rhs_shape) ) ;
192
214
if let Some ( ref rhs_result) = rhs_orig_result {
193
215
// If the length of the lhs is equal to or shorter than the tab width or
@@ -201,13 +223,13 @@ where
201
223
. unwrap_or ( false ) ;
202
224
if !rhs_result. contains ( '\n' ) || allow_same_line {
203
225
let one_line_width = last_line_width ( & lhs_result)
204
- + pp . infix . len ( )
226
+ + infix_result . len ( )
205
227
+ first_line_width ( rhs_result)
206
228
+ pp. suffix . len ( ) ;
207
229
if one_line_width <= shape. width {
208
230
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
211
233
) ) ;
212
234
}
213
235
}
@@ -228,20 +250,45 @@ where
228
250
} ;
229
251
let infix = match separator_place {
230
252
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
232
265
} ;
233
266
if separator_place == SeparatorPlace :: Front {
234
267
rhs_shape = rhs_shape. offset_left ( infix. len ( ) ) ?;
235
268
}
236
269
let rhs_result = rhs. rewrite ( context, rhs_shape) ?;
237
270
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
241
288
} ;
242
289
Some ( format ! (
243
290
"{}{}{}{}" ,
244
- lhs_result, infix_with_sep, rhs_result , pp. suffix
291
+ lhs_result, infix_with_sep, rhs_with_sep , pp. suffix
245
292
) )
246
293
}
247
294
0 commit comments