Skip to content

Commit 775de8a

Browse files
nrcmarcusklaas
authored andcommitted
Optionally put short struct variants on one line (#997)
Closes #418
1 parent 9589cac commit 775de8a

File tree

7 files changed

+44
-57
lines changed

7 files changed

+44
-57
lines changed

src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ create_config! {
334334
"Maximum width of the args of a function call before falling back to vertical formatting";
335335
struct_lit_width: usize, 16,
336336
"Maximum width in the body of a struct lit before falling back to vertical formatting";
337+
struct_variant_width: usize, 35,
338+
"Maximum width in the body of a struct variant before falling back to vertical formatting";
337339
force_explicit_abi: bool, true, "Always print the abi for extern items";
338340
newline_style: NewlineStyle, NewlineStyle::Unix, "Unix or Windows line endings";
339341
fn_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for functions";

src/issues.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,8 @@ impl ReportTactic {
3232

3333
#[derive(Clone, Copy)]
3434
enum Seeking {
35-
Issue {
36-
todo_idx: usize,
37-
fixme_idx: usize,
38-
},
39-
Number {
40-
issue: Issue,
41-
part: NumberPart,
42-
},
35+
Issue { todo_idx: usize, fixme_idx: usize },
36+
Number { issue: Issue, part: NumberPart },
4337
}
4438

4539
#[derive(Clone, Copy)]

src/items.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use Indent;
1414
use utils::{CodeMapSpanUtils, format_mutability, format_visibility, contains_skip, end_typaram,
1515
wrap_str, last_line_width, semicolon_for_expr, format_unsafety, trim_newlines};
1616
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic,
17-
DefinitiveListTactic, definitive_tactic, format_item_list};
17+
DefinitiveListTactic, ListTactic, definitive_tactic, format_item_list};
1818
use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs};
1919
use comment::{FindUncommented, contains_comment};
2020
use visitor::FmtVisitor;
@@ -419,7 +419,8 @@ impl<'a> FmtVisitor<'a> {
419419
&field.node.data,
420420
None,
421421
field.span,
422-
indent)
422+
indent,
423+
Some(self.config.struct_variant_width))
423424
}
424425
ast::VariantData::Unit(..) => {
425426
let tag = if let Some(ref expr) = field.node.disr_expr {
@@ -588,7 +589,8 @@ pub fn format_struct(context: &RewriteContext,
588589
struct_def: &ast::VariantData,
589590
generics: Option<&ast::Generics>,
590591
span: Span,
591-
offset: Indent)
592+
offset: Indent,
593+
one_line_width: Option<usize>)
592594
-> Option<String> {
593595
match *struct_def {
594596
ast::VariantData::Unit(..) => format_unit_struct(item_name, ident, vis),
@@ -610,7 +612,8 @@ pub fn format_struct(context: &RewriteContext,
610612
fields,
611613
generics,
612614
span,
613-
offset)
615+
offset,
616+
one_line_width)
614617
}
615618
}
616619
}
@@ -758,7 +761,8 @@ fn format_struct_struct(context: &RewriteContext,
758761
fields: &[ast::StructField],
759762
generics: Option<&ast::Generics>,
760763
span: Span,
761-
offset: Indent)
764+
offset: Indent,
765+
one_line_width: Option<usize>)
762766
-> Option<String> {
763767
let mut result = String::with_capacity(1024);
764768

@@ -813,23 +817,35 @@ fn format_struct_struct(context: &RewriteContext,
813817
|field| field.ty.span.hi,
814818
|field| field.rewrite(context, item_budget, item_indent),
815819
context.codemap.span_after(span, "{"),
816-
span.hi);
820+
span.hi)
821+
.collect::<Vec<_>>();
817822
// 1 = ,
818823
let budget = context.config.max_width - offset.width() + context.config.tab_spaces - 1;
824+
825+
let tactic = match one_line_width {
826+
Some(w) => definitive_tactic(&items, ListTactic::LimitedHorizontalVertical(w), budget),
827+
None => DefinitiveListTactic::Vertical,
828+
};
829+
819830
let fmt = ListFormatting {
820-
tactic: DefinitiveListTactic::Vertical,
831+
tactic: tactic,
821832
separator: ",",
822833
trailing_separator: context.config.struct_trailing_comma,
823834
indent: item_indent,
824835
width: budget,
825836
ends_with_newline: true,
826837
config: context.config,
827838
};
828-
Some(format!("{}\n{}{}\n{}}}",
829-
result,
830-
offset.block_indent(context.config).to_string(context.config),
831-
try_opt!(write_list(items, &fmt)),
832-
offset.to_string(context.config)))
839+
let items_str = try_opt!(write_list(&items, &fmt));
840+
if one_line_width.is_some() && !items_str.contains('\n') {
841+
Some(format!("{} {} }}", result, items_str))
842+
} else {
843+
Some(format!("{}\n{}{}\n{}}}",
844+
result,
845+
offset.block_indent(context.config).to_string(context.config),
846+
items_str,
847+
offset.to_string(context.config)))
848+
}
833849
}
834850

835851
fn format_tuple_struct(context: &RewriteContext,

src/visitor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ impl<'a> FmtVisitor<'a> {
267267
def,
268268
Some(generics),
269269
item.span,
270-
indent)
270+
indent,
271+
None)
271272
.map(|s| {
272273
match *def {
273274
ast::VariantData::Tuple(..) => s + ";",

tests/target/enum-no_trailing_comma.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,11 @@ enum TupY {
2121
}
2222

2323
enum StructX {
24-
A {
25-
s: u16,
26-
},
27-
B {
28-
u: u32,
29-
i: i32,
30-
}
24+
A { s: u16 },
25+
B { u: u32, i: i32 }
3126
}
3227

3328
enum StructY {
34-
A {
35-
s: u16,
36-
},
37-
B {
38-
u: u32,
39-
i: i32,
40-
}
29+
A { s: u16 },
30+
B { u: u32, i: i32 }
4131
}

tests/target/enum.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ enum StructLikeVariants {
4242
#[Attr50]
4343
y: SomeType, // Aanother Comment
4444
},
45-
SL {
46-
a: A,
47-
},
45+
SL { a: A },
4846
}
4947

5048
enum X {
@@ -64,10 +62,7 @@ pub enum EnumWithAttributes {
6462
SkippedItem(String,String,), // Post-comment
6563
#[another_attr]
6664
#[attr2]
67-
ItemStruct {
68-
x: usize,
69-
y: usize,
70-
}, /* Comment AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
65+
ItemStruct { x: usize, y: usize }, /* Comment AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
7166
// And another
7267
ForcedPreflight, /* AAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
7368
* AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
@@ -81,24 +76,15 @@ pub enum SingleTuple {
8176
}
8277

8378
pub enum SingleStruct {
84-
Match {
85-
name: String,
86-
loc: usize,
87-
}, // Post-comment
79+
Match { name: String, loc: usize }, // Post-comment
8880
}
8981

9082
pub enum GenericEnum<I, T>
9183
where I: Iterator<Item = T>
9284
{
9385
// Pre Comment
94-
Left {
95-
list: I,
96-
root: T,
97-
}, // Post-comment
98-
Right {
99-
list: I,
100-
root: T,
101-
}, // Post Comment
86+
Left { list: I, root: T }, // Post-comment
87+
Right { list: I, root: T }, // Post Comment
10288
}
10389

10490

tests/target/where-trailing-comma.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ enum E<S, T>
3333
where S: P,
3434
T: P,
3535
{
36-
A {
37-
a: T,
38-
},
36+
A { a: T },
3937
}
4038

4139
type Double<T>

0 commit comments

Comments
 (0)