Heuristic max width for function calls

This commit is contained in:
Nick Cameron 2015-09-24 11:00:14 +12:00
parent e161de0daa
commit ebf64ca35d
4 changed files with 29 additions and 4 deletions

View file

@ -215,9 +215,11 @@ macro_rules! create_config {
create_config! {
max_width: usize, "Maximum width of each line",
ideal_width: usize, "Ideal width of each line",
leeway: usize, "Leeway of line width",
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",
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",
@ -256,6 +258,7 @@ impl Default for Config {
ideal_width: 80,
leeway: 5,
tab_spaces: 4,
list_width: 50,
newline_style: NewlineStyle::Unix,
fn_brace_style: BraceStyle::SameLineWhere,
fn_return_indent: ReturnIndent::WithArgs,

View file

@ -948,7 +948,7 @@ impl<'a> FmtVisitor<'a> {
item.item = ty;
}
let fmt = ListFormatting::for_fn(h_budget, offset, self.config);
let fmt = ListFormatting::for_item(h_budget, offset, self.config);
let list_str = try_opt!(write_list(&items, &fmt));
Some(format!("<{}>", list_str))

View file

@ -26,6 +26,8 @@ pub enum ListTactic {
Horizontal,
// Try Horizontal layout, if that fails then vertical
HorizontalVertical,
// HorizontalVertical with a soft limit.
LimitedHorizontalVertical(usize),
// Pack as many items as possible per row over (possibly) many rows.
Mixed,
}
@ -59,6 +61,19 @@ 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),
separator: ",",
trailing_separator: SeparatorTactic::Never,
indent: offset,
h_width: width,
v_width: width,
ends_with_newline: false,
config: config,
}
}
pub fn for_item(width: usize, offset: Indent, config: &'a Config) -> ListFormatting<'a> {
ListFormatting {
tactic: ListTactic::HorizontalVertical,
separator: ",",
@ -118,6 +133,13 @@ pub fn write_list<'b>(items: &[ListItem], formatting: &ListFormatting<'b>) -> Op
let fits_single = total_width + total_sep_len <= formatting.h_width;
// Check if we need to fallback from horizontal listing, if possible.
if let ListTactic::LimitedHorizontalVertical(limit) = tactic {
if total_width > limit {
tactic = ListTactic::Vertical;
} else {
tactic = ListTactic::HorizontalVertical;
}
}
if tactic == ListTactic::HorizontalVertical {
debug!("write_list: total_width: {}, total_sep_len: {}, h_width: {}",
total_width,

View file

@ -230,7 +230,7 @@ fn rewrite_segment(segment: &ast::PathSegment,
list_lo,
span_hi);
let fmt = ListFormatting::for_fn(list_width, offset + extra_offset, context.config);
let fmt = ListFormatting::for_item(list_width, offset + extra_offset, context.config);
let list_str = try_opt!(write_list(&items.collect::<Vec<_>>(), &fmt));
// Update position of last bracket.