@@ -8,7 +8,9 @@ use syntax::symbol::keywords;
8
8
use crate :: config:: lists:: * ;
9
9
use crate :: config:: { IndentStyle , TypeDensity } ;
10
10
use crate :: expr:: { format_expr, rewrite_assign_rhs, rewrite_tuple, rewrite_unary_prefix, ExprType } ;
11
- use crate :: lists:: { definitive_tactic, itemize_list, write_list, ListFormatting , Separator } ;
11
+ use crate :: lists:: {
12
+ definitive_tactic, itemize_list, write_list, ListFormatting , ListItem , Separator ,
13
+ } ;
12
14
use crate :: macros:: { rewrite_macro, MacroPosition } ;
13
15
use crate :: overflow;
14
16
use crate :: pairs:: { rewrite_pair, PairParts } ;
@@ -314,46 +316,76 @@ where
314
316
let offset = shape. indent + 1 ;
315
317
Shape :: legacy ( budget, offset)
316
318
} ;
319
+
317
320
let list_lo = context. snippet_provider . span_after ( span, "(" ) ;
318
- let items = itemize_list (
319
- context. snippet_provider ,
320
- inputs,
321
- ")" ,
322
- "," ,
323
- |arg| arg. span ( ) . lo ( ) ,
324
- |arg| arg. span ( ) . hi ( ) ,
325
- |arg| arg. rewrite ( context, list_shape) ,
326
- list_lo,
327
- span. hi ( ) ,
328
- false ,
329
- ) ;
321
+ let ( list_str, tactic) = if inputs. len ( ) == 0 {
322
+ let tactic = if output. contains ( '\n' ) {
323
+ DefinitiveListTactic :: Vertical
324
+ } else {
325
+ definitive_tactic (
326
+ & Vec :: < ListItem > :: new ( ) ,
327
+ ListTactic :: HorizontalVertical ,
328
+ Separator :: Comma ,
329
+ shape. width . saturating_sub ( 2 + output. len ( ) ) ,
330
+ )
331
+ } ;
332
+ let list_hi = context. snippet_provider . span_after_last ( span, ")" ) ;
333
+ let comment = context
334
+ . snippet_provider
335
+ . span_to_snippet ( mk_sp ( list_lo, list_hi - BytePos ( 1 ) ) ) ?
336
+ . trim ( ) ;
337
+ let comment = if comment. starts_with ( "//" ) {
338
+ format ! (
339
+ "{}{}{}" ,
340
+ & shape. indent. to_string_with_newline( context. config) ,
341
+ comment,
342
+ & shape. indent. to_string_with_newline( context. config)
343
+ )
344
+ } else {
345
+ comment. to_string ( )
346
+ } ;
347
+ ( comment, tactic)
348
+ } else {
349
+ let items = itemize_list (
350
+ context. snippet_provider ,
351
+ inputs,
352
+ ")" ,
353
+ "," ,
354
+ |arg| arg. span ( ) . lo ( ) ,
355
+ |arg| arg. span ( ) . hi ( ) ,
356
+ |arg| arg. rewrite ( context, list_shape) ,
357
+ list_lo,
358
+ span. hi ( ) ,
359
+ false ,
360
+ ) ;
330
361
331
- let item_vec: Vec < _ > = items. collect ( ) ;
362
+ let item_vec: Vec < _ > = items. collect ( ) ;
332
363
333
- // If the return type is multi-lined, then force to use multiple lines for
334
- // arguments as well.
335
- let tactic = if output. contains ( '\n' ) {
336
- DefinitiveListTactic :: Vertical
337
- } else {
338
- definitive_tactic (
339
- & * item_vec,
340
- ListTactic :: HorizontalVertical ,
341
- Separator :: Comma ,
342
- shape. width . saturating_sub ( 2 + output. len ( ) ) ,
343
- )
344
- } ;
345
- let trailing_separator = if !context. use_block_indent ( ) || variadic {
346
- SeparatorTactic :: Never
347
- } else {
348
- context. config . trailing_comma ( )
349
- } ;
364
+ // If the return type is multi-lined, then force to use multiple lines for
365
+ // arguments as well.
366
+ let tactic = if output. contains ( '\n' ) {
367
+ DefinitiveListTactic :: Vertical
368
+ } else {
369
+ definitive_tactic (
370
+ & * item_vec,
371
+ ListTactic :: HorizontalVertical ,
372
+ Separator :: Comma ,
373
+ shape. width . saturating_sub ( 2 + output. len ( ) ) ,
374
+ )
375
+ } ;
376
+ let trailing_separator = if !context. use_block_indent ( ) || variadic {
377
+ SeparatorTactic :: Never
378
+ } else {
379
+ context. config . trailing_comma ( )
380
+ } ;
350
381
351
- let fmt = ListFormatting :: new ( list_shape, context. config )
352
- . tactic ( tactic)
353
- . trailing_separator ( trailing_separator)
354
- . ends_with_newline ( tactic. ends_with_newline ( context. config . indent_style ( ) ) )
355
- . preserve_newline ( true ) ;
356
- let list_str = write_list ( & item_vec, & fmt) ?;
382
+ let fmt = ListFormatting :: new ( list_shape, context. config )
383
+ . tactic ( tactic)
384
+ . trailing_separator ( trailing_separator)
385
+ . ends_with_newline ( tactic. ends_with_newline ( context. config . indent_style ( ) ) )
386
+ . preserve_newline ( true ) ;
387
+ ( write_list ( & item_vec, & fmt) ?, tactic)
388
+ } ;
357
389
358
390
let args = if tactic == DefinitiveListTactic :: Horizontal || !context. use_block_indent ( ) {
359
391
format ! ( "({})" , list_str)
0 commit comments