Skip to content

Commit 1ef7c47

Browse files
author
yanglinshu
committed
Merge vertical lists whenever possible
1 parent 17fa824 commit 1ef7c47

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/patterns.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use rustc_ast::ptr;
55
use rustc_span::{BytePos, Span};
66

77
use crate::comment::{combine_strs_with_missing_comments, FindUncommented};
8-
use crate::config::lists::*;
98
use crate::config::Version;
9+
use crate::config::{lists::*, IndentStyle};
1010
use crate::expr::{can_be_overflowed_expr, rewrite_unary_prefix, wrap_struct_field};
1111
use crate::lists::{
1212
definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape,
@@ -365,9 +365,25 @@ fn rewrite_struct_pat(
365365
// code ignored it and directly uses the tactic for the fields.
366366
let tactic = if ellipsis {
367367
let field_item_vec = &item_vec[..item_vec.len() - 1];
368-
let field_tactic = struct_lit_tactic(h_shape, context, field_item_vec);
369-
let rest_item_vec = &item_vec[item_vec.len() - 1..];
370-
let rest_tactic = struct_lit_tactic(h_shape, context, rest_item_vec);
368+
let mut field_tactic = struct_lit_tactic(h_shape, context, field_item_vec);
369+
let rest_item_vec = &item_vec[item_vec.len() - 1..];
370+
let mut rest_tactic = struct_lit_tactic(h_shape, context, rest_item_vec);
371+
372+
if !(context.config.struct_lit_single_line()
373+
|| context.config.indent_style() == IndentStyle::Visual && field_item_vec.len() == 1)
374+
{
375+
// FIXME: Figure out the exact behavior of mixed tactic. The old
376+
// produces mixed tactic by ignoring the `..` when determining the
377+
// tactic. See #5066 for an example.
378+
let nested_shape = shape_for_tactic(field_tactic, h_shape, v_shape);
379+
let fmt = struct_lit_formatting(nested_shape, field_tactic, context, false);
380+
let fields_str = write_list(field_item_vec, &fmt)?;
381+
if !(fields_str.contains('\n') || fields_str.len() > one_line_width) {
382+
field_tactic = DefinitiveListTactic::Horizontal;
383+
rest_tactic = DefinitiveListTactic::Horizontal;
384+
}
385+
}
386+
371387
match (field_tactic, rest_tactic) {
372388
(DefinitiveListTactic::Vertical, _) | (_, DefinitiveListTactic::Vertical) => {
373389
DefinitiveListTactic::Vertical

0 commit comments

Comments
 (0)