fix alignment of a struct's fields with the visual style

- rewrite_with_alignment was called from the expr module with the wrong
  shape that missed the extra offset needed for the visual style
- rewrite_with_alignment was indenting the given shape although that
  should have been the caller's responsability
This commit is contained in:
Stéphane Campinas 2018-11-04 10:51:38 +01:00
parent b8a133d432
commit 7132fe03a0
No known key found for this signature in database
GPG key ID: 6D5620D908210133
8 changed files with 46 additions and 14 deletions

View file

@ -1565,7 +1565,7 @@ fn rewrite_struct_lit<'a>(
rewrite_with_alignment(
fields,
context,
shape,
v_shape,
mk_sp(body_lo, span.hi()),
one_line_width,
)?

View file

@ -1265,7 +1265,7 @@ pub fn format_struct_struct(
let items_str = rewrite_with_alignment(
fields,
context,
Shape::indented(offset, context.config).sub_width(1)?,
Shape::indented(offset.block_indent(context.config), context.config).sub_width(1)?,
mk_sp(body_lo, span.hi()),
one_line_budget,
)?;

View file

@ -172,16 +172,13 @@ pub fn rewrite_with_alignment<T: AlignedItem>(
} else {
let rest_span = mk_sp(init_last_pos, span.hi());
let rest_str = rewrite_with_alignment(rest, context, shape, rest_span, one_line_width)?;
Some(
result
+ spaces
+ "\n"
+ &shape
.indent
.block_indent(context.config)
.to_string(context.config)
+ &rest_str,
)
Some(format!(
"{}{}\n{}{}",
result,
spaces,
&shape.indent.to_string(context.config),
&rest_str
))
}
}
@ -217,9 +214,8 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
offset: Indent,
one_line_width: usize,
) -> Option<String> {
let item_indent = offset.block_indent(context.config);
// 1 = ","
let item_shape = Shape::indented(item_indent, context.config).sub_width(1)?;
let item_shape = Shape::indented(offset, context.config).sub_width(1)?;
let (mut field_prefix_max_width, field_prefix_min_width) =
struct_field_prefix_max_min_width(context, fields, item_shape);
let max_diff = field_prefix_max_width.saturating_sub(field_prefix_min_width);

View file

@ -0,0 +1,8 @@
// rustfmt-struct_field_align_threshold: 50
fn func() {
Ok(ServerInformation { name: unwrap_message_string(items.get(0)),
vendor: unwrap_message_string(items.get(1)),
version: unwrap_message_string(items.get(2)),
spec_version: unwrap_message_string(items.get(3)), });
}

View file

@ -0,0 +1,9 @@
// rustfmt-struct_field_align_threshold: 50
// rustfmt-indent_style: Visual
fn func() {
Ok(ServerInformation { name: unwrap_message_string(items.get(0)),
vendor: unwrap_message_string(items.get(1)),
version: unwrap_message_string(items.get(2)),
spec_version: unwrap_message_string(items.get(3)), });
}

View file

@ -0,0 +1,10 @@
// rustfmt-struct_field_align_threshold: 50
fn func() {
Ok(ServerInformation {
name: unwrap_message_string(items.get(0)),
vendor: unwrap_message_string(items.get(1)),
version: unwrap_message_string(items.get(2)),
spec_version: unwrap_message_string(items.get(3)),
});
}

View file

@ -0,0 +1,9 @@
// rustfmt-struct_field_align_threshold: 50
// rustfmt-indent_style: Visual
fn func() {
Ok(ServerInformation { name: unwrap_message_string(items.get(0)),
vendor: unwrap_message_string(items.get(1)),
version: unwrap_message_string(items.get(2)),
spec_version: unwrap_message_string(items.get(3)), });
}