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

Commit 78d9091

Browse files
committed
do not vertically align list items in case the tactic is Horizontal
1 parent 8b709c0 commit 78d9091

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/vertical.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syntax::source_map::{BytePos, Span};
1919
use comment::{combine_strs_with_missing_comments, contains_comment};
2020
use expr::rewrite_field;
2121
use items::{rewrite_struct_field, rewrite_struct_field_prefix};
22-
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
22+
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
2323
use rewrite::{Rewrite, RewriteContext};
2424
use shape::{Indent, Shape};
2525
use source_map::SpanUtils;
@@ -227,7 +227,7 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
227227
field_prefix_max_width = 0;
228228
}
229229

230-
let items = itemize_list(
230+
let mut items = itemize_list(
231231
context.snippet_provider,
232232
fields.iter(),
233233
"}",
@@ -248,6 +248,20 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
248248
one_line_width,
249249
);
250250

251+
if tactic == DefinitiveListTactic::Horizontal {
252+
// since the items fits on a line, there is no need to align them
253+
let do_rewrite =
254+
|field: &T| -> Option<String> { field.rewrite_aligned_item(context, item_shape, 0) };
255+
fields
256+
.iter()
257+
.zip(items.iter_mut())
258+
.for_each(|(field, list_item): (&T, &mut ListItem)| {
259+
if list_item.item.is_some() {
260+
list_item.item = do_rewrite(field);
261+
}
262+
});
263+
}
264+
251265
let fmt = ListFormatting::new(item_shape, context.config)
252266
.tactic(tactic)
253267
.trailing_separator(context.config.trailing_comma())

tests/target/issue-2633.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// rustfmt-struct_field_align_threshold: 5
2+
3+
#[derive(Fail, Debug, Clone)]
4+
pub enum BuildError {
5+
LineTooLong { length: usize, limit: usize },
6+
DisallowedByte { b: u8, pos: usize },
7+
ContainsNewLine { pos: usize },
8+
}
9+
10+
enum Foo {
11+
A { a: usize, bbbbb: () },
12+
B { a: (), bbbbb: () },
13+
}

0 commit comments

Comments
 (0)