diff --git a/src/config.rs b/src/config.rs index 12dfce4de33..457c2eb2adf 100644 --- a/src/config.rs +++ b/src/config.rs @@ -91,6 +91,8 @@ create_config! { fn_args_paren_newline: bool, fn_args_layout: Density, fn_arg_indent: BlockIndentStyle, + where_density: Density, // Should we at least try to put the where clause on the same line as + // the rest of the function decl? where_indent: BlockIndentStyle, // Visual will be treated like Tabbed where_layout: ListTactic, where_pred_indent: BlockIndentStyle, @@ -121,6 +123,7 @@ impl Default for Config { fn_args_paren_newline: true, fn_args_layout: Density::Tall, fn_arg_indent: BlockIndentStyle::Visual, + where_density: Density::Tall, where_indent: BlockIndentStyle::Tabbed, where_layout: ListTactic::Vertical, where_pred_indent: BlockIndentStyle::Visual, diff --git a/src/items.rs b/src/items.rs index b93db659eba..cc431792c10 100644 --- a/src/items.rs +++ b/src/items.rs @@ -18,7 +18,7 @@ use expr::rewrite_assign_rhs; use comment::FindUncommented; use visitor::FmtVisitor; use rewrite::Rewrite; -use config::{Config, BlockIndentStyle}; +use config::{Config, BlockIndentStyle, Density}; use syntax::{ast, abi}; use syntax::codemap::{self, Span, BytePos}; @@ -99,7 +99,7 @@ impl<'a> FmtVisitor<'a> { vis: ast::Visibility, span: Span) -> String { - let newline_brace = self.newline_for_brace(&generics.where_clause); + let mut newline_brace = self.newline_for_brace(&generics.where_clause); let mut result = self.rewrite_fn_base(indent, ident, @@ -113,6 +113,10 @@ impl<'a> FmtVisitor<'a> { span, newline_brace); + if self.config.fn_brace_style != BraceStyle::AlwaysNextLine && !result.contains('\n') { + newline_brace = false; + } + // Prepare for the function body by possibly adding a newline and // indent. // FIXME we'll miss anything between the end of the signature and the @@ -281,10 +285,18 @@ impl<'a> FmtVisitor<'a> { } } + let where_density = if self.config.where_density == Density::Compressed && + !result.contains('\n') { + Density::Compressed + } else { + Density::Tall + }; + // Where clause. result.push_str(&self.rewrite_where_clause(where_clause, self.config, indent, + where_density, span.hi)); result @@ -682,6 +694,7 @@ impl<'a> FmtVisitor<'a> { result.push_str(&self.rewrite_where_clause(&generics.where_clause, self.config, self.block_indent, + Density::Tall, span.hi)); result.push_str(&make_indent(self.block_indent)); result.push('\n'); @@ -794,6 +807,7 @@ impl<'a> FmtVisitor<'a> { where_clause: &ast::WhereClause, config: &Config, indent: usize, + density: Density, span_end: BytePos) -> String { if where_clause.predicates.is_empty() { @@ -839,10 +853,17 @@ impl<'a> FmtVisitor<'a> { v_width: budget, ends_with_newline: true, }; + let preds_str = write_list(&items.collect::>(), &fmt); - format!("\n{}where {}", - make_indent(indent + extra_indent), - write_list(&items.collect::>(), &fmt)) + // 9 = " where ".len() + " {".len() + if density == Density::Tall || preds_str.contains('\n') || + indent + 9 + preds_str.len() > self.config.max_width { + format!("\n{}where {}", + make_indent(indent + extra_indent), + preds_str) + } else { + format!(" where {}", preds_str) + } } fn rewrite_return(&self, ret: &ast::FunctionRetTy) -> String { diff --git a/tests/config/small_tabs.toml b/tests/config/small_tabs.toml index 912638c91f8..146ceb2a62c 100644 --- a/tests/config/small_tabs.toml +++ b/tests/config/small_tabs.toml @@ -8,6 +8,7 @@ fn_return_indent = "WithArgs" fn_args_paren_newline = true fn_args_layout = "Tall" fn_arg_indent = "Visual" +where_density = "Tall" where_indent = "Tabbed" where_layout = "Vertical" where_pred_indent = "Visual" diff --git a/tests/source/fn-custom-4.rs b/tests/source/fn-custom-4.rs index 0293f1b1959..7b3e4c4a163 100644 --- a/tests/source/fn-custom-4.rs +++ b/tests/source/fn-custom-4.rs @@ -1,6 +1,15 @@ // rustfmt-where_pred_indent: Tabbed +// rustfmt-where_density: Compressed // Test different indents. fn qux() where X: TTTTTTTTTTTTTTTTTTTTTTTTTTTT, X: TTTTTTTTTTTTTTTTTTTTTTTTTTTT, X: TTTTTTTTTTTTTTTTTTTTTTTTTTTT, X: TTTTTTTTTTTTTTTTTTTTTTTTTTTT { baz(); } + +fn qux() where X: TTTTTTTTTTTTTTTTTTTTTTTTTTTT { + baz(); +} + +fn qux(a: Aaaaaaaaaaaaaaaaa) where X: TTTTTTTTTTTTTTTTTTTTTTTTTTTT, X: TTTTTTTTTTTTTTTTTTTTTTTTTTTT { + baz(); +} diff --git a/tests/target/fn-custom-4.rs b/tests/target/fn-custom-4.rs index e1da6524565..70ec49f34bc 100644 --- a/tests/target/fn-custom-4.rs +++ b/tests/target/fn-custom-4.rs @@ -1,4 +1,5 @@ // rustfmt-where_pred_indent: Tabbed +// rustfmt-where_density: Compressed // Test different indents. fn qux() @@ -9,3 +10,14 @@ fn qux() { baz(); } + +fn qux() where X: TTTTTTTTTTTTTTTTTTTTTTTTTTTT { + baz(); +} + +fn qux(a: Aaaaaaaaaaaaaaaaa) + where X: TTTTTTTTTTTTTTTTTTTTTTTTTTTT, + X: TTTTTTTTTTTTTTTTTTTTTTTTTTTT +{ + baz(); +}