mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-12 00:24:03 +00:00
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:
commit
e677f05805
38
src/items.rs
38
src/items.rs
@ -476,10 +476,13 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
|
||||
let indent = offset + result.len();
|
||||
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,
|
||||
&generics.where_clause,
|
||||
context.config,
|
||||
context.config.item_brace_style,
|
||||
context.block_indent,
|
||||
where_budget,
|
||||
context.config.where_density,
|
||||
"{",
|
||||
None));
|
||||
@ -678,27 +681,32 @@ fn format_tuple_struct(context: &RewriteContext,
|
||||
|
||||
let body_lo = fields[0].span.lo;
|
||||
|
||||
let (generics_str, where_clause_str) = match generics {
|
||||
let where_clause_str = match generics {
|
||||
Some(ref generics) => {
|
||||
let generics_str = try_opt!(rewrite_generics(context,
|
||||
generics,
|
||||
offset,
|
||||
offset + header_str.len(),
|
||||
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,
|
||||
&generics.where_clause,
|
||||
context.config,
|
||||
context.config.item_brace_style,
|
||||
context.block_indent,
|
||||
where_budget,
|
||||
Density::Compressed,
|
||||
";",
|
||||
None));
|
||||
|
||||
(generics_str, where_clause_str)
|
||||
where_clause_str
|
||||
}
|
||||
None => ("".to_owned(), "".to_owned()),
|
||||
None => "".to_owned(),
|
||||
};
|
||||
result.push_str(&generics_str);
|
||||
result.push('(');
|
||||
|
||||
let item_indent = context.block_indent + result.len();
|
||||
@ -1168,10 +1176,13 @@ fn rewrite_fn_base(context: &RewriteContext,
|
||||
};
|
||||
|
||||
// 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,
|
||||
where_clause,
|
||||
context.config,
|
||||
context.config.fn_brace_style,
|
||||
indent,
|
||||
where_budget,
|
||||
where_density,
|
||||
"{",
|
||||
Some(span.hi)));
|
||||
@ -1408,7 +1419,9 @@ fn rewrite_generics(context: &RewriteContext,
|
||||
fn rewrite_where_clause(context: &RewriteContext,
|
||||
where_clause: &ast::WhereClause,
|
||||
config: &Config,
|
||||
brace_style: BraceStyle,
|
||||
indent: Indent,
|
||||
width: usize,
|
||||
density: Density,
|
||||
terminator: &str,
|
||||
span_end: Option<BytePos>)
|
||||
@ -1462,9 +1475,19 @@ fn rewrite_where_clause(context: &RewriteContext,
|
||||
};
|
||||
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') ||
|
||||
indent.width() + 9 + preds_str.len() > context.config.max_width {
|
||||
indent.width() + " where ".len() + preds_str.len() + end_length > width {
|
||||
Some(format!("\n{}where {}",
|
||||
(indent + extra_indent).to_string(context.config),
|
||||
preds_str))
|
||||
@ -1490,10 +1513,13 @@ fn format_generics(context: &RewriteContext,
|
||||
let mut result = try_opt!(rewrite_generics(context, generics, offset, generics_offset, span));
|
||||
|
||||
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,
|
||||
&generics.where_clause,
|
||||
context.config,
|
||||
brace_style,
|
||||
context.block_indent,
|
||||
budget,
|
||||
Density::Tall,
|
||||
terminator,
|
||||
Some(span.hi)));
|
||||
|
16
tests/source/where-clause.rs
Normal file
16
tests/source/where-clause.rs
Normal 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
|
||||
}
|
21
tests/target/where-clause.rs
Normal file
21
tests/target/where-clause.rs
Normal 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,
|
||||
}
|
Loading…
Reference in New Issue
Block a user