Use a width heuristic for struct lits.

Closes #123
This commit is contained in:
Nick Cameron 2015-09-26 18:25:41 +12:00
parent 9a5688f7c2
commit b894dd8abf
3 changed files with 12 additions and 5 deletions

View File

@ -218,8 +218,10 @@ pub fn get_docs() -> Vec<ConfigHelpItem> {
ideal_width: usize, "Ideal width of each line (only used for comments)",
leeway: usize, "Leeway of line width (deprecated)",
tab_spaces: usize, "Number of spaces per tab",
list_width: usize, "Maximum width in a struct literal or function \
call before faling back to vertical formatting",
fn_call_width: usize, "Maximum width of the args of a function call\
before faling back to vertical formatting",
struct_lit_width: usize, "Maximum width in the body of a struct lit\
before faling back to vertical formatting",
newline_style: NewlineStyle, "Unix or Windows line endings",
fn_brace_style: BraceStyle, "Brace style for functions",
fn_return_indent: ReturnIndent, "Location of return type in function declaration",
@ -258,7 +260,8 @@ fn default() -> Config {
ideal_width: 80,
leeway: 5,
tab_spaces: 4,
list_width: 50,
fn_call_width: 50,
struct_lit_width: 12,
newline_style: NewlineStyle::Unix,
fn_brace_style: BraceStyle::SameLineWhere,
fn_return_indent: ReturnIndent::WithArgs,

View File

@ -1226,11 +1226,15 @@ enum StructLitField<'a> {
span_after(span, "{", context.codemap),
span.hi);
let tactic = match (context.config.struct_lit_style, fields.len()) {
let mut tactic = match (context.config.struct_lit_style, fields.len()) {
(StructLitStyle::Visual, 1) => ListTactic::HorizontalVertical,
_ => context.config.struct_lit_multiline_style.to_list_tactic(),
};
if tactic == ListTactic::HorizontalVertical && fields.len() > 1 {
tactic = ListTactic::LimitedHorizontalVertical(context.config.struct_lit_width);
}
let fmt = ListFormatting {
tactic: tactic,
separator: ",",

View File

@ -62,7 +62,7 @@ pub struct ListFormatting<'a> {
impl<'a> ListFormatting<'a> {
pub fn for_fn(width: usize, offset: Indent, config: &'a Config) -> ListFormatting<'a> {
ListFormatting {
tactic: ListTactic::LimitedHorizontalVertical(config.list_width),
tactic: ListTactic::LimitedHorizontalVertical(config.fn_call_width),
separator: ",",
trailing_separator: SeparatorTactic::Never,
indent: offset,