@@ -3,7 +3,7 @@ use syntax::{ast, ptr};
3
3
4
4
use crate :: attr:: get_attrs_from_stmt;
5
5
use crate :: config:: lists:: * ;
6
- use crate :: config:: Version ;
6
+ use crate :: config:: { IndentStyle , SeparatorTactic , Version } ;
7
7
use crate :: expr:: { block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond} ;
8
8
use crate :: items:: { span_hi_for_param, span_lo_for_param} ;
9
9
use crate :: lists:: { definitive_tactic, itemize_list, write_list, ListFormatting , Separator } ;
@@ -238,9 +238,16 @@ fn rewrite_closure_fn_decl(
238
238
. shrink_left ( is_async. len ( ) + mover. len ( ) + immovable. len ( ) ) ?
239
239
. sub_width ( 4 ) ?;
240
240
241
+ let indent_style = context. config . indent_style ( ) ;
242
+
241
243
// 1 = |
242
244
let param_offset = nested_shape. indent + 1 ;
243
- let param_shape = nested_shape. offset_left ( 1 ) ?. visual_indent ( 0 ) ;
245
+ let param_shape = match indent_style {
246
+ IndentStyle :: Block => {
247
+ Shape :: indented ( shape. indent . block_indent ( context. config ) , context. config )
248
+ }
249
+ IndentStyle :: Visual => nested_shape. offset_left ( 1 ) ?. visual_indent ( 0 ) ,
250
+ } ;
244
251
let ret_str = fn_decl. output . rewrite ( context, param_shape) ?;
245
252
246
253
let param_items = itemize_list (
@@ -273,10 +280,31 @@ fn rewrite_closure_fn_decl(
273
280
. tactic ( tactic)
274
281
. preserve_newline ( true ) ;
275
282
let list_str = write_list ( & item_vec, & fmt) ?;
276
- let mut prefix = format ! ( "{}{}{}|{}|" , is_async, immovable, mover, list_str) ;
283
+ let one_line_budget = context. budget ( param_shape. indent . width ( ) ) ;
284
+ let multi_line_params = match indent_style {
285
+ IndentStyle :: Block => list_str. contains ( '\n' ) || list_str. len ( ) > one_line_budget,
286
+ _ => false ,
287
+ } ;
288
+ let put_params_in_block = multi_line_params && !item_vec. is_empty ( ) ;
289
+ let param_str = if put_params_in_block {
290
+ let trailing_comma = match context. config . trailing_comma ( ) {
291
+ SeparatorTactic :: Never => "" ,
292
+ _ => "," ,
293
+ } ;
294
+ format ! (
295
+ "{}{}{}{}" ,
296
+ param_shape. indent. to_string_with_newline( context. config) ,
297
+ & list_str,
298
+ trailing_comma,
299
+ shape. indent. to_string_with_newline( context. config)
300
+ )
301
+ } else {
302
+ list_str
303
+ } ;
304
+ let mut prefix = format ! ( "{}{}{}|{}|" , is_async, immovable, mover, param_str) ;
277
305
278
306
if !ret_str. is_empty ( ) {
279
- if prefix. contains ( '\n' ) {
307
+ if prefix. contains ( '\n' ) && !put_params_in_block {
280
308
prefix. push ( '\n' ) ;
281
309
prefix. push_str ( & param_offset. to_string ( context. config ) ) ;
282
310
} else {
0 commit comments