mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-30 10:45:18 +00:00
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:
parent
b8a133d432
commit
7132fe03a0
@ -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,
|
||||
)?
|
||||
|
@ -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,
|
||||
)?;
|
||||
|
@ -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);
|
||||
|
8
tests/source/alignment_2633/block_style.rs
Normal file
8
tests/source/alignment_2633/block_style.rs
Normal 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)), });
|
||||
}
|
9
tests/source/alignment_2633/visual_style.rs
Normal file
9
tests/source/alignment_2633/visual_style.rs
Normal 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)), });
|
||||
}
|
10
tests/target/alignment_2633/block_style.rs
Normal file
10
tests/target/alignment_2633/block_style.rs
Normal 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)),
|
||||
});
|
||||
}
|
9
tests/target/alignment_2633/visual_style.rs
Normal file
9
tests/target/alignment_2633/visual_style.rs
Normal 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)), });
|
||||
}
|
Loading…
Reference in New Issue
Block a user