Decide whether args fit in a single line inside rewrite_call_args

This commit is contained in:
topecongiro 2017-05-25 06:10:00 +09:00
parent 93c556c6ba
commit c8ad521c9f
3 changed files with 44 additions and 13 deletions

View File

@ -1673,15 +1673,9 @@ fn rewrite_call_inner<R>(context: &RewriteContext,
one_line_width, one_line_width,
force_trailing_comma) force_trailing_comma)
.ok_or(Ordering::Less)?; .ok_or(Ordering::Less)?;
let arg_one_line_budget = min(one_line_width, context.config.fn_call_width());
Ok(format!("{}{}", Ok(format!("{}{}",
callee_str, callee_str,
wrap_args_with_parens(context, wrap_args_with_parens(context, &list_str, extendable, shape, nested_shape)))
&list_str,
extendable,
arg_one_line_budget,
shape,
nested_shape)))
} }
fn rewrite_call_args(context: &RewriteContext, fn rewrite_call_args(context: &RewriteContext,
@ -1771,12 +1765,16 @@ fn rewrite_call_args(context: &RewriteContext,
config: context.config, config: context.config,
}; };
let args_in_single_line = let almost_no_newline =
item_vec item_vec
.iter() .iter()
.rev() .rev()
.skip(1) .skip(1)
.all(|item| item.item.as_ref().map_or(false, |s| !s.contains('\n'))); .all(|item| item.item.as_ref().map_or(false, |s| !s.contains('\n')));
let extendable = almost_no_newline &&
item_vec.iter().fold(0, |acc, item| {
acc + item.item.as_ref().map_or(0, |s| 2 + first_line_width(s))
}) <= min(one_line_width, context.config.fn_call_width()) + 2;
match write_list(&item_vec, &fmt) { match write_list(&item_vec, &fmt) {
// If arguments do not fit in a single line and do not contain newline, // If arguments do not fit in a single line and do not contain newline,
@ -1791,7 +1789,15 @@ fn rewrite_call_args(context: &RewriteContext,
fmt.tactic = DefinitiveListTactic::Vertical; fmt.tactic = DefinitiveListTactic::Vertical;
write_list(&item_vec, &fmt).map(|rw| (false, rw)) write_list(&item_vec, &fmt).map(|rw| (false, rw))
} }
rewrite @ _ => rewrite.map(|rw| (args_in_single_line && is_extendable(args), rw)), rewrite @ _ => {
rewrite.map(|rw| {
(extendable &&
rw.chars()
.last()
.map_or(true, |c| force_trailing_comma || c != ','),
rw)
})
}
} }
} }
@ -1841,14 +1847,11 @@ fn is_extendable(args: &[ptr::P<ast::Expr>]) -> bool {
fn wrap_args_with_parens(context: &RewriteContext, fn wrap_args_with_parens(context: &RewriteContext,
args_str: &str, args_str: &str,
is_extendable: bool, is_extendable: bool,
one_line_budget: usize,
shape: Shape, shape: Shape,
nested_shape: Shape) nested_shape: Shape)
-> String { -> String {
if context.config.fn_call_style() == IndentStyle::Visual || if context.config.fn_call_style() == IndentStyle::Visual ||
(context.inside_macro && !args_str.contains('\n')) || (context.inside_macro && !args_str.contains('\n')) || is_extendable {
((is_extendable || !args_str.contains('\n')) &&
first_line_width(&args_str) <= one_line_budget) {
if context.config.spaces_within_parens() && args_str.len() > 0 { if context.config.spaces_within_parens() && args_str.len() > 0 {
format!("( {} )", args_str) format!("( {} )", args_str)
} else { } else {

View File

@ -96,3 +96,17 @@ impl Cursor {
}); });
} }
} }
fn issue1581() {
bootstrap.checks.register(
"PERSISTED_LOCATIONS",
move || if locations2.0.inner_mut.lock().poisoned {
Check::new(
State::Error,
"Persisted location storage is poisoned due to a write failure",
)
} else {
Check::new(State::Healthy, "Persisted location storage is healthy")
},
);
}

View File

@ -106,3 +106,17 @@ impl Cursor {
}); });
} }
} }
fn issue1581() {
bootstrap.checks.register(
"PERSISTED_LOCATIONS",
move || if locations2.0.inner_mut.lock().poisoned {
Check::new(
State::Error,
"Persisted location storage is poisoned due to a write failure",
)
} else {
Check::new(State::Healthy, "Persisted location storage is healthy")
},
);
}