Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 2a34414

Browse files
authored
Merge pull request rust-lang#3165 from scampi/alignment
fix alignment of a struct's fields with the visual style
2 parents b8a133d + 7132fe0 commit 2a34414

File tree

8 files changed

+46
-14
lines changed

8 files changed

+46
-14
lines changed

src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1565,7 +1565,7 @@ fn rewrite_struct_lit<'a>(
15651565
rewrite_with_alignment(
15661566
fields,
15671567
context,
1568-
shape,
1568+
v_shape,
15691569
mk_sp(body_lo, span.hi()),
15701570
one_line_width,
15711571
)?

src/items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,7 @@ pub fn format_struct_struct(
12651265
let items_str = rewrite_with_alignment(
12661266
fields,
12671267
context,
1268-
Shape::indented(offset, context.config).sub_width(1)?,
1268+
Shape::indented(offset.block_indent(context.config), context.config).sub_width(1)?,
12691269
mk_sp(body_lo, span.hi()),
12701270
one_line_budget,
12711271
)?;

src/vertical.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,13 @@ pub fn rewrite_with_alignment<T: AlignedItem>(
172172
} else {
173173
let rest_span = mk_sp(init_last_pos, span.hi());
174174
let rest_str = rewrite_with_alignment(rest, context, shape, rest_span, one_line_width)?;
175-
Some(
176-
result
177-
+ spaces
178-
+ "\n"
179-
+ &shape
180-
.indent
181-
.block_indent(context.config)
182-
.to_string(context.config)
183-
+ &rest_str,
184-
)
175+
Some(format!(
176+
"{}{}\n{}{}",
177+
result,
178+
spaces,
179+
&shape.indent.to_string(context.config),
180+
&rest_str
181+
))
185182
}
186183
}
187184

@@ -217,9 +214,8 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
217214
offset: Indent,
218215
one_line_width: usize,
219216
) -> Option<String> {
220-
let item_indent = offset.block_indent(context.config);
221217
// 1 = ","
222-
let item_shape = Shape::indented(item_indent, context.config).sub_width(1)?;
218+
let item_shape = Shape::indented(offset, context.config).sub_width(1)?;
223219
let (mut field_prefix_max_width, field_prefix_min_width) =
224220
struct_field_prefix_max_min_width(context, fields, item_shape);
225221
let max_diff = field_prefix_max_width.saturating_sub(field_prefix_min_width);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-struct_field_align_threshold: 50
2+
3+
fn func() {
4+
Ok(ServerInformation { name: unwrap_message_string(items.get(0)),
5+
vendor: unwrap_message_string(items.get(1)),
6+
version: unwrap_message_string(items.get(2)),
7+
spec_version: unwrap_message_string(items.get(3)), });
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// rustfmt-struct_field_align_threshold: 50
2+
// rustfmt-indent_style: Visual
3+
4+
fn func() {
5+
Ok(ServerInformation { name: unwrap_message_string(items.get(0)),
6+
vendor: unwrap_message_string(items.get(1)),
7+
version: unwrap_message_string(items.get(2)),
8+
spec_version: unwrap_message_string(items.get(3)), });
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-struct_field_align_threshold: 50
2+
3+
fn func() {
4+
Ok(ServerInformation {
5+
name: unwrap_message_string(items.get(0)),
6+
vendor: unwrap_message_string(items.get(1)),
7+
version: unwrap_message_string(items.get(2)),
8+
spec_version: unwrap_message_string(items.get(3)),
9+
});
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// rustfmt-struct_field_align_threshold: 50
2+
// rustfmt-indent_style: Visual
3+
4+
fn func() {
5+
Ok(ServerInformation { name: unwrap_message_string(items.get(0)),
6+
vendor: unwrap_message_string(items.get(1)),
7+
version: unwrap_message_string(items.get(2)),
8+
spec_version: unwrap_message_string(items.get(3)), });
9+
}

0 commit comments

Comments
 (0)