Skip to content

Commit

Permalink
Optionally put short struct variants on one line (#997)
Browse files Browse the repository at this point in the history
Closes #418
  • Loading branch information
nrc authored and marcusklaas committed May 18, 2016
1 parent 9589cac commit 775de8a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 57 deletions.
2 changes: 2 additions & 0 deletions src/config.rs
Expand Up @@ -334,6 +334,8 @@ create_config! {
"Maximum width of the args of a function call before falling back to vertical formatting";
struct_lit_width: usize, 16,
"Maximum width in the body of a struct lit before falling back to vertical formatting";
struct_variant_width: usize, 35,
"Maximum width in the body of a struct variant before falling back to vertical formatting";
force_explicit_abi: bool, true, "Always print the abi for extern items";
newline_style: NewlineStyle, NewlineStyle::Unix, "Unix or Windows line endings";
fn_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for functions";
Expand Down
10 changes: 2 additions & 8 deletions src/issues.rs
Expand Up @@ -32,14 +32,8 @@ impl ReportTactic {

#[derive(Clone, Copy)]
enum Seeking {
Issue {
todo_idx: usize,
fixme_idx: usize,
},
Number {
issue: Issue,
part: NumberPart,
},
Issue { todo_idx: usize, fixme_idx: usize },
Number { issue: Issue, part: NumberPart },
}

#[derive(Clone, Copy)]
Expand Down
40 changes: 28 additions & 12 deletions src/items.rs
Expand Up @@ -14,7 +14,7 @@ use Indent;
use utils::{CodeMapSpanUtils, format_mutability, format_visibility, contains_skip, end_typaram,
wrap_str, last_line_width, semicolon_for_expr, format_unsafety, trim_newlines};
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic,
DefinitiveListTactic, definitive_tactic, format_item_list};
DefinitiveListTactic, ListTactic, definitive_tactic, format_item_list};
use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs};
use comment::{FindUncommented, contains_comment};
use visitor::FmtVisitor;
Expand Down Expand Up @@ -419,7 +419,8 @@ impl<'a> FmtVisitor<'a> {
&field.node.data,
None,
field.span,
indent)
indent,
Some(self.config.struct_variant_width))
}
ast::VariantData::Unit(..) => {
let tag = if let Some(ref expr) = field.node.disr_expr {
Expand Down Expand Up @@ -588,7 +589,8 @@ pub fn format_struct(context: &RewriteContext,
struct_def: &ast::VariantData,
generics: Option<&ast::Generics>,
span: Span,
offset: Indent)
offset: Indent,
one_line_width: Option<usize>)
-> Option<String> {
match *struct_def {
ast::VariantData::Unit(..) => format_unit_struct(item_name, ident, vis),
Expand All @@ -610,7 +612,8 @@ pub fn format_struct(context: &RewriteContext,
fields,
generics,
span,
offset)
offset,
one_line_width)
}
}
}
Expand Down Expand Up @@ -758,7 +761,8 @@ fn format_struct_struct(context: &RewriteContext,
fields: &[ast::StructField],
generics: Option<&ast::Generics>,
span: Span,
offset: Indent)
offset: Indent,
one_line_width: Option<usize>)
-> Option<String> {
let mut result = String::with_capacity(1024);

Expand Down Expand Up @@ -813,23 +817,35 @@ fn format_struct_struct(context: &RewriteContext,
|field| field.ty.span.hi,
|field| field.rewrite(context, item_budget, item_indent),
context.codemap.span_after(span, "{"),
span.hi);
span.hi)
.collect::<Vec<_>>();
// 1 = ,
let budget = context.config.max_width - offset.width() + context.config.tab_spaces - 1;

let tactic = match one_line_width {
Some(w) => definitive_tactic(&items, ListTactic::LimitedHorizontalVertical(w), budget),
None => DefinitiveListTactic::Vertical,
};

let fmt = ListFormatting {
tactic: DefinitiveListTactic::Vertical,
tactic: tactic,
separator: ",",
trailing_separator: context.config.struct_trailing_comma,
indent: item_indent,
width: budget,
ends_with_newline: true,
config: context.config,
};
Some(format!("{}\n{}{}\n{}}}",
result,
offset.block_indent(context.config).to_string(context.config),
try_opt!(write_list(items, &fmt)),
offset.to_string(context.config)))
let items_str = try_opt!(write_list(&items, &fmt));
if one_line_width.is_some() && !items_str.contains('\n') {
Some(format!("{} {} }}", result, items_str))
} else {
Some(format!("{}\n{}{}\n{}}}",
result,
offset.block_indent(context.config).to_string(context.config),
items_str,
offset.to_string(context.config)))
}
}

fn format_tuple_struct(context: &RewriteContext,
Expand Down
3 changes: 2 additions & 1 deletion src/visitor.rs
Expand Up @@ -267,7 +267,8 @@ impl<'a> FmtVisitor<'a> {
def,
Some(generics),
item.span,
indent)
indent,
None)
.map(|s| {
match *def {
ast::VariantData::Tuple(..) => s + ";",
Expand Down
18 changes: 4 additions & 14 deletions tests/target/enum-no_trailing_comma.rs
Expand Up @@ -21,21 +21,11 @@ enum TupY {
}

enum StructX {
A {
s: u16,
},
B {
u: u32,
i: i32,
}
A { s: u16 },
B { u: u32, i: i32 }
}

enum StructY {
A {
s: u16,
},
B {
u: u32,
i: i32,
}
A { s: u16 },
B { u: u32, i: i32 }
}
24 changes: 5 additions & 19 deletions tests/target/enum.rs
Expand Up @@ -42,9 +42,7 @@ enum StructLikeVariants {
#[Attr50]
y: SomeType, // Aanother Comment
},
SL {
a: A,
},
SL { a: A },
}

enum X {
Expand All @@ -64,10 +62,7 @@ pub enum EnumWithAttributes {
SkippedItem(String,String,), // Post-comment
#[another_attr]
#[attr2]
ItemStruct {
x: usize,
y: usize,
}, /* Comment AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
ItemStruct { x: usize, y: usize }, /* Comment AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
// And another
ForcedPreflight, /* AAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
* AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
Expand All @@ -81,24 +76,15 @@ pub enum SingleTuple {
}

pub enum SingleStruct {
Match {
name: String,
loc: usize,
}, // Post-comment
Match { name: String, loc: usize }, // Post-comment
}

pub enum GenericEnum<I, T>
where I: Iterator<Item = T>
{
// Pre Comment
Left {
list: I,
root: T,
}, // Post-comment
Right {
list: I,
root: T,
}, // Post Comment
Left { list: I, root: T }, // Post-comment
Right { list: I, root: T }, // Post Comment
}


Expand Down
4 changes: 1 addition & 3 deletions tests/target/where-trailing-comma.rs
Expand Up @@ -33,9 +33,7 @@ enum E<S, T>
where S: P,
T: P,
{
A {
a: T,
},
A { a: T },
}

type Double<T>
Expand Down

0 comments on commit 775de8a

Please sign in to comment.