From ebf64ca35df03737e9850ab940125b21ba64ff16 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Thu, 24 Sep 2015 11:00:14 +1200 Subject: [PATCH] Heuristic max width for function calls --- src/config.rs | 7 +++++-- src/items.rs | 2 +- src/lists.rs | 22 ++++++++++++++++++++++ src/types.rs | 2 +- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index c5b777e1d81c..4779d0e9b98c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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, diff --git a/src/items.rs b/src/items.rs index c2a8f46b3db1..3656fcdf982b 100644 --- a/src/items.rs +++ b/src/items.rs @@ -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)) diff --git a/src/lists.rs b/src/lists.rs index cccb8d37b081..cf1d433a25c2 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -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, diff --git a/src/types.rs b/src/types.rs index 284654049887..b1af98afbda6 100644 --- a/src/types.rs +++ b/src/types.rs @@ -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::>(), &fmt)); // Update position of last bracket.