Merge pull request #659 from Marwes/where_clause

Fix where clauses not taking the width of the line into account
This commit is contained in:
Marcus Klaas de Vries 2015-12-02 19:06:24 +01:00
commit e677f05805
3 changed files with 69 additions and 6 deletions

View File

@ -476,10 +476,13 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
let indent = offset + result.len(); let indent = offset + result.len();
result.push_str(&*try_opt!(self_ty.rewrite(context, budget, indent))); result.push_str(&*try_opt!(self_ty.rewrite(context, budget, indent)));
let where_budget = try_opt!(context.config.max_width.checked_sub(last_line_width(&result)));
let where_clause_str = try_opt!(rewrite_where_clause(context, let where_clause_str = try_opt!(rewrite_where_clause(context,
&generics.where_clause, &generics.where_clause,
context.config, context.config,
context.config.item_brace_style,
context.block_indent, context.block_indent,
where_budget,
context.config.where_density, context.config.where_density,
"{", "{",
None)); None));
@ -678,27 +681,32 @@ fn format_tuple_struct(context: &RewriteContext,
let body_lo = fields[0].span.lo; let body_lo = fields[0].span.lo;
let (generics_str, where_clause_str) = match generics { let where_clause_str = match generics {
Some(ref generics) => { Some(ref generics) => {
let generics_str = try_opt!(rewrite_generics(context, let generics_str = try_opt!(rewrite_generics(context,
generics, generics,
offset, offset,
offset + header_str.len(), offset + header_str.len(),
mk_sp(span.lo, body_lo))); mk_sp(span.lo, body_lo)));
result.push_str(&generics_str);
let where_budget = try_opt!(context.config
.max_width
.checked_sub(last_line_width(&result)));
let where_clause_str = try_opt!(rewrite_where_clause(context, let where_clause_str = try_opt!(rewrite_where_clause(context,
&generics.where_clause, &generics.where_clause,
context.config, context.config,
context.config.item_brace_style,
context.block_indent, context.block_indent,
where_budget,
Density::Compressed, Density::Compressed,
";", ";",
None)); None));
(generics_str, where_clause_str) where_clause_str
} }
None => ("".to_owned(), "".to_owned()), None => "".to_owned(),
}; };
result.push_str(&generics_str);
result.push('('); result.push('(');
let item_indent = context.block_indent + result.len(); let item_indent = context.block_indent + result.len();
@ -1168,10 +1176,13 @@ fn rewrite_fn_base(context: &RewriteContext,
}; };
// Where clause. // Where clause.
let where_budget = try_opt!(context.config.max_width.checked_sub(last_line_width(&result)));
let where_clause_str = try_opt!(rewrite_where_clause(context, let where_clause_str = try_opt!(rewrite_where_clause(context,
where_clause, where_clause,
context.config, context.config,
context.config.fn_brace_style,
indent, indent,
where_budget,
where_density, where_density,
"{", "{",
Some(span.hi))); Some(span.hi)));
@ -1408,7 +1419,9 @@ fn rewrite_generics(context: &RewriteContext,
fn rewrite_where_clause(context: &RewriteContext, fn rewrite_where_clause(context: &RewriteContext,
where_clause: &ast::WhereClause, where_clause: &ast::WhereClause,
config: &Config, config: &Config,
brace_style: BraceStyle,
indent: Indent, indent: Indent,
width: usize,
density: Density, density: Density,
terminator: &str, terminator: &str,
span_end: Option<BytePos>) span_end: Option<BytePos>)
@ -1462,9 +1475,19 @@ fn rewrite_where_clause(context: &RewriteContext,
}; };
let preds_str = try_opt!(write_list(&item_vec, &fmt)); let preds_str = try_opt!(write_list(&item_vec, &fmt));
// 9 = " where ".len() + " {".len() let end_length = if terminator == "{" {
// If the brace is on the next line we don't need to count it otherwise it needs two
// characters " {"
match brace_style {
BraceStyle::AlwaysNextLine => 0,
BraceStyle::PreferSameLine => 2,
BraceStyle::SameLineWhere => 0,
}
} else {
terminator.len()
};
if density == Density::Tall || preds_str.contains('\n') || if density == Density::Tall || preds_str.contains('\n') ||
indent.width() + 9 + preds_str.len() > context.config.max_width { indent.width() + " where ".len() + preds_str.len() + end_length > width {
Some(format!("\n{}where {}", Some(format!("\n{}where {}",
(indent + extra_indent).to_string(context.config), (indent + extra_indent).to_string(context.config),
preds_str)) preds_str))
@ -1490,10 +1513,13 @@ fn format_generics(context: &RewriteContext,
let mut result = try_opt!(rewrite_generics(context, generics, offset, generics_offset, span)); let mut result = try_opt!(rewrite_generics(context, generics, offset, generics_offset, span));
if !generics.where_clause.predicates.is_empty() || result.contains('\n') { if !generics.where_clause.predicates.is_empty() || result.contains('\n') {
let budget = try_opt!(context.config.max_width.checked_sub(last_line_width(&result)));
let where_clause_str = try_opt!(rewrite_where_clause(context, let where_clause_str = try_opt!(rewrite_where_clause(context,
&generics.where_clause, &generics.where_clause,
context.config, context.config,
brace_style,
context.block_indent, context.block_indent,
budget,
Density::Tall, Density::Tall,
terminator, terminator,
Some(span.hi))); Some(span.hi)));

View File

@ -0,0 +1,16 @@
pub trait Test {
fn very_long_method_name<F>(self, f: F) -> MyVeryLongReturnType where F: FnMut(Self::Item) -> bool;
fn exactly_100_chars1<F>(self, f: F) -> MyVeryLongReturnType where F: FnMut(Self::Item) -> bool;
}
fn very_long_function_name<F>(very_long_argument: F) -> MyVeryLongReturnType where F: FnMut(Self::Item) -> bool { }
struct VeryLongTupleStructName<A, B, C, D, E>(LongLongTypename, LongLongTypename, i32, i32) where A: LongTrait;
struct Exactly100CharsToSemicolon<A, B, C, D, E>
(LongLongTypename, i32, i32)
where A: LongTrait1234;
struct AlwaysOnNextLine<LongLongTypename, LongTypename, A, B, C, D, E, F> where A: LongTrait {
x: i32
}

View File

@ -0,0 +1,21 @@
pub trait Test {
fn very_long_method_name<F>(self, f: F) -> MyVeryLongReturnType
where F: FnMut(Self::Item) -> bool;
fn exactly_100_chars1<F>(self, f: F) -> MyVeryLongReturnType where F: FnMut(Self::Item) -> bool;
}
fn very_long_function_name<F>(very_long_argument: F) -> MyVeryLongReturnType
where F: FnMut(Self::Item) -> bool
{
}
struct VeryLongTupleStructName<A, B, C, D, E>(LongLongTypename, LongLongTypename, i32, i32)
where A: LongTrait;
struct Exactly100CharsToSemicolon<A, B, C, D, E>(LongLongTypename, i32, i32) where A: LongTrait1234;
struct AlwaysOnNextLine<LongLongTypename, LongTypename, A, B, C, D, E, F>
where A: LongTrait
{
x: i32,
}