Support where clauses on the same line as the function decl where it all fits on one line.

This commit is contained in:
Nick Cameron 2015-09-01 19:36:00 +12:00
parent 6216dce14e
commit 8d81aa1991
5 changed files with 51 additions and 5 deletions

View File

@ -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,

View File

@ -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::<Vec<_>>(), &fmt);
format!("\n{}where {}",
make_indent(indent + extra_indent),
write_list(&items.collect::<Vec<_>>(), &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 {

View File

@ -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"

View File

@ -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();
}

View File

@ -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();
}